2 test_cases_set_log_cb.c -- Execution of specific meshlink black box test cases
3 Copyright (C) 2018 Guus Sliepen <guus@meshlink.io>
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.
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.
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.
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"
34 /* Modify this to change the logging level of Meshlink */
35 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
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);
44 /* log variable gives access to the log callback to know whether invoked or not */
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",
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",
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);
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);
68 /* Test Steps for meshlink_set_receive_cb Test Case # 1
71 1. Run relay and Open NUT
72 2. Set log callback for the NUT and Start NUT
75 log callback should be invoked when NUT joins with relay.
77 static bool test_set_log_cb_01(void) {
78 meshlink_destroy("logconf");
80 // Create meshlink instance for NUT
82 meshlink_handle_t *mesh = meshlink_open("logconf", "nut", "test", DEV_CLASS_STATIONARY);
85 // Set up logging for Meshlink with the newly acquired Mesh Handle
88 meshlink_set_log_cb(mesh, TEST_MESHLINK_LOG_LEVEL, log_cb);
90 // Starting node to log
92 bool mesh_start = meshlink_start(mesh);
97 assert_int_equal(ret, true);
99 // closing meshes and destroying confbase
101 meshlink_close(mesh);
102 meshlink_destroy("logconf");
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);
111 /* Test Steps for meshlink_set_poll_cb Test Case # 2
114 1. Calling meshlink_set_poll_cb with some invalid integer other than the valid enums.
117 set poll callback handles the invalid parameter when called by giving proper error number.
119 static bool test_set_log_cb_02(void) {
121 // Setting an invalid level
123 meshlink_set_log_cb(NULL, 1000, NULL);
124 assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
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)
137 total_tests += sizeof(blackbox_log_tests) / sizeof(blackbox_log_tests[0]);
139 int failed = cmocka_run_group_tests(blackbox_log_tests, NULL, NULL);