2 This file is part of GNUnet
3 (C) 2008--2013 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file testbed/test_testbed_api_testbed_run.c
23 * @brief Test cases for testing high-level testbed management
24 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
28 #include "gnunet_common.h"
29 #include "gnunet_testbed_service.h"
32 * Number of peers we want to start
37 * The array of peers; we fill this as the peers are given to us by the testbed
39 static struct GNUNET_TESTBED_Peer *peers[NUM_PEERS];
44 static struct GNUNET_TESTBED_Operation *op;
47 * Abort task identifier
49 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
54 static unsigned int peer_id;
62 * Should we wait forever after testbed is initialized?
64 static int wait_forever;
71 * @param tc the task context
74 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
76 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
77 GNUNET_SCHEDULER_cancel (abort_task);
78 GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
83 * abort task to run on test timed out
86 * @param tc the task context
89 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
91 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
92 abort_task = GNUNET_SCHEDULER_NO_TASK;
93 (void) GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
98 * Signature of a main function for a testcase.
101 * @param num_peers number of peers in 'peers'
102 * @param peers_ handle to peers run in the testbed
103 * @param links_succeeded the number of overlay link connection attempts that
105 * @param links_failed the number of overlay link connection attempts that
109 test_master (void *cls, unsigned int num_peers,
110 struct GNUNET_TESTBED_Peer **peers_,
111 unsigned int links_succeeded,
112 unsigned int links_failed)
115 if (GNUNET_YES == wait_forever)
117 GNUNET_SCHEDULER_cancel (abort_task);
118 abort_task = GNUNET_SCHEDULER_NO_TASK;
119 (void) GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
123 GNUNET_assert (NULL != peers[0]);
124 op = GNUNET_TESTBED_peer_stop (NULL, peers[0], NULL, NULL);
125 GNUNET_assert (NULL != op);
130 * Controller event callback
133 * @param event the controller event
136 controller_event_cb (void *cls,
137 const struct GNUNET_TESTBED_EventInformation *event)
142 case GNUNET_TESTBED_ET_PEER_START:
143 GNUNET_assert (NULL == peers[peer_id]);
144 GNUNET_assert (NULL != event->details.peer_start.peer);
145 peers[peer_id++] = event->details.peer_start.peer;
147 case GNUNET_TESTBED_ET_PEER_STOP:
148 GNUNET_assert (NULL != op);
149 GNUNET_TESTBED_operation_done (op);
150 GNUNET_assert (peers[0] == event->details.peer_stop.peer);
151 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
163 * @param args arguments passed to GNUNET_PROGRAM_run
164 * @param cfgfile the path to configuration file
165 * @param cfg the configuration file handle
168 run (void *cls, char *const *args, const char *cfgfile,
169 const struct GNUNET_CONFIGURATION_Handle *config)
174 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
175 event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
176 GNUNET_TESTBED_run (NULL, config, NUM_PEERS, event_mask, &controller_event_cb,
177 NULL, &test_master, NULL);
179 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
180 (GNUNET_TIME_UNIT_SECONDS, 300), &do_abort,
189 main (int argc, char **argv)
192 "test_testbed_api_testbed_run",
196 struct GNUNET_GETOPT_CommandLineOption options[] = {
197 GNUNET_GETOPT_OPTION_END
200 char *config_filename;
203 if (NULL == (testname = strrchr (argv[0], (int) '_')))
209 testname = GNUNET_strdup (testname);
214 /* check and remove .exe extension */
215 period = strrchr (testname, (int) '.');
219 GNUNET_break (0); /* Windows with no .exe? */
222 if (0 == strcmp ("waitforever", testname))
223 wait_forever = GNUNET_YES;
224 if ( (GNUNET_YES != wait_forever) && (0 != strcmp ("run", testname)) )
226 GNUNET_asprintf (&config_filename, "test_testbed_api_testbed_run_%s.conf",
230 config_filename = GNUNET_strdup ("test_testbed_api.conf");
231 GNUNET_free (testname);
232 argv2[2] = config_filename;
233 result = GNUNET_SYSERR;
235 GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
236 "test_testbed_api_testbed_run", "nohelp", options,
238 GNUNET_free (config_filename);
239 if ((GNUNET_OK != ret) || (GNUNET_OK != result))
244 /* end of test_testbed_api_testbed_run.c */