- read whitelist from database
[oweals/gnunet.git] / src / testbed / test_testbed_api_test.c
1 /*
2   This file is part of GNUnet
3   (C) 2008--2013 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18   Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file src/testbed/test_testbed_api_test.c
23  * @brief testing cases for testing high level testbed api helper functions
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
31
32 /**
33  * Generic logging shortcut
34  */
35 #define LOG(kind,...)                           \
36   GNUNET_log (kind, __VA_ARGS__)
37
38 /**
39  * Number of peers we want to start
40  */
41 #define NUM_PEERS 25
42
43 /**
44  * Array of peers
45  */
46 static struct GNUNET_TESTBED_Peer **peers;
47
48 /**
49  * Operation handle
50  */
51 static struct GNUNET_TESTBED_Operation *op;
52
53 /**
54  * Abort task identifier
55  */
56 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
57
58 /**
59  * shutdown task identifier
60  */
61 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
62
63 /**
64  * Testing result
65  */
66 static int result;
67
68
69 /**
70  * Shutdown nicely
71  *
72  * @param cls NULL
73  * @param tc the task context
74  */
75 static void
76 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
77 {
78   shutdown_task = GNUNET_SCHEDULER_NO_TASK;
79   if (GNUNET_SCHEDULER_NO_TASK != abort_task)
80     GNUNET_SCHEDULER_cancel (abort_task);
81   if (NULL != op)
82     GNUNET_TESTBED_operation_done (op);
83   GNUNET_SCHEDULER_shutdown ();
84 }
85
86 /**
87  * shortcut to exit during failure
88  */
89 #define FAIL_TEST(cond) do {                                            \
90     if (!(cond)) {                                                      \
91       GNUNET_break(0);                                                  \
92       if (GNUNET_SCHEDULER_NO_TASK != abort_task)                       \
93         GNUNET_SCHEDULER_cancel (abort_task);                           \
94       abort_task = GNUNET_SCHEDULER_NO_TASK;                            \
95       if (GNUNET_SCHEDULER_NO_TASK == shutdown_task)                    \
96         shutdown_task = GNUNET_SCHEDULER_add_now (do_shutdown, NULL);   \
97       return;                                                           \
98     }                                                                   \
99   } while (0)
100
101
102 /**
103  * abort task to run on test timed out
104  *
105  * @param cls NULL
106  * @param tc the task context
107  */
108 static void
109 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
110 {
111   LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
112   abort_task = GNUNET_SCHEDULER_NO_TASK;
113   if (GNUNET_SCHEDULER_NO_TASK != shutdown_task)
114     GNUNET_SCHEDULER_cancel (shutdown_task);
115   do_shutdown (cls, tc);
116 }
117
118
119 /**
120  * Callback to be called when the requested peer information is available
121  *
122  * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
123  * @param op the operation this callback corresponds to
124  * @param pinfo the result; will be NULL if the operation has failed
125  * @param emsg error message if the operation has failed; will be NULL if the
126  *          operation is successfull
127  */
128 static void
129 peerinfo_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op_,
130              const struct GNUNET_TESTBED_PeerInformation *pinfo,
131              const char *emsg)
132 {
133   FAIL_TEST (op == op_);
134   FAIL_TEST (NULL == cb_cls);
135   FAIL_TEST (NULL == emsg);
136   FAIL_TEST (GNUNET_TESTBED_PIT_IDENTITY == pinfo->pit);
137   FAIL_TEST (NULL != pinfo->result.id);
138   GNUNET_TESTBED_operation_done (op);
139   op = NULL;
140   result = GNUNET_OK;
141   shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
142 }
143
144
145 /**
146  * Callback to be called when an operation is completed
147  *
148  * @param cls the callback closure from functions generating an operation
149  * @param op the operation that has been finished
150  * @param emsg error message in case the operation has failed; will be NULL if
151  *          operation has executed successfully.
152  */
153 static void
154 op_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op_, const char *emsg)
155 {
156   FAIL_TEST (NULL == cls);
157   FAIL_TEST (op == op_);
158   if (NULL != emsg)
159   {
160     LOG (GNUNET_ERROR_TYPE_WARNING, "%s\n", emsg);
161     FAIL_TEST (0);
162   }
163   GNUNET_TESTBED_operation_done (op);
164   op = GNUNET_TESTBED_peer_get_information (peers[0],
165                                             GNUNET_TESTBED_PIT_IDENTITY,
166                                             &peerinfo_cb, NULL);
167 }
168
169
170 /**
171  * Controller event callback
172  *
173  * @param cls NULL
174  * @param event the controller event
175  */
176 static void
177 controller_event_cb (void *cls,
178                      const struct GNUNET_TESTBED_EventInformation *event)
179 {
180   switch (event->type)
181   {
182   case GNUNET_TESTBED_ET_CONNECT:
183     FAIL_TEST (event->details.peer_connect.peer1 == peers[0]);
184     FAIL_TEST (event->details.peer_connect.peer2 == peers[1]);
185     break;
186   default:
187     FAIL_TEST (0);
188   }
189 }
190
191
192 /**
193  * Signature of a main function for a testcase.
194  *
195  * @param cls closure
196  * @param h the run handle
197  * @param num_peers number of peers in 'peers'
198  * @param peers- handle to peers run in the testbed
199  * @param links_succeeded the number of overlay link connection attempts that
200  *          succeeded
201  * @param links_failed the number of overlay link connection attempts that
202  *          failed
203  */
204 static void
205 test_master (void *cls,
206              struct GNUNET_TESTBED_RunHandle *h,
207              unsigned int num_peers,
208              struct GNUNET_TESTBED_Peer **peers_,
209              unsigned int links_succeeded,
210              unsigned int links_failed)
211 {
212   unsigned int peer;
213
214   FAIL_TEST (NULL == cls);
215   FAIL_TEST (NUM_PEERS == num_peers);
216   FAIL_TEST (NULL != peers_);
217   for (peer = 0; peer < num_peers; peer++)
218     FAIL_TEST (NULL != peers_[peer]);
219   peers = peers_;
220   op = GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL, peers[0],
221                                        peers[1]);
222   abort_task =
223       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
224                                     (GNUNET_TIME_UNIT_MINUTES, 3), &do_abort,
225                                     NULL);
226 }
227
228
229 /**
230  * Main function
231  */
232 int
233 main (int argc, char **argv)
234 {
235   uint64_t event_mask;
236
237   result = GNUNET_SYSERR;
238   event_mask = 0;
239   event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
240   event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
241   (void) GNUNET_TESTBED_test_run ("test_testbed_api_test",
242                                   "test_testbed_api.conf", NUM_PEERS,
243                                   event_mask, &controller_event_cb, NULL,
244                                   &test_master, NULL);
245   if (GNUNET_OK != result)
246     return 1;
247   return 0;
248 }
249
250 /* end of test_testbed_api_test.c */