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