diff options
author | Christian Grothoff <christian@grothoff.org> | 2016-07-23 22:32:35 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2016-07-23 22:32:35 +0000 |
commit | 658c3cf583b0c09e55c41a78a9ec8dbe28cb515e (patch) | |
tree | 236759ed9a5b6a5e3493667c34f384279bc12bbe /src/transport/test_transport_api_restart_2peers.c | |
parent | 1d188d2b9437f8a7685d0d139da7a6b9fb1b7c9c (diff) | |
download | gnunet-658c3cf583b0c09e55c41a78a9ec8dbe28cb515e.tar.gz gnunet-658c3cf583b0c09e55c41a78a9ec8dbe28cb515e.zip |
more refactoring of tests for new send API
Diffstat (limited to 'src/transport/test_transport_api_restart_2peers.c')
-rw-r--r-- | src/transport/test_transport_api_restart_2peers.c | 298 |
1 files changed, 0 insertions, 298 deletions
diff --git a/src/transport/test_transport_api_restart_2peers.c b/src/transport/test_transport_api_restart_2peers.c deleted file mode 100644 index 40f287623..000000000 --- a/src/transport/test_transport_api_restart_2peers.c +++ /dev/null | |||
@@ -1,298 +0,0 @@ | |||
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_1peer.c | ||
22 | * @brief base test case for transport implementations | ||
23 | * | ||
24 | * This test case starts 2 peers, connects and exchanges a message | ||
25 | * 1 peer is restarted and tested if peers reconnect | ||
26 | * C code apparently. | ||
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 | * How long until we give up on transmitting the message? | ||
39 | */ | ||
40 | #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10) | ||
41 | |||
42 | #define MTYPE 12345 | ||
43 | |||
44 | |||
45 | static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc; | ||
46 | |||
47 | static struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh; | ||
48 | |||
49 | static int p1_connected; | ||
50 | |||
51 | static int p2_connected; | ||
52 | |||
53 | static struct GNUNET_TRANSPORT_TransmitHandle *th; | ||
54 | |||
55 | static struct GNUNET_SCHEDULER_Task *send_task; | ||
56 | |||
57 | static int restarted; | ||
58 | |||
59 | |||
60 | static void | ||
61 | custom_shutdown (void *cls) | ||
62 | { | ||
63 | if (NULL != ats_sh) | ||
64 | { | ||
65 | GNUNET_ATS_connectivity_suggest_cancel (ats_sh); | ||
66 | ats_sh = NULL; | ||
67 | } | ||
68 | if (NULL != th) | ||
69 | { | ||
70 | GNUNET_TRANSPORT_notify_transmit_ready_cancel (th); | ||
71 | th = NULL; | ||
72 | } | ||
73 | if (NULL != send_task) | ||
74 | { | ||
75 | GNUNET_SCHEDULER_cancel (send_task); | ||
76 | send_task = NULL; | ||
77 | } | ||
78 | } | ||
79 | |||
80 | |||
81 | static void | ||
82 | restart_cb (struct GNUNET_TRANSPORT_TESTING_PeerContext *p, | ||
83 | void *cls) | ||
84 | { | ||
85 | static unsigned int c; | ||
86 | |||
87 | c++; | ||
88 | if (c != 2) | ||
89 | return; | ||
90 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
91 | "Restarted peer %u (`%s'), issuing reconnect\n", | ||
92 | p->no, | ||
93 | GNUNET_i2s (&p->id)); | ||
94 | ats_sh = GNUNET_ATS_connectivity_suggest (ccc->p[0]->ats, | ||
95 | &ccc->p[1]->id, | ||
96 | 1); | ||
97 | } | ||
98 | |||
99 | |||
100 | static void | ||
101 | restart (struct GNUNET_TRANSPORT_TESTING_PeerContext *p) | ||
102 | { | ||
103 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
104 | "Restarting peer %u (`%s')\n", | ||
105 | p->no, | ||
106 | GNUNET_i2s (&p->id)); | ||
107 | GNUNET_assert (GNUNET_OK == | ||
108 | GNUNET_TRANSPORT_TESTING_restart_peer (p, | ||
109 | &restart_cb, | ||
110 | p)); | ||
111 | } | ||
112 | |||
113 | |||
114 | static void | ||
115 | notify_receive (void *cls, | ||
116 | struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver, | ||
117 | const struct GNUNET_PeerIdentity *sender, | ||
118 | const struct GNUNET_MessageHeader *message) | ||
119 | { | ||
120 | { | ||
121 | char *ps = GNUNET_strdup (GNUNET_i2s (&receiver->id)); | ||
122 | |||
123 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
124 | "Peer %u (`%s') received message of type %d and size %u size from peer %s!\n", | ||
125 | receiver->no, | ||
126 | ps, | ||
127 | ntohs (message->type), | ||
128 | ntohs (message->size), | ||
129 | GNUNET_i2s (sender)); | ||
130 | GNUNET_free (ps); | ||
131 | } | ||
132 | if ((MTYPE == ntohs (message->type)) && | ||
133 | (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size))) | ||
134 | { | ||
135 | if (GNUNET_NO == restarted) | ||
136 | { | ||
137 | restarted = GNUNET_YES; | ||
138 | restart (ccc->p[0]); | ||
139 | restart (ccc->p[1]); | ||
140 | return; | ||
141 | } | ||
142 | else | ||
143 | { | ||
144 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
145 | "Restarted peers connected and message was sent, stopping test...\n"); | ||
146 | ccc->global_ret = GNUNET_OK; | ||
147 | GNUNET_SCHEDULER_shutdown (); | ||
148 | } | ||
149 | } | ||
150 | else | ||
151 | { | ||
152 | GNUNET_break (0); | ||
153 | ccc->global_ret = GNUNET_SYSERR; | ||
154 | GNUNET_SCHEDULER_shutdown (); | ||
155 | } | ||
156 | } | ||
157 | |||
158 | |||
159 | static size_t | ||
160 | notify_ready (void *cls, | ||
161 | size_t size, | ||
162 | void *buf) | ||
163 | { | ||
164 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls; | ||
165 | struct GNUNET_MessageHeader *hdr; | ||
166 | |||
167 | th = NULL; | ||
168 | if (NULL == buf) | ||
169 | { | ||
170 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
171 | "Timeout occurred while waiting for transmit_ready\n"); | ||
172 | GNUNET_SCHEDULER_shutdown (); | ||
173 | ccc->global_ret = 42; | ||
174 | return 0; | ||
175 | } | ||
176 | |||
177 | GNUNET_assert (size >= 256); | ||
178 | hdr = buf; | ||
179 | hdr->size = htons (sizeof (struct GNUNET_MessageHeader)); | ||
180 | hdr->type = htons (MTYPE); | ||
181 | |||
182 | { | ||
183 | char *ps = GNUNET_strdup (GNUNET_i2s (&ccc->p[1]->id)); | ||
184 | |||
185 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
186 | "Peer %u (`%s') sending message with type %u and size %u bytes to peer %u (`%s')\n", | ||
187 | ccc->p[1]->no, | ||
188 | ps, | ||
189 | ntohs (hdr->type), | ||
190 | ntohs (hdr->size), | ||
191 | p->no, | ||
192 | GNUNET_i2s (&p->id)); | ||
193 | GNUNET_free (ps); | ||
194 | } | ||
195 | return sizeof (struct GNUNET_MessageHeader); | ||
196 | } | ||
197 | |||
198 | |||
199 | static void | ||
200 | sendtask (void *cls) | ||
201 | { | ||
202 | send_task = NULL; | ||
203 | { | ||
204 | char *receiver_s = GNUNET_strdup (GNUNET_i2s (&ccc->p[0]->id)); | ||
205 | |||
206 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
207 | "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n", | ||
208 | ccc->p[1]->no, | ||
209 | GNUNET_i2s (&ccc->p[1]->id), | ||
210 | ccc->p[0]->no, | ||
211 | receiver_s); | ||
212 | GNUNET_free (receiver_s); | ||
213 | } | ||
214 | th = GNUNET_TRANSPORT_notify_transmit_ready (ccc->p[1]->th, | ||
215 | &ccc->p[0]->id, | ||
216 | 256, | ||
217 | TIMEOUT_TRANSMIT, | ||
218 | ¬ify_ready, | ||
219 | ccc->p[0]); | ||
220 | } | ||
221 | |||
222 | |||
223 | static void | ||
224 | notify_connect (void *cls, | ||
225 | struct GNUNET_TRANSPORT_TESTING_PeerContext *me, | ||
226 | const struct GNUNET_PeerIdentity *other) | ||
227 | { | ||
228 | GNUNET_TRANSPORT_TESTING_log_connect (cls, | ||
229 | me, | ||
230 | other); | ||
231 | if (0 == memcmp (other, | ||
232 | &ccc->p[0]->id, | ||
233 | sizeof (struct GNUNET_PeerIdentity))) | ||
234 | p1_connected = GNUNET_YES; | ||
235 | if (0 == memcmp (other, | ||
236 | &ccc->p[1]->id, | ||
237 | sizeof (struct GNUNET_PeerIdentity))) | ||
238 | p2_connected = GNUNET_YES; | ||
239 | |||
240 | if ( (GNUNET_YES == restarted) && | ||
241 | (GNUNET_YES == p1_connected) && | ||
242 | (GNUNET_YES == p2_connected) ) | ||
243 | { | ||
244 | /* Peer was restarted and we received 3 connect messages (2 from first connect, 1 from reconnect) */ | ||
245 | send_task = GNUNET_SCHEDULER_add_now (&sendtask, | ||
246 | NULL); | ||
247 | } | ||
248 | } | ||
249 | |||
250 | |||
251 | static void | ||
252 | notify_disconnect (void *cls, | ||
253 | struct GNUNET_TRANSPORT_TESTING_PeerContext *me, | ||
254 | const struct GNUNET_PeerIdentity *other) | ||
255 | { | ||
256 | GNUNET_TRANSPORT_TESTING_log_disconnect (cls, | ||
257 | me, | ||
258 | other); | ||
259 | if (me == ccc->p[0]) | ||
260 | p1_connected = GNUNET_NO; | ||
261 | if (me == ccc->p[1]) | ||
262 | p2_connected = GNUNET_NO; | ||
263 | if (NULL != th) | ||
264 | { | ||
265 | GNUNET_TRANSPORT_notify_transmit_ready_cancel (th); | ||
266 | th = NULL; | ||
267 | } | ||
268 | if (NULL != send_task) | ||
269 | { | ||
270 | GNUNET_SCHEDULER_cancel (send_task); | ||
271 | send_task = NULL; | ||
272 | } | ||
273 | } | ||
274 | |||
275 | |||
276 | int | ||
277 | main (int argc, char *argv[]) | ||
278 | { | ||
279 | struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = { | ||
280 | .connect_continuation = &sendtask, | ||
281 | .config_file = "test_transport_api_data.conf", | ||
282 | .rec = ¬ify_receive, | ||
283 | .nc = ¬ify_connect, | ||
284 | .nd = ¬ify_disconnect, | ||
285 | .shutdown_task = &custom_shutdown, | ||
286 | .timeout = TIMEOUT | ||
287 | }; | ||
288 | |||
289 | ccc = &my_ccc; | ||
290 | if (GNUNET_OK != | ||
291 | GNUNET_TRANSPORT_TESTING_main (2, | ||
292 | &GNUNET_TRANSPORT_TESTING_connect_check, | ||
293 | ccc)) | ||
294 | return 1; | ||
295 | return 0; | ||
296 | } | ||
297 | |||
298 | /* end of test_transport_api_restart_1peer.c */ | ||