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