blob: df90e91f252df5640db062bbb310d7ef2538a28b [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);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093026 if (!expected) {
Andrew Jefferyd30d7572023-04-18 14:51:51 +093027 assert(!found);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093028 }
Andrew Jefferyd30d7572023-04-18 14:51:51 +093029 if (expected) {
30 assert(found);
31 assert(!strcmp(expected, found));
32 }
33 config_fini(ctx);
34}
35
36static void test_config_parse_basic(void)
37{
38 execute_test("tty = ttyS0", "tty", "ttyS0");
39}
40
41static void test_config_parse_no_key(void)
42{
43 execute_test("= ttyS0", "tty", NULL);
44}
45
46static void test_config_parse_no_value(void)
47{
48 execute_test("tty =", "tty", NULL);
49}
50
51static void test_config_parse_no_operator(void)
52{
53 execute_test("tty ttyS0", "tty", NULL);
54}
55
56static void test_config_parse_no_spaces(void)
57{
58 execute_test("tty=ttyS0", "tty", "ttyS0");
59}
60
61static void test_config_parse_empty(void)
62{
63 execute_test("", "tty", NULL);
64}
65
66int main(void)
67{
68 test_config_parse_basic();
69 test_config_parse_no_key();
70 test_config_parse_no_value();
71 test_config_parse_no_operator();
72 test_config_parse_no_spaces();
73 test_config_parse_empty();
74
75 return EXIT_SUCCESS;
76}