1 #include <assert.h>
2 
3 #define TEST_CONSOLE_ID "test"
4 
5 #include "config.c"
6 
test_independence_cmdline_optarg(void)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 
test_independence_config_console_id(void)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 
test_independence_config_socket_id(void)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 	/*
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));
54 
55 	config_fini(ctx);
56 }
57 
test_independence_default(void)58 static 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 
test_precedence_cmdline_optarg(void)71 static void test_precedence_cmdline_optarg(void)
72 {
73 	static const char *const config = "console-id = console\n";
74 	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"));
85 	assert(!strcmp(console_id, TEST_CONSOLE_ID));
86 
87 	config_fini(ctx);
88 }
89 
test_precedence_config_console_id(void)90 static void test_precedence_config_console_id(void)
91 {
92 	static const char *const config = "console-id = console\n";
93 	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"));
104 	assert(!strcmp(console_id, "console"));
105 
106 	config_fini(ctx);
107 }
108 
main(void)109 int 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();
117 
118 	return EXIT_SUCCESS;
119 }
120