]> git.meshlink.io Git - meshlink/blob - test/blackbox/test_case_meta_conn_03/node_sim_nut.c
Fix __warn_unused_result__, add more of it and fix the resulting warnings.
[meshlink] / test / blackbox / test_case_meta_conn_03 / node_sim_nut.c
1 /*
2     node_sim.c -- Implementation of Node Simulation for Meshlink Testing
3                     for meta connection test case 01 - re-connection of
4                     two nodes when relay node goes down
5     Copyright (C) 2018  Guus Sliepen <guus@meshlink.io>
6
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License along
18     with this program; if not, write to the Free Software Foundation, Inc.,
19     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <pthread.h>
25 #include "../common/common_handlers.h"
26 #include "../common/test_step.h"
27 #include "../common/mesh_event_handler.h"
28
29 static bool conn_status = false;
30
31 void callback_logger(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
32         (void)mesh;
33         (void)level;
34
35         char connection_match_msg[100];
36
37         fprintf(stderr, "meshlink>> %s\n", text);
38
39         if(strstr(text, "Connection") || strstr(text, "connection")) {
40                 assert(snprintf(connection_match_msg, sizeof(connection_match_msg),
41                                 "Connection with peer") >= 0);
42
43                 if(strstr(text, connection_match_msg) && strstr(text, "activated")) {
44                         conn_status = true;
45                         return;
46                 }
47
48                 assert(snprintf(connection_match_msg, sizeof(connection_match_msg),
49                                 "Already connected to peer") >= 0);
50
51                 if(strstr(text, connection_match_msg)) {
52                         conn_status = true;
53                         return;
54                 }
55
56                 assert(snprintf(connection_match_msg, sizeof(connection_match_msg),
57                                 "Connection closed by peer") >= 0);
58
59                 if(strstr(text, connection_match_msg)) {
60                         conn_status = false;
61                         return;
62                 }
63
64                 assert(snprintf(connection_match_msg, sizeof(connection_match_msg),
65                                 "Closing connection with peer") >= 0);
66
67                 if(strstr(text, connection_match_msg)) {
68                         conn_status = false;
69                         return;
70                 }
71         }
72
73         return;
74 }
75
76 int main(int argc, char *argv[]) {
77         (void)argc;
78
79         int client_id = -1;
80         bool result = false;
81         int i;
82
83         if((argv[3]) && (argv[4])) {
84                 client_id = atoi(argv[3]);
85                 mesh_event_sock_connect(argv[4]);
86         }
87
88         execute_open(argv[1], argv[2]);
89         meshlink_set_log_cb(mesh_handle, MESHLINK_DEBUG, callback_logger);
90
91         if(argv[5]) {
92                 execute_join(argv[5]);
93         }
94
95         execute_start();
96         mesh_event_sock_send(client_id, NODE_STARTED, NULL, 0);
97
98         /* Connectivity of peer is checked using meshlink_get_node API */
99         while(!conn_status) {
100                 sleep(1);
101         }
102
103         sleep(1);
104         fprintf(stderr, "Connected with Peer\n");
105         mesh_event_sock_send(client_id, META_CONN_SUCCESSFUL, NULL, 0);
106
107         conn_status = false;
108         fprintf(stderr, "Waiting 120 sec for peer to be re-connected\n");
109
110         for(i = 0; i < 120; i++) {
111                 if(conn_status) {
112                         result = true;
113                         break;
114                 }
115
116                 sleep(1);
117         }
118
119         if(result) {
120                 fprintf(stderr, "Re-connected with Peer\n");
121                 mesh_event_sock_send(client_id, META_RECONN_SUCCESSFUL, NULL, 0);
122         } else {
123                 fprintf(stderr, "Failed to reconnect with Peer\n");
124                 mesh_event_sock_send(client_id, META_RECONN_FAILURE, NULL, 0);
125         }
126
127         execute_close();
128         assert(meshlink_destroy(argv[1]));
129
130         return 0;
131 }