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