blob: 84bc5b360d113c7cb21023e87c77fc22ddb7b13f [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
7static 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
20static 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
37static 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
Andrew Jefferydfda5af2023-06-07 13:33:47 +093049 /*
50 * socket-id is no-longer an alias for console-id, therefore we should observe
51 * DEFAULT_CONSOLE_ID and not TEST_CONSOLE_ID
52 */
53 assert(!strcmp(console_id, DEFAULT_CONSOLE_ID));
Ninad Palsule5ba20b52023-05-12 14:03:15 -050054
55 config_fini(ctx);
56}
57
58static void test_independence_default(void)
59{
60 const char *console_id;
61 struct config *ctx;
62
63 ctx = calloc(1, sizeof(*ctx));
64 console_id = config_resolve_console_id(ctx, NULL);
65
66 assert(!strcmp(console_id, DEFAULT_CONSOLE_ID));
67
68 config_fini(ctx);
69}
70
71static void test_precedence_cmdline_optarg(void)
72{
Andrew Jefferydfda5af2023-06-07 13:33:47 +093073 static const char *const config = "console-id = console\n";
Ninad Palsule5ba20b52023-05-12 14:03:15 -050074 const char *console_id;
75 struct config *ctx;
76 char *buf;
77
78 ctx = calloc(1, sizeof(*ctx));
79 buf = strdup(config);
80 config_parse(ctx, buf);
81 free(buf);
82 console_id = config_resolve_console_id(ctx, TEST_CONSOLE_ID);
83
84 assert(config_get_value(ctx, "console-id"));
Ninad Palsule5ba20b52023-05-12 14:03:15 -050085 assert(!strcmp(console_id, TEST_CONSOLE_ID));
86
87 config_fini(ctx);
88}
89
90static void test_precedence_config_console_id(void)
91{
Andrew Jefferydfda5af2023-06-07 13:33:47 +093092 static const char *const config = "console-id = console\n";
Ninad Palsule5ba20b52023-05-12 14:03:15 -050093 const char *console_id;
94 struct config *ctx;
95 char *buf;
96
97 ctx = calloc(1, sizeof(*ctx));
98 buf = strdup(config);
99 config_parse(ctx, buf);
100 free(buf);
101 console_id = config_resolve_console_id(ctx, NULL);
102
103 assert(config_get_value(ctx, "console-id"));
Ninad Palsule5ba20b52023-05-12 14:03:15 -0500104 assert(!strcmp(console_id, "console"));
105
106 config_fini(ctx);
107}
108
Ninad Palsule5ba20b52023-05-12 14:03:15 -0500109int main(void)
110{
111 test_independence_cmdline_optarg();
112 test_independence_config_console_id();
113 test_independence_config_socket_id();
114 test_independence_default();
115 test_precedence_cmdline_optarg();
116 test_precedence_config_console_id();
Ninad Palsule5ba20b52023-05-12 14:03:15 -0500117
118 return EXIT_SUCCESS;
119}