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);
42 /* log variable gives access to the log callback to know whether invoked or not */
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",
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",
57 static void log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
61 fprintf(stderr, "Received log text : %s\n", text);
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);
69 /* Test Steps for meshlink_set_receive_cb Test Case # 1
72 1. Run relay and Open NUT
73 2. Set log callback for the NUT and Start NUT
76 log callback should be invoked when NUT joins with relay.
78 static bool test_set_log_cb_01(void) {
79 meshlink_destroy("logconf");
81 // Create meshlink instance for NUT
83 meshlink_handle_t *mesh = meshlink_open("logconf", "nut", "test", DEV_CLASS_STATIONARY);
86 // Set up logging for Meshlink with the newly acquired Mesh Handle
89 meshlink_set_log_cb(mesh, TEST_MESHLINK_LOG_LEVEL, log_cb);
91 // Starting node to log
93 bool mesh_start = meshlink_start(mesh);
98 assert_int_equal(ret, true);
100 // closing meshes and destroying confbase
102 meshlink_close(mesh);
103 meshlink_destroy("logconf");
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);
112 /* Test Steps for meshlink_set_poll_cb Test Case # 2
115 1. Calling meshlink_set_poll_cb with some invalid integer other than the valid enums.
118 set poll callback handles the invalid parameter when called by giving proper error number.
120 static bool test_set_log_cb_02(void) {
122 // Setting an invalid level
124 meshlink_set_log_cb(NULL, 1000, NULL);
125 assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
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)
138 total_tests += sizeof(blackbox_log_tests) / sizeof(blackbox_log_tests[0]);
140 int failed = cmocka_run_group_tests(blackbox_log_tests, NULL, NULL);