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 | |
Alexander Hansen | 1e04f44 | 2024-06-12 16:35:58 +0200 | [diff] [blame] | 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 | |
Ninad Palsule | 5ba20b5 | 2023-05-12 14:03:15 -0500 | [diff] [blame] | 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; |
Ninad Palsule | 5ba20b5 | 2023-05-12 14:03:15 -0500 | [diff] [blame] | 44 | |
Alexander Hansen | 1e04f44 | 2024-06-12 16:35:58 +0200 | [diff] [blame] | 45 | ctx = config_mock("console-id", TEST_CONSOLE_ID); |
Ninad Palsule | 5ba20b5 | 2023-05-12 14:03:15 -0500 | [diff] [blame] | 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; |
Ninad Palsule | 5ba20b5 | 2023-05-12 14:03:15 -0500 | [diff] [blame] | 57 | |
Alexander Hansen | 1e04f44 | 2024-06-12 16:35:58 +0200 | [diff] [blame] | 58 | ctx = config_mock("socket-id", TEST_CONSOLE_ID); |
Ninad Palsule | 5ba20b5 | 2023-05-12 14:03:15 -0500 | [diff] [blame] | 59 | console_id = config_resolve_console_id(ctx, NULL); |
| 60 | |
Andrew Jeffery | dfda5af | 2023-06-07 13:33:47 +0930 | [diff] [blame] | 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)); |
Ninad Palsule | 5ba20b5 | 2023-05-12 14:03:15 -0500 | [diff] [blame] | 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 | { |
Ninad Palsule | 5ba20b5 | 2023-05-12 14:03:15 -0500 | [diff] [blame] | 85 | const char *console_id; |
| 86 | struct config *ctx; |
Ninad Palsule | 5ba20b5 | 2023-05-12 14:03:15 -0500 | [diff] [blame] | 87 | |
Alexander Hansen | 1e04f44 | 2024-06-12 16:35:58 +0200 | [diff] [blame] | 88 | ctx = config_mock("console-id", "console"); |
Ninad Palsule | 5ba20b5 | 2023-05-12 14:03:15 -0500 | [diff] [blame] | 89 | console_id = config_resolve_console_id(ctx, TEST_CONSOLE_ID); |
| 90 | |
| 91 | assert(config_get_value(ctx, "console-id")); |
Ninad Palsule | 5ba20b5 | 2023-05-12 14:03:15 -0500 | [diff] [blame] | 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 | { |
Ninad Palsule | 5ba20b5 | 2023-05-12 14:03:15 -0500 | [diff] [blame] | 99 | const char *console_id; |
| 100 | struct config *ctx; |
Ninad Palsule | 5ba20b5 | 2023-05-12 14:03:15 -0500 | [diff] [blame] | 101 | |
Alexander Hansen | 1e04f44 | 2024-06-12 16:35:58 +0200 | [diff] [blame] | 102 | ctx = config_mock("console-id", "console"); |
Ninad Palsule | 5ba20b5 | 2023-05-12 14:03:15 -0500 | [diff] [blame] | 103 | console_id = config_resolve_console_id(ctx, NULL); |
| 104 | |
| 105 | assert(config_get_value(ctx, "console-id")); |
Ninad Palsule | 5ba20b5 | 2023-05-12 14:03:15 -0500 | [diff] [blame] | 106 | assert(!strcmp(console_id, "console")); |
| 107 | |
| 108 | config_fini(ctx); |
| 109 | } |
| 110 | |
Ninad Palsule | 5ba20b5 | 2023-05-12 14:03:15 -0500 | [diff] [blame] | 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(); |
Ninad Palsule | 5ba20b5 | 2023-05-12 14:03:15 -0500 | [diff] [blame] | 119 | |
| 120 | return EXIT_SUCCESS; |
| 121 | } |