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_1peer.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_1peer.c')
-rw-r--r-- | src/transport/test_transport_api_restart_1peer.c | 293 |
1 files changed, 0 insertions, 293 deletions
diff --git a/src/transport/test_transport_api_restart_1peer.c b/src/transport/test_transport_api_restart_1peer.c deleted file mode 100644 index 7a63ed16b..000000000 --- a/src/transport/test_transport_api_restart_1peer.c +++ /dev/null | |||
@@ -1,293 +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 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
86 | "Restarted peer %u (`%s'), issuing reconnect\n", | ||
87 | p->no, | ||
88 | GNUNET_i2s (&p->id)); | ||
89 | ats_sh = GNUNET_ATS_connectivity_suggest (p->ats, | ||
90 | &ccc->p[1]->id, | ||
91 | 1); | ||
92 | } | ||
93 | |||
94 | |||
95 | static void | ||
96 | restart (struct GNUNET_TRANSPORT_TESTING_PeerContext *p) | ||
97 | { | ||
98 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
99 | "Restarting peer %u (`%s')\n", | ||
100 | p->no, | ||
101 | GNUNET_i2s (&p->id)); | ||
102 | GNUNET_assert (GNUNET_OK == | ||
103 | GNUNET_TRANSPORT_TESTING_restart_peer (p, | ||
104 | &restart_cb, | ||
105 | p)); | ||
106 | } | ||
107 | |||
108 | |||
109 | static void | ||
110 | notify_receive (void *cls, | ||
111 | struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver, | ||
112 | const struct GNUNET_PeerIdentity *sender, | ||
113 | const struct GNUNET_MessageHeader *message) | ||
114 | { | ||
115 | { | ||
116 | char *ps = GNUNET_strdup (GNUNET_i2s (&receiver->id)); | ||
117 | |||
118 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
119 | "Peer %u (`%s') received message of type %d and size %u size from peer %s!\n", | ||
120 | receiver->no, | ||
121 | ps, | ||
122 | ntohs (message->type), | ||
123 | ntohs (message->size), | ||
124 | GNUNET_i2s (sender)); | ||
125 | GNUNET_free (ps); | ||
126 | } | ||
127 | if ((MTYPE == ntohs (message->type)) && | ||
128 | (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size))) | ||
129 | { | ||
130 | if (GNUNET_NO == restarted) | ||
131 | { | ||
132 | restarted = GNUNET_YES; | ||
133 | restart (ccc->p[0]); | ||
134 | return; | ||
135 | } | ||
136 | else | ||
137 | { | ||
138 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
139 | "Restarted peers connected and message was sent, stopping test...\n"); | ||
140 | ccc->global_ret = GNUNET_OK; | ||
141 | GNUNET_SCHEDULER_shutdown (); | ||
142 | } | ||
143 | } | ||
144 | else | ||
145 | { | ||
146 | GNUNET_break (0); | ||
147 | ccc->global_ret = GNUNET_SYSERR; | ||
148 | GNUNET_SCHEDULER_shutdown (); | ||
149 | } | ||
150 | } | ||
151 | |||
152 | |||
153 | static size_t | ||
154 | notify_ready (void *cls, | ||
155 | size_t size, | ||
156 | void *buf) | ||
157 | { | ||
158 | struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls; | ||
159 | struct GNUNET_MessageHeader *hdr; | ||
160 | |||
161 | th = NULL; | ||
162 | if (NULL == buf) | ||
163 | { | ||
164 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
165 | "Timeout occurred while waiting for transmit_ready\n"); | ||
166 | GNUNET_SCHEDULER_shutdown (); | ||
167 | ccc->global_ret = 42; | ||
168 | return 0; | ||
169 | } | ||
170 | |||
171 | GNUNET_assert (size >= 256); | ||
172 | hdr = buf; | ||
173 | hdr->size = htons (sizeof (struct GNUNET_MessageHeader)); | ||
174 | hdr->type = htons (MTYPE); | ||
175 | |||
176 | { | ||
177 | char *ps = GNUNET_strdup (GNUNET_i2s (&ccc->p[1]->id)); | ||
178 | |||
179 | |||
180 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
181 | "Peer %u (`%s') sending message with type %u and size %u bytes to peer %u (`%s')\n", | ||
182 | ccc->p[1]->no, | ||
183 | ps, | ||
184 | ntohs (hdr->type), | ||
185 | ntohs (hdr->size), | ||
186 | p->no, | ||
187 | GNUNET_i2s (&p->id)); | ||
188 | GNUNET_free (ps); | ||
189 | } | ||
190 | return sizeof (struct GNUNET_MessageHeader); | ||
191 | } | ||
192 | |||
193 | |||
194 | static void | ||
195 | sendtask (void *cls) | ||
196 | { | ||
197 | send_task = NULL; | ||
198 | { | ||
199 | char *receiver_s = GNUNET_strdup (GNUNET_i2s (&ccc->p[0]->id)); | ||
200 | |||
201 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
202 | "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n", | ||
203 | ccc->p[1]->no, | ||
204 | GNUNET_i2s (&ccc->p[1]->id), | ||
205 | ccc->p[0]->no, | ||
206 | receiver_s); | ||
207 | GNUNET_free (receiver_s); | ||
208 | } | ||
209 | th = GNUNET_TRANSPORT_notify_transmit_ready (ccc->p[1]->th, | ||
210 | &ccc->p[0]->id, | ||
211 | 256, | ||
212 | TIMEOUT_TRANSMIT, | ||
213 | ¬ify_ready, | ||
214 | ccc->p[0]); | ||
215 | } | ||
216 | |||
217 | |||
218 | static void | ||
219 | notify_connect (void *cls, | ||
220 | struct GNUNET_TRANSPORT_TESTING_PeerContext *me, | ||
221 | const struct GNUNET_PeerIdentity *other) | ||
222 | { | ||
223 | GNUNET_TRANSPORT_TESTING_log_connect (cls, | ||
224 | me, | ||
225 | other); | ||
226 | if (0 == memcmp (other, | ||
227 | &ccc->p[0]->id, | ||
228 | sizeof (struct GNUNET_PeerIdentity))) | ||
229 | p1_connected = GNUNET_YES; | ||
230 | if (0 == memcmp (other, | ||
231 | &ccc->p[1]->id, | ||
232 | sizeof (struct GNUNET_PeerIdentity))) | ||
233 | p2_connected = GNUNET_YES; | ||
234 | |||
235 | if ( (GNUNET_YES == restarted) && | ||
236 | (GNUNET_YES == p1_connected) && | ||
237 | (GNUNET_YES == p2_connected) ) | ||
238 | { | ||
239 | /* Peer was restarted and we received 3 connect messages (2 from first connect, 1 from reconnect) */ | ||
240 | send_task = GNUNET_SCHEDULER_add_now (&sendtask, | ||
241 | NULL); | ||
242 | } | ||
243 | } | ||
244 | |||
245 | |||
246 | static void | ||
247 | notify_disconnect (void *cls, | ||
248 | struct GNUNET_TRANSPORT_TESTING_PeerContext *me, | ||
249 | const struct GNUNET_PeerIdentity *other) | ||
250 | { | ||
251 | GNUNET_TRANSPORT_TESTING_log_disconnect (cls, | ||
252 | me, | ||
253 | other); | ||
254 | if (me == ccc->p[0]) | ||
255 | p1_connected = GNUNET_NO; | ||
256 | if (me == ccc->p[1]) | ||
257 | p2_connected = GNUNET_NO; | ||
258 | if (NULL != th) | ||
259 | { | ||
260 | GNUNET_TRANSPORT_notify_transmit_ready_cancel (th); | ||
261 | th = NULL; | ||
262 | } | ||
263 | if (NULL != send_task) | ||
264 | { | ||
265 | GNUNET_SCHEDULER_cancel (send_task); | ||
266 | send_task = NULL; | ||
267 | } | ||
268 | } | ||
269 | |||
270 | |||
271 | int | ||
272 | main (int argc, char *argv[]) | ||
273 | { | ||
274 | struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = { | ||
275 | .connect_continuation = &sendtask, | ||
276 | .config_file = "test_transport_api_data.conf", | ||
277 | .rec = ¬ify_receive, | ||
278 | .nc = ¬ify_connect, | ||
279 | .nd = ¬ify_disconnect, | ||
280 | .shutdown_task = &custom_shutdown, | ||
281 | .timeout = TIMEOUT | ||
282 | }; | ||
283 | |||
284 | ccc = &my_ccc; | ||
285 | if (GNUNET_OK != | ||
286 | GNUNET_TRANSPORT_TESTING_main (2, | ||
287 | &GNUNET_TRANSPORT_TESTING_connect_check, | ||
288 | ccc)) | ||
289 | return 1; | ||
290 | return 0; | ||
291 | } | ||
292 | |||
293 | /* end of test_transport_api_restart_1peer.c */ | ||