glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / testbed / test_testbed_api_barriers.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
16 /**
17  * @file testbed/test_testbed_api_barriers.c
18  * @brief testcase binary for testing testbed barriers API
19  * @author Sree Harsha Totakura <sreeharsha@totakura.in>
20  */
21
22 #include "platform.h"
23 #include "gnunet_util_lib.h"
24 #include "gnunet_testbed_service.h"
25 #include "test_testbed_api_barriers.h"
26
27
28 /**
29  * logging short hand
30  */
31 #define LOG(type,...) \
32   GNUNET_log (type, __VA_ARGS__);
33
34 /**
35  * Number of peers we start in this test case
36  */
37 #define NUM_PEERS 3
38
39
40 /**
41  * Our barrier
42  */
43 struct GNUNET_TESTBED_Barrier *barrier;
44
45 /**
46  * Identifier for the shutdown task
47  */
48 static struct GNUNET_SCHEDULER_Task *timeout_task;
49
50 /**
51  * Result of this test case
52  */
53 static int result;
54
55
56 /**
57  * Handle SIGINT and SIGTERM
58  */
59 static void
60 shutdown_handler(void *cls)
61 {
62   if (NULL != timeout_task)
63   {
64     GNUNET_SCHEDULER_cancel(timeout_task);
65     timeout_task = NULL;
66   }
67 }
68
69
70 /**
71  * Shutdown this test case when it takes too long
72  *
73  * @param cls NULL
74  */
75 static void
76 do_timeout (void *cls)
77 {
78   timeout_task = NULL;
79   if (barrier != NULL)
80       GNUNET_TESTBED_barrier_cancel (barrier);
81   GNUNET_SCHEDULER_shutdown ();
82 }
83
84
85 /**
86  * Functions of this type are to be given as callback argument to
87  * GNUNET_TESTBED_barrier_init().  The callback will be called when status
88  * information is available for the barrier.
89  *
90  * @param cls the closure given to GNUNET_TESTBED_barrier_init()
91  * @param name the name of the barrier
92  * @param barrier the barrier handle
93  * @param status status of the barrier; #GNUNET_OK if the barrier is crossed;
94  *   #GNUNET_SYSERR upon error
95  * @param emsg if the status were to be #GNUNET_SYSERR, this parameter has the
96  *   error messsage
97  */
98 static void
99 barrier_cb (void *cls,
100             const char *name,
101             struct GNUNET_TESTBED_Barrier *_barrier,
102             enum GNUNET_TESTBED_BarrierStatus status,
103             const char *emsg)
104 {
105   static enum GNUNET_TESTBED_BarrierStatus old_status;
106
107   GNUNET_assert (NULL == cls);
108   GNUNET_assert (_barrier == barrier);
109   switch (status)
110   {
111   case GNUNET_TESTBED_BARRIERSTATUS_INITIALISED:
112     LOG (GNUNET_ERROR_TYPE_INFO,
113          "Barrier initialised\n");
114     old_status = status;
115     return;
116   case GNUNET_TESTBED_BARRIERSTATUS_ERROR:
117     LOG (GNUNET_ERROR_TYPE_ERROR,
118          "Barrier initialisation failed: %s",
119          (NULL == emsg) ? "unknown reason" : emsg);
120     break;
121   case GNUNET_TESTBED_BARRIERSTATUS_CROSSED:
122     LOG (GNUNET_ERROR_TYPE_INFO,
123          "Barrier crossed\n");
124     if (old_status == GNUNET_TESTBED_BARRIERSTATUS_INITIALISED)
125       result = GNUNET_OK;
126     break;
127   default:
128     GNUNET_assert (0);
129     return;
130   }
131   barrier = NULL;
132   GNUNET_SCHEDULER_shutdown ();
133 }
134
135
136 /**
137  * Signature of a main function for a testcase.
138  *
139  * @param cls closure
140  * @param h the run handle
141  * @param num_peers number of peers in 'peers'
142  * @param peers_ handle to peers run in the testbed
143  * @param links_succeeded the number of overlay link connection attempts that
144  *          succeeded
145  * @param links_failed the number of overlay link connection attempts that
146  *          failed
147  */
148 static void
149 test_master (void *cls,
150              struct GNUNET_TESTBED_RunHandle *h,
151              unsigned int num_peers,
152              struct GNUNET_TESTBED_Peer **peers_,
153              unsigned int links_succeeded,
154              unsigned int links_failed)
155 {
156   struct GNUNET_TESTBED_Controller *c;
157
158   GNUNET_assert (NULL == cls);
159   if (NULL == peers_)
160   {
161     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
162                 "Failing test due to timeout\n");
163     return;
164   }
165   GNUNET_assert (NUM_PEERS == num_peers);
166   c = GNUNET_TESTBED_run_get_controller_handle (h);
167   barrier = GNUNET_TESTBED_barrier_init (c,
168                                          TEST_BARRIER_NAME,
169                                          100,
170                                          &barrier_cb,
171                                          NULL);
172   timeout_task =
173       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
174                                     (GNUNET_TIME_UNIT_SECONDS,
175                                      10 * (NUM_PEERS + 1)),
176                                     &do_timeout, NULL);
177   GNUNET_SCHEDULER_add_shutdown(&shutdown_handler, NULL);
178 }
179
180
181 #ifndef PATH_MAX
182 /**
183  * Assumed maximum path length (for the log file name).
184  */
185 #define PATH_MAX 4096
186 #endif
187
188
189 /**
190  * Main function
191  */
192 int
193 main (int argc, char **argv)
194 {
195   struct GNUNET_CONFIGURATION_Handle *cfg;
196   char pwd[PATH_MAX];
197   char *binary;
198   uint64_t event_mask;
199
200   result = GNUNET_SYSERR;
201   event_mask = 0;
202   cfg = GNUNET_CONFIGURATION_create ();
203   GNUNET_assert (GNUNET_YES ==
204                  GNUNET_CONFIGURATION_parse (cfg,
205                                              "test_testbed_api_barriers.conf.in"));
206   if (NULL == getcwd (pwd, PATH_MAX))
207     return 1;
208   GNUNET_assert (0 < GNUNET_asprintf (&binary, "%s/%s", pwd,
209                                       "gnunet-service-test-barriers"));
210   GNUNET_CONFIGURATION_set_value_string (cfg, "test-barriers","BINARY", binary);
211   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_write
212                  (cfg, "test_testbed_api_barriers.conf"));
213   GNUNET_CONFIGURATION_destroy (cfg);
214   cfg = NULL;
215   GNUNET_free (binary);
216   binary = NULL;
217   (void) GNUNET_TESTBED_test_run ("test_testbed_api_barriers",
218                                   "test_testbed_api_barriers.conf", NUM_PEERS,
219                                   event_mask, NULL, NULL,
220                                   &test_master, NULL);
221   (void) unlink ("test_testbed_api_barriers.conf");
222   if (GNUNET_OK != result)
223     return 1;
224   return 0;
225 }