Add basic pledge/unveil sandbox on OpenBSD
[tinc] / test / unit / test_utils.c
1 #include "unittest.h"
2 #include "../../src/utils.h"
3
4 #define FAKE_PATH "nonexistentreallyfakepath"
5
6 typedef struct {
7         const char *arg;
8         const char *want;
9 } testcase_t;
10
11 static void test_unix_absolute_path_on_absolute_returns_it(void **state) {
12         (void)state;
13
14         const char *paths[] = {"/", "/foo", "/foo/./../bar"};
15
16         for(size_t i = 0; i < sizeof(paths) / sizeof(*paths); ++i) {
17                 char *got = absolute_path(paths[i]);
18                 assert_ptr_not_equal(paths[i], got);
19                 assert_string_equal(paths[i], got);
20                 free(got);
21         }
22 }
23
24 static void test_unix_absolute_path_on_empty_returns_null(void **state) {
25         (void)state;
26         assert_null(absolute_path(NULL));
27         assert_null(absolute_path("\0"));
28 }
29
30 static void test_unix_absolute_path_relative(void **state) {
31         (void)state;
32
33         testcase_t cases[] = {
34                 {".", "/"},
35                 {"foo", "/foo"},
36                 {"./"FAKE_PATH, "/./"FAKE_PATH},
37                 {"../foo/./../"FAKE_PATH, "/../foo/./../"FAKE_PATH},
38         };
39
40         for(size_t i = 0; i < sizeof(cases) / sizeof(*cases); ++i) {
41                 char *got = absolute_path(cases[i].arg);
42                 assert_string_equal(cases[i].want, got);
43                 free(got);
44         }
45 }
46
47 static void test_int_to_str(const char *ref, int num) {
48         char *result = int_to_str(num);
49         assert_string_equal(ref, result);
50         free(result);
51 }
52
53 static void test_int_to_str_return_expected(void **state) {
54         (void)state;
55
56         test_int_to_str("0", 0);
57         test_int_to_str("-1337", -1337);
58         test_int_to_str("65535", 65535);
59 }
60
61 static void test_is_decimal_fail_empty(void **state) {
62         (void)state;
63
64         assert_false(is_decimal(NULL));
65         assert_false(is_decimal(""));
66 }
67
68 static void test_is_decimal_fail_hex(void **state) {
69         (void)state;
70
71         assert_false(is_decimal("DEADBEEF"));
72         assert_false(is_decimal("0xCAFE"));
73 }
74
75 static void test_is_decimal_fail_junk_suffix(void **state) {
76         (void)state;
77
78         assert_false(is_decimal("123foobar"));
79         assert_false(is_decimal("777 "));
80 }
81
82 static void test_is_decimal_pass_simple(void **state) {
83         (void)state;
84
85         assert_true(is_decimal("0"));
86         assert_true(is_decimal("123"));
87 }
88
89 static void test_is_decimal_pass_signs(void **state) {
90         (void)state;
91
92         assert_true(is_decimal("-123"));
93         assert_true(is_decimal("+123"));
94 }
95
96 static void test_is_decimal_pass_whitespace_prefix(void **state) {
97         (void)state;
98
99         assert_true(is_decimal(" \r\n\t 777"));
100 }
101
102 static int setup_path_unix(void **state) {
103         (void)state;
104         assert_int_equal(0, chdir("/"));
105         return 0;
106 }
107
108 static void test_string_eq(void **state) {
109         (void)state;
110
111         assert_true(string_eq(NULL, NULL));
112         assert_true(string_eq("", ""));
113         assert_true(string_eq("\tfoo 123", "\tfoo 123"));
114
115         assert_false(string_eq(NULL, ""));
116         assert_false(string_eq("", NULL));
117         assert_false(string_eq("foo", "FOO"));
118         assert_false(string_eq("foo", " foo"));
119 }
120
121 int main(void) {
122         const struct CMUnitTest tests[] = {
123                 cmocka_unit_test_setup(test_unix_absolute_path_on_absolute_returns_it, setup_path_unix),
124                 cmocka_unit_test_setup(test_unix_absolute_path_on_empty_returns_null, setup_path_unix),
125                 cmocka_unit_test_setup(test_unix_absolute_path_relative, setup_path_unix),
126                 cmocka_unit_test(test_int_to_str_return_expected),
127                 cmocka_unit_test(test_is_decimal_fail_empty),
128                 cmocka_unit_test(test_is_decimal_fail_hex),
129                 cmocka_unit_test(test_is_decimal_fail_junk_suffix),
130                 cmocka_unit_test(test_is_decimal_pass_simple),
131                 cmocka_unit_test(test_is_decimal_pass_signs),
132                 cmocka_unit_test(test_is_decimal_pass_whitespace_prefix),
133                 cmocka_unit_test(test_string_eq),
134         };
135
136 #ifdef HAVE_WINDOWS
137         cmocka_set_skip_filter("test_unix_*");
138 #endif
139
140         return cmocka_run_group_tests(tests, NULL, NULL);
141 }