]> git.meshlink.io Git - meshlink/blob - test/blackbox/run_blackbox_tests/test_cases_submesh02.c
Add meshlink_get_submesh API
[meshlink] / test / blackbox / run_blackbox_tests / test_cases_submesh02.c
1 /*
2     test_cases_submesh02.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_submesh02.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 APP2NODE1_ID  "2"
35 #define CORENODE2_ID  "3"
36 #define APP1NODE2_ID  "4"
37 #define APP2NODE2_ID  "5"
38
39 #define INIT_ST         0
40
41 static bool test_case_status = false;
42
43 static void test_case_submesh_02(void **state);
44 static bool test_steps_submesh_02(void);
45
46 static char event_node_name[][10] = {"CORENODE1", "APP1NODE1", "APP2NODE1", "CORENODE2",
47                                      "APP1NODE2", "APP2NODE2"
48                                     };
49 static const char *node_ids[] = { "corenode1", "app1node1", "app2node1", "corenode2",
50                                   "app1node2", "app2node2"
51                                 };
52
53 static mesh_event_t core_node1[] = { NODE_STARTED, CHANNEL_OPENED, CHANNEL_DATA_RECIEVED};
54
55 static mesh_event_t core_node2[] = { NODE_STARTED, NODE_JOINED, CHANNEL_OPENED, CHANNEL_DATA_RECIEVED};
56
57 static mesh_event_t app1_node1[] = { NODE_STARTED, NODE_JOINED, CHANNEL_OPENED, CHANNEL_DATA_RECIEVED};
58
59 static mesh_event_t app2_node1[] = { NODE_STARTED, NODE_JOINED, CHANNEL_OPENED, CHANNEL_DATA_RECIEVED};
60
61 static mesh_event_t app1_node2[] = { NODE_STARTED, NODE_JOINED, CHANNEL_OPENED, CHANNEL_DATA_RECIEVED, CHANNEL_OPENED, CHANNEL_DATA_RECIEVED, MESH_EVENT_COMPLETED};
62
63 static mesh_event_t app2_node2[] = { NODE_STARTED, NODE_JOINED, CHANNEL_OPENED, CHANNEL_DATA_RECIEVED, CHANNEL_OPENED, CHANNEL_DATA_RECIEVED, MESH_EVENT_COMPLETED};
64
65 /* State structure for SubMesh Test Case #1 */
66 static char *test_case_submesh_2_nodes[] = { "corenode1", "app1node1", "app2node1", "corenode2", "app1node2", "app2node2" };
67 static black_box_state_t test_case_submesh_2_state = {
68         .test_case_name =  "test_cases_submesh02",
69         .node_names =  test_case_submesh_2_nodes,
70         .num_nodes =  6
71 };
72
73 static int black_box_group0_setup(void **state) {
74         const char *nodes[] = { "corenode1", "app1node1", "app2node1", "corenode2", "app1node2", "app2node2" };
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         PRINT_TEST_CASE_MSG("Destroying Containers\n");
86         destroy_containers();
87
88         return 0;
89 }
90
91 static int black_box_all_nodes_setup(void **state) {
92         const char *nodes[] = { "corenode1" };
93         int num_nodes = sizeof(nodes) / sizeof(nodes[0]);
94
95         PRINT_TEST_CASE_MSG("Creating Containers\n");
96         destroy_containers();
97         create_containers(nodes, num_nodes);
98         PRINT_TEST_CASE_MSG("Created Containers\n");
99         return 0;
100 }
101
102 static bool event_cb(mesh_event_payload_t payload) {
103         static node_status_t node_status[6] = {
104                 {core_node1, 0, 3, false},
105                 {app1_node1, 0, 4, false},
106                 {app2_node1, 0, 4, false},
107                 {core_node2, 0, 4, false},
108                 {app1_node2, 0, 7, false},
109                 {app2_node2, 0, 7, false}
110         };
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, 5, (char **)node_ids);
117         }
118
119         if(check_nodes_finished(node_status, 6)) {
120                 test_case_status = true;
121                 return true;
122         }
123
124         return false;
125 }
126
127 /* Execute SubMesh Test Case # 2 */
128 static void test_case_submesh_02(void **state) {
129         execute_test(test_steps_submesh_02, state);
130 }
131
132 /* Test Steps for SubMesh Test Case # 2
133
134     Test Steps:
135     1. Run corenode1, app1node1, app2node1, corenode2, app1node2 and app2node2
136     2. Generate invites to app1node1, app2node1, corenode2, app1node2 and app2node2
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 fetch the list of all nodes and check if the nodes in other submesh doesnot
140        appear in the list.
141     5. Try fetch all the nodes with a submesh handle and check only if both the nodes joining
142        the submesh are present.
143
144     Expected Result:
145     Channels should be formed between nodes of sub-mesh & coremesh, nodes with in sub-mesh
146     and should be able to exchange data. Lis of all nodes should only contain four nodes
147     and the list of submesh should only contain two nodes of that submesh.
148 */
149 static bool test_steps_submesh_02(void) {
150         char *invite_corenode2, *invite_app1node1, *invite_app2node1, *invite_app1node2, *invite_app2node2;
151         bool result = false;
152         int i;
153         char *import;
154         pthread_t thread1, thread2;
155
156         import = mesh_event_sock_create(eth_if_name);
157         invite_corenode2 = invite_in_container("corenode1", "corenode2");
158         invite_app1node1 = submesh_invite_in_container("corenode1", "app1node1", "app1");
159         invite_app2node1 = submesh_invite_in_container("corenode1", "app2node1", "app2");
160         invite_app1node2 = submesh_invite_in_container("corenode1", "app1node2", "app1");
161         invite_app2node2 = submesh_invite_in_container("corenode1", "app2node2", "app2");
162
163         node_sim_in_container_event("corenode1", "1", NULL, CORENODE1_ID, import);
164         node_sim_in_container_event("corenode2", "1", invite_corenode2, CORENODE2_ID, import);
165         node_sim_in_container_event("app1node1", "1", invite_app1node1, APP1NODE1_ID, import);
166         node_sim_in_container_event("app2node1", "1", invite_app2node1, APP2NODE1_ID, import);
167         node_sim_in_container_event("app1node2", "1", invite_app1node2, APP1NODE2_ID, import);
168         node_sim_in_container_event("app2node2", "1", invite_app2node2, APP2NODE2_ID, import);
169
170         PRINT_TEST_CASE_MSG("Waiting for nodes to get connected with corenode1\n");
171
172         assert(wait_for_event(event_cb, 240));
173         assert(test_case_status);
174
175         free(invite_corenode2);
176         free(invite_app1node1);
177         free(invite_app2node1);
178         free(invite_app1node2);
179         free(invite_app2node2);
180
181         mesh_event_destroy();
182
183         return true;
184 }
185
186 int test_cases_submesh02(void) {
187         const struct CMUnitTest blackbox_group0_tests[] = {
188                 cmocka_unit_test_prestate_setup_teardown(test_case_submesh_02, setup_test, teardown_test,
189                                 (void *)&test_case_submesh_2_state)
190         };
191         total_tests += sizeof(blackbox_group0_tests) / sizeof(blackbox_group0_tests[0]);
192
193         return cmocka_run_group_tests(blackbox_group0_tests, black_box_group0_setup, black_box_group0_teardown);
194 }