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 {
56 * Next element in the DLL
58 struct GNUNET_TRANSPORT_TESTING_PeerContext *next;
61 * Previous element in the DLL
63 struct GNUNET_TRANSPORT_TESTING_PeerContext *prev;
66 * Transport testing handle this peer belongs to
68 struct GNUNET_TRANSPORT_TESTING_Handle *tth;
71 * Peer's configuration
73 struct GNUNET_CONFIGURATION_Handle *cfg;
76 * Peer's transport service handle
78 struct GNUNET_TRANSPORT_CoreHandle *th;
81 * Peer's transport service manipulation handle
83 struct GNUNET_TRANSPORT_ManipulationHandle *tmh;
88 struct GNUNET_ATS_ConnectivityHandle *ats;
91 * Peer's transport get hello handle to retrieve peer's HELLO message
93 struct GNUNET_TRANSPORT_HelloGetHandle *ghh;
96 * Peer's testing handle
98 struct GNUNET_TESTING_Peer *peer;
103 struct GNUNET_PeerIdentity id;
106 * Handle for the peer's ARM process
108 struct GNUNET_OS_Process *arm_proc;
113 struct GNUNET_MQ_MessageHandler *handlers;
116 * Notify connect callback
118 GNUNET_TRANSPORT_NotifyConnect nc;
121 * Notify disconnect callback
123 GNUNET_TRANSPORT_NotifyDisconnect nd;
126 * Startup completed callback
128 GNUNET_SCHEDULER_TaskCallback start_cb;
131 * Peers HELLO Message
133 struct GNUNET_HELLO_Message *hello;
136 * Closure for the @a nc and @a nd callbacks
141 * Closure for @e start_cb.
146 * An unique number to identify the peer
153 * Handle for a request to connect two peers.
155 struct GNUNET_TRANSPORT_TESTING_ConnectRequest {
159 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *next;
164 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *prev;
167 * Peer we want to connect.
169 struct GNUNET_TRANSPORT_TESTING_PeerContext *p1;
172 * Peer we want to connect.
174 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2;
177 * Task by which we accomplish the connection.
179 struct GNUNET_SCHEDULER_Task *tct;
182 * Handle by which we ask ATS to faciliate the connection.
184 struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
187 * Handle by which we inform the peer about the HELLO of
190 struct GNUNET_TRANSPORT_OfferHelloHandle *oh;
193 * Function to call upon completion.
195 GNUNET_SCHEDULER_TaskCallback cb;
203 * Message queue for sending from @a p1 to @a p2.
205 struct GNUNET_MQ_Handle *mq;
208 * Set if peer1 says the connection is up to peer2.
213 * Set if peer2 says the connection is up to peer1.
218 * #GNUNET_YES if both @e p1_c and @e p2_c are #GNUNET_YES.
225 * Handle for a test run.
227 struct GNUNET_TRANSPORT_TESTING_Handle {
229 * Testing library system handle
231 struct GNUNET_TESTING_System *tl_system;
234 * head DLL of connect contexts
236 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc_head;
239 * head DLL of connect contexts
241 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc_tail;
246 struct GNUNET_TRANSPORT_TESTING_PeerContext *p_head;
251 struct GNUNET_TRANSPORT_TESTING_PeerContext *p_tail;
256 * Initialize the transport testing
258 * @return transport testing handle
260 struct GNUNET_TRANSPORT_TESTING_Handle *
261 GNUNET_TRANSPORT_TESTING_init(void);
265 * Clean up the transport testing
267 * @param tth transport testing handle
270 GNUNET_TRANSPORT_TESTING_done(struct GNUNET_TRANSPORT_TESTING_Handle *tth);
274 * Start a peer with the given configuration
276 * @param tth the testing handle
277 * @param cfgname configuration file
278 * @param peer_id the peer_id
279 * @param handlers functions for receiving messages
280 * @param nc connect callback
281 * @param nd disconnect callback
282 * @param cb_cls closure for @a nc and @a nd callback
283 * @param start_cb start callback
284 * @param start_cb_cls closure for @a start_cb
285 * @return the peer context
287 struct GNUNET_TRANSPORT_TESTING_PeerContext *
288 GNUNET_TRANSPORT_TESTING_start_peer(
289 struct GNUNET_TRANSPORT_TESTING_Handle *tth,
292 const struct GNUNET_MQ_MessageHandler *handlers,
293 GNUNET_TRANSPORT_NotifyConnect nc,
294 GNUNET_TRANSPORT_NotifyDisconnect nd,
296 GNUNET_SCHEDULER_TaskCallback start_cb,
301 * Shutdown the given peer
306 GNUNET_TRANSPORT_TESTING_stop_peer(
307 struct GNUNET_TRANSPORT_TESTING_PeerContext *pc);
311 * Stops and restarts the given peer, sleeping (!) for 5s in between.
314 * @param restart_cb restart callback
315 * @param restart_cb_cls callback closure
316 * @return #GNUNET_OK in success otherwise #GNUNET_SYSERR
319 GNUNET_TRANSPORT_TESTING_restart_peer(
320 struct GNUNET_TRANSPORT_TESTING_PeerContext *p,
321 GNUNET_SCHEDULER_TaskCallback restart_cb,
322 void *restart_cb_cls);
326 * Connect the given peers and call the callback when both peers
327 * report the inbound connection. Remarks: start_peer's notify_connect
328 * callback can be called before.
332 * @param cb the callback to call when both peers notified that they are
334 * @param cls callback cls
335 * @return a connect request handle
337 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *
338 GNUNET_TRANSPORT_TESTING_connect_peers(
339 struct GNUNET_TRANSPORT_TESTING_PeerContext *p1,
340 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2,
341 GNUNET_SCHEDULER_TaskCallback cb,
346 * Cancel the request to connect two peers. You MUST cancel the
347 * request if you stop the peers before the peers connected
350 * @param cc a connect request handle
353 GNUNET_TRANSPORT_TESTING_connect_peers_cancel(
354 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc);
358 * Function called on matching connect requests.
361 * @param cc request matching the query
363 typedef void (*GNUNET_TRANSPORT_TESTING_ConnectContextCallback) (
365 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc);
369 * Find any connecting context matching the given pair of peers.
371 * @param p1 first peer
372 * @param p2 second peer
373 * @param cb function to call
374 * @param cb_cls closure for @a cb
377 GNUNET_TRANSPORT_TESTING_find_connecting_context(
378 struct GNUNET_TRANSPORT_TESTING_PeerContext *p1,
379 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2,
380 GNUNET_TRANSPORT_TESTING_ConnectContextCallback cb,
384 /* ********************** high-level process functions *************** */
388 * Function called once the peers have been launched and
389 * connected by #GNUNET_TRANSPORT_TESTING_connect_check().
392 * @param num_peers size of the @a p array
393 * @param p the peers that were launched
395 typedef void (*GNUNET_TRANSPORT_TESTING_ConnectContinuation) (
397 unsigned int num_peers,
398 struct GNUNET_TRANSPORT_TESTING_PeerContext *p[]);
402 * Internal data structure.
404 struct GNUNET_TRANSPORT_TESTING_ConnectRequestList;
407 * Internal data structure.
409 struct GNUNET_TRANSPORT_TESTING_InternalPeerContext;
412 GNUNET_NETWORK_STRUCT_BEGIN
413 struct GNUNET_TRANSPORT_TESTING_TestMessage {
415 * Type is (usually) #GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE.
417 struct GNUNET_MessageHeader header;
420 * Monotonically increasing counter throughout the test.
422 uint32_t num GNUNET_PACKED;
424 GNUNET_NETWORK_STRUCT_END
428 * Function called by the transport for each received message.
431 * @param receiver receiver of the message
432 * @param sender sender of the message
433 * @param message the message
435 typedef void (*GNUNET_TRANSPORT_TESTING_ReceiveCallback) (
437 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
438 const struct GNUNET_PeerIdentity *sender,
439 const struct GNUNET_TRANSPORT_TESTING_TestMessage *message);
443 * Function called to notify transport users that another
444 * peer connected to us.
447 * @param me peer experiencing the event
448 * @param other peer that connected to @a me
450 typedef void (*GNUNET_TRANSPORT_TESTING_NotifyConnect) (
452 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
453 const struct GNUNET_PeerIdentity *other);
457 * Function called to notify transport users that another
458 * peer disconnected from us.
461 * @param me peer experiencing the event
462 * @param other peer that disconnected from @a me
464 typedef void (*GNUNET_TRANSPORT_TESTING_NotifyDisconnect) (
466 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
467 const struct GNUNET_PeerIdentity *other);
471 * Closure that must be passed to
472 * #GNUNET_TRANSPORT_TESTING_connect_check.
474 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext {
476 * How should we continue after the connect?
478 GNUNET_SCHEDULER_TaskCallback connect_continuation;
481 * Closure for @e connect_continuation.
483 void *connect_continuation_cls;
486 * Which configuration file should we pass to the
487 * #GNUNET_PROGRAM_run() of the testcase?
489 const char *config_file;
492 * Receiver argument to give for peers we start.
494 GNUNET_TRANSPORT_TESTING_ReceiveCallback rec;
497 * Notify connect argument to give for peers we start.
499 GNUNET_TRANSPORT_TESTING_NotifyConnect nc;
502 * Notify disconnect argument to give for peers we start.
504 GNUNET_TRANSPORT_TESTING_NotifyDisconnect nd;
507 * Closure for @e rec, @e nc and @e nd.
512 * Custom task to run on shutdown.
514 GNUNET_SCHEDULER_TaskCallback shutdown_task;
517 * Closure for @e shutdown_task.
519 void *shutdown_task_cls;
522 * Custom task to run after peers were started but before we try to
523 * connect them. If this function is set, we wait ONE second after
524 * running this function until we continue with connecting the
527 GNUNET_SCHEDULER_TaskCallback pre_connect_task;
530 * Closure for @e shutdown_task.
532 void *pre_connect_task_cls;
535 * When should the testcase time out?
537 struct GNUNET_TIME_Relative timeout;
540 * Should we try to create connections in both directions?
544 /* ******* fields set by #GNUNET_TRANSPORT_TESTING_connect_check **** */
547 * Number of peers involved in the test.
549 unsigned int num_peers;
552 * Configuration files we have, array with @e num_peers entries.
557 * Array with @e num_peers entries.
559 struct GNUNET_TRANSPORT_TESTING_PeerContext **p;
562 * Name of the plugin.
564 const char *test_plugin;
567 * Name of the testcase.
569 const char *test_name;
572 * Configuration object for the testcase.
574 const struct GNUNET_CONFIGURATION_Handle *cfg;
577 * Main testing handle.
579 struct GNUNET_TRANSPORT_TESTING_Handle *tth;
582 * Result from the main function, set to #GNUNET_OK on success.
583 * Clients should set to #GNUNET_SYSERR to indicate test failure.
588 * Generator for the `num` field in test messages. Incremented each
589 * time #GNUNET_TRANSPORT_TESTING_simple_send or
590 * #GNUNET_TRANSPORT_TESTING_large_send are used to transmit a
593 uint32_t send_num_gen;
595 /* ******* internal state, clients should not mess with this **** */
598 * Task run on timeout.
600 struct GNUNET_SCHEDULER_Task *timeout_task;
603 * Task run to connect peers.
605 struct GNUNET_SCHEDULER_Task *connect_task;
608 * Number of peers that have been started.
610 unsigned int started;
613 * DLL of active connect requests.
615 struct GNUNET_TRANSPORT_TESTING_ConnectRequestList *crl_head;
618 * DLL of active connect requests.
620 struct GNUNET_TRANSPORT_TESTING_ConnectRequestList *crl_tail;
623 * Array with @e num_peers entries.
625 struct GNUNET_TRANSPORT_TESTING_InternalPeerContext *ip;
630 * Find peer by peer ID.
632 * @param ccc context to search
633 * @param peer peer to look for
634 * @return NULL if @a peer was not found
636 struct GNUNET_TRANSPORT_TESTING_PeerContext *
637 GNUNET_TRANSPORT_TESTING_find_peer(
638 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc,
639 const struct GNUNET_PeerIdentity *peer);
643 * Common implementation of the #GNUNET_TRANSPORT_TESTING_CheckCallback.
644 * Starts and connects the two peers, then invokes the
645 * `connect_continuation` from @a cls. Sets up a timeout to
646 * abort the test, and a shutdown handler to clean up properly
649 * @param cls closure of type `struct
650 * GNUNET_TRANSPORT_TESTING_ConnectCheckContext`
651 * @param tth_ initialized testing handle
652 * @param test_plugin_ name of the plugin
653 * @param test_name_ name of the test
654 * @param num_peers number of entries in the @a cfg_file array
655 * @param cfg_files array of names of configuration files for the peers
656 * @return #GNUNET_SYSERR on error
659 GNUNET_TRANSPORT_TESTING_connect_check(
661 struct GNUNET_TRANSPORT_TESTING_Handle *tth_,
662 const char *test_plugin_,
663 const char *test_name_,
664 unsigned int num_peers,
669 * Main function of a testcase. Called with the initial setup data
670 * for the test as derived from the source name and the binary name.
673 * @param tth_ initialized testing handle
674 * @param test_plugin_ name of the plugin
675 * @param test_name_ name of the test
676 * @param num_peers number of entries in the @a cfg_file array
677 * @param cfg_files array of names of configuration files for the peers
678 * @return #GNUNET_SYSERR on error
680 typedef int (*GNUNET_TRANSPORT_TESTING_CheckCallback) (
682 struct GNUNET_TRANSPORT_TESTING_Handle *tth_,
683 const char *test_plugin_,
684 const char *test_name_,
685 unsigned int num_peers,
690 * Setup testcase. Calls @a check with the data the test needs.
692 * @param argv0 binary name (argv[0])
693 * @param filename source file name (__FILE__)
694 * @param num_peers number of peers to start
695 * @param check main function to run
696 * @param check_cls closure for @a check
697 * @return #GNUNET_OK on success
700 GNUNET_TRANSPORT_TESTING_main_(const char *argv0,
701 const char *filename,
702 unsigned int num_peers,
703 GNUNET_TRANSPORT_TESTING_CheckCallback check,
708 * Setup testcase. Calls @a check with the data the test needs.
710 * @param num_peers number of peers to start
711 * @param check main function to run
712 * @param check_cls closure for @a check
713 * @return #GNUNET_OK on success
715 #define GNUNET_TRANSPORT_TESTING_main(num_peers, check, check_cls) \
716 GNUNET_TRANSPORT_TESTING_main_(argv[0], \
722 /* ***************** Convenience functions for sending ********* */
725 * Send a test message of type @a mtype and size @a msize from
726 * peer @a sender to peer @a receiver. The peers should be
727 * connected when this function is called.
729 * @param sender the sending peer
730 * @param receiver the receiving peer
731 * @param mtype message type to use
732 * @param msize size of the message, at least `sizeof (struct
733 * GNUNET_TRANSPORT_TESTING_TestMessage)`
734 * @param num unique message number
735 * @param cont continuation to call after transmission
736 * @param cont_cls closure for @a cont
737 * @return #GNUNET_OK if message was queued,
738 * #GNUNET_NO if peers are not connected
739 * #GNUNET_SYSERR if @a msize is illegal
742 GNUNET_TRANSPORT_TESTING_send(
743 struct GNUNET_TRANSPORT_TESTING_PeerContext *sender,
744 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
748 GNUNET_SCHEDULER_TaskCallback cont,
753 * Message type used by #GNUNET_TRANSPORT_TESTING_simple_send().
755 #define GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE 12345
758 * Alternative message type for tests.
760 #define GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE2 12346
764 * Type of the closure argument to pass to
765 * #GNUNET_TRANSPORT_TESTING_simple_send() and
766 * #GNUNET_TRANSPORT_TESTING_large_send().
768 struct GNUNET_TRANSPORT_TESTING_SendClosure {
770 * Context for the transmission.
772 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
775 * Function that returns the desired message size. Overrides
776 * the message size, can be NULL in which case the message
777 * size is the default.
779 size_t (*get_size_cb) (unsigned int n);
782 * Number of messages to be transmitted in a loop.
783 * Use zero for "forever" (until external shutdown).
785 unsigned int num_messages;
788 * Function to call after all transmissions, can be NULL.
790 GNUNET_SCHEDULER_TaskCallback cont;
793 * Closure for @e cont.
800 * Task that sends a minimalistic test message from the
801 * first peer to the second peer.
803 * @param cls the `struct GNUNET_TRANSPORT_TESTING_SendClosure`
804 * which should contain at least two peers, the first two
805 * of which should be currently connected
808 GNUNET_TRANSPORT_TESTING_simple_send(void *cls);
811 * Size of a message sent with
812 * #GNUNET_TRANSPORT_TESTING_large_send(). Big enough
813 * to usually force defragmentation.
815 #define GNUNET_TRANSPORT_TESTING_LARGE_MESSAGE_SIZE 2600
818 * Task that sends a large test message from the
819 * first peer to the second peer.
821 * @param cls the `struct GNUNET_TRANSPORT_TESTING_SendClosure`
822 * which should contain at least two peers, the first two
823 * of which should be currently connected
826 GNUNET_TRANSPORT_TESTING_large_send(void *cls);
829 /* ********************** log-only convenience functions ************* */
833 * Log a connect event.
836 * @param me peer that had the event
837 * @param other peer that connected.
840 GNUNET_TRANSPORT_TESTING_log_connect(
842 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
843 const struct GNUNET_PeerIdentity *other);
847 * Log a disconnect event.
850 * @param me peer that had the event
851 * @param other peer that disconnected.
854 GNUNET_TRANSPORT_TESTING_log_disconnect(
856 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
857 const struct GNUNET_PeerIdentity *other);
860 /* ********************** low-level filename functions *************** */
864 * Extracts the test filename from an absolute file name and removes
867 * @param file absolute file name
868 * @return resulting test name
871 GNUNET_TRANSPORT_TESTING_get_test_name(const char *file);
875 * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
876 * if existing ".exe"-prefix and adds the peer-number
878 * @param file filename of the test, e.g. argv[0]
879 * @param count peer number
880 * @return configuration name to use
883 GNUNET_TRANSPORT_TESTING_get_config_name(const char *file, int count);
887 * Extracts the plugin anme from an absolute file name and the test name
888 * @param file absolute file name
889 * @param test test name
890 * @return the plugin name
893 GNUNET_TRANSPORT_TESTING_get_test_plugin_name(const char *executable,
894 const char *testname);
898 * Extracts the filename from an absolute file name and removes the
901 * @param file absolute file name
902 * @return the source name
905 GNUNET_TRANSPORT_TESTING_get_test_source_name(const char *file);
908 /* end of transport_testing.h */