2 This file is part of GNUnet.
3 Copyright (C) 2006, 2009, 2015, 2016 GNUnet e.V.
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.
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.
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.
22 * @file transport-testing.h
23 * @brief testing lib for transport service
24 * @author Matthias Wachs
25 * @author Christian Grothoff
27 #ifndef TRANSPORT_TESTING_H
28 #define TRANSPORT_TESTING_H
30 #include "gnunet_util_lib.h"
31 #include "gnunet_hello_lib.h"
32 #include "gnunet_transport_service.h"
33 #include "gnunet_testing_lib.h"
36 /* ************* Basic functions for starting/stopping/connecting *********** */
39 * Context for a single peer
41 struct GNUNET_TRANSPORT_TESTING_PeerContext;
44 * Definition for a transport testing handle
46 struct GNUNET_TRANSPORT_TESTING_Handle;
50 * Callback when two peers are connected and both have called the connect callback
51 * to notify clients about a new peer
53 * @param p FIXME: remove ASAP.
57 (*GNUNET_TRANSPORT_TESTING_StartCallback) (struct GNUNET_TRANSPORT_TESTING_PeerContext *p,
63 * Context for a single peer
65 struct GNUNET_TRANSPORT_TESTING_PeerContext
68 * Next element in the DLL
70 struct GNUNET_TRANSPORT_TESTING_PeerContext *next;
73 * Previous element in the DLL
75 struct GNUNET_TRANSPORT_TESTING_PeerContext *prev;
78 * Transport testing handle this peer belongs to
80 struct GNUNET_TRANSPORT_TESTING_Handle *tth;
83 * Peer's configuration
85 struct GNUNET_CONFIGURATION_Handle *cfg;
88 * Peer's transport service handle
90 struct GNUNET_TRANSPORT_Handle *th;
95 struct GNUNET_ATS_ConnectivityHandle *ats;
98 * Peer's transport get hello handle to retrieve peer's HELLO message
100 struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
103 * Peer's testing handle
105 struct GNUNET_TESTING_Peer *peer;
110 struct GNUNET_PeerIdentity id;
113 * Handle for the peer's ARM process
115 struct GNUNET_OS_Process *arm_proc;
120 GNUNET_TRANSPORT_ReceiveCallback rec;
123 * Notify connect callback
125 GNUNET_TRANSPORT_NotifyConnect nc;
128 * Notify disconnect callback
130 GNUNET_TRANSPORT_NotifyDisconnect nd;
133 * Startup completed callback
135 GNUNET_TRANSPORT_TESTING_StartCallback start_cb;
138 * Peers HELLO Message
140 struct GNUNET_HELLO_Message *hello;
143 * Closure for the callbacks
148 * An unique number to identify the peer
155 * Handle for a request to connect two peers.
157 struct GNUNET_TRANSPORT_TESTING_ConnectRequest
162 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *next;
167 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *prev;
170 * Peer we want to connect.
172 struct GNUNET_TRANSPORT_TESTING_PeerContext *p1;
175 * Peer we want to connect.
177 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2;
180 * Task by which we accomplish the connection.
182 struct GNUNET_SCHEDULER_Task *tct;
185 * Handle by which we ask ATS to faciliate the connection.
187 struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
190 * Handle by which we inform the peer about the HELLO of
193 struct GNUNET_TRANSPORT_OfferHelloHandle *oh;
196 * Function to call upon completion.
198 GNUNET_SCHEDULER_TaskCallback cb;
211 * Handle for a test run.
213 struct GNUNET_TRANSPORT_TESTING_Handle
216 * Testing library system handle
218 struct GNUNET_TESTING_System *tl_system;
221 * head DLL of connect contexts
223 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc_head;
226 * head DLL of connect contexts
228 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc_tail;
233 struct GNUNET_TRANSPORT_TESTING_PeerContext *p_head;
238 struct GNUNET_TRANSPORT_TESTING_PeerContext *p_tail;
243 * Initialize the transport testing
245 * @return transport testing handle
247 struct GNUNET_TRANSPORT_TESTING_Handle *
248 GNUNET_TRANSPORT_TESTING_init (void);
252 * Clean up the transport testing
254 * @param tth transport testing handle
257 GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_Handle *tth);
261 * Start a peer with the given configuration
263 * @param tth the testing handle
264 * @param cfgname configuration file
265 * @param peer_id the peer_id
266 * @param rec receive callback
267 * @param nc connect callback
268 * @param nd disconnect callback
269 * @param start_cb start callback
270 * @param cb_cls closure for callback
271 * @return the peer context
273 struct GNUNET_TRANSPORT_TESTING_PeerContext *
274 GNUNET_TRANSPORT_TESTING_start_peer (struct GNUNET_TRANSPORT_TESTING_Handle *tth,
277 GNUNET_TRANSPORT_ReceiveCallback rec,
278 GNUNET_TRANSPORT_NotifyConnect nc,
279 GNUNET_TRANSPORT_NotifyDisconnect nd,
280 GNUNET_TRANSPORT_TESTING_StartCallback start_cb,
285 * Shutdown the given peer
290 GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_PeerContext *pc);
294 * Stops and restarts the given peer, sleeping (!) for 5s in between.
297 * @param restart_cb restart callback
298 * @param cb_cls callback closure
299 * @return #GNUNET_OK in success otherwise #GNUNET_SYSERR
302 GNUNET_TRANSPORT_TESTING_restart_peer (struct GNUNET_TRANSPORT_TESTING_PeerContext *p,
303 GNUNET_TRANSPORT_TESTING_StartCallback restart_cb,
309 * Connect the given peers and call the callback when both peers
310 * report the inbound connection. Remarks: start_peer's notify_connect
311 * callback can be called before.
315 * @param cb the callback to call when both peers notified that they are connected
316 * @param cls callback cls
317 * @return a connect request handle
319 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *
320 GNUNET_TRANSPORT_TESTING_connect_peers (struct GNUNET_TRANSPORT_TESTING_PeerContext *p1,
321 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2,
322 GNUNET_SCHEDULER_TaskCallback cb,
327 * Cancel the request to connect two peers. You MUST cancel the
328 * request if you stop the peers before the peers connected
331 * @param cc a connect request handle
334 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc);
337 /* ********************** high-level process functions *************** */
341 * Function called once the peers have been launched and
342 * connected by #GNUNET_TRANSPORT_TESTING_connect_check().
345 * @param num_peers size of the @a p array
346 * @param p the peers that were launched
349 (*GNUNET_TRANSPORT_TESTING_ConnectContinuation)(void *cls,
350 unsigned int num_peers,
351 struct GNUNET_TRANSPORT_TESTING_PeerContext *p[]);
355 * Internal data structure.
357 struct GNUNET_TRANSPORT_TESTING_ConnectRequestList;
360 * Internal data structure.
362 struct GNUNET_TRANSPORT_TESTING_InternalPeerContext;
366 * Function called by the transport for each received message.
369 * @param receiver receiver of the message
370 * @param sender sender of the message
371 * @param message the message
374 (*GNUNET_TRANSPORT_TESTING_ReceiveCallback) (void *cls,
375 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
376 const struct GNUNET_PeerIdentity *sender,
377 const struct GNUNET_MessageHeader *message);
381 * Function called to notify transport users that another
382 * peer connected to us.
385 * @param me peer experiencing the event
386 * @param other peer that connected to @a me
389 (*GNUNET_TRANSPORT_TESTING_NotifyConnect) (void *cls,
390 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
391 const struct GNUNET_PeerIdentity *other);
395 * Function called to notify transport users that another
396 * peer disconnected from us.
399 * @param me peer experiencing the event
400 * @param other peer that disconnected from @a me
403 (*GNUNET_TRANSPORT_TESTING_NotifyDisconnect) (void *cls,
404 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
405 const struct GNUNET_PeerIdentity *other);
409 * Closure that must be passed to
410 * #GNUNET_TRANSPORT_TESTING_connect_check.
412 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext
416 * How should we continue after the connect?
418 GNUNET_SCHEDULER_TaskCallback connect_continuation;
421 * Closure for @e connect_continuation.
423 void *connect_continuation_cls;
426 * Which configuration file should we pass to the
427 * #GNUNET_PROGRAM_run() of the testcase?
429 const char *config_file;
432 * Receiver argument to give for peers we start.
434 GNUNET_TRANSPORT_TESTING_ReceiveCallback rec;
437 * Notify connect argument to give for peers we start.
439 GNUNET_TRANSPORT_TESTING_NotifyConnect nc;
442 * Notify disconnect argument to give for peers we start.
444 GNUNET_TRANSPORT_TESTING_NotifyDisconnect nd;
447 * Closure for @e rec, @e nc and @e nd.
452 * Custom task to run on shutdown.
454 GNUNET_SCHEDULER_TaskCallback shutdown_task;
457 * Closure for @e shutdown_task.
459 void *shutdown_task_cls;
462 * Custom task to run after peers were started but before we try to
463 * connect them. If this function is set, we wait ONE second after
464 * running this function until we continue with connecting the
467 GNUNET_SCHEDULER_TaskCallback pre_connect_task;
470 * Closure for @e shutdown_task.
472 void *pre_connect_task_cls;
475 * When should the testcase time out?
477 struct GNUNET_TIME_Relative timeout;
480 * Should we try to create connections in both directions?
484 /* ******* fields set by #GNUNET_TRANSPORT_TESTING_connect_check **** */
487 * Number of peers involved in the test.
489 unsigned int num_peers;
492 * Configuration files we have, array with @e num_peers entries.
497 * Array with @e num_peers entries.
499 struct GNUNET_TRANSPORT_TESTING_PeerContext **p;
502 * Name of the plugin.
504 const char *test_plugin;
507 * Name of the testcase.
509 const char *test_name;
512 * Configuration object for the testcase.
514 const struct GNUNET_CONFIGURATION_Handle *cfg;
517 * Main testing handle.
519 struct GNUNET_TRANSPORT_TESTING_Handle *tth;
522 * Result from the main function, set to #GNUNET_OK on success.
523 * Clients should set to #GNUNET_SYSERR to indicate test failure.
527 /* ******* internal state, clients should not mess with this **** */
530 * Task run on timeout.
532 struct GNUNET_SCHEDULER_Task *timeout_task;
535 * Task run to connect peers.
537 struct GNUNET_SCHEDULER_Task *connect_task;
540 * Number of peers that have been started.
542 unsigned int started;
545 * DLL of active connect requests.
547 struct GNUNET_TRANSPORT_TESTING_ConnectRequestList *crl_head;
550 * DLL of active connect requests.
552 struct GNUNET_TRANSPORT_TESTING_ConnectRequestList *crl_tail;
555 * Array with @e num_peers entries.
557 struct GNUNET_TRANSPORT_TESTING_InternalPeerContext *ip;
563 * Find peer by peer ID.
565 * @param ccc context to search
566 * @param peer peer to look for
567 * @return NULL if @a peer was not found
569 struct GNUNET_TRANSPORT_TESTING_PeerContext *
570 GNUNET_TRANSPORT_TESTING_find_peer (struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc,
571 const struct GNUNET_PeerIdentity *peer);
575 * Common implementation of the #GNUNET_TRANSPORT_TESTING_CheckCallback.
576 * Starts and connects the two peers, then invokes the
577 * `connect_continuation` from @a cls. Sets up a timeout to
578 * abort the test, and a shutdown handler to clean up properly
581 * @param cls closure of type `struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext`
582 * @param tth_ initialized testing handle
583 * @param test_plugin_ name of the plugin
584 * @param test_name_ name of the test
585 * @param num_peers number of entries in the @a cfg_file array
586 * @param cfg_files array of names of configuration files for the peers
587 * @return #GNUNET_SYSERR on error
590 GNUNET_TRANSPORT_TESTING_connect_check (void *cls,
591 struct GNUNET_TRANSPORT_TESTING_Handle *tth_,
592 const char *test_plugin_,
593 const char *test_name_,
594 unsigned int num_peers,
599 * Main function of a testcase. Called with the initial setup data
600 * for the test as derived from the source name and the binary name.
603 * @param tth_ initialized testing handle
604 * @param test_plugin_ name of the plugin
605 * @param test_name_ name of the test
606 * @param num_peers number of entries in the @a cfg_file array
607 * @param cfg_files array of names of configuration files for the peers
608 * @return #GNUNET_SYSERR on error
611 (*GNUNET_TRANSPORT_TESTING_CheckCallback)(void *cls,
612 struct GNUNET_TRANSPORT_TESTING_Handle *tth_,
613 const char *test_plugin_,
614 const char *test_name_,
615 unsigned int num_peers,
620 * Setup testcase. Calls @a check with the data the test needs.
622 * @param argv0 binary name (argv[0])
623 * @param filename source file name (__FILE__)
624 * @param num_peers number of peers to start
625 * @param check main function to run
626 * @param check_cls closure for @a check
627 * @return #GNUNET_OK on success
630 GNUNET_TRANSPORT_TESTING_main_ (const char *argv0,
631 const char *filename,
632 unsigned int num_peers,
633 GNUNET_TRANSPORT_TESTING_CheckCallback check,
638 * Setup testcase. Calls @a check with the data the test needs.
640 * @param num_peers number of peers to start
641 * @param check main function to run
642 * @param check_cls closure for @a check
643 * @return #GNUNET_OK on success
645 #define GNUNET_TRANSPORT_TESTING_main(num_peers,check,check_cls) \
646 GNUNET_TRANSPORT_TESTING_main_ (argv[0], __FILE__, num_peers, check, check_cls)
650 /* ********************** log-only convenience functions ************* */
654 * Log a connect event.
657 * @param me peer that had the event
658 * @param other peer that connected.
661 GNUNET_TRANSPORT_TESTING_log_connect (void *cls,
662 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
663 const struct GNUNET_PeerIdentity *other);
667 * Log a disconnect event.
670 * @param me peer that had the event
671 * @param other peer that disconnected.
674 GNUNET_TRANSPORT_TESTING_log_disconnect (void *cls,
675 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
676 const struct GNUNET_PeerIdentity *other);
680 /* ********************** low-level filename functions *************** */
684 * Extracts the test filename from an absolute file name and removes
687 * @param file absolute file name
688 * @return resulting test name
691 GNUNET_TRANSPORT_TESTING_get_test_name (const char *file);
695 * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
696 * if existing ".exe"-prefix and adds the peer-number
698 * @param file filename of the test, e.g. argv[0]
699 * @param count peer number
700 * @return configuration name to use
703 GNUNET_TRANSPORT_TESTING_get_config_name (const char *file,
708 * Extracts the plugin anme from an absolute file name and the test name
709 * @param file absolute file name
710 * @param test test name
711 * @return the plugin name
714 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *executable,
715 const char *testname);
719 * Extracts the filename from an absolute file name and removes the
722 * @param file absolute file name
723 * @return the source name
726 GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file);
729 /* end of transport_testing.h */