Ninad Palsule | 5ba20b5 | 2023-05-12 14:03:15 -0500 | [diff] [blame] | 1 | #include <assert.h> |
| 2 | |
| 3 | #define TEST_CONSOLE_ID "test" |
| 4 | |
| 5 | #include "config.c" |
| 6 | |
| 7 | static void test_independence_cmdline_optarg(void) |
| 8 | { |
| 9 | const char *console_id; |
| 10 | struct config *ctx; |
| 11 | |
| 12 | ctx = calloc(1, sizeof(*ctx)); |
| 13 | console_id = config_resolve_console_id(ctx, TEST_CONSOLE_ID); |
| 14 | |
| 15 | assert(!strcmp(console_id, TEST_CONSOLE_ID)); |
| 16 | |
| 17 | config_fini(ctx); |
| 18 | } |
| 19 | |
| 20 | static void test_independence_config_console_id(void) |
| 21 | { |
| 22 | const char *console_id; |
| 23 | struct config *ctx; |
| 24 | char *buf; |
| 25 | |
| 26 | ctx = calloc(1, sizeof(*ctx)); |
| 27 | buf = strdup("console-id = " TEST_CONSOLE_ID); |
| 28 | config_parse(ctx, buf); |
| 29 | free(buf); |
| 30 | console_id = config_resolve_console_id(ctx, NULL); |
| 31 | |
| 32 | assert(!strcmp(console_id, TEST_CONSOLE_ID)); |
| 33 | |
| 34 | config_fini(ctx); |
| 35 | } |
| 36 | |
| 37 | static void test_independence_config_socket_id(void) |
| 38 | { |
| 39 | const char *console_id; |
| 40 | struct config *ctx; |
| 41 | char *buf; |
| 42 | |
| 43 | ctx = calloc(1, sizeof(*ctx)); |
| 44 | buf = strdup("socket-id = " TEST_CONSOLE_ID); |
| 45 | config_parse(ctx, buf); |
| 46 | free(buf); |
| 47 | console_id = config_resolve_console_id(ctx, NULL); |
| 48 | |
| 49 | assert(!strcmp(console_id, TEST_CONSOLE_ID)); |
| 50 | |
| 51 | config_fini(ctx); |
| 52 | } |
| 53 | |
| 54 | static void test_independence_default(void) |
| 55 | { |
| 56 | const char *console_id; |
| 57 | struct config *ctx; |
| 58 | |
| 59 | ctx = calloc(1, sizeof(*ctx)); |
| 60 | console_id = config_resolve_console_id(ctx, NULL); |
| 61 | |
| 62 | assert(!strcmp(console_id, DEFAULT_CONSOLE_ID)); |
| 63 | |
| 64 | config_fini(ctx); |
| 65 | } |
| 66 | |
| 67 | static void test_precedence_cmdline_optarg(void) |
| 68 | { |
| 69 | static const char *const config = "console-id = console\n" |
| 70 | "socket-id = socket\n"; |
| 71 | const char *console_id; |
| 72 | struct config *ctx; |
| 73 | char *buf; |
| 74 | |
| 75 | ctx = calloc(1, sizeof(*ctx)); |
| 76 | buf = strdup(config); |
| 77 | config_parse(ctx, buf); |
| 78 | free(buf); |
| 79 | console_id = config_resolve_console_id(ctx, TEST_CONSOLE_ID); |
| 80 | |
| 81 | assert(config_get_value(ctx, "console-id")); |
| 82 | assert(config_get_value(ctx, "socket-id")); |
| 83 | assert(!strcmp(console_id, TEST_CONSOLE_ID)); |
| 84 | |
| 85 | config_fini(ctx); |
| 86 | } |
| 87 | |
| 88 | static void test_precedence_config_console_id(void) |
| 89 | { |
| 90 | static const char *const config = "console-id = console\n" |
| 91 | "socket-id = socket\n"; |
| 92 | const char *console_id; |
| 93 | struct config *ctx; |
| 94 | char *buf; |
| 95 | |
| 96 | ctx = calloc(1, sizeof(*ctx)); |
| 97 | buf = strdup(config); |
| 98 | config_parse(ctx, buf); |
| 99 | free(buf); |
| 100 | console_id = config_resolve_console_id(ctx, NULL); |
| 101 | |
| 102 | assert(config_get_value(ctx, "console-id")); |
| 103 | assert(config_get_value(ctx, "socket-id")); |
| 104 | assert(!strcmp(console_id, "console")); |
| 105 | |
| 106 | config_fini(ctx); |
| 107 | } |
| 108 | |
| 109 | static void test_precedence_config_socket_id(void) |
| 110 | { |
| 111 | static const char *const config = "socket-id = socket\n"; |
| 112 | const char *console_id; |
| 113 | struct config *ctx; |
| 114 | char *buf; |
| 115 | |
| 116 | ctx = calloc(1, sizeof(*ctx)); |
| 117 | buf = strdup(config); |
| 118 | config_parse(ctx, buf); |
| 119 | free(buf); |
| 120 | console_id = config_resolve_console_id(ctx, NULL); |
| 121 | |
| 122 | assert(!config_get_value(ctx, "console-id")); |
| 123 | assert(config_get_value(ctx, "socket-id")); |
| 124 | assert(!strcmp(console_id, "socket")); |
| 125 | |
| 126 | config_fini(ctx); |
| 127 | } |
| 128 | |
| 129 | int main(void) |
| 130 | { |
| 131 | test_independence_cmdline_optarg(); |
| 132 | test_independence_config_console_id(); |
| 133 | test_independence_config_socket_id(); |
| 134 | test_independence_default(); |
| 135 | test_precedence_cmdline_optarg(); |
| 136 | test_precedence_config_console_id(); |
| 137 | test_precedence_config_socket_id(); |
| 138 | |
| 139 | return EXIT_SUCCESS; |
| 140 | } |