1 #include <assert.h> 2 3 #define TEST_CONSOLE_ID "test" 4 5 #include "config.c" 6 7 static struct config *config_mock(char *key, char *value) 8 { 9 char buf[CONFIG_MAX_KEY_LENGTH]; 10 struct config *config; 11 int rc; 12 13 config = malloc(sizeof(struct config)); 14 assert(config != NULL); 15 16 config->dict = dictionary_new(1); 17 assert(config->dict != NULL); 18 19 rc = snprintf(buf, CONFIG_MAX_KEY_LENGTH, ":%s", key); 20 assert(rc >= 0 && (size_t)rc < sizeof(buf)); 21 22 dictionary_set(config->dict, buf, value); 23 24 return config; 25 } 26 27 static void test_independence_cmdline_optarg(void) 28 { 29 const char *console_id; 30 struct config *ctx; 31 32 ctx = calloc(1, sizeof(*ctx)); 33 console_id = config_resolve_console_id(ctx, TEST_CONSOLE_ID); 34 35 assert(!strcmp(console_id, TEST_CONSOLE_ID)); 36 37 config_fini(ctx); 38 } 39 40 static void test_independence_config_console_id(void) 41 { 42 const char *console_id; 43 struct config *ctx; 44 45 ctx = config_mock("console-id", TEST_CONSOLE_ID); 46 console_id = config_resolve_console_id(ctx, NULL); 47 48 assert(!strcmp(console_id, TEST_CONSOLE_ID)); 49 50 config_fini(ctx); 51 } 52 53 static void test_independence_config_socket_id(void) 54 { 55 const char *console_id; 56 struct config *ctx; 57 58 ctx = config_mock("socket-id", TEST_CONSOLE_ID); 59 console_id = config_resolve_console_id(ctx, NULL); 60 61 /* 62 * socket-id is no-longer an alias for console-id, therefore we should observe 63 * DEFAULT_CONSOLE_ID and not TEST_CONSOLE_ID 64 */ 65 assert(!strcmp(console_id, DEFAULT_CONSOLE_ID)); 66 67 config_fini(ctx); 68 } 69 70 static void test_independence_default(void) 71 { 72 const char *console_id; 73 struct config *ctx; 74 75 ctx = calloc(1, sizeof(*ctx)); 76 console_id = config_resolve_console_id(ctx, NULL); 77 78 assert(!strcmp(console_id, DEFAULT_CONSOLE_ID)); 79 80 config_fini(ctx); 81 } 82 83 static void test_precedence_cmdline_optarg(void) 84 { 85 const char *console_id; 86 struct config *ctx; 87 88 ctx = config_mock("console-id", "console"); 89 console_id = config_resolve_console_id(ctx, TEST_CONSOLE_ID); 90 91 assert(config_get_value(ctx, "console-id")); 92 assert(!strcmp(console_id, TEST_CONSOLE_ID)); 93 94 config_fini(ctx); 95 } 96 97 static void test_precedence_config_console_id(void) 98 { 99 const char *console_id; 100 struct config *ctx; 101 102 ctx = config_mock("console-id", "console"); 103 console_id = config_resolve_console_id(ctx, NULL); 104 105 assert(config_get_value(ctx, "console-id")); 106 assert(!strcmp(console_id, "console")); 107 108 config_fini(ctx); 109 } 110 111 int main(void) 112 { 113 test_independence_cmdline_optarg(); 114 test_independence_config_console_id(); 115 test_independence_config_socket_id(); 116 test_independence_default(); 117 test_precedence_cmdline_optarg(); 118 test_precedence_config_console_id(); 119 120 return EXIT_SUCCESS; 121 } 122