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
481 * How should we continue after the connect?
483 GNUNET_SCHEDULER_TaskCallback connect_continuation;
486 * Closure for @e connect_continuation.
488 void *connect_continuation_cls;
491 * Which configuration file should we pass to the
492 * #GNUNET_PROGRAM_run() of the testcase?
494 const char *config_file;
497 * Receiver argument to give for peers we start.
499 GNUNET_TRANSPORT_TESTING_ReceiveCallback rec;
502 * Notify connect argument to give for peers we start.
504 GNUNET_TRANSPORT_TESTING_NotifyConnect nc;
507 * Notify disconnect argument to give for peers we start.
509 GNUNET_TRANSPORT_TESTING_NotifyDisconnect nd;
512 * Closure for @e rec, @e nc and @e nd.
517 * Custom task to run on shutdown.
519 GNUNET_SCHEDULER_TaskCallback shutdown_task;
522 * Closure for @e shutdown_task.
524 void *shutdown_task_cls;
527 * Custom task to run after peers were started but before we try to
528 * connect them. If this function is set, we wait ONE second after
529 * running this function until we continue with connecting the
532 GNUNET_SCHEDULER_TaskCallback pre_connect_task;
535 * Closure for @e shutdown_task.
537 void *pre_connect_task_cls;
540 * When should the testcase time out?
542 struct GNUNET_TIME_Relative timeout;
545 * Should we try to create connections in both directions?
549 /* ******* fields set by #GNUNET_TRANSPORT_TESTING_connect_check **** */
552 * Number of peers involved in the test.
554 unsigned int num_peers;
557 * Configuration files we have, array with @e num_peers entries.
562 * Array with @e num_peers entries.
564 struct GNUNET_TRANSPORT_TESTING_PeerContext **p;
567 * Name of the plugin.
569 const char *test_plugin;
572 * Name of the testcase.
574 const char *test_name;
577 * Configuration object for the testcase.
579 const struct GNUNET_CONFIGURATION_Handle *cfg;
582 * Main testing handle.
584 struct GNUNET_TRANSPORT_TESTING_Handle *tth;
587 * Result from the main function, set to #GNUNET_OK on success.
588 * Clients should set to #GNUNET_SYSERR to indicate test failure.
593 * Generator for the `num` field in test messages. Incremented each
594 * time #GNUNET_TRANSPORT_TESTING_simple_send or
595 * #GNUNET_TRANSPORT_TESTING_large_send are used to transmit a
598 uint32_t send_num_gen;
600 /* ******* internal state, clients should not mess with this **** */
603 * Task run on timeout.
605 struct GNUNET_SCHEDULER_Task *timeout_task;
608 * Task run to connect peers.
610 struct GNUNET_SCHEDULER_Task *connect_task;
613 * Number of peers that have been started.
615 unsigned int started;
618 * DLL of active connect requests.
620 struct GNUNET_TRANSPORT_TESTING_ConnectRequestList *crl_head;
623 * DLL of active connect requests.
625 struct GNUNET_TRANSPORT_TESTING_ConnectRequestList *crl_tail;
628 * Array with @e num_peers entries.
630 struct GNUNET_TRANSPORT_TESTING_InternalPeerContext *ip;
635 * Find peer by peer ID.
637 * @param ccc context to search
638 * @param peer peer to look for
639 * @return NULL if @a peer was not found
641 struct GNUNET_TRANSPORT_TESTING_PeerContext *
642 GNUNET_TRANSPORT_TESTING_find_peer (
643 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc,
644 const struct GNUNET_PeerIdentity *peer);
648 * Common implementation of the #GNUNET_TRANSPORT_TESTING_CheckCallback.
649 * Starts and connects the two peers, then invokes the
650 * `connect_continuation` from @a cls. Sets up a timeout to
651 * abort the test, and a shutdown handler to clean up properly
654 * @param cls closure of type `struct
655 * GNUNET_TRANSPORT_TESTING_ConnectCheckContext`
656 * @param tth_ initialized testing handle
657 * @param test_plugin_ name of the plugin
658 * @param test_name_ name of the test
659 * @param num_peers number of entries in the @a cfg_file array
660 * @param cfg_files array of names of configuration files for the peers
661 * @return #GNUNET_SYSERR on error
664 GNUNET_TRANSPORT_TESTING_connect_check (
666 struct GNUNET_TRANSPORT_TESTING_Handle *tth_,
667 const char *test_plugin_,
668 const char *test_name_,
669 unsigned int num_peers,
674 * Main function of a testcase. Called with the initial setup data
675 * for the test as derived from the source name and the binary name.
678 * @param tth_ initialized testing handle
679 * @param test_plugin_ name of the plugin
680 * @param test_name_ name of the test
681 * @param num_peers number of entries in the @a cfg_file array
682 * @param cfg_files array of names of configuration files for the peers
683 * @return #GNUNET_SYSERR on error
685 typedef int (*GNUNET_TRANSPORT_TESTING_CheckCallback) (
687 struct GNUNET_TRANSPORT_TESTING_Handle *tth_,
688 const char *test_plugin_,
689 const char *test_name_,
690 unsigned int num_peers,
695 * Setup testcase. Calls @a check with the data the test needs.
697 * @param argv0 binary name (argv[0])
698 * @param filename source file name (__FILE__)
699 * @param num_peers number of peers to start
700 * @param check main function to run
701 * @param check_cls closure for @a check
702 * @return #GNUNET_OK on success
705 GNUNET_TRANSPORT_TESTING_main_ (const char *argv0,
706 const char *filename,
707 unsigned int num_peers,
708 GNUNET_TRANSPORT_TESTING_CheckCallback check,
713 * Setup testcase. Calls @a check with the data the test needs.
715 * @param num_peers number of peers to start
716 * @param check main function to run
717 * @param check_cls closure for @a check
718 * @return #GNUNET_OK on success
720 #define GNUNET_TRANSPORT_TESTING_main(num_peers, check, check_cls) \
721 GNUNET_TRANSPORT_TESTING_main_ (argv[0], \
727 /* ***************** Convenience functions for sending ********* */
730 * Send a test message of type @a mtype and size @a msize from
731 * peer @a sender to peer @a receiver. The peers should be
732 * connected when this function is called.
734 * @param sender the sending peer
735 * @param receiver the receiving peer
736 * @param mtype message type to use
737 * @param msize size of the message, at least `sizeof (struct
738 * GNUNET_TRANSPORT_TESTING_TestMessage)`
739 * @param num unique message number
740 * @param cont continuation to call after transmission
741 * @param cont_cls closure for @a cont
742 * @return #GNUNET_OK if message was queued,
743 * #GNUNET_NO if peers are not connected
744 * #GNUNET_SYSERR if @a msize is illegal
747 GNUNET_TRANSPORT_TESTING_send (
748 struct GNUNET_TRANSPORT_TESTING_PeerContext *sender,
749 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
753 GNUNET_SCHEDULER_TaskCallback cont,
758 * Message type used by #GNUNET_TRANSPORT_TESTING_simple_send().
760 #define GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE 12345
763 * Alternative message type for tests.
765 #define GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE2 12346
769 * Type of the closure argument to pass to
770 * #GNUNET_TRANSPORT_TESTING_simple_send() and
771 * #GNUNET_TRANSPORT_TESTING_large_send().
773 struct GNUNET_TRANSPORT_TESTING_SendClosure
776 * Context for the transmission.
778 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
781 * Function that returns the desired message size. Overrides
782 * the message size, can be NULL in which case the message
783 * size is the default.
785 size_t (*get_size_cb) (unsigned int n);
788 * Number of messages to be transmitted in a loop.
789 * Use zero for "forever" (until external shutdown).
791 unsigned int num_messages;
794 * Function to call after all transmissions, can be NULL.
796 GNUNET_SCHEDULER_TaskCallback cont;
799 * Closure for @e cont.
806 * Task that sends a minimalistic test message from the
807 * first peer to the second peer.
809 * @param cls the `struct GNUNET_TRANSPORT_TESTING_SendClosure`
810 * which should contain at least two peers, the first two
811 * of which should be currently connected
814 GNUNET_TRANSPORT_TESTING_simple_send (void *cls);
817 * Size of a message sent with
818 * #GNUNET_TRANSPORT_TESTING_large_send(). Big enough
819 * to usually force defragmentation.
821 #define GNUNET_TRANSPORT_TESTING_LARGE_MESSAGE_SIZE 2600
824 * Task that sends a large test message from the
825 * first peer to the second peer.
827 * @param cls the `struct GNUNET_TRANSPORT_TESTING_SendClosure`
828 * which should contain at least two peers, the first two
829 * of which should be currently connected
832 GNUNET_TRANSPORT_TESTING_large_send (void *cls);
835 /* ********************** log-only convenience functions ************* */
839 * Log a connect event.
842 * @param me peer that had the event
843 * @param other peer that connected.
846 GNUNET_TRANSPORT_TESTING_log_connect (
848 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
849 const struct GNUNET_PeerIdentity *other);
853 * Log a disconnect event.
856 * @param me peer that had the event
857 * @param other peer that disconnected.
860 GNUNET_TRANSPORT_TESTING_log_disconnect (
862 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
863 const struct GNUNET_PeerIdentity *other);
866 /* ********************** low-level filename functions *************** */
870 * Extracts the test filename from an absolute file name and removes
873 * @param file absolute file name
874 * @return resulting test name
877 GNUNET_TRANSPORT_TESTING_get_test_name (const char *file);
881 * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
882 * if existing ".exe"-prefix and adds the peer-number
884 * @param file filename of the test, e.g. argv[0]
885 * @param count peer number
886 * @return configuration name to use
889 GNUNET_TRANSPORT_TESTING_get_config_name (const char *file, int count);
893 * Extracts the plugin anme from an absolute file name and the test name
894 * @param file absolute file name
895 * @param test test name
896 * @return the plugin name
899 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *executable,
900 const char *testname);
904 * Extracts the filename from an absolute file name and removes the
907 * @param file absolute file name
908 * @return the source name
911 GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file);
914 /* end of transport_testing.h */