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 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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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_transport_hello_service.h"
34 #include "gnunet_transport_manipulation_service.h"
35 #include "gnunet_testing_lib.h"
38 /* ************* Basic functions for starting/stopping/connecting *********** */
41 * Context for a single peer
43 struct GNUNET_TRANSPORT_TESTING_PeerContext;
46 * Definition for a transport testing handle
48 struct GNUNET_TRANSPORT_TESTING_Handle;
52 * Context for a single peer
54 struct GNUNET_TRANSPORT_TESTING_PeerContext
57 * Next element in the DLL
59 struct GNUNET_TRANSPORT_TESTING_PeerContext *next;
62 * Previous element in the DLL
64 struct GNUNET_TRANSPORT_TESTING_PeerContext *prev;
67 * Transport testing handle this peer belongs to
69 struct GNUNET_TRANSPORT_TESTING_Handle *tth;
72 * Peer's configuration
74 struct GNUNET_CONFIGURATION_Handle *cfg;
77 * Peer's transport service handle
79 struct GNUNET_TRANSPORT_CoreHandle *th;
82 * Peer's transport service manipulation handle
84 struct GNUNET_TRANSPORT_ManipulationHandle *tmh;
89 struct GNUNET_ATS_ConnectivityHandle *ats;
92 * Peer's transport get hello handle to retrieve peer's HELLO message
94 struct GNUNET_TRANSPORT_HelloGetHandle *ghh;
97 * Peer's testing handle
99 struct GNUNET_TESTING_Peer *peer;
104 struct GNUNET_PeerIdentity id;
107 * Handle for the peer's ARM process
109 struct GNUNET_OS_Process *arm_proc;
114 struct GNUNET_MQ_MessageHandler *handlers;
117 * Notify connect callback
119 GNUNET_TRANSPORT_NotifyConnect nc;
122 * Notify disconnect callback
124 GNUNET_TRANSPORT_NotifyDisconnect nd;
127 * Startup completed callback
129 GNUNET_SCHEDULER_TaskCallback start_cb;
132 * Peers HELLO Message
134 struct GNUNET_HELLO_Message *hello;
137 * Closure for the @a nc and @a nd callbacks
142 * Closure for @e start_cb.
147 * An unique number to identify the peer
154 * Handle for a request to connect two peers.
156 struct GNUNET_TRANSPORT_TESTING_ConnectRequest
161 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *next;
166 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *prev;
169 * Peer we want to connect.
171 struct GNUNET_TRANSPORT_TESTING_PeerContext *p1;
174 * Peer we want to connect.
176 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2;
179 * Task by which we accomplish the connection.
181 struct GNUNET_SCHEDULER_Task *tct;
184 * Handle by which we ask ATS to faciliate the connection.
186 struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
189 * Handle by which we inform the peer about the HELLO of
192 struct GNUNET_TRANSPORT_OfferHelloHandle *oh;
195 * Function to call upon completion.
197 GNUNET_SCHEDULER_TaskCallback cb;
205 * Message queue for sending from @a p1 to @a p2.
207 struct GNUNET_MQ_Handle *mq;
210 * Set if peer1 says the connection is up to peer2.
215 * Set if peer2 says the connection is up to peer1.
220 * #GNUNET_YES if both @e p1_c and @e p2_c are #GNUNET_YES.
227 * Handle for a test run.
229 struct GNUNET_TRANSPORT_TESTING_Handle
232 * Testing library system handle
234 struct GNUNET_TESTING_System *tl_system;
237 * head DLL of connect contexts
239 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc_head;
242 * head DLL of connect contexts
244 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc_tail;
249 struct GNUNET_TRANSPORT_TESTING_PeerContext *p_head;
254 struct GNUNET_TRANSPORT_TESTING_PeerContext *p_tail;
259 * Initialize the transport testing
261 * @return transport testing handle
263 struct GNUNET_TRANSPORT_TESTING_Handle *
264 GNUNET_TRANSPORT_TESTING_init (void);
268 * Clean up the transport testing
270 * @param tth transport testing handle
273 GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_Handle *tth);
277 * Start a peer with the given configuration
279 * @param tth the testing handle
280 * @param cfgname configuration file
281 * @param peer_id the peer_id
282 * @param handlers functions for receiving messages
283 * @param nc connect callback
284 * @param nd disconnect callback
285 * @param cb_cls closure for @a nc and @a nd callback
286 * @param start_cb start callback
287 * @param start_cb_cls closure for @a start_cb
288 * @return the peer context
290 struct GNUNET_TRANSPORT_TESTING_PeerContext *
291 GNUNET_TRANSPORT_TESTING_start_peer (
292 struct GNUNET_TRANSPORT_TESTING_Handle *tth,
295 const struct GNUNET_MQ_MessageHandler *handlers,
296 GNUNET_TRANSPORT_NotifyConnect nc,
297 GNUNET_TRANSPORT_NotifyDisconnect nd,
299 GNUNET_SCHEDULER_TaskCallback start_cb,
304 * Shutdown the given peer
309 GNUNET_TRANSPORT_TESTING_stop_peer (
310 struct GNUNET_TRANSPORT_TESTING_PeerContext *pc);
314 * Stops and restarts the given peer, sleeping (!) for 5s in between.
317 * @param restart_cb restart callback
318 * @param restart_cb_cls callback closure
319 * @return #GNUNET_OK in success otherwise #GNUNET_SYSERR
322 GNUNET_TRANSPORT_TESTING_restart_peer (
323 struct GNUNET_TRANSPORT_TESTING_PeerContext *p,
324 GNUNET_SCHEDULER_TaskCallback restart_cb,
325 void *restart_cb_cls);
329 * Connect the given peers and call the callback when both peers
330 * report the inbound connection. Remarks: start_peer's notify_connect
331 * callback can be called before.
335 * @param cb the callback to call when both peers notified that they are
337 * @param cls callback cls
338 * @return a connect request handle
340 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *
341 GNUNET_TRANSPORT_TESTING_connect_peers (
342 struct GNUNET_TRANSPORT_TESTING_PeerContext *p1,
343 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2,
344 GNUNET_SCHEDULER_TaskCallback cb,
349 * Cancel the request to connect two peers. You MUST cancel the
350 * request if you stop the peers before the peers connected
353 * @param cc a connect request handle
356 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (
357 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc);
361 * Function called on matching connect requests.
364 * @param cc request matching the query
366 typedef void (*GNUNET_TRANSPORT_TESTING_ConnectContextCallback) (
368 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc);
372 * Find any connecting context matching the given pair of peers.
374 * @param p1 first peer
375 * @param p2 second peer
376 * @param cb function to call
377 * @param cb_cls closure for @a cb
380 GNUNET_TRANSPORT_TESTING_find_connecting_context (
381 struct GNUNET_TRANSPORT_TESTING_PeerContext *p1,
382 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2,
383 GNUNET_TRANSPORT_TESTING_ConnectContextCallback cb,
387 /* ********************** high-level process functions *************** */
391 * Function called once the peers have been launched and
392 * connected by #GNUNET_TRANSPORT_TESTING_connect_check().
395 * @param num_peers size of the @a p array
396 * @param p the peers that were launched
398 typedef void (*GNUNET_TRANSPORT_TESTING_ConnectContinuation) (
400 unsigned int num_peers,
401 struct GNUNET_TRANSPORT_TESTING_PeerContext *p[]);
405 * Internal data structure.
407 struct GNUNET_TRANSPORT_TESTING_ConnectRequestList;
410 * Internal data structure.
412 struct GNUNET_TRANSPORT_TESTING_InternalPeerContext;
415 GNUNET_NETWORK_STRUCT_BEGIN
416 struct GNUNET_TRANSPORT_TESTING_TestMessage
419 * Type is (usually) #GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE.
421 struct GNUNET_MessageHeader header;
424 * Monotonically increasing counter throughout the test.
426 uint32_t num GNUNET_PACKED;
428 GNUNET_NETWORK_STRUCT_END
432 * Function called by the transport for each received message.
435 * @param receiver receiver of the message
436 * @param sender sender of the message
437 * @param message the message
439 typedef void (*GNUNET_TRANSPORT_TESTING_ReceiveCallback) (
441 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
442 const struct GNUNET_PeerIdentity *sender,
443 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message);
447 * Function called to notify transport users that another
448 * peer connected to us.
451 * @param me peer experiencing the event
452 * @param other peer that connected to @a me
454 typedef void (*GNUNET_TRANSPORT_TESTING_NotifyConnect) (
456 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
457 const struct GNUNET_PeerIdentity *other);
461 * Function called to notify transport users that another
462 * peer disconnected from us.
465 * @param me peer experiencing the event
466 * @param other peer that disconnected from @a me
468 typedef void (*GNUNET_TRANSPORT_TESTING_NotifyDisconnect) (
470 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
471 const struct GNUNET_PeerIdentity *other);
475 * Closure that must be passed to
476 * #GNUNET_TRANSPORT_TESTING_connect_check.
478 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext
482 * How should we continue after the connect?
484 GNUNET_SCHEDULER_TaskCallback connect_continuation;
487 * Closure for @e connect_continuation.
489 void *connect_continuation_cls;
492 * Which configuration file should we pass to the
493 * #GNUNET_PROGRAM_run() of the testcase?
495 const char *config_file;
498 * Receiver argument to give for peers we start.
500 GNUNET_TRANSPORT_TESTING_ReceiveCallback rec;
503 * Notify connect argument to give for peers we start.
505 GNUNET_TRANSPORT_TESTING_NotifyConnect nc;
508 * Notify disconnect argument to give for peers we start.
510 GNUNET_TRANSPORT_TESTING_NotifyDisconnect nd;
513 * Closure for @e rec, @e nc and @e nd.
518 * Custom task to run on shutdown.
520 GNUNET_SCHEDULER_TaskCallback shutdown_task;
523 * Closure for @e shutdown_task.
525 void *shutdown_task_cls;
528 * Custom task to run after peers were started but before we try to
529 * connect them. If this function is set, we wait ONE second after
530 * running this function until we continue with connecting the
533 GNUNET_SCHEDULER_TaskCallback pre_connect_task;
536 * Closure for @e shutdown_task.
538 void *pre_connect_task_cls;
541 * When should the testcase time out?
543 struct GNUNET_TIME_Relative timeout;
546 * Should we try to create connections in both directions?
550 /* ******* fields set by #GNUNET_TRANSPORT_TESTING_connect_check **** */
553 * Number of peers involved in the test.
555 unsigned int num_peers;
558 * Configuration files we have, array with @e num_peers entries.
563 * Array with @e num_peers entries.
565 struct GNUNET_TRANSPORT_TESTING_PeerContext **p;
568 * Name of the plugin.
570 const char *test_plugin;
573 * Name of the testcase.
575 const char *test_name;
578 * Configuration object for the testcase.
580 const struct GNUNET_CONFIGURATION_Handle *cfg;
583 * Main testing handle.
585 struct GNUNET_TRANSPORT_TESTING_Handle *tth;
588 * Result from the main function, set to #GNUNET_OK on success.
589 * Clients should set to #GNUNET_SYSERR to indicate test failure.
594 * Generator for the `num` field in test messages. Incremented each
595 * time #GNUNET_TRANSPORT_TESTING_simple_send or
596 * #GNUNET_TRANSPORT_TESTING_large_send are used to transmit a
599 uint32_t send_num_gen;
601 /* ******* internal state, clients should not mess with this **** */
604 * Task run on timeout.
606 struct GNUNET_SCHEDULER_Task *timeout_task;
609 * Task run to connect peers.
611 struct GNUNET_SCHEDULER_Task *connect_task;
614 * Number of peers that have been started.
616 unsigned int started;
619 * DLL of active connect requests.
621 struct GNUNET_TRANSPORT_TESTING_ConnectRequestList *crl_head;
624 * DLL of active connect requests.
626 struct GNUNET_TRANSPORT_TESTING_ConnectRequestList *crl_tail;
629 * Array with @e num_peers entries.
631 struct GNUNET_TRANSPORT_TESTING_InternalPeerContext *ip;
636 * Find peer by peer ID.
638 * @param ccc context to search
639 * @param peer peer to look for
640 * @return NULL if @a peer was not found
642 struct GNUNET_TRANSPORT_TESTING_PeerContext *
643 GNUNET_TRANSPORT_TESTING_find_peer (
644 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc,
645 const struct GNUNET_PeerIdentity *peer);
649 * Common implementation of the #GNUNET_TRANSPORT_TESTING_CheckCallback.
650 * Starts and connects the two peers, then invokes the
651 * `connect_continuation` from @a cls. Sets up a timeout to
652 * abort the test, and a shutdown handler to clean up properly
655 * @param cls closure of type `struct
656 * GNUNET_TRANSPORT_TESTING_ConnectCheckContext`
657 * @param tth_ initialized testing handle
658 * @param test_plugin_ name of the plugin
659 * @param test_name_ name of the test
660 * @param num_peers number of entries in the @a cfg_file array
661 * @param cfg_files array of names of configuration files for the peers
662 * @return #GNUNET_SYSERR on error
665 GNUNET_TRANSPORT_TESTING_connect_check (
667 struct GNUNET_TRANSPORT_TESTING_Handle *tth_,
668 const char *test_plugin_,
669 const char *test_name_,
670 unsigned int num_peers,
675 * Main function of a testcase. Called with the initial setup data
676 * for the test as derived from the source name and the binary name.
679 * @param tth_ initialized testing handle
680 * @param test_plugin_ name of the plugin
681 * @param test_name_ name of the test
682 * @param num_peers number of entries in the @a cfg_file array
683 * @param cfg_files array of names of configuration files for the peers
684 * @return #GNUNET_SYSERR on error
686 typedef int (*GNUNET_TRANSPORT_TESTING_CheckCallback) (
688 struct GNUNET_TRANSPORT_TESTING_Handle *tth_,
689 const char *test_plugin_,
690 const char *test_name_,
691 unsigned int num_peers,
696 * Setup testcase. Calls @a check with the data the test needs.
698 * @param argv0 binary name (argv[0])
699 * @param filename source file name (__FILE__)
700 * @param num_peers number of peers to start
701 * @param check main function to run
702 * @param check_cls closure for @a check
703 * @return #GNUNET_OK on success
706 GNUNET_TRANSPORT_TESTING_main_ (const char *argv0,
707 const char *filename,
708 unsigned int num_peers,
709 GNUNET_TRANSPORT_TESTING_CheckCallback check,
714 * Setup testcase. Calls @a check with the data the test needs.
716 * @param num_peers number of peers to start
717 * @param check main function to run
718 * @param check_cls closure for @a check
719 * @return #GNUNET_OK on success
721 #define GNUNET_TRANSPORT_TESTING_main(num_peers, check, check_cls) \
722 GNUNET_TRANSPORT_TESTING_main_ (argv[0], \
728 /* ***************** Convenience functions for sending ********* */
731 * Send a test message of type @a mtype and size @a msize from
732 * peer @a sender to peer @a receiver. The peers should be
733 * connected when this function is called.
735 * @param sender the sending peer
736 * @param receiver the receiving peer
737 * @param mtype message type to use
738 * @param msize size of the message, at least `sizeof (struct
739 * GNUNET_TRANSPORT_TESTING_TestMessage)`
740 * @param num unique message number
741 * @param cont continuation to call after transmission
742 * @param cont_cls closure for @a cont
743 * @return #GNUNET_OK if message was queued,
744 * #GNUNET_NO if peers are not connected
745 * #GNUNET_SYSERR if @a msize is illegal
748 GNUNET_TRANSPORT_TESTING_send (
749 struct GNUNET_TRANSPORT_TESTING_PeerContext *sender,
750 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
754 GNUNET_SCHEDULER_TaskCallback cont,
759 * Message type used by #GNUNET_TRANSPORT_TESTING_simple_send().
761 #define GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE 12345
764 * Alternative message type for tests.
766 #define GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE2 12346
770 * Type of the closure argument to pass to
771 * #GNUNET_TRANSPORT_TESTING_simple_send() and
772 * #GNUNET_TRANSPORT_TESTING_large_send().
774 struct GNUNET_TRANSPORT_TESTING_SendClosure
777 * Context for the transmission.
779 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
782 * Function that returns the desired message size. Overrides
783 * the message size, can be NULL in which case the message
784 * size is the default.
786 size_t (*get_size_cb) (unsigned int n);
789 * Number of messages to be transmitted in a loop.
790 * Use zero for "forever" (until external shutdown).
792 unsigned int num_messages;
795 * Function to call after all transmissions, can be NULL.
797 GNUNET_SCHEDULER_TaskCallback cont;
800 * Closure for @e cont.
807 * Task that sends a minimalistic test message from the
808 * first peer to the second peer.
810 * @param cls the `struct GNUNET_TRANSPORT_TESTING_SendClosure`
811 * which should contain at least two peers, the first two
812 * of which should be currently connected
815 GNUNET_TRANSPORT_TESTING_simple_send (void *cls);
818 * Size of a message sent with
819 * #GNUNET_TRANSPORT_TESTING_large_send(). Big enough
820 * to usually force defragmentation.
822 #define GNUNET_TRANSPORT_TESTING_LARGE_MESSAGE_SIZE 2600
825 * Task that sends a large test message from the
826 * first peer to the second peer.
828 * @param cls the `struct GNUNET_TRANSPORT_TESTING_SendClosure`
829 * which should contain at least two peers, the first two
830 * of which should be currently connected
833 GNUNET_TRANSPORT_TESTING_large_send (void *cls);
836 /* ********************** log-only convenience functions ************* */
840 * Log a connect event.
843 * @param me peer that had the event
844 * @param other peer that connected.
847 GNUNET_TRANSPORT_TESTING_log_connect (
849 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
850 const struct GNUNET_PeerIdentity *other);
854 * Log a disconnect event.
857 * @param me peer that had the event
858 * @param other peer that disconnected.
861 GNUNET_TRANSPORT_TESTING_log_disconnect (
863 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
864 const struct GNUNET_PeerIdentity *other);
867 /* ********************** low-level filename functions *************** */
871 * Extracts the test filename from an absolute file name and removes
874 * @param file absolute file name
875 * @return resulting test name
878 GNUNET_TRANSPORT_TESTING_get_test_name (const char *file);
882 * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
883 * if existing ".exe"-prefix and adds the peer-number
885 * @param file filename of the test, e.g. argv[0]
886 * @param count peer number
887 * @return configuration name to use
890 GNUNET_TRANSPORT_TESTING_get_config_name (const char *file, int count);
894 * Extracts the plugin anme from an absolute file name and the test name
895 * @param file absolute file name
896 * @param test test name
897 * @return the plugin name
900 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *executable,
901 const char *testname);
905 * Extracts the filename from an absolute file name and removes the
908 * @param file absolute file name
909 * @return the source name
912 GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file);
915 /* end of transport_testing.h */