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