diff options
Diffstat (limited to 'src/transport/test_communicator_basic.c')
-rw-r--r-- | src/transport/test_communicator_basic.c | 420 |
1 files changed, 420 insertions, 0 deletions
diff --git a/src/transport/test_communicator_basic.c b/src/transport/test_communicator_basic.c new file mode 100644 index 000000000..ef18d6a81 --- /dev/null +++ b/src/transport/test_communicator_basic.c | |||
@@ -0,0 +1,420 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2019 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 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file transport/test_communicator_basic.c | ||
23 | * @brief test the communicators | ||
24 | * @author Julius Bünger | ||
25 | * @author Martin Schanzenbach | ||
26 | */ | ||
27 | #include "platform.h" | ||
28 | #include "gnunet_util_lib.h" | ||
29 | #include "transport-testing2.h" | ||
30 | #include "gnunet_ats_transport_service.h" | ||
31 | #include "gnunet_signatures.h" | ||
32 | #include "gnunet_testing_lib.h" | ||
33 | #include "transport.h" | ||
34 | |||
35 | #include <inttypes.h> | ||
36 | |||
37 | |||
38 | #define LOG(kind, ...) GNUNET_log_from (kind, \ | ||
39 | "test_transport_communicator", \ | ||
40 | __VA_ARGS__) | ||
41 | |||
42 | #define NUM_PEERS 2 | ||
43 | |||
44 | static struct GNUNET_PeerIdentity peer_id[NUM_PEERS]; | ||
45 | |||
46 | static char *communicator_binary; | ||
47 | |||
48 | static struct | ||
49 | GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_hs[NUM_PEERS]; | ||
50 | |||
51 | static struct GNUNET_CONFIGURATION_Handle *cfg_peers[NUM_PEERS]; | ||
52 | |||
53 | static char **cfg_peers_name; | ||
54 | |||
55 | static int ret; | ||
56 | |||
57 | static struct GNUNET_TIME_Absolute start_short; | ||
58 | |||
59 | static struct GNUNET_TIME_Absolute start_long; | ||
60 | |||
61 | static struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *my_tc; | ||
62 | |||
63 | #define SHORT_MESSAGE_SIZE 128 | ||
64 | |||
65 | #define LONG_MESSAGE_SIZE 32000 | ||
66 | |||
67 | #define SHORT_BURST_SECONDS 2 | ||
68 | |||
69 | #define LONG_BURST_SECONDS 2 | ||
70 | |||
71 | #define SHORT_BURST_WINDOW \ | ||
72 | GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,SHORT_BURST_SECONDS) | ||
73 | |||
74 | #define LONG_BURST_WINDOW \ | ||
75 | GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,SHORT_BURST_SECONDS) | ||
76 | |||
77 | #define BURST_SHORT 0 | ||
78 | |||
79 | #define BURST_LONG 1 | ||
80 | |||
81 | #define SIZE_CHECK 2 | ||
82 | |||
83 | #define MAX_BUF_LEN 10 | ||
84 | |||
85 | static int buf_len = 0; | ||
86 | |||
87 | static char short_payload[SHORT_MESSAGE_SIZE]; | ||
88 | |||
89 | static char long_payload[LONG_MESSAGE_SIZE]; | ||
90 | |||
91 | static uint32_t ack = 0; | ||
92 | |||
93 | static int phase; | ||
94 | |||
95 | static size_t long_received = 0; | ||
96 | |||
97 | static size_t short_received = 0; | ||
98 | |||
99 | static void | ||
100 | communicator_available_cb (void *cls, | ||
101 | struct | ||
102 | GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle | ||
103 | *tc_h, | ||
104 | enum GNUNET_TRANSPORT_CommunicatorCharacteristics cc, | ||
105 | char *address_prefix) | ||
106 | { | ||
107 | LOG (GNUNET_ERROR_TYPE_INFO, | ||
108 | "Communicator available. (cc: %u, prefix: %s)\n", | ||
109 | cc, | ||
110 | address_prefix); | ||
111 | } | ||
112 | |||
113 | |||
114 | static void | ||
115 | add_address_cb (void *cls, | ||
116 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle * | ||
117 | tc_h, | ||
118 | const char *address, | ||
119 | struct GNUNET_TIME_Relative expiration, | ||
120 | uint32_t aid, | ||
121 | enum GNUNET_NetworkType nt) | ||
122 | { | ||
123 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
124 | "New address. (addr: %s, expir: %" PRIu32 ", ID: %" PRIu32 ", nt: %u\n", | ||
125 | address, | ||
126 | expiration.rel_value_us, | ||
127 | aid, | ||
128 | nt); | ||
129 | // addresses[1] = GNUNET_strdup (address); | ||
130 | if (0 == strcmp ((char*) cls, cfg_peers_name[NUM_PEERS - 1])) | ||
131 | GNUNET_TRANSPORT_TESTING_transport_communicator_open_queue (tc_hs[0], | ||
132 | &peer_id[ | ||
133 | NUM_PEERS | ||
134 | - 1], | ||
135 | address); | ||
136 | } | ||
137 | |||
138 | |||
139 | /** | ||
140 | * @brief Callback that informs whether the requested queue will be | ||
141 | * established | ||
142 | * | ||
143 | * Implements #GNUNET_TRANSPORT_TESTING_QueueCreateReplyCallback. | ||
144 | * | ||
145 | * @param cls Closure - unused | ||
146 | * @param tc_h Communicator handle - unused | ||
147 | * @param will_try #GNUNET_YES if queue will be established | ||
148 | * #GNUNET_NO if queue will not be established (bogous address) | ||
149 | */ | ||
150 | static void | ||
151 | queue_create_reply_cb (void *cls, | ||
152 | struct | ||
153 | GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle * | ||
154 | tc_h, | ||
155 | int will_try) | ||
156 | { | ||
157 | if (GNUNET_YES == will_try) | ||
158 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
159 | "Queue will be established!\n"); | ||
160 | else | ||
161 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
162 | "Queue won't be established (bougus address?)!\n"); | ||
163 | } | ||
164 | |||
165 | |||
166 | static void | ||
167 | size_test (void *cls) | ||
168 | { | ||
169 | char payload[ack]; | ||
170 | phase = SIZE_CHECK; | ||
171 | |||
172 | memset (payload, 0, ack); | ||
173 | if (ack < 64000) //Leave some room for our protocol. | ||
174 | { | ||
175 | GNUNET_TRANSPORT_TESTING_transport_communicator_send (my_tc, | ||
176 | &payload, | ||
177 | sizeof(payload)); | ||
178 | return; | ||
179 | } | ||
180 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
181 | "Finished\n"); | ||
182 | GNUNET_SCHEDULER_shutdown (); | ||
183 | // Finished! | ||
184 | } | ||
185 | |||
186 | |||
187 | static void | ||
188 | long_test (void *cls) | ||
189 | { | ||
190 | struct GNUNET_TIME_Relative duration = GNUNET_TIME_absolute_get_duration ( | ||
191 | start_long); | ||
192 | if (LONG_BURST_WINDOW.rel_value_us > duration.rel_value_us) | ||
193 | { | ||
194 | if (buf_len < MAX_BUF_LEN) | ||
195 | { | ||
196 | GNUNET_TRANSPORT_TESTING_transport_communicator_send (my_tc, | ||
197 | &long_payload, | ||
198 | sizeof(long_payload)); | ||
199 | buf_len++; | ||
200 | } | ||
201 | GNUNET_SCHEDULER_add_now (&long_test, NULL); | ||
202 | return; | ||
203 | } | ||
204 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, | ||
205 | "LONG Goodput (bytes/s): %lu - received packets: %lu\n", | ||
206 | (LONG_MESSAGE_SIZE * long_received) / LONG_BURST_SECONDS, | ||
207 | long_received); | ||
208 | ack = 5; | ||
209 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &size_test, NULL); | ||
210 | } | ||
211 | |||
212 | |||
213 | static void | ||
214 | short_test (void *cls) | ||
215 | { | ||
216 | struct GNUNET_TIME_Relative duration = GNUNET_TIME_absolute_get_duration ( | ||
217 | start_short); | ||
218 | if (SHORT_BURST_WINDOW.rel_value_us > duration.rel_value_us) | ||
219 | { | ||
220 | if (buf_len < MAX_BUF_LEN) | ||
221 | { | ||
222 | GNUNET_TRANSPORT_TESTING_transport_communicator_send (my_tc, | ||
223 | &short_payload, | ||
224 | sizeof(short_payload)); | ||
225 | buf_len++; | ||
226 | } | ||
227 | GNUNET_SCHEDULER_add_now (&short_test, NULL); | ||
228 | return; | ||
229 | } | ||
230 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
231 | "Short test done!\n"); | ||
232 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, | ||
233 | "SHORT Goodput (bytes/s): %lu - received packets: %lu\n", | ||
234 | (SHORT_MESSAGE_SIZE * short_received) / SHORT_BURST_SECONDS, | ||
235 | short_received); | ||
236 | start_long = GNUNET_TIME_absolute_get (); | ||
237 | phase = BURST_LONG; | ||
238 | buf_len = 0; | ||
239 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &long_test, NULL); | ||
240 | } | ||
241 | |||
242 | |||
243 | /** | ||
244 | * @brief Handle opening of queue | ||
245 | * | ||
246 | * Issues sending of test data | ||
247 | * | ||
248 | * Implements #GNUNET_TRANSPORT_TESTING_AddQueueCallback | ||
249 | * | ||
250 | * @param cls Closure | ||
251 | * @param tc_h Communicator handle | ||
252 | * @param tc_queue Handle to newly opened queue | ||
253 | */ | ||
254 | static void | ||
255 | add_queue_cb (void *cls, | ||
256 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h, | ||
257 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue * | ||
258 | tc_queue) | ||
259 | { | ||
260 | if (0 != strcmp ((char*) cls, cfg_peers_name[0])) | ||
261 | return; // TODO? | ||
262 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
263 | "Queue established, starting test...\n"); | ||
264 | start_short = GNUNET_TIME_absolute_get (); | ||
265 | my_tc = tc_queue; | ||
266 | buf_len = 0; | ||
267 | phase = BURST_SHORT; | ||
268 | GNUNET_SCHEDULER_add_now (&short_test, tc_queue); | ||
269 | } | ||
270 | |||
271 | |||
272 | /** | ||
273 | * @brief Handle an incoming message | ||
274 | * | ||
275 | * Implements #GNUNET_TRANSPORT_TESTING_IncomingMessageCallback | ||
276 | |||
277 | * @param cls Closure | ||
278 | * @param tc_h Handle to the receiving communicator | ||
279 | * @param msg Received message | ||
280 | */ | ||
281 | void | ||
282 | incoming_message_cb (void *cls, | ||
283 | struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle | ||
284 | *tc_h, | ||
285 | const char*payload, | ||
286 | size_t payload_len) | ||
287 | { | ||
288 | if (0 != strcmp ((char*) cls, cfg_peers_name[NUM_PEERS - 1])) | ||
289 | return; // TODO? | ||
290 | if (phase == BURST_SHORT) | ||
291 | { | ||
292 | GNUNET_assert (SHORT_MESSAGE_SIZE == payload_len); | ||
293 | short_received++; | ||
294 | buf_len--; | ||
295 | } | ||
296 | else if (phase == BURST_LONG) | ||
297 | { | ||
298 | if (LONG_MESSAGE_SIZE != payload_len) | ||
299 | return; // Ignore | ||
300 | long_received++; | ||
301 | buf_len--; | ||
302 | } | ||
303 | else // if (phase == SIZE_CHECK) { | ||
304 | { | ||
305 | // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
306 | // "Receiving payload with size %lu...\n", payload_len); | ||
307 | if (ack != payload_len) | ||
308 | { | ||
309 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
310 | "Error receiving message, corrupted.\n"); | ||
311 | ret = 1; | ||
312 | GNUNET_SCHEDULER_shutdown (); | ||
313 | return; | ||
314 | } | ||
315 | ack += 5; // Next expected message size | ||
316 | GNUNET_SCHEDULER_add_now (&size_test, NULL); | ||
317 | } | ||
318 | } | ||
319 | |||
320 | |||
321 | /** | ||
322 | * @brief Main function called by the scheduler | ||
323 | * | ||
324 | * @param cls Closure - Handle to configuration | ||
325 | */ | ||
326 | static void | ||
327 | run (void *cls) | ||
328 | { | ||
329 | ret = 0; | ||
330 | memset (long_payload, 0, LONG_MESSAGE_SIZE); | ||
331 | memset (short_payload, 0, SHORT_MESSAGE_SIZE); | ||
332 | for (int i = 0; i < NUM_PEERS; i++) | ||
333 | { | ||
334 | tc_hs[i] = GNUNET_TRANSPORT_TESTING_transport_communicator_service_start ( | ||
335 | "transport", | ||
336 | communicator_binary, | ||
337 | cfg_peers_name[i], | ||
338 | &communicator_available_cb, | ||
339 | &add_address_cb, | ||
340 | &queue_create_reply_cb, | ||
341 | &add_queue_cb, | ||
342 | &incoming_message_cb, | ||
343 | cfg_peers_name[i]); /* cls */ | ||
344 | } | ||
345 | } | ||
346 | |||
347 | |||
348 | int | ||
349 | main (int argc, | ||
350 | char *const *argv) | ||
351 | { | ||
352 | struct GNUNET_CRYPTO_EddsaPrivateKey *private_key; | ||
353 | char *communicator_name; | ||
354 | char *cfg_peer; | ||
355 | ret = 1; | ||
356 | |||
357 | communicator_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]); | ||
358 | GNUNET_asprintf (&communicator_binary, "gnunet-communicator-%s", | ||
359 | communicator_name); | ||
360 | cfg_peers_name = GNUNET_malloc (sizeof(char*) * NUM_PEERS); | ||
361 | if (GNUNET_OK != GNUNET_log_setup ("test_communicator_basic", | ||
362 | "DEBUG", | ||
363 | "test_communicator_basic.log")) | ||
364 | { | ||
365 | fprintf (stderr, "Unable to setup log\n"); | ||
366 | GNUNET_break (0); | ||
367 | return 2; | ||
368 | } | ||
369 | for (int i = 0; i < NUM_PEERS; i++) | ||
370 | { | ||
371 | GNUNET_asprintf ((&cfg_peer), | ||
372 | "test_communicator_%s_peer%u.conf", | ||
373 | communicator_name, i + 1); | ||
374 | cfg_peers_name[i] = cfg_peer; | ||
375 | cfg_peers[i] = GNUNET_CONFIGURATION_create (); | ||
376 | if (GNUNET_YES == | ||
377 | GNUNET_DISK_file_test (cfg_peers_name[i])) | ||
378 | { | ||
379 | if (GNUNET_SYSERR == | ||
380 | GNUNET_CONFIGURATION_load (cfg_peers[i], | ||
381 | cfg_peers_name[i])) | ||
382 | { | ||
383 | fprintf (stderr, | ||
384 | "Malformed configuration file `%s', exiting ...\n", | ||
385 | cfg_peers_name[i]); | ||
386 | return 1; | ||
387 | } | ||
388 | } | ||
389 | else | ||
390 | { | ||
391 | if (GNUNET_SYSERR == | ||
392 | GNUNET_CONFIGURATION_load (cfg_peers[i], | ||
393 | NULL)) | ||
394 | { | ||
395 | fprintf (stderr, | ||
396 | "Configuration file %s does not exist, exiting ...\n", | ||
397 | cfg_peers_name[i]); | ||
398 | return 1; | ||
399 | } | ||
400 | } | ||
401 | private_key = | ||
402 | GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg_peers[i]); | ||
403 | if (NULL == private_key) | ||
404 | { | ||
405 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
406 | "Unable to get peer ID\n"); | ||
407 | return 1; | ||
408 | } | ||
409 | GNUNET_CRYPTO_eddsa_key_get_public (private_key, | ||
410 | &peer_id[i].public_key); | ||
411 | GNUNET_free (private_key); | ||
412 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
413 | "Identity of peer %u is %s\n", | ||
414 | i, GNUNET_i2s_full (&peer_id[i])); | ||
415 | } | ||
416 | fprintf (stderr, "Starting test...\n"); | ||
417 | GNUNET_SCHEDULER_run (&run, | ||
418 | NULL); | ||
419 | return ret; | ||
420 | } | ||