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