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