blob: 7d2d687888afd9ed535ae11566ad9aa1103c1da9 [file] [log] [blame]
Andrew Jefferyd30d7572023-04-18 14:51:51 +09301
2#include <assert.h>
3#include <stdlib.h>
4#include <stdint.h>
5#include <stdio.h>
6
7#ifndef SYSCONFDIR
8// Bypass compilation error due to -DSYSCONFDIR not provided
9#define SYSCONFDIR
10#endif
11
12#include "config.c"
13
Andrew Jefferya72711a2023-04-18 18:19:41 +093014static void execute_test(const char *input, const char *key,
15 const char *expected)
Andrew Jefferyd30d7572023-04-18 14:51:51 +093016{
17 struct config *ctx;
18 const char *found;
19 char *buf;
20
21 ctx = calloc(1, sizeof(*ctx));
22 buf = strdup(input);
23 config_parse(ctx, buf);
24 free(buf);
25 found = config_get_value(ctx, key);
26 if (!expected)
27 assert(!found);
28 if (expected) {
29 assert(found);
30 assert(!strcmp(expected, found));
31 }
32 config_fini(ctx);
33}
34
35static void test_config_parse_basic(void)
36{
37 execute_test("tty = ttyS0", "tty", "ttyS0");
38}
39
40static void test_config_parse_no_key(void)
41{
42 execute_test("= ttyS0", "tty", NULL);
43}
44
45static void test_config_parse_no_value(void)
46{
47 execute_test("tty =", "tty", NULL);
48}
49
50static void test_config_parse_no_operator(void)
51{
52 execute_test("tty ttyS0", "tty", NULL);
53}
54
55static void test_config_parse_no_spaces(void)
56{
57 execute_test("tty=ttyS0", "tty", "ttyS0");
58}
59
60static void test_config_parse_empty(void)
61{
62 execute_test("", "tty", NULL);
63}
64
65int main(void)
66{
67 test_config_parse_basic();
68 test_config_parse_no_key();
69 test_config_parse_no_value();
70 test_config_parse_no_operator();
71 test_config_parse_no_spaces();
72 test_config_parse_empty();
73
74 return EXIT_SUCCESS;
75}