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 @a nc and @a nd callbacks
148 * Closure for @e start_cb.
153 * An unique number to identify the peer
160 * Handle for a request to connect two peers.
162 struct GNUNET_TRANSPORT_TESTING_ConnectRequest
167 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *next;
172 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *prev;
175 * Peer we want to connect.
177 struct GNUNET_TRANSPORT_TESTING_PeerContext *p1;
180 * Peer we want to connect.
182 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2;
185 * Task by which we accomplish the connection.
187 struct GNUNET_SCHEDULER_Task *tct;
190 * Handle by which we ask ATS to faciliate the connection.
192 struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
195 * Handle by which we inform the peer about the HELLO of
198 struct GNUNET_TRANSPORT_OfferHelloHandle *oh;
201 * Function to call upon completion.
203 GNUNET_SCHEDULER_TaskCallback cb;
211 * Set if peer1 says the connection is up to peer2.
216 * Set if peer2 says the connection is up to peer1.
221 * #GNUNET_YES if both @e p1_c and @e p2_c are #GNUNET_YES.
228 * Information we keep for active transmission jobs.
230 struct TRANSPORT_TESTING_SendJob
236 struct TRANSPORT_TESTING_SendJob *next;
241 struct TRANSPORT_TESTING_SendJob *prev;
244 * Sender of the message.
246 struct GNUNET_TRANSPORT_TESTING_PeerContext *sender;
249 * Receiver of the message.
251 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver;
256 struct GNUNET_TRANSPORT_TransmitHandle *th;
259 * Function to call upon completion.
261 GNUNET_SCHEDULER_TaskCallback cont;
264 * Closure for @e cont.
269 * Number of the message.
274 * Type of message to send.
279 * Length of the message.
287 * Handle for a test run.
289 struct GNUNET_TRANSPORT_TESTING_Handle
292 * Testing library system handle
294 struct GNUNET_TESTING_System *tl_system;
297 * head DLL of connect contexts
299 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc_head;
302 * head DLL of connect contexts
304 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc_tail;
309 struct TRANSPORT_TESTING_SendJob *sj_head;
314 struct TRANSPORT_TESTING_SendJob *sj_tail;
319 struct GNUNET_TRANSPORT_TESTING_PeerContext *p_head;
324 struct GNUNET_TRANSPORT_TESTING_PeerContext *p_tail;
329 * Initialize the transport testing
331 * @return transport testing handle
333 struct GNUNET_TRANSPORT_TESTING_Handle *
334 GNUNET_TRANSPORT_TESTING_init (void);
338 * Clean up the transport testing
340 * @param tth transport testing handle
343 GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_Handle *tth);
347 * Start a peer with the given configuration
349 * @param tth the testing handle
350 * @param cfgname configuration file
351 * @param peer_id the peer_id
352 * @param rec receive callback
353 * @param nc connect callback
354 * @param nd disconnect callback
355 * @param cb_cls closure for @a nc and @a nd callback
356 * @param start_cb start callback
357 * @param start_cb_cls closure for @a start_cb
358 * @return the peer context
360 struct GNUNET_TRANSPORT_TESTING_PeerContext *
361 GNUNET_TRANSPORT_TESTING_start_peer (struct GNUNET_TRANSPORT_TESTING_Handle *tth,
364 GNUNET_TRANSPORT_ReceiveCallback rec,
365 GNUNET_TRANSPORT_NotifyConnect nc,
366 GNUNET_TRANSPORT_NotifyDisconnect nd,
368 GNUNET_TRANSPORT_TESTING_StartCallback start_cb,
373 * Shutdown the given peer
378 GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_PeerContext *pc);
382 * Stops and restarts the given peer, sleeping (!) for 5s in between.
385 * @param restart_cb restart callback
386 * @param restart_cb_cls callback closure
387 * @return #GNUNET_OK in success otherwise #GNUNET_SYSERR
390 GNUNET_TRANSPORT_TESTING_restart_peer (struct GNUNET_TRANSPORT_TESTING_PeerContext *p,
391 GNUNET_TRANSPORT_TESTING_StartCallback restart_cb,
392 void *restart_cb_cls);
397 * Connect the given peers and call the callback when both peers
398 * report the inbound connection. Remarks: start_peer's notify_connect
399 * callback can be called before.
403 * @param cb the callback to call when both peers notified that they are connected
404 * @param cls callback cls
405 * @return a connect request handle
407 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *
408 GNUNET_TRANSPORT_TESTING_connect_peers (struct GNUNET_TRANSPORT_TESTING_PeerContext *p1,
409 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2,
410 GNUNET_SCHEDULER_TaskCallback cb,
415 * Cancel the request to connect two peers. You MUST cancel the
416 * request if you stop the peers before the peers connected
419 * @param cc a connect request handle
422 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc);
426 * Function called on matching connect requests.
429 * @param cc request matching the query
432 (*GNUNET_TRANSPORT_TESTING_ConnectContextCallback)(void *cls,
433 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cc);
437 * Find any connecting context matching the given pair of peers.
439 * @param p1 first peer
440 * @param p2 second peer
441 * @param cb function to call
442 * @param cb_cls closure for @a cb
445 GNUNET_TRANSPORT_TESTING_find_connecting_context (struct GNUNET_TRANSPORT_TESTING_PeerContext *p1,
446 struct GNUNET_TRANSPORT_TESTING_PeerContext *p2,
447 GNUNET_TRANSPORT_TESTING_ConnectContextCallback cb,
451 /* ********************** high-level process functions *************** */
455 * Function called once the peers have been launched and
456 * connected by #GNUNET_TRANSPORT_TESTING_connect_check().
459 * @param num_peers size of the @a p array
460 * @param p the peers that were launched
463 (*GNUNET_TRANSPORT_TESTING_ConnectContinuation)(void *cls,
464 unsigned int num_peers,
465 struct GNUNET_TRANSPORT_TESTING_PeerContext *p[]);
469 * Internal data structure.
471 struct GNUNET_TRANSPORT_TESTING_ConnectRequestList;
474 * Internal data structure.
476 struct GNUNET_TRANSPORT_TESTING_InternalPeerContext;
480 * Function called by the transport for each received message.
483 * @param receiver receiver of the message
484 * @param sender sender of the message
485 * @param message the message
488 (*GNUNET_TRANSPORT_TESTING_ReceiveCallback) (void *cls,
489 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
490 const struct GNUNET_PeerIdentity *sender,
491 const struct GNUNET_MessageHeader *message);
495 * Function called to notify transport users that another
496 * peer connected to us.
499 * @param me peer experiencing the event
500 * @param other peer that connected to @a me
503 (*GNUNET_TRANSPORT_TESTING_NotifyConnect) (void *cls,
504 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
505 const struct GNUNET_PeerIdentity *other);
509 * Function called to notify transport users that another
510 * peer disconnected from us.
513 * @param me peer experiencing the event
514 * @param other peer that disconnected from @a me
517 (*GNUNET_TRANSPORT_TESTING_NotifyDisconnect) (void *cls,
518 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
519 const struct GNUNET_PeerIdentity *other);
523 * Closure that must be passed to
524 * #GNUNET_TRANSPORT_TESTING_connect_check.
526 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext
530 * How should we continue after the connect?
532 GNUNET_SCHEDULER_TaskCallback connect_continuation;
535 * Closure for @e connect_continuation.
537 void *connect_continuation_cls;
540 * Which configuration file should we pass to the
541 * #GNUNET_PROGRAM_run() of the testcase?
543 const char *config_file;
546 * Receiver argument to give for peers we start.
548 GNUNET_TRANSPORT_TESTING_ReceiveCallback rec;
551 * Notify connect argument to give for peers we start.
553 GNUNET_TRANSPORT_TESTING_NotifyConnect nc;
556 * Notify disconnect argument to give for peers we start.
558 GNUNET_TRANSPORT_TESTING_NotifyDisconnect nd;
561 * Closure for @e rec, @e nc and @e nd.
566 * Custom task to run on shutdown.
568 GNUNET_SCHEDULER_TaskCallback shutdown_task;
571 * Closure for @e shutdown_task.
573 void *shutdown_task_cls;
576 * Custom task to run after peers were started but before we try to
577 * connect them. If this function is set, we wait ONE second after
578 * running this function until we continue with connecting the
581 GNUNET_SCHEDULER_TaskCallback pre_connect_task;
584 * Closure for @e shutdown_task.
586 void *pre_connect_task_cls;
589 * When should the testcase time out?
591 struct GNUNET_TIME_Relative timeout;
594 * Should we try to create connections in both directions?
598 /* ******* fields set by #GNUNET_TRANSPORT_TESTING_connect_check **** */
601 * Number of peers involved in the test.
603 unsigned int num_peers;
606 * Configuration files we have, array with @e num_peers entries.
611 * Array with @e num_peers entries.
613 struct GNUNET_TRANSPORT_TESTING_PeerContext **p;
616 * Name of the plugin.
618 const char *test_plugin;
621 * Name of the testcase.
623 const char *test_name;
626 * Configuration object for the testcase.
628 const struct GNUNET_CONFIGURATION_Handle *cfg;
631 * Main testing handle.
633 struct GNUNET_TRANSPORT_TESTING_Handle *tth;
636 * Result from the main function, set to #GNUNET_OK on success.
637 * Clients should set to #GNUNET_SYSERR to indicate test failure.
642 * Generator for the `num` field in test messages. Incremented each
643 * time #GNUNET_TRANSPORT_TESTING_simple_send or
644 * #GNUNET_TRANSPORT_TESTING_large_send are used to transmit a
647 uint32_t send_num_gen;
649 /* ******* internal state, clients should not mess with this **** */
652 * Task run on timeout.
654 struct GNUNET_SCHEDULER_Task *timeout_task;
657 * Task run to connect peers.
659 struct GNUNET_SCHEDULER_Task *connect_task;
662 * Number of peers that have been started.
664 unsigned int started;
667 * DLL of active connect requests.
669 struct GNUNET_TRANSPORT_TESTING_ConnectRequestList *crl_head;
672 * DLL of active connect requests.
674 struct GNUNET_TRANSPORT_TESTING_ConnectRequestList *crl_tail;
677 * Array with @e num_peers entries.
679 struct GNUNET_TRANSPORT_TESTING_InternalPeerContext *ip;
685 * Find peer by peer ID.
687 * @param ccc context to search
688 * @param peer peer to look for
689 * @return NULL if @a peer was not found
691 struct GNUNET_TRANSPORT_TESTING_PeerContext *
692 GNUNET_TRANSPORT_TESTING_find_peer (struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc,
693 const struct GNUNET_PeerIdentity *peer);
697 * Common implementation of the #GNUNET_TRANSPORT_TESTING_CheckCallback.
698 * Starts and connects the two peers, then invokes the
699 * `connect_continuation` from @a cls. Sets up a timeout to
700 * abort the test, and a shutdown handler to clean up properly
703 * @param cls closure of type `struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext`
704 * @param tth_ initialized testing handle
705 * @param test_plugin_ name of the plugin
706 * @param test_name_ name of the test
707 * @param num_peers number of entries in the @a cfg_file array
708 * @param cfg_files array of names of configuration files for the peers
709 * @return #GNUNET_SYSERR on error
712 GNUNET_TRANSPORT_TESTING_connect_check (void *cls,
713 struct GNUNET_TRANSPORT_TESTING_Handle *tth_,
714 const char *test_plugin_,
715 const char *test_name_,
716 unsigned int num_peers,
721 * Main function of a testcase. Called with the initial setup data
722 * for the test as derived from the source name and the binary name.
725 * @param tth_ initialized testing handle
726 * @param test_plugin_ name of the plugin
727 * @param test_name_ name of the test
728 * @param num_peers number of entries in the @a cfg_file array
729 * @param cfg_files array of names of configuration files for the peers
730 * @return #GNUNET_SYSERR on error
733 (*GNUNET_TRANSPORT_TESTING_CheckCallback)(void *cls,
734 struct GNUNET_TRANSPORT_TESTING_Handle *tth_,
735 const char *test_plugin_,
736 const char *test_name_,
737 unsigned int num_peers,
742 * Setup testcase. Calls @a check with the data the test needs.
744 * @param argv0 binary name (argv[0])
745 * @param filename source file name (__FILE__)
746 * @param num_peers number of peers to start
747 * @param check main function to run
748 * @param check_cls closure for @a check
749 * @return #GNUNET_OK on success
752 GNUNET_TRANSPORT_TESTING_main_ (const char *argv0,
753 const char *filename,
754 unsigned int num_peers,
755 GNUNET_TRANSPORT_TESTING_CheckCallback check,
760 * Setup testcase. Calls @a check with the data the test needs.
762 * @param num_peers number of peers to start
763 * @param check main function to run
764 * @param check_cls closure for @a check
765 * @return #GNUNET_OK on success
767 #define GNUNET_TRANSPORT_TESTING_main(num_peers,check,check_cls) \
768 GNUNET_TRANSPORT_TESTING_main_ (argv[0], __FILE__, num_peers, check, check_cls)
770 /* ***************** Convenience functions for sending ********* */
773 // - need to have continuation after send is done!
774 // - need easy way to specify continuation in case
775 // of the scheduler tasks
778 * Send a test message of type @a mtype and size @a msize from
779 * peer @a sender to peer @a receiver. The peers should be
780 * connected when this function is called.
782 * @param sender the sending peer
783 * @param receiver the receiving peer
784 * @param mtype message type to use
785 * @param msize size of the message, at least `sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage)`
786 * @param num unique message number
787 * @param cont continuation to call after transmission
788 * @param cont_cls closure for @a cont
789 * @return #GNUNET_OK if message was queued,
790 * #GNUNET_NO if peers are not connected
791 * #GNUNET_SYSERR if @a msize is illegal
794 GNUNET_TRANSPORT_TESTING_send (struct GNUNET_TRANSPORT_TESTING_PeerContext *sender,
795 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
799 GNUNET_SCHEDULER_TaskCallback cont,
804 * Message type used by #GNUNET_TRANSPORT_TESTING_simple_send().
806 #define GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE 12345
808 GNUNET_NETWORK_STRUCT_BEGIN
809 struct GNUNET_TRANSPORT_TESTING_TestMessage
812 * Type is #GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE.
814 struct GNUNET_MessageHeader header;
817 * Monotonically increasing counter throughout the test.
819 uint32_t num GNUNET_PACKED;
821 GNUNET_NETWORK_STRUCT_END
825 * Type of the closure argument to pass to
826 * #GNUNET_TRANSPORT_TESTING_simple_send() and
827 * #GNUNET_TRANSPORT_TESTING_large_send().
829 struct GNUNET_TRANSPORT_TESTING_SendClosure
832 * Context for the transmission.
834 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
837 * Function that returns the desired message size. Overrides
838 * the message size, can be NULL in which case the message
839 * size is the default.
841 size_t (*get_size_cb)(unsigned int n);
844 * Number of messages to be transmitted in a loop.
845 * Use zero for "forever" (until external shutdown).
847 unsigned int num_messages;
850 * Function to call after all transmissions, can be NULL.
852 GNUNET_SCHEDULER_TaskCallback cont;
855 * Closure for @e cont.
863 * Task that sends a minimalistic test message from the
864 * first peer to the second peer.
866 * @param cls the `struct GNUNET_TRANSPORT_TESTING_SendClosure`
867 * which should contain at least two peers, the first two
868 * of which should be currently connected
871 GNUNET_TRANSPORT_TESTING_simple_send (void *cls);
874 * Size of a message sent with
875 * #GNUNET_TRANSPORT_TESTING_large_send(). Big enough
876 * to usually force defragmentation.
878 #define GNUNET_TRANSPORT_TESTING_LARGE_MESSAGE_SIZE 2600
881 * Task that sends a large test message from the
882 * first peer to the second peer.
884 * @param cls the `struct GNUNET_TRANSPORT_TESTING_SendClosure`
885 * which should contain at least two peers, the first two
886 * of which should be currently connected
889 GNUNET_TRANSPORT_TESTING_large_send (void *cls);
892 /* ********************** log-only convenience functions ************* */
896 * Log a connect event.
899 * @param me peer that had the event
900 * @param other peer that connected.
903 GNUNET_TRANSPORT_TESTING_log_connect (void *cls,
904 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
905 const struct GNUNET_PeerIdentity *other);
909 * Log a disconnect event.
912 * @param me peer that had the event
913 * @param other peer that disconnected.
916 GNUNET_TRANSPORT_TESTING_log_disconnect (void *cls,
917 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
918 const struct GNUNET_PeerIdentity *other);
922 /* ********************** low-level filename functions *************** */
926 * Extracts the test filename from an absolute file name and removes
929 * @param file absolute file name
930 * @return resulting test name
933 GNUNET_TRANSPORT_TESTING_get_test_name (const char *file);
937 * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
938 * if existing ".exe"-prefix and adds the peer-number
940 * @param file filename of the test, e.g. argv[0]
941 * @param count peer number
942 * @return configuration name to use
945 GNUNET_TRANSPORT_TESTING_get_config_name (const char *file,
950 * Extracts the plugin anme from an absolute file name and the test name
951 * @param file absolute file name
952 * @param test test name
953 * @return the plugin name
956 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *executable,
957 const char *testname);
961 * Extracts the filename from an absolute file name and removes the
964 * @param file absolute file name
965 * @return the source name
968 GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file);
971 /* end of transport_testing.h */