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.
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"
38 /* Modify this to change the logging level of Meshlink */
39 #define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG
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);
46 /* log variable gives access to the log callback to know whether invoked or not */
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",
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",
61 static void log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
65 fprintf(stderr, "Received log text : %s\n", text);
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);
73 /* Test Steps for meshlink_set_receive_cb Test Case # 1
76 1. Run relay and Open NUT
77 2. Set log callback for the NUT and Start NUT
80 log callback should be invoked when NUT joins with relay.
82 static bool test_set_log_cb_01(void) {
83 assert(meshlink_destroy("logconf"));
85 // Create meshlink instance for NUT
87 meshlink_handle_t *mesh = meshlink_open("logconf", "nut", "test", DEV_CLASS_STATIONARY);
90 // Set up logging for Meshlink with the newly acquired Mesh Handle
93 meshlink_set_log_cb(mesh, TEST_MESHLINK_LOG_LEVEL, log_cb);
95 // Starting node to log
97 bool mesh_start = meshlink_start(mesh);
102 assert_int_equal(ret, true);
104 // closing meshes and destroying confbase
106 meshlink_close(mesh);
107 assert(meshlink_destroy("logconf"));
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);
116 /* Test Steps for meshlink_set_poll_cb Test Case # 2
119 1. Calling meshlink_set_poll_cb with some invalid integer other than the valid enums.
122 set poll callback handles the invalid parameter when called by giving proper error number.
124 static bool test_set_log_cb_02(void) {
126 // Setting an invalid level
128 meshlink_set_log_cb(NULL, 1000, NULL);
129 assert_int_equal(meshlink_errno, MESHLINK_EINVAL);
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)
142 total_tests += sizeof(blackbox_log_tests) / sizeof(blackbox_log_tests[0]);
144 int failed = cmocka_run_group_tests(blackbox_log_tests, NULL, NULL);