Extract filesystem-related functions into fs.c
[tinc] / test / unit / test_utils.c
1 #include "unittest.h"
2 #include "../../src/utils.h"
3
4 static void test_int_to_str(const char *ref, int num) {
5         char *result = int_to_str(num);
6         assert_string_equal(ref, result);
7         free(result);
8 }
9
10 static void test_int_to_str_return_expected(void **state) {
11         (void)state;
12
13         test_int_to_str("0", 0);
14         test_int_to_str("-1337", -1337);
15         test_int_to_str("65535", 65535);
16 }
17
18 static void test_is_decimal_fail_empty(void **state) {
19         (void)state;
20
21         assert_false(is_decimal(NULL));
22         assert_false(is_decimal(""));
23 }
24
25 static void test_is_decimal_fail_hex(void **state) {
26         (void)state;
27
28         assert_false(is_decimal("DEADBEEF"));
29         assert_false(is_decimal("0xCAFE"));
30 }
31
32 static void test_is_decimal_fail_junk_suffix(void **state) {
33         (void)state;
34
35         assert_false(is_decimal("123foobar"));
36         assert_false(is_decimal("777 "));
37 }
38
39 static void test_is_decimal_pass_simple(void **state) {
40         (void)state;
41
42         assert_true(is_decimal("0"));
43         assert_true(is_decimal("123"));
44 }
45
46 static void test_is_decimal_pass_signs(void **state) {
47         (void)state;
48
49         assert_true(is_decimal("-123"));
50         assert_true(is_decimal("+123"));
51 }
52
53 static void test_is_decimal_pass_whitespace_prefix(void **state) {
54         (void)state;
55
56         assert_true(is_decimal(" \r\n\t 777"));
57 }
58
59 static void test_string_eq(void **state) {
60         (void)state;
61
62         assert_true(string_eq(NULL, NULL));
63         assert_true(string_eq("", ""));
64         assert_true(string_eq("\tfoo 123", "\tfoo 123"));
65
66         assert_false(string_eq(NULL, ""));
67         assert_false(string_eq("", NULL));
68         assert_false(string_eq("foo", "FOO"));
69         assert_false(string_eq("foo", " foo"));
70 }
71
72 int main(void) {
73         const struct CMUnitTest tests[] = {
74                 cmocka_unit_test(test_int_to_str_return_expected),
75                 cmocka_unit_test(test_is_decimal_fail_empty),
76                 cmocka_unit_test(test_is_decimal_fail_hex),
77                 cmocka_unit_test(test_is_decimal_fail_junk_suffix),
78                 cmocka_unit_test(test_is_decimal_pass_simple),
79                 cmocka_unit_test(test_is_decimal_pass_signs),
80                 cmocka_unit_test(test_is_decimal_pass_whitespace_prefix),
81                 cmocka_unit_test(test_string_eq),
82         };
83
84 #ifdef HAVE_WINDOWS
85         cmocka_set_skip_filter("test_unix_*");
86 #endif
87
88         return cmocka_run_group_tests(tests, NULL, NULL);
89 }