aboutsummaryrefslogtreecommitdiff
path: root/src/transport/test_transport_api_restart_reconnect.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/transport/test_transport_api_restart_reconnect.c')
-rw-r--r--src/transport/test_transport_api_restart_reconnect.c215
1 files changed, 215 insertions, 0 deletions
diff --git a/src/transport/test_transport_api_restart_reconnect.c b/src/transport/test_transport_api_restart_reconnect.c
new file mode 100644
index 000000000..6fd969918
--- /dev/null
+++ b/src/transport/test_transport_api_restart_reconnect.c
@@ -0,0 +1,215 @@
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
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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
38static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
39
40static struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
41
42static int p1_connected;
43
44static int p2_connected;
45
46static int restarted;
47
48
49static void
50custom_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
60static void
61restart_cb (struct GNUNET_TRANSPORT_TESTING_PeerContext *p,
62 void *cls)
63{
64 static unsigned int c;
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
81static void
82restart (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
95static void
96notify_receive (void *cls,
97 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
98 const struct GNUNET_PeerIdentity *sender,
99 const struct GNUNET_MessageHeader *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->type),
109 ntohs (message->size),
110 GNUNET_i2s (sender));
111 GNUNET_free (ps);
112 }
113 if ( (GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE == ntohs (message->type)) &&
114 (sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage) == ntohs (message->size)) )
115 {
116 if (GNUNET_NO == restarted)
117 {
118 restarted = GNUNET_YES;
119 fprintf (stderr, "TN: %s\n", ccc->test_name);
120 restart (ccc->p[0]);
121 if (NULL != strstr (ccc->test_name,
122 "2peers"))
123 restart (ccc->p[1]);
124 return;
125 }
126 else
127 {
128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
129 "Restarted peers connected and message was sent, stopping test...\n");
130 ccc->global_ret = GNUNET_OK;
131 GNUNET_SCHEDULER_shutdown ();
132 }
133 }
134 else
135 {
136 GNUNET_break (0);
137 ccc->global_ret = GNUNET_SYSERR;
138 GNUNET_SCHEDULER_shutdown ();
139 }
140}
141
142
143static void
144notify_connect (void *cls,
145 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
146 const struct GNUNET_PeerIdentity *other)
147{
148 static struct GNUNET_TRANSPORT_TESTING_SendClosure sc = {
149 .num_messages = 1
150 };
151
152 sc.ccc = ccc;
153 GNUNET_TRANSPORT_TESTING_log_connect (cls,
154 me,
155 other);
156 if (me == ccc->p[0])
157 p1_connected = GNUNET_YES;
158 if (me == ccc->p[1])
159 p2_connected = GNUNET_YES;
160
161 if ( (GNUNET_YES == restarted) &&
162 (GNUNET_YES == p1_connected) &&
163 (GNUNET_YES == p2_connected) )
164 {
165 /* Peer was restarted and we received 3 connect messages (2 from first connect, 1 from reconnect) */
166 GNUNET_SCHEDULER_add_now (&GNUNET_TRANSPORT_TESTING_simple_send,
167 &sc);
168 }
169}
170
171
172static void
173notify_disconnect (void *cls,
174 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
175 const struct GNUNET_PeerIdentity *other)
176{
177 GNUNET_TRANSPORT_TESTING_log_disconnect (cls,
178 me,
179 other);
180 if (me == ccc->p[0])
181 p1_connected = GNUNET_NO;
182 if (me == ccc->p[1])
183 p2_connected = GNUNET_NO;
184}
185
186
187int
188main (int argc,
189 char *argv[])
190{
191 struct GNUNET_TRANSPORT_TESTING_SendClosure sc = {
192 .num_messages = 1
193 };
194 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
195 .connect_continuation = &GNUNET_TRANSPORT_TESTING_simple_send,
196 .connect_continuation_cls = &sc,
197 .config_file = "test_transport_api_data.conf",
198 .rec = &notify_receive,
199 .nc = &notify_connect,
200 .nd = &notify_disconnect,
201 .shutdown_task = &custom_shutdown,
202 .timeout = TIMEOUT
203 };
204
205 ccc = &my_ccc;
206 sc.ccc = ccc;
207 if (GNUNET_OK !=
208 GNUNET_TRANSPORT_TESTING_main (2,
209 &GNUNET_TRANSPORT_TESTING_connect_check,
210 ccc))
211 return 1;
212 return 0;
213}
214
215/* end of test_transport_api_restart_1peer.c */