Add unit tests suite using cmocka library
[tinc] / test / unit / test_net.c
1 #include "unittest.h"
2 #include "../../src/net.h"
3 #include "../../src/script.h"
4
5 static environment_t *device_env = NULL;
6
7 // silence -Wmissing-prototypes
8 void __wrap_environment_init(environment_t *env);
9 void __wrap_environment_exit(environment_t *env);
10 bool __wrap_execute_script(const char *name, environment_t *env);
11
12 void __wrap_environment_init(environment_t *env) {
13         assert_non_null(env);
14         assert_null(device_env);
15         device_env = env;
16 }
17
18 void __wrap_environment_exit(environment_t *env) {
19         assert_ptr_equal(device_env, env);
20         device_env = NULL;
21 }
22
23 bool __wrap_execute_script(const char *name, environment_t *env) {
24         (void)env;
25
26         check_expected_ptr(name);
27
28         // Used instead of mock_type(bool) to silence clang warning
29         return mock() ? true : false;
30 }
31
32 static void run_device_enable_disable(void (*device_func)(void),
33                                       const char *script) {
34         expect_string(__wrap_execute_script, name, script);
35         will_return(__wrap_execute_script, true);
36
37         device_func();
38 }
39
40 static void test_device_enable_calls_tinc_up(void **state) {
41         (void)state;
42
43         run_device_enable_disable(&device_enable, "tinc-up");
44 }
45
46 static void test_device_disable_calls_tinc_down(void **state) {
47         (void)state;
48
49         run_device_enable_disable(&device_disable, "tinc-down");
50 }
51
52 int main(void) {
53         const struct CMUnitTest tests[] = {
54                 cmocka_unit_test(test_device_enable_calls_tinc_up),
55                 cmocka_unit_test(test_device_disable_calls_tinc_down),
56         };
57         return cmocka_run_group_tests(tests, NULL, NULL);
58 }