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