paragraph for gnunet devs that don't know how to use the web
[oweals/gnunet.git] / src / testbed / test_testbed_api_testbed_run.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
19 /**
20  * @file testbed/test_testbed_api_testbed_run.c
21  * @brief Test cases for testing high-level testbed management
22  * @author Sree Harsha Totakura <sreeharsha@totakura.in>
23  */
24
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_testbed_service.h"
28
29 /**
30  * Number of peers we want to start
31  */
32 #define NUM_PEERS 5
33
34 /**
35  * The array of peers; we fill this as the peers are given to us by the testbed
36  */
37 static struct GNUNET_TESTBED_Peer *peers[NUM_PEERS];
38
39 /**
40  * Operation handle
41  */
42 static struct GNUNET_TESTBED_Operation *op;
43
44 /**
45  * Abort task identifier
46  */
47 static struct GNUNET_SCHEDULER_Task * abort_task;
48
49 /**
50  * Current peer id
51  */
52 static unsigned int peer_id;
53
54 /**
55  * Testing result
56  */
57 static int result;
58
59 /**
60  * Should we wait forever after testbed is initialized?
61  */
62 static int wait_forever;
63
64
65 /**
66  * Shutdown nicely
67  *
68  * @param cls NULL
69  */
70 static void
71 do_shutdown (void *cls)
72 {
73   if (NULL != abort_task)
74     GNUNET_SCHEDULER_cancel (abort_task);
75   GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
76 }
77
78
79 /**
80  * abort task to run on test timed out
81  *
82  * @param cls NULL
83  */
84 static void
85 do_abort (void *cls)
86 {
87   abort_task = NULL;
88   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
89               "Test timed out -- Aborting\n");
90   GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
91 }
92
93
94 /**
95  * Signature of a main function for a testcase.
96  *
97  * @param cls closure
98  * @param h the run handle
99  * @param num_peers number of peers in 'peers'
100  * @param peers_ handle to peers run in the testbed
101  * @param links_succeeded the number of overlay link connection attempts that
102  *          succeeded
103  * @param links_failed the number of overlay link connection attempts that
104  *          failed
105  */
106 static void
107 test_master (void *cls,
108              struct GNUNET_TESTBED_RunHandle *h,
109              unsigned int num_peers,
110              struct GNUNET_TESTBED_Peer **peers_,
111              unsigned int links_succeeded,
112              unsigned int links_failed)
113 {
114   result = GNUNET_OK;
115   if (GNUNET_YES == wait_forever)
116   {
117     if (NULL == abort_task)
118       return;                   /* abort already scheduled */
119     GNUNET_SCHEDULER_cancel (abort_task);
120     abort_task = NULL;
121     GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
122     return;
123   }
124   GNUNET_assert (NULL != peers[0]);
125   op = GNUNET_TESTBED_peer_stop (NULL, peers[0], NULL, NULL);
126   GNUNET_assert (NULL != op);
127 }
128
129
130 /**
131  * Controller event callback
132  *
133  * @param cls NULL
134  * @param event the controller event
135  */
136 static void
137 controller_event_cb (void *cls,
138                      const struct GNUNET_TESTBED_EventInformation *event)
139 {
140
141   switch (event->type)
142   {
143   case GNUNET_TESTBED_ET_PEER_START:
144     GNUNET_assert (NULL == peers[peer_id]);
145     GNUNET_assert (NULL != event->details.peer_start.peer);
146     peers[peer_id++] = event->details.peer_start.peer;
147     break;
148   case GNUNET_TESTBED_ET_PEER_STOP:
149     GNUNET_assert (NULL != op);
150     GNUNET_TESTBED_operation_done (op);
151     GNUNET_assert (peers[0] == event->details.peer_stop.peer);
152     GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
153     break;
154   default:
155     GNUNET_assert (0);
156   }
157 }
158
159
160 /**
161  * Main run function.
162  *
163  * @param cls NULL
164  * @param args arguments passed to GNUNET_PROGRAM_run
165  * @param cfgfile the path to configuration file
166  * @param cfg the configuration file handle
167  */
168 static void
169 run (void *cls,
170      char *const *args,
171      const char *cfgfile,
172      const struct GNUNET_CONFIGURATION_Handle *config)
173 {
174   uint64_t event_mask;
175
176   event_mask = 0;
177   event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
178   event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
179   GNUNET_TESTBED_run (NULL, config, NUM_PEERS, event_mask,
180                       &controller_event_cb, NULL,
181                       &test_master, NULL);
182   abort_task =
183       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
184                                     (GNUNET_TIME_UNIT_SECONDS, 300),
185                                     &do_abort,
186                                     NULL);
187 }
188
189
190 /**
191  * Main function
192  */
193 int
194 main (int argc, char **argv)
195 {
196   char *argv2[] = {
197     "test_testbed_api_testbed_run",
198     "-c", NULL,
199     NULL
200   };
201   struct GNUNET_GETOPT_CommandLineOption options[] = {
202     GNUNET_GETOPT_OPTION_END
203   };
204   char *testname;
205   char *config_filename;
206   int ret;
207
208   if (NULL == (testname = strrchr (argv[0], (int) '_')))
209   {
210     GNUNET_break (0);
211     return 1;
212   }
213   testname++;
214   testname = GNUNET_strdup (testname);
215 #ifdef MINGW
216   {
217     char *period;
218
219     /* check and remove .exe extension */
220     period = strrchr (testname, (int) '.');
221     if (NULL != period)
222       *period = '\0';
223     else
224       GNUNET_break (0);         /* Windows with no .exe? */
225   }
226 #endif
227   if (0 == strcmp ("waitforever", testname))
228     wait_forever = GNUNET_YES;
229   if ( (GNUNET_YES != wait_forever) && (0 != strcmp ("run", testname)) )
230   {
231     GNUNET_asprintf (&config_filename, "test_testbed_api_testbed_run_%s.conf",
232                      testname);
233   }
234   else
235     config_filename = GNUNET_strdup ("test_testbed_api.conf");
236   GNUNET_free (testname);
237   argv2[2] = config_filename;
238   result = GNUNET_SYSERR;
239   ret =
240       GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
241                           "test_testbed_api_testbed_run", "nohelp", options,
242                           &run, NULL);
243   GNUNET_free (config_filename);
244   if ((GNUNET_OK != ret) || (GNUNET_OK != result))
245     return 1;
246   return 0;
247 }
248
249 /* end of test_testbed_api_testbed_run.c */