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