tighten formatting rules
[oweals/gnunet.git] / src / testbed / testbed_api_test.c
1 /*
2       This file is part of GNUnet
3       Copyright (C) 2008--2013 GNUnet e.V.
4
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.
9
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.
14
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/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20
21 /**
22  * @file testbed/testbed_api_test.c
23  * @brief high-level test function
24  * @author Christian Grothoff
25  * @author Sree Harsha Totakura
26  */
27 #include "platform.h"
28 #include "gnunet_testbed_service.h"
29
30
31 /**
32  * Context information for test run
33  */
34 struct TestRunContext
35 {
36   /**
37    * Test master callback
38    */
39   GNUNET_TESTBED_TestMaster test_master;
40
41   /**
42    * Closure for test master
43    */
44   void *test_master_cls;
45
46   /**
47    * The controller event callback
48    */
49   GNUNET_TESTBED_ControllerCallback cc;
50
51   /**
52    * Closure for the above callback
53    */
54   void *cc_cls;
55
56   /**
57    * event mask for the controller callback
58    */
59   uint64_t event_mask;
60
61   /**
62    * Number of peers to start
63    */
64   unsigned int num_peers;
65 };
66
67
68 /**
69  * Main run function.
70  *
71  * @param cls NULL
72  * @param args arguments passed to GNUNET_PROGRAM_run
73  * @param cfgfile the path to configuration file
74  * @param config the configuration file handle
75  */
76 static void
77 run (void *cls, char *const *args, const char *cfgfile,
78      const struct GNUNET_CONFIGURATION_Handle *config)
79 {
80   struct TestRunContext *rc = cls;
81
82   GNUNET_TESTBED_run (NULL, config, rc->num_peers, rc->event_mask, rc->cc,
83                       rc->cc_cls, rc->test_master, rc->test_master_cls);
84 }
85
86
87 /**
88  * Convenience method for running a "simple" test on the local system
89  * with a single call from 'main'.  Underlay and overlay topology are
90  * configured using the "UNDERLAY" and "OVERLAY" options in the
91  * "[testbed]" section of the configuration (with possible options
92  * given in "UNDERLAY_XXX" and/or "OVERLAY_XXX").
93  *
94  * The test is to be terminated using a call to
95  * "GNUNET_SCHEDULER_shutdown".  If starting the test fails,
96  * the program is stopped without 'master' ever being run.
97  *
98  * NOTE: this function should be called from 'main', NOT from
99  * within a GNUNET_SCHEDULER-loop.  This function will initialze
100  * the scheduler loop, the testbed and then pass control to
101  * 'master'.
102  *
103  * @param testname name of the testcase (to configure logging, etc.)
104  * @param cfg_filename configuration filename to use
105  *              (for testbed, controller and peers)
106  * @param num_peers number of peers to start
107  * @param event_mask bit mask with set of events to call 'cc' for;
108  *                   or-ed values of "1LL" shifted by the
109  *                   respective 'enum GNUNET_TESTBED_EventType'
110  *                   (i.e.  "(1LL << GNUNET_TESTBED_ET_CONNECT) || ...")
111  * @param cc controller callback to invoke on events; This callback is called
112  *        for all peer start events even if GNUNET_TESTBED_ET_PEER_START isn't
113  *        set in the event_mask as this is the only way get access to the
114  *        handle of each peer
115  * @param cc_cls closure for cc
116  * @param test_master task to run once the test is ready
117  * @param test_master_cls closure for @a test_master
118  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
119  */
120 int
121 GNUNET_TESTBED_test_run (const char *testname,
122                          const char *cfg_filename,
123                          unsigned int num_peers,
124                          uint64_t event_mask,
125                          GNUNET_TESTBED_ControllerCallback cc,
126                          void *cc_cls,
127                          GNUNET_TESTBED_TestMaster test_master,
128                          void *test_master_cls)
129 {
130   char *argv2[] = {
131     NULL,
132     "-c",
133     NULL,
134     NULL
135   };
136   struct GNUNET_GETOPT_CommandLineOption options[] = {
137     GNUNET_GETOPT_OPTION_END
138   };
139   struct TestRunContext *rc;
140   int ret;
141
142   argv2[0] = GNUNET_strdup (testname);
143   argv2[2] = GNUNET_strdup (cfg_filename);
144   GNUNET_assert (NULL != test_master);
145   GNUNET_assert (num_peers > 0);
146   rc = GNUNET_malloc (sizeof(struct TestRunContext)
147                       + (num_peers * sizeof(struct GNUNET_TESTBED_Peer *)));
148   rc->test_master = test_master;
149   rc->test_master_cls = test_master_cls;
150   rc->num_peers = num_peers;
151   rc->event_mask = event_mask;
152   rc->cc = cc;
153   rc->cc_cls = cc_cls;
154   ret = GNUNET_PROGRAM_run ((sizeof(argv2) / sizeof(char *)) - 1, argv2,
155                             testname, "nohelp", options, &run, rc);
156   GNUNET_free (rc);
157   GNUNET_free (argv2[0]);
158   GNUNET_free (argv2[2]);
159   return ret;
160 }
161
162
163 /* end of testbed_api_test.c */