]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_set_log_cb.c
Fix compiler warnings in the test suites.
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_set_log_cb.c
1 /*
2     test_cases_set_log_cb.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 #include "execute_tests.h"
21 #include "test_cases_set_log_cb.h"
22 #include "../common/containers.h"
23 #include "../common/test_step.h"
24 #include "../common/common_handlers.h"
25 #include <assert.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <setjmp.h>
30 #include <cmocka.h>
31 #include <pthread.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_set_log_cb_01(void **state);
38 static bool test_set_log_cb_01(void);
39 static void test_case_set_log_cb_02(void **state);
40 static bool test_set_log_cb_02(void);
41
42 /* log variable gives access to the log callback to know whether invoked or not */
43 static bool log;
44
45 /* State structure for log callback Test Case #1 */
46 static black_box_state_t test_case_set_log_cb_01_state = {
47         .test_case_name = "test_case_set_log_cb_01",
48 };
49
50 /* State structure for log callback Test Case #2 */
51 static black_box_state_t test_case_set_log_cb_02_state = {
52         .test_case_name = "test_case_set_log_cb_02",
53 };
54
55
56 /* log callback */
57 static void log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
58         (void)mesh;
59         (void)level;
60
61         fprintf(stderr, "Received log text : %s\n", text);
62         log = true;
63 }
64
65 /* Execute meshlink_set_log_cb Test Case # 1 - Valid case */
66 static void test_case_set_log_cb_01(void **state) {
67         execute_test(test_set_log_cb_01, state);
68 }
69 /* Test Steps for meshlink_set_receive_cb Test Case # 1
70
71     Test Steps:
72     1. Run relay and Open NUT
73     2. Set log callback for the NUT and Start NUT
74
75     Expected Result:
76     log callback should be invoked when NUT joins with relay.
77 */
78 static bool test_set_log_cb_01(void) {
79         meshlink_destroy("logconf");
80
81         // Create meshlink instance for NUT
82
83         meshlink_handle_t *mesh = meshlink_open("logconf", "nut", "test", DEV_CLASS_STATIONARY);
84         assert(mesh != NULL);
85
86         // Set up logging for Meshlink with the newly acquired Mesh Handle
87
88         log = false;
89         meshlink_set_log_cb(mesh, TEST_MESHLINK_LOG_LEVEL, log_cb);
90
91         // Starting node to log
92
93         bool mesh_start = meshlink_start(mesh);
94         assert(mesh_start);
95
96         bool ret = log;
97
98         assert_int_equal(ret, true);
99
100         // closing meshes and destroying confbase
101
102         meshlink_close(mesh);
103         meshlink_destroy("logconf");
104
105         return true;
106 }
107
108 /* Execute meshlink_set_log_cb Test Case # 2 - Invalid case */
109 static void test_case_set_log_cb_02(void **state) {
110         execute_test(test_set_log_cb_02, state);
111 }
112 /* Test Steps for meshlink_set_poll_cb Test Case # 2
113
114     Test Steps:
115     1. Calling meshlink_set_poll_cb with some invalid integer other than the valid enums.
116
117     Expected Result:
118     set poll callback handles the invalid parameter when called by giving proper error number.
119 */
120 static bool test_set_log_cb_02(void) {
121
122         // Setting an invalid level
123
124         meshlink_set_log_cb(NULL, 1000, NULL);
125         assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
126
127         return true;
128 }
129
130
131 int test_meshlink_set_log_cb(void) {
132         const struct CMUnitTest blackbox_log_tests[] = {
133                 cmocka_unit_test_prestate_setup_teardown(test_case_set_log_cb_01, NULL, NULL,
134                                 (void *)&test_case_set_log_cb_01_state),
135                 cmocka_unit_test_prestate_setup_teardown(test_case_set_log_cb_02, NULL, NULL,
136                                 (void *)&test_case_set_log_cb_02_state)
137         };
138         total_tests += sizeof(blackbox_log_tests) / sizeof(blackbox_log_tests[0]);
139
140         int failed = cmocka_run_group_tests(blackbox_log_tests, NULL, NULL);
141
142         return failed;
143 }