]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_destroy.c
Ensure NDEBUG is not set in the test suite.
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_destroy.c
1 /*
2     test_cases_destroy.c -- Execution of specific meshlink black box test cases
3     Copyright (C) 2018  Guus Sliepen <guus@meshlink.io>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifdef NDEBUG
21 #undef NDEBUG
22 #endif
23
24 #include "execute_tests.h"
25 #include "test_cases_destroy.h"
26 #include "../common/containers.h"
27 #include "../common/test_step.h"
28 #include "../common/common_handlers.h"
29 #include <assert.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <stdarg.h>
33 #include <setjmp.h>
34 #include <cmocka.h>
35 #include <sys/types.h>
36 #include <dirent.h>
37
38
39 /* Modify this to change the logging level of Meshlink */
40 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
41
42 static void test_case_meshlink_destroy_01(void **state);
43 static bool test_meshlink_destroy_01(void);
44 static void test_case_meshlink_destroy_02(void **state);
45 static bool test_meshlink_destroy_02(void);
46 static void test_case_meshlink_destroy_03(void **state);
47 static bool test_meshlink_destroy_03(void);
48
49 static black_box_state_t test_case_meshlink_destroy_01_state = {
50         .test_case_name = "test_case_meshlink_destroy_01",
51 };
52 static black_box_state_t test_case_meshlink_destroy_02_state = {
53         .test_case_name = "test_case_meshlink_destroy_02",
54 };
55 static black_box_state_t test_case_meshlink_destroy_03_state = {
56         .test_case_name = "test_case_meshlink_destroy_03",
57 };
58
59
60 /* Execute destroy Test Case # 1 - valid case*/
61 static void test_case_meshlink_destroy_01(void **state) {
62         execute_test(test_meshlink_destroy_01, state);
63 }
64
65 /* Test Steps for destroy Test Case # 1 - Valid case
66     Test Steps:
67     1. Open instance for NUT
68     2. Close NUT, and destroy the confbase
69     3. Open the same confbase directory
70
71     Expected Result:
72     confbase should be deleted
73 */
74 static bool test_meshlink_destroy_01(void) {
75         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
76
77         // Create meshlink instance
78         char *confbase = "destroyconf";
79         mesh_handle = meshlink_open(confbase, "nut", "node_sim", 1);
80         assert(mesh_handle);
81
82         meshlink_close(mesh_handle);
83
84         // Destroying NUT's confbase
85         bool result = meshlink_destroy(confbase);
86         assert_int_equal(result, true);
87
88         // Verify whether confbase is removed or not
89         DIR *dir = opendir(confbase);
90         assert_int_equal(dir, NULL);
91
92         return true;
93 }
94
95 /* Execute destroy Test Case # 2 - passing NULL argument to the API */
96 static void test_case_meshlink_destroy_02(void **state) {
97         execute_test(test_meshlink_destroy_02, state);
98 }
99
100 /* Test Steps for destroy Test Case # 2 - Invalid case
101     Test Steps:
102     1. Just passing NULL as argument to the API
103
104     Expected Result:
105     Return false reporting failure
106 */
107 static bool test_meshlink_destroy_02(void) {
108         // Passing NULL as an argument to meshlink_destroy
109         bool result = meshlink_destroy(NULL);
110         assert_int_equal(result, false);
111
112         return true;
113 }
114
115 /* Execute status Test Case # 3 - destroying non existing file */
116 static void test_case_meshlink_destroy_03(void **state) {
117         execute_test(test_meshlink_destroy_03, state);
118 }
119 /* Test Steps for destroy Test Case # 3 - Invalid case
120     Test Steps:
121     1. unlink if there's any such test file
122     2. Call API with that file name
123
124     Expected Result:
125     Return false reporting failure
126 */
127 static bool test_meshlink_destroy_03(void) {
128         bool result = false;
129
130         // Deletes if there is any file named 'non_existing' already
131         unlink("non_existing");
132
133         // Passing non-existing file as an argument to meshlink_destroy
134         result = meshlink_destroy("non_existing");
135         assert_int_equal(result, false);
136
137         return true;
138 }
139
140
141 int test_meshlink_destroy(void) {
142         const struct CMUnitTest blackbox_destroy_tests[] = {
143                 cmocka_unit_test_prestate_setup_teardown(test_case_meshlink_destroy_01, NULL, NULL,
144                                 (void *)&test_case_meshlink_destroy_01_state),
145                 cmocka_unit_test_prestate_setup_teardown(test_case_meshlink_destroy_02, NULL, NULL,
146                                 (void *)&test_case_meshlink_destroy_02_state),
147                 cmocka_unit_test_prestate_setup_teardown(test_case_meshlink_destroy_03, NULL, NULL,
148                                 (void *)&test_case_meshlink_destroy_03_state)
149         };
150
151         total_tests += sizeof(blackbox_destroy_tests) / sizeof(blackbox_destroy_tests[0]);
152
153         return cmocka_run_group_tests(blackbox_destroy_tests, NULL, NULL);
154 }