plugin datastore mysql
[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
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.
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       General Public License for more details.
14
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., 51 Franklin Street, Fifth Floor,
18       Boston, MA 02110-1301, USA.
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 * shutdown_task;
54
55 /**
56  * Result of this test case
57  */
58 static int result;
59
60
61 /**
62  * Shutdown this test case when it takes too long
63  *
64  * @param cls NULL
65  */
66 static void
67 do_shutdown (void *cls)
68 {
69   shutdown_task = NULL;
70   if (NULL != barrier)
71   {
72     GNUNET_TESTBED_barrier_cancel (barrier);
73     barrier = NULL;
74   }
75
76   GNUNET_SCHEDULER_shutdown ();
77 }
78
79
80 /**
81  * Functions of this type are to be given as callback argument to
82  * GNUNET_TESTBED_barrier_init().  The callback will be called when status
83  * information is available for the barrier.
84  *
85  * @param cls the closure given to GNUNET_TESTBED_barrier_init()
86  * @param name the name of the barrier
87  * @param barrier the barrier handle
88  * @param status status of the barrier; GNUNET_OK if the barrier is crossed;
89  *   GNUNET_SYSERR upon error
90  * @param emsg if the status were to be GNUNET_SYSERR, this parameter has the
91  *   error messsage
92  */
93 static void
94 barrier_cb (void *cls,
95             const char *name,
96             struct GNUNET_TESTBED_Barrier *_barrier,
97             enum GNUNET_TESTBED_BarrierStatus status,
98             const char *emsg)
99 {
100   static enum GNUNET_TESTBED_BarrierStatus old_status;
101
102   GNUNET_assert (NULL == cls);
103   GNUNET_assert (_barrier == barrier);
104   switch (status)
105   {
106   case GNUNET_TESTBED_BARRIERSTATUS_INITIALISED:
107     LOG (GNUNET_ERROR_TYPE_INFO, "Barrier initialised\n");
108     old_status = status;
109     return;
110   case GNUNET_TESTBED_BARRIERSTATUS_ERROR:
111     LOG (GNUNET_ERROR_TYPE_ERROR, "Barrier initialisation failed: %s",
112          (NULL == emsg) ? "unknown reason" : emsg);
113     barrier = NULL;
114     GNUNET_SCHEDULER_shutdown ();
115     return;
116   case GNUNET_TESTBED_BARRIERSTATUS_CROSSED:
117     LOG (GNUNET_ERROR_TYPE_INFO, "Barrier crossed\n");
118     if (old_status == GNUNET_TESTBED_BARRIERSTATUS_INITIALISED)
119       result = GNUNET_OK;
120     barrier = NULL;
121     GNUNET_SCHEDULER_shutdown ();
122     return;
123   default:
124     GNUNET_assert (0);
125   }
126 }
127
128
129 /**
130  * Signature of a main function for a testcase.
131  *
132  * @param cls closure
133  * @param h the run handle
134  * @param num_peers number of peers in 'peers'
135  * @param peers_ handle to peers run in the testbed
136  * @param links_succeeded the number of overlay link connection attempts that
137  *          succeeded
138  * @param links_failed the number of overlay link connection attempts that
139  *          failed
140  */
141 static void
142 test_master (void *cls,
143              struct GNUNET_TESTBED_RunHandle *h,
144              unsigned int num_peers,
145              struct GNUNET_TESTBED_Peer **peers_,
146              unsigned int links_succeeded,
147              unsigned int links_failed)
148 {
149   struct GNUNET_TESTBED_Controller *c;
150
151   GNUNET_assert (NULL == cls);
152   if (NULL == peers_)
153   {
154     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failing test due to timeout\n");
155     return;
156   }
157   GNUNET_assert (NUM_PEERS == num_peers);
158   c = GNUNET_TESTBED_run_get_controller_handle (h);
159   barrier = GNUNET_TESTBED_barrier_init (c, TEST_BARRIER_NAME, 100,
160                                          &barrier_cb, NULL);
161   shutdown_task =
162       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
163                                     (GNUNET_TIME_UNIT_SECONDS,
164                                      10 * (NUM_PEERS + 1)),
165                                     &do_shutdown, NULL);
166 }
167
168
169 #ifndef PATH_MAX
170 /**
171  * Assumed maximum path length (for the log file name).
172  */
173 #define PATH_MAX 4096
174 #endif
175
176
177 /**
178  * Main function
179  */
180 int
181 main (int argc, char **argv)
182 {
183   struct GNUNET_CONFIGURATION_Handle *cfg;
184   char pwd[PATH_MAX];
185   char *binary;
186   uint64_t event_mask;
187
188   result = GNUNET_SYSERR;
189   event_mask = 0;
190   cfg = GNUNET_CONFIGURATION_create ();
191   GNUNET_assert (GNUNET_YES ==
192                  GNUNET_CONFIGURATION_parse (cfg,
193                                              "test_testbed_api_barriers.conf.in"));
194   if (NULL == getcwd (pwd, PATH_MAX))
195     return 1;
196   GNUNET_assert (0 < GNUNET_asprintf (&binary, "%s/%s", pwd,
197                                       "gnunet-service-test-barriers"));
198   GNUNET_CONFIGURATION_set_value_string (cfg, "test-barriers","BINARY", binary);
199   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_write
200                  (cfg, "test_testbed_api_barriers.conf"));
201   GNUNET_CONFIGURATION_destroy (cfg);
202   cfg = NULL;
203   GNUNET_free (binary);
204   binary = NULL;
205   (void) GNUNET_TESTBED_test_run ("test_testbed_api_barriers",
206                                   "test_testbed_api_barriers.conf", NUM_PEERS,
207                                   event_mask, NULL, NULL,
208                                   &test_master, NULL);
209   (void) unlink ("test_testbed_api_barriers.conf");
210   if (GNUNET_OK != result)
211     return 1;
212   return 0;
213 }