]> git.meshlink.io Git - meshlink/blob - test/blackbox/test_case_meta_conn_01/node_sim_nut.c
Fix __warn_unused_result__, add more of it and fix the resulting warnings.
[meshlink] / test / blackbox / test_case_meta_conn_01 / 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 #define CMD_LINE_ARG_NODENAME   1
30 #define CMD_LINE_ARG_DEVCLASS   2
31 #define CMD_LINE_ARG_CLIENTID   3
32 #define CMD_LINE_ARG_IMPORTSTR  4
33 #define CMD_LINE_ARG_INVITEURL  5
34
35 static bool conn_status = false;
36
37 static void callback_logger(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
38         (void)mesh;
39         (void)level;
40
41         char connection_match_msg[100];
42
43         fprintf(stderr, "meshlink>> %s\n", text);
44
45         if(strstr(text, "Connection") || strstr(text, "connection")) {
46                 assert(snprintf(connection_match_msg, sizeof(connection_match_msg),
47                                 "Connection with peer") >= 0);
48
49                 if(strstr(text, connection_match_msg) && strstr(text, "activated")) {
50                         conn_status = true;
51                         return;
52                 }
53
54                 assert(snprintf(connection_match_msg, sizeof(connection_match_msg),
55                                 "Already connected to peer") >= 0);
56
57                 if(strstr(text, connection_match_msg)) {
58                         conn_status = true;
59                         return;
60                 }
61
62                 assert(snprintf(connection_match_msg, sizeof(connection_match_msg),
63                                 "Connection closed by peer") >= 0);
64
65                 if(strstr(text, connection_match_msg)) {
66                         conn_status = false;
67                         return;
68                 }
69
70                 assert(snprintf(connection_match_msg, sizeof(connection_match_msg),
71                                 "Closing connection with peer") >= 0);
72
73                 if(strstr(text, connection_match_msg)) {
74                         conn_status = false;
75                         return;
76                 }
77         }
78
79         return;
80 }
81
82 int main(int argc, char *argv[]) {
83         (void)argc;
84
85         int client_id = -1;
86
87         if((argv[3]) && (argv[4])) {
88                 client_id = atoi(argv[3]);
89                 mesh_event_sock_connect(argv[4]);
90         }
91
92         execute_open(argv[1], argv[2]);
93         meshlink_set_log_cb(mesh_handle, MESHLINK_DEBUG, callback_logger);
94
95         if(argv[5]) {
96                 execute_join(argv[5]);
97         }
98
99         execute_start();
100
101         if(!mesh_event_sock_send(client_id, NODE_STARTED, NULL, 0)) {
102                 fprintf(stderr, "Trying to resend mesh event\n");
103                 sleep(1);
104         }
105
106         /* Connectivity of peer */
107         while(!conn_status) {
108                 sleep(1);
109         }
110
111         fprintf(stderr, "Connected with Peer\n");
112         assert(mesh_event_sock_send(client_id, META_CONN_SUCCESSFUL, NULL, 0));
113
114         /* Connectivity of peer */
115         while(conn_status) {
116                 sleep(1);
117         }
118
119         fprintf(stderr, "Closed connection with Peer\n");
120         assert(mesh_event_sock_send(client_id, META_CONN_CLOSED, NULL, 0));
121
122         /* Connectivity of peer */
123         while(!conn_status) {
124                 sleep(1);
125         }
126
127         fprintf(stderr, "Connected with Peer\n");
128         assert(mesh_event_sock_send(client_id, META_RECONN_SUCCESSFUL, NULL, 0));
129
130         execute_close();
131         assert(meshlink_destroy(argv[1]));
132         return 0;
133 }