]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_destroy.c
Add the blackbox container based 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 #include "execute_tests.h"
20 #include "test_cases_destroy.h"
21 #include "../common/containers.h"
22 #include "../common/test_step.h"
23 #include "../common/common_handlers.h"
24 #include <assert.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <setjmp.h>
29 #include <cmocka.h>
30 #include <sys/types.h>
31 #include <dirent.h>
32
33
34 /* Modify this to change the logging level of Meshlink */
35 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
36
37 static void test_case_meshlink_destroy_01(void **state);
38 static bool test_meshlink_destroy_01(void);
39 static void test_case_meshlink_destroy_02(void **state);
40 static bool test_meshlink_destroy_02(void);
41 static void test_case_meshlink_destroy_03(void **state);
42 static bool test_meshlink_destroy_03(void);
43
44 static black_box_state_t test_case_meshlink_destroy_01_state = {
45         .test_case_name = "test_case_meshlink_destroy_01",
46 };
47 static black_box_state_t test_case_meshlink_destroy_02_state = {
48         .test_case_name = "test_case_meshlink_destroy_02",
49 };
50 static black_box_state_t test_case_meshlink_destroy_03_state = {
51         .test_case_name = "test_case_meshlink_destroy_03",
52 };
53
54
55 /* Execute destroy Test Case # 1 - valid case*/
56 static void test_case_meshlink_destroy_01(void **state) {
57         execute_test(test_meshlink_destroy_01, state);
58 }
59
60 /* Test Steps for destroy Test Case # 1 - Valid case
61     Test Steps:
62     1. Open instance for NUT
63     2. Close NUT, and destroy the confbase
64     3. Open the same confbase directory
65
66     Expected Result:
67     confbase should be deleted
68 */
69 static bool test_meshlink_destroy_01(void) {
70         meshlink_set_log_cb(NULL, TEST_MESHLINK_LOG_LEVEL, meshlink_callback_logger);
71
72         // Create meshlink instance
73         char *confbase = "destroyconf";
74         mesh_handle = meshlink_open(confbase, "nut", "node_sim", 1);
75         assert(mesh_handle);
76
77         meshlink_close(mesh_handle);
78
79         // Destroying NUT's confbase
80         bool result = meshlink_destroy(confbase);
81         assert_int_equal(result, true);
82
83         // Verify whether confbase is removed or not
84         DIR *dir = opendir(confbase);
85         assert_int_equal(dir, NULL);
86
87         return true;
88 }
89
90 /* Execute destroy Test Case # 2 - passing NULL argument to the API */
91 static void test_case_meshlink_destroy_02(void **state) {
92         execute_test(test_meshlink_destroy_02, state);
93 }
94
95 /* Test Steps for destroy Test Case # 2 - Invalid case
96     Test Steps:
97     1. Just passing NULL as argument to the API
98
99     Expected Result:
100     Return false reporting failure
101 */
102 static bool test_meshlink_destroy_02(void) {
103         // Passing NULL as an argument to meshlink_destroy
104         bool result = meshlink_destroy(NULL);
105         assert_int_equal(result, false);
106
107         return true;
108 }
109
110 /* Execute status Test Case # 3 - destroying non existing file */
111 static void test_case_meshlink_destroy_03(void **state) {
112         execute_test(test_meshlink_destroy_03, state);
113 }
114 /* Test Steps for destroy Test Case # 3 - Invalid case
115     Test Steps:
116     1. unlink if there's any such test file
117     2. Call API with that file name
118
119     Expected Result:
120     Return false reporting failure
121 */
122 static bool test_meshlink_destroy_03(void) {
123         bool result = false;
124
125         // Deletes if there is any file named 'non_existing' already
126         unlink("non_existing");
127
128         // Passing non-existing file as an argument to meshlink_destroy
129         result = meshlink_destroy("non_existing");
130         assert_int_equal(result, false);
131
132         return true;
133 }
134
135
136 int test_meshlink_destroy(void) {
137         const struct CMUnitTest blackbox_destroy_tests[] = {
138                 cmocka_unit_test_prestate_setup_teardown(test_case_meshlink_destroy_01, NULL, NULL,
139                                 (void *)&test_case_meshlink_destroy_01_state),
140                 cmocka_unit_test_prestate_setup_teardown(test_case_meshlink_destroy_02, NULL, NULL,
141                                 (void *)&test_case_meshlink_destroy_02_state),
142                 cmocka_unit_test_prestate_setup_teardown(test_case_meshlink_destroy_03, NULL, NULL,
143                                 (void *)&test_case_meshlink_destroy_03_state)
144         };
145
146         total_tests += sizeof(blackbox_destroy_tests) / sizeof(blackbox_destroy_tests[0]);
147
148         return cmocka_run_group_tests(blackbox_destroy_tests, NULL, NULL);
149 }