]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_submesh03.c
Ensure NDEBUG is not set in the test suite.
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_submesh03.c
1 /*
2     test_cases_submesh03.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 <stdlib.h>
25 #include <stdarg.h>
26 #include <setjmp.h>
27 #include <cmocka.h>
28 #include <assert.h>
29 #include "execute_tests.h"
30 #include "test_cases_submesh03.h"
31 #include "pthread.h"
32 #include "../common/containers.h"
33 #include "../common/test_step.h"
34 #include "../common/common_handlers.h"
35 #include "../common/mesh_event_handler.h"
36
37 #define CORENODE1_ID  "0"
38 #define APP1NODE1_ID  "1"
39 #define APP1NODE2_ID  "2"
40
41 #define INIT_ST         0
42
43 static bool test_case_status = false;
44
45 static void test_case_submesh_03(void **state);
46 static bool test_steps_submesh_03(void);
47
48 static char event_node_name[][10] = {"CORENODE1", "APP1NODE1", "APP1NODE2"};
49 static const char *node_ids[] = { "corenode1", "app1node1", "app1node2" };
50
51 static mesh_event_t core_node1[] = { NODE_STARTED, CHANNEL_OPENED, CHANNEL_DATA_RECIEVED};
52
53 static mesh_event_t app1_node1[] = { NODE_STARTED, NODE_JOINED, CHANNEL_OPENED, CHANNEL_DATA_RECIEVED};
54
55 static mesh_event_t app1_node2[] = { NODE_STARTED, NODE_JOINED, CHANNEL_OPENED, CHANNEL_DATA_RECIEVED, CHANNEL_OPENED, CHANNEL_DATA_RECIEVED, MESH_EVENT_COMPLETED};
56
57 static node_status_t node_status[3] = {
58         {core_node1, 0, 3},
59         {app1_node1, 0, 4},
60         {app1_node2, 0, 7},
61 };
62
63 /* State structure for SubMesh Test Case #3 */
64 static char *test_case_submesh_3_nodes[] = { "corenode1", "app1node1", "app1node2" };
65 static black_box_state_t test_case_submesh_3_state = {
66         .test_case_name =  "test_cases_submesh03",
67         .node_names =  test_case_submesh_3_nodes,
68         .num_nodes =  3
69 };
70
71 static int black_box_group0_setup(void **state) {
72         (void)state;
73
74         const char *nodes[] = { "corenode1", "app1node1", "app1node2" };
75         int num_nodes = sizeof(nodes) / sizeof(nodes[0]);
76
77         PRINT_TEST_CASE_MSG("Creating Containers\n");
78         destroy_containers();
79         create_containers(nodes, num_nodes);
80
81         return 0;
82 }
83
84 static int black_box_group0_teardown(void **state) {
85         (void)state;
86
87         PRINT_TEST_CASE_MSG("Destroying Containers\n");
88         destroy_containers();
89
90         return 0;
91 }
92
93 static void restart_all_nodes(char *import) {
94         int i;
95
96         for(i = 0; i < 3; i++) {
97                 node_step_in_container(node_ids[i], "SIGTERM");
98                 node_status[i].current_index = 0;
99         }
100
101         sleep(5);
102
103         node_sim_in_container_event("corenode1", "1", NULL, CORENODE1_ID, import);
104         node_sim_in_container_event("app1node1", "1", NULL, APP1NODE1_ID, import);
105         node_sim_in_container_event("app1node2", "1", NULL, APP1NODE2_ID, import);
106 }
107
108 static bool event_cb(mesh_event_payload_t payload) {
109
110         fprintf(stderr, "%s(%lu) : %s\n", event_node_name[payload.client_id], time(NULL), event_status[payload.mesh_event]);
111         assert(change_state(&node_status[payload.client_id], payload.mesh_event));
112
113         if(payload.mesh_event == NODE_JOINED) {
114                 signal_node_start(node_status, 1, 2, (char **)node_ids);
115         }
116
117         if(check_nodes_finished(node_status, 3)) {
118                 test_case_status = true;
119                 return true;
120         }
121
122         return false;
123 }
124
125 /* Execute SubMesh Test Case # 3 */
126 static void test_case_submesh_03(void **state) {
127         execute_test(test_steps_submesh_03, state);
128 }
129
130 /* Test Steps for SubMesh Test Case # 3
131
132     Test Steps:
133     1. Run corenode1, app1node1, and app1node2
134     2. Generate invites to app1node1, and app1node2
135         from corenode1 to join corenode1.
136     3. After Join is successful start channels from all nodes and exchange data on channels
137     4. Try to restart all the nodes at the same time.
138
139     Expected Result:
140     Channels should be formed between nodes of sub-mesh & coremesh, nodes with in sub-mesh
141     and should be able to exchange data even after restart.
142 */
143 static bool test_steps_submesh_03(void) {
144         char *invite_app1node1, *invite_app1node2;
145         char *import;
146
147         import = mesh_event_sock_create(eth_if_name);
148         invite_app1node1 = invite_in_container("corenode1", "app1node1");
149         invite_app1node2 = invite_in_container("corenode1", "app1node2");
150
151         node_sim_in_container_event("corenode1", "1", NULL, CORENODE1_ID, import);
152         node_sim_in_container_event("app1node1", "1", invite_app1node1, APP1NODE1_ID, import);
153         node_sim_in_container_event("app1node2", "1", invite_app1node2, APP1NODE2_ID, import);
154
155         PRINT_TEST_CASE_MSG("Waiting for nodes to get connected with corenode1\n");
156
157         assert(wait_for_event(event_cb, 120));
158         assert(test_case_status);
159
160         test_case_status = false;
161
162         restart_all_nodes(import);
163         PRINT_TEST_CASE_MSG("Waiting for nodes to get restarted\n");
164
165         assert(wait_for_event(event_cb, 120));
166         assert(test_case_status);
167
168         free(invite_app1node1);
169         free(invite_app1node2);
170
171         mesh_event_destroy();
172
173         return true;
174 }
175
176 int test_cases_submesh03(void) {
177         const struct CMUnitTest blackbox_group0_tests[] = {
178                 cmocka_unit_test_prestate_setup_teardown(test_case_submesh_03, setup_test, teardown_test,
179                                 (void *)&test_case_submesh_3_state)
180         };
181         total_tests += sizeof(blackbox_group0_tests) / sizeof(blackbox_group0_tests[0]);
182
183         return cmocka_run_group_tests(blackbox_group0_tests, black_box_group0_setup, black_box_group0_teardown);
184 }