blob: d84ebd18b23e0d131fa0db63bc61f05330ed036a [file] [log] [blame]
Ninad Palsule5ba20b52023-05-12 14:03:15 -05001#include <assert.h>
2
3#define TEST_CONSOLE_ID "test"
4
5#include "config.c"
6
Alexander Hansen1e04f442024-06-12 16:35:58 +02007static 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 Palsule5ba20b52023-05-12 14:03:15 -050027static 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
40static void test_independence_config_console_id(void)
41{
42 const char *console_id;
43 struct config *ctx;
Ninad Palsule5ba20b52023-05-12 14:03:15 -050044
Alexander Hansen1e04f442024-06-12 16:35:58 +020045 ctx = config_mock("console-id", TEST_CONSOLE_ID);
Ninad Palsule5ba20b52023-05-12 14:03:15 -050046 console_id = config_resolve_console_id(ctx, NULL);
47
48 assert(!strcmp(console_id, TEST_CONSOLE_ID));
49
50 config_fini(ctx);
51}
52
53static void test_independence_config_socket_id(void)
54{
55 const char *console_id;
56 struct config *ctx;
Ninad Palsule5ba20b52023-05-12 14:03:15 -050057
Alexander Hansen1e04f442024-06-12 16:35:58 +020058 ctx = config_mock("socket-id", TEST_CONSOLE_ID);
Ninad Palsule5ba20b52023-05-12 14:03:15 -050059 console_id = config_resolve_console_id(ctx, NULL);
60
Andrew Jefferydfda5af2023-06-07 13:33:47 +093061 /*
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 Palsule5ba20b52023-05-12 14:03:15 -050066
67 config_fini(ctx);
68}
69
70static 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
83static void test_precedence_cmdline_optarg(void)
84{
Ninad Palsule5ba20b52023-05-12 14:03:15 -050085 const char *console_id;
86 struct config *ctx;
Ninad Palsule5ba20b52023-05-12 14:03:15 -050087
Alexander Hansen1e04f442024-06-12 16:35:58 +020088 ctx = config_mock("console-id", "console");
Ninad Palsule5ba20b52023-05-12 14:03:15 -050089 console_id = config_resolve_console_id(ctx, TEST_CONSOLE_ID);
90
91 assert(config_get_value(ctx, "console-id"));
Ninad Palsule5ba20b52023-05-12 14:03:15 -050092 assert(!strcmp(console_id, TEST_CONSOLE_ID));
93
94 config_fini(ctx);
95}
96
97static void test_precedence_config_console_id(void)
98{
Ninad Palsule5ba20b52023-05-12 14:03:15 -050099 const char *console_id;
100 struct config *ctx;
Ninad Palsule5ba20b52023-05-12 14:03:15 -0500101
Alexander Hansen1e04f442024-06-12 16:35:58 +0200102 ctx = config_mock("console-id", "console");
Ninad Palsule5ba20b52023-05-12 14:03:15 -0500103 console_id = config_resolve_console_id(ctx, NULL);
104
105 assert(config_get_value(ctx, "console-id"));
Ninad Palsule5ba20b52023-05-12 14:03:15 -0500106 assert(!strcmp(console_id, "console"));
107
108 config_fini(ctx);
109}
110
Ninad Palsule5ba20b52023-05-12 14:03:15 -0500111int 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 Palsule5ba20b52023-05-12 14:03:15 -0500119
120 return EXIT_SUCCESS;
121}