paragraph for gnunet devs that don't know how to use the web
[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 /**
19  * @file transport/test_transport_api_restart_reconnect.c
20  * @brief base test case for transport implementations
21  *
22  * This test case starts 2 peers, connects and exchanges a message.
23  * Then, 1 or 2 peers are restarted and it is tested if peers reconnect.
24  * How many peers are restarted is determined by the name of the binary.
25  */
26 #include "platform.h"
27 #include "gnunet_transport_service.h"
28 #include "transport-testing.h"
29
30 /**
31  * How long until we give up on transmitting the message?
32  */
33 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
34
35
36 static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
37
38 static struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
39
40 static int p1_connected;
41
42 static int p2_connected;
43
44 static int restarted;
45
46
47 static void
48 custom_shutdown (void *cls)
49 {
50   if (NULL != ats_sh)
51   {
52     GNUNET_ATS_connectivity_suggest_cancel (ats_sh);
53     ats_sh = NULL;
54   }
55 }
56
57
58 static void
59 restart_cb (void *cls)
60 {
61   static unsigned int c;
62   struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
63   
64   c++;
65   if ( (2 != c) &&
66        (NULL != strstr (ccc->test_name,
67                         "2peers")) )
68     return;
69   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
70               "Restarted peer %u (`%s'), issuing reconnect\n",
71               p->no,
72               GNUNET_i2s (&p->id));
73   ats_sh = GNUNET_ATS_connectivity_suggest (p->ats,
74                                             &ccc->p[1]->id,
75                                             1);
76 }
77
78
79 static void
80 restart (struct GNUNET_TRANSPORT_TESTING_PeerContext *p)
81 {
82   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
83               "Restarting peer %u (`%s')\n",
84               p->no,
85               GNUNET_i2s (&p->id));
86   GNUNET_assert (GNUNET_OK ==
87                  GNUNET_TRANSPORT_TESTING_restart_peer (p,
88                                                         &restart_cb,
89                                                         p));
90 }
91
92
93 static void
94 notify_receive (void *cls,
95                 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
96                 const struct GNUNET_PeerIdentity *sender,
97                 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
98 {
99   {
100     char *ps = GNUNET_strdup (GNUNET_i2s (&receiver->id));
101
102     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
103                 "Peer %u (`%s') received message of type %d and size %u size from peer %s!\n",
104                 receiver->no,
105                 ps,
106                 ntohs (message->header.type),
107                 ntohs (message->header.size),
108                 GNUNET_i2s (sender));
109     GNUNET_free (ps);
110   }
111   if ( (GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE == ntohs (message->header.type)) &&
112        (sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage) == ntohs (message->header.size)) )
113   {
114     if (GNUNET_NO == restarted)
115     {
116       restarted = GNUNET_YES;
117       fprintf (stderr, "TN: %s\n", ccc->test_name);
118       restart (ccc->p[0]);
119       if (NULL != strstr (ccc->test_name,
120                           "2peers"))
121         restart (ccc->p[1]);
122       return;
123     }
124     else
125     {
126       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
127                   "Restarted peers connected and message was sent, stopping test...\n");
128       ccc->global_ret = GNUNET_OK;
129       GNUNET_SCHEDULER_shutdown ();
130     }
131   }
132   else
133   {
134     GNUNET_break (0);
135     ccc->global_ret = GNUNET_SYSERR;
136     GNUNET_SCHEDULER_shutdown ();    
137   }
138 }
139
140
141 static void
142 notify_connect (void *cls,
143                 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
144                 const struct GNUNET_PeerIdentity *other)
145 {
146   static struct GNUNET_TRANSPORT_TESTING_SendClosure sc = {
147     .num_messages = 1
148   };
149
150   sc.ccc = ccc;
151   GNUNET_TRANSPORT_TESTING_log_connect (cls,
152                                         me,
153                                         other);
154   if (me == ccc->p[0])
155     p1_connected = GNUNET_YES;
156   if (me == ccc->p[1])
157     p2_connected = GNUNET_YES;
158
159   if ( (GNUNET_YES == restarted) &&
160        (GNUNET_YES == p1_connected) &&
161        (GNUNET_YES == p2_connected) )
162   {
163     /* Peer was restarted and we received 3 connect messages (2 from first connect, 1 from reconnect) */
164     GNUNET_SCHEDULER_add_now (&GNUNET_TRANSPORT_TESTING_simple_send,
165                               &sc);
166   }
167 }
168
169
170 static void
171 notify_disconnect (void *cls,
172                    struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
173                    const struct GNUNET_PeerIdentity *other)
174 {
175   GNUNET_TRANSPORT_TESTING_log_disconnect (cls,
176                                            me,
177                                            other);
178   if (me == ccc->p[0])
179     p1_connected = GNUNET_NO;
180   if (me == ccc->p[1])
181     p2_connected = GNUNET_NO;
182 }
183
184
185 int
186 main (int argc,
187       char *argv[])
188 {
189   struct GNUNET_TRANSPORT_TESTING_SendClosure sc = {
190     .num_messages = 1
191   };
192   struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
193     .connect_continuation = &GNUNET_TRANSPORT_TESTING_simple_send,
194     .connect_continuation_cls = &sc,
195     .config_file = "test_transport_api_data.conf",
196     .rec = &notify_receive,
197     .nc = &notify_connect,
198     .nd = &notify_disconnect,
199     .shutdown_task = &custom_shutdown,
200     .timeout = TIMEOUT
201   };
202
203   ccc = &my_ccc;
204   sc.ccc = ccc;
205   if (GNUNET_OK !=
206       GNUNET_TRANSPORT_TESTING_main (2,
207                                      &GNUNET_TRANSPORT_TESTING_connect_check,
208                                      ccc))
209     return 1;
210   return 0;
211 }
212
213 /* end of test_transport_api_restart_1peer.c */