also config files
[oweals/gnunet.git] / src / transport / test_transport_api_restart_reconnect.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2010, 2015, 2016 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      Affero General Public License for more details.
14
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 /**
21  * @file transport/test_transport_api_restart_reconnect.c
22  * @brief base test case for transport implementations
23  *
24  * This test case starts 2 peers, connects and exchanges a message.
25  * Then, 1 or 2 peers are restarted and it is tested if peers reconnect.
26  * How many peers are restarted is determined by the name of the binary.
27  */
28 #include "platform.h"
29 #include "gnunet_transport_service.h"
30 #include "transport-testing.h"
31
32 /**
33  * How long until we give up on transmitting the message?
34  */
35 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
36
37
38 static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
39
40 static struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
41
42 static int p1_connected;
43
44 static int p2_connected;
45
46 static int restarted;
47
48
49 static void
50 custom_shutdown (void *cls)
51 {
52   if (NULL != ats_sh)
53   {
54     GNUNET_ATS_connectivity_suggest_cancel (ats_sh);
55     ats_sh = NULL;
56   }
57 }
58
59
60 static void
61 restart_cb (void *cls)
62 {
63   static unsigned int c;
64   struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
65
66   c++;
67   if ((2 != c) &&
68       (NULL != strstr (ccc->test_name,
69                        "2peers")))
70     return;
71   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
72               "Restarted peer %u (`%s'), issuing reconnect\n",
73               p->no,
74               GNUNET_i2s (&p->id));
75   ats_sh = GNUNET_ATS_connectivity_suggest (p->ats,
76                                             &ccc->p[1]->id,
77                                             1);
78 }
79
80
81 static void
82 restart (struct GNUNET_TRANSPORT_TESTING_PeerContext *p)
83 {
84   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
85               "Restarting peer %u (`%s')\n",
86               p->no,
87               GNUNET_i2s (&p->id));
88   GNUNET_assert (GNUNET_OK ==
89                  GNUNET_TRANSPORT_TESTING_restart_peer (p,
90                                                         &restart_cb,
91                                                         p));
92 }
93
94
95 static void
96 notify_receive (void *cls,
97                 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
98                 const struct GNUNET_PeerIdentity *sender,
99                 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
100 {
101   {
102     char *ps = GNUNET_strdup (GNUNET_i2s (&receiver->id));
103
104     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
105                 "Peer %u (`%s') received message of type %d and size %u size from peer %s!\n",
106                 receiver->no,
107                 ps,
108                 ntohs (message->header.type),
109                 ntohs (message->header.size),
110                 GNUNET_i2s (sender));
111     GNUNET_free (ps);
112   }
113   if ((GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE == ntohs (message->header.type)) &&
114       (sizeof(struct GNUNET_TRANSPORT_TESTING_TestMessage) == ntohs (
115          message->header.size)))
116   {
117     if (GNUNET_NO == restarted)
118     {
119       restarted = GNUNET_YES;
120       fprintf (stderr, "TN: %s\n", ccc->test_name);
121       restart (ccc->p[0]);
122       if (NULL != strstr (ccc->test_name,
123                           "2peers"))
124         restart (ccc->p[1]);
125       return;
126     }
127     else
128     {
129       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
130                   "Restarted peers connected and message was sent, stopping test...\n");
131       ccc->global_ret = GNUNET_OK;
132       GNUNET_SCHEDULER_shutdown ();
133     }
134   }
135   else
136   {
137     GNUNET_break (0);
138     ccc->global_ret = GNUNET_SYSERR;
139     GNUNET_SCHEDULER_shutdown ();
140   }
141 }
142
143
144 static void
145 notify_connect (void *cls,
146                 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
147                 const struct GNUNET_PeerIdentity *other)
148 {
149   static struct GNUNET_TRANSPORT_TESTING_SendClosure sc = {
150     .num_messages = 1
151   };
152
153   sc.ccc = ccc;
154   GNUNET_TRANSPORT_TESTING_log_connect (cls,
155                                         me,
156                                         other);
157   if (me == ccc->p[0])
158     p1_connected = GNUNET_YES;
159   if (me == ccc->p[1])
160     p2_connected = GNUNET_YES;
161
162   if ((GNUNET_YES == restarted) &&
163       (GNUNET_YES == p1_connected) &&
164       (GNUNET_YES == p2_connected))
165   {
166     /* Peer was restarted and we received 3 connect messages (2 from first connect, 1 from reconnect) */
167     GNUNET_SCHEDULER_add_now (&GNUNET_TRANSPORT_TESTING_simple_send,
168                               &sc);
169   }
170 }
171
172
173 static void
174 notify_disconnect (void *cls,
175                    struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
176                    const struct GNUNET_PeerIdentity *other)
177 {
178   GNUNET_TRANSPORT_TESTING_log_disconnect (cls,
179                                            me,
180                                            other);
181   if (me == ccc->p[0])
182     p1_connected = GNUNET_NO;
183   if (me == ccc->p[1])
184     p2_connected = GNUNET_NO;
185 }
186
187
188 int
189 main (int argc,
190       char *argv[])
191 {
192   struct GNUNET_TRANSPORT_TESTING_SendClosure sc = {
193     .num_messages = 1
194   };
195   struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
196     .connect_continuation = &GNUNET_TRANSPORT_TESTING_simple_send,
197     .connect_continuation_cls = &sc,
198     .config_file = "test_transport_api_data.conf",
199     .rec = &notify_receive,
200     .nc = &notify_connect,
201     .nd = &notify_disconnect,
202     .shutdown_task = &custom_shutdown,
203     .timeout = TIMEOUT
204   };
205
206   ccc = &my_ccc;
207   sc.ccc = ccc;
208   if (GNUNET_OK !=
209       GNUNET_TRANSPORT_TESTING_main (2,
210                                      &GNUNET_TRANSPORT_TESTING_connect_check,
211                                      ccc))
212     return 1;
213   return 0;
214 }
215
216
217 /* end of test_transport_api_restart_1peer.c */