diff options
-rw-r--r-- | src/transport/test_transport_api_blacklisting.c | 264 |
1 files changed, 254 insertions, 10 deletions
diff --git a/src/transport/test_transport_api_blacklisting.c b/src/transport/test_transport_api_blacklisting.c index 8e5e14725..ee120cfc5 100644 --- a/src/transport/test_transport_api_blacklisting.c +++ b/src/transport/test_transport_api_blacklisting.c | |||
@@ -35,11 +35,22 @@ | |||
35 | #include "transport.h" | 35 | #include "transport.h" |
36 | #include "transport-testing.h" | 36 | #include "transport-testing.h" |
37 | 37 | ||
38 | #define VERBOSE GNUNET_EXTRA_LOGGING | 38 | #define VERBOSE GNUNET_YES |
39 | #define VERBOSE_ARM GNUNET_EXTRA_LOGGING | 39 | #define VERBOSE_ARM GNUNET_EXTRA_LOGGING |
40 | 40 | ||
41 | #define START_ARM GNUNET_YES | 41 | #define START_ARM GNUNET_YES |
42 | 42 | ||
43 | |||
44 | struct PeerContext *p1; | ||
45 | |||
46 | struct PeerContext *p2; | ||
47 | |||
48 | static GNUNET_TRANSPORT_TESTING_ConnectRequest cc; | ||
49 | |||
50 | struct GNUNET_TRANSPORT_TransmitHandle *th; | ||
51 | |||
52 | struct GNUNET_TRANSPORT_TESTING_handle *tth; | ||
53 | |||
43 | /** | 54 | /** |
44 | * How long until we give up on transmitting the message? | 55 | * How long until we give up on transmitting the message? |
45 | */ | 56 | */ |
@@ -50,14 +61,21 @@ | |||
50 | */ | 61 | */ |
51 | #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60) | 62 | #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60) |
52 | 63 | ||
64 | #define MSIZE 2600 | ||
65 | |||
53 | #define MTYPE 12345 | 66 | #define MTYPE 12345 |
54 | 67 | ||
68 | |||
55 | static int ok; | 69 | static int ok; |
56 | 70 | ||
57 | struct GNUNET_TRANSPORT_Blacklist * blacklist; | 71 | struct GNUNET_TRANSPORT_Blacklist * blacklist_p1; |
72 | |||
73 | struct GNUNET_TRANSPORT_Blacklist * blacklist_p2; | ||
58 | 74 | ||
59 | static GNUNET_SCHEDULER_TaskIdentifier die_task; | 75 | static GNUNET_SCHEDULER_TaskIdentifier die_task; |
60 | 76 | ||
77 | static GNUNET_SCHEDULER_TaskIdentifier send_task; | ||
78 | |||
61 | #if VERBOSE | 79 | #if VERBOSE |
62 | #define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0) | 80 | #define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0) |
63 | #else | 81 | #else |
@@ -70,45 +88,267 @@ end () | |||
70 | { | 88 | { |
71 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping\n"); | 89 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping\n"); |
72 | 90 | ||
91 | if (send_task != GNUNET_SCHEDULER_NO_TASK) | ||
92 | GNUNET_SCHEDULER_cancel (send_task); | ||
93 | |||
73 | if (die_task != GNUNET_SCHEDULER_NO_TASK) | 94 | if (die_task != GNUNET_SCHEDULER_NO_TASK) |
74 | GNUNET_SCHEDULER_cancel (die_task); | 95 | GNUNET_SCHEDULER_cancel (die_task); |
75 | 96 | ||
97 | if (th != NULL) | ||
98 | GNUNET_TRANSPORT_notify_transmit_ready_cancel (th); | ||
99 | th = NULL; | ||
100 | |||
101 | GNUNET_TRANSPORT_blacklist_cancel (blacklist_p1); | ||
102 | |||
103 | GNUNET_TRANSPORT_blacklist_cancel (blacklist_p2); | ||
104 | |||
105 | GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1); | ||
106 | GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2); | ||
107 | |||
76 | ok = 0; | 108 | ok = 0; |
77 | } | 109 | } |
78 | 110 | ||
79 | static void | 111 | static void |
80 | end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | 112 | end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) |
81 | { | 113 | { |
82 | die_task = GNUNET_SCHEDULER_NO_TASK; | 114 | if (send_task != GNUNET_SCHEDULER_NO_TASK) |
115 | GNUNET_SCHEDULER_cancel (send_task); | ||
116 | |||
117 | if (cc != NULL) | ||
118 | { | ||
119 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n")); | ||
120 | GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc); | ||
121 | cc = NULL; | ||
122 | } | ||
123 | |||
124 | if (th != NULL) | ||
125 | GNUNET_TRANSPORT_notify_transmit_ready_cancel (th); | ||
126 | th = NULL; | ||
127 | |||
128 | if (blacklist_p1 != NULL) | ||
129 | GNUNET_TRANSPORT_blacklist_cancel (blacklist_p1); | ||
130 | |||
131 | if (blacklist_p2 != NULL) | ||
132 | GNUNET_TRANSPORT_blacklist_cancel (blacklist_p2); | ||
83 | 133 | ||
84 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail!\n"); | 134 | if (p1 != NULL) |
135 | GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1); | ||
136 | if (p2 != NULL) | ||
137 | GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2); | ||
85 | 138 | ||
86 | ok = GNUNET_SYSERR; | 139 | ok = GNUNET_SYSERR; |
87 | } | 140 | } |
88 | 141 | ||
89 | 142 | ||
143 | static void | ||
144 | notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer, | ||
145 | const struct GNUNET_MessageHeader *message, | ||
146 | const struct GNUNET_ATS_Information *ats, uint32_t ats_count) | ||
147 | { | ||
148 | struct PeerContext *p = cls; | ||
149 | struct PeerContext *t = NULL; | ||
150 | |||
151 | if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity))) | ||
152 | t = p1; | ||
153 | if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity))) | ||
154 | t = p2; | ||
155 | GNUNET_assert (t != NULL); | ||
156 | |||
157 | char *ps = GNUNET_strdup (GNUNET_i2s (&p->id)); | ||
158 | |||
159 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
160 | "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n", | ||
161 | p->no, ps, ntohs (message->type), ntohs (message->size), t->no, | ||
162 | GNUNET_i2s (&t->id)); | ||
163 | GNUNET_free (ps); | ||
164 | |||
165 | if ((MTYPE == ntohs (message->type)) && | ||
166 | (MSIZE == ntohs (message->size))) | ||
167 | { | ||
168 | ok = 0; | ||
169 | end (); | ||
170 | } | ||
171 | else | ||
172 | { | ||
173 | GNUNET_break (0); | ||
174 | ok = 1; | ||
175 | end (); | ||
176 | } | ||
177 | } | ||
178 | |||
179 | |||
180 | static size_t | ||
181 | notify_ready (void *cls, size_t size, void *buf) | ||
182 | { | ||
183 | struct PeerContext *p = cls; | ||
184 | struct GNUNET_MessageHeader *hdr; | ||
185 | |||
186 | th = NULL; | ||
187 | |||
188 | if (buf == NULL) | ||
189 | { | ||
190 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
191 | "Timeout occurred while waiting for transmit_ready\n"); | ||
192 | if (GNUNET_SCHEDULER_NO_TASK != die_task) | ||
193 | GNUNET_SCHEDULER_cancel (die_task); | ||
194 | die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL); | ||
195 | ok = 42; | ||
196 | return 0; | ||
197 | } | ||
198 | |||
199 | GNUNET_assert (size >= MSIZE); | ||
200 | |||
201 | if (buf != NULL) | ||
202 | { | ||
203 | hdr = buf; | ||
204 | hdr->size = htons (MSIZE); | ||
205 | hdr->type = htons (MTYPE); | ||
206 | } | ||
207 | |||
208 | char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id)); | ||
209 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
210 | "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n", | ||
211 | p2->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no, | ||
212 | GNUNET_i2s (&p->id)); | ||
213 | GNUNET_free (ps); | ||
214 | |||
215 | return MSIZE; | ||
216 | } | ||
217 | |||
218 | static void | ||
219 | sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
220 | { | ||
221 | send_task = GNUNET_SCHEDULER_NO_TASK; | ||
222 | |||
223 | if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0) | ||
224 | return; | ||
225 | char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id)); | ||
226 | |||
227 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
228 | "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n", | ||
229 | p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s); | ||
230 | GNUNET_free (receiver_s); | ||
231 | |||
232 | th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, MSIZE, 0, | ||
233 | TIMEOUT_TRANSMIT, ¬ify_ready, | ||
234 | p1); | ||
235 | } | ||
236 | |||
237 | static void | ||
238 | notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer, | ||
239 | const struct GNUNET_ATS_Information *ats, uint32_t ats_count) | ||
240 | { | ||
241 | static int c; | ||
242 | |||
243 | c++; | ||
244 | struct PeerContext *p = cls; | ||
245 | struct PeerContext *t = NULL; | ||
246 | |||
247 | if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity))) | ||
248 | t = p1; | ||
249 | if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity))) | ||
250 | t = p2; | ||
251 | GNUNET_assert (t != NULL); | ||
252 | |||
253 | char *ps = GNUNET_strdup (GNUNET_i2s (&p->id)); | ||
254 | |||
255 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
256 | "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps, | ||
257 | t->no, GNUNET_i2s (peer)); | ||
258 | GNUNET_free (ps); | ||
259 | } | ||
260 | |||
261 | |||
262 | static void | ||
263 | notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer) | ||
264 | { | ||
265 | struct PeerContext *p = cls; | ||
266 | char *ps = GNUNET_strdup (GNUNET_i2s (&p->id)); | ||
267 | |||
268 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
269 | "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps, | ||
270 | GNUNET_i2s (peer)); | ||
271 | |||
272 | GNUNET_free (ps); | ||
273 | |||
274 | if (th != NULL) | ||
275 | GNUNET_TRANSPORT_notify_transmit_ready_cancel (th); | ||
276 | th = NULL; | ||
277 | } | ||
278 | |||
279 | static void | ||
280 | testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls) | ||
281 | { | ||
282 | cc = NULL; | ||
283 | char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id)); | ||
284 | |||
285 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n", | ||
286 | p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id)); | ||
287 | GNUNET_free (p1_c); | ||
288 | |||
289 | send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL); | ||
290 | } | ||
291 | |||
292 | |||
90 | int blacklist_cb (void *cls, | 293 | int blacklist_cb (void *cls, |
91 | const struct | 294 | const struct |
92 | GNUNET_PeerIdentity * pid) | 295 | GNUNET_PeerIdentity * pid) |
93 | { | 296 | { |
297 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Blacklist request\n", GNUNET_i2s (pid)); | ||
94 | return GNUNET_YES; | 298 | return GNUNET_YES; |
95 | } | 299 | } |
96 | 300 | ||
301 | void | ||
302 | start_cb (struct PeerContext *p, void *cls) | ||
303 | { | ||
304 | static int started; | ||
305 | |||
306 | started++; | ||
307 | |||
308 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no, | ||
309 | GNUNET_i2s (&p->id)); | ||
310 | |||
311 | if (started != 2) | ||
312 | return; | ||
313 | |||
314 | char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id)); | ||
315 | |||
316 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
317 | "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n", | ||
318 | p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id)); | ||
319 | GNUNET_free (sender_c); | ||
320 | |||
321 | cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb, | ||
322 | NULL); | ||
323 | |||
324 | } | ||
325 | |||
97 | static void | 326 | static void |
98 | run (void *cls, char *const *args, const char *cfgfile, | 327 | run (void *cls, char *const *args, const char *cfgfile, |
99 | const struct GNUNET_CONFIGURATION_Handle *cfg) | 328 | const struct GNUNET_CONFIGURATION_Handle *cfg) |
100 | { | 329 | { |
101 | die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL); | 330 | die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL); |
102 | 331 | ||
103 | blacklist = GNUNET_TRANSPORT_blacklist (cfg, | 332 | p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_api_tcp_peer1.conf", 1, |
104 | &blacklist_cb, | 333 | ¬ify_receive, ¬ify_connect, |
105 | NULL); | 334 | ¬ify_disconnect, &start_cb, |
335 | NULL); | ||
106 | 336 | ||
107 | GNUNET_assert (blacklist != NULL); | 337 | p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, "test_transport_api_tcp_peer2.conf", 2, |
338 | ¬ify_receive, ¬ify_connect, | ||
339 | ¬ify_disconnect, &start_cb, | ||
340 | NULL); | ||
108 | 341 | ||
109 | GNUNET_TRANSPORT_blacklist_cancel (blacklist); | 342 | blacklist_p1 = GNUNET_TRANSPORT_blacklist (p1->cfg, |
343 | &blacklist_cb, | ||
344 | p1); | ||
110 | 345 | ||
111 | end (); | 346 | blacklist_p2 = GNUNET_TRANSPORT_blacklist (p1->cfg, |
347 | &blacklist_cb, | ||
348 | p2); | ||
349 | |||
350 | GNUNET_assert (blacklist_p1 != NULL); | ||
351 | GNUNET_assert (blacklist_p2 != NULL); | ||
112 | } | 352 | } |
113 | 353 | ||
114 | 354 | ||
@@ -151,8 +391,12 @@ main (int argc, char *argv[]) | |||
151 | #endif | 391 | #endif |
152 | NULL); | 392 | NULL); |
153 | 393 | ||
394 | tth = GNUNET_TRANSPORT_TESTING_init (); | ||
395 | |||
154 | ret = check (); | 396 | ret = check (); |
155 | 397 | ||
398 | GNUNET_TRANSPORT_TESTING_done (tth); | ||
399 | |||
156 | return ret; | 400 | return ret; |
157 | } | 401 | } |
158 | 402 | ||