add $(GN_LIBINTL) to Makefile.am (fixes 0005902)
[oweals/gnunet.git] / src / testbed / test_testbed_api_peers_manage_services.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 testbed/test_testbed_api_peers_manage_services.c
23  * @brief testcase for testing GNUNET_TESTBED_peer_manage_service()
24  *          implementation
25  * @author Sree Harsha Totakura <sreeharsha@totakura.in>
26  */
27
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_testbed_service.h"
31
32 /**
33  * Number of peers we want to start
34  */
35 #define NUM_PEERS 2
36
37 /**
38  * The array of peers; we get them from the testbed
39  */
40 static struct GNUNET_TESTBED_Peer **peers;
41
42 /**
43  * Operation handle
44  */
45 static struct GNUNET_TESTBED_Operation *op;
46
47 /**
48  * dummy pointer
49  */
50 static void *dummy_cls = (void *) 0xDEAD0001;
51
52 /**
53  * Abort task identifier
54  */
55 static struct GNUNET_SCHEDULER_Task *abort_task;
56
57 /**
58  * States in this test
59  */
60 enum
61 {
62   /**
63    * Test has just been initialized
64    */
65   STATE_INIT,
66
67   /**
68    * Peers have been started
69    */
70   STATE_PEERS_STARTED,
71
72   /**
73    * statistics service went down
74    */
75   STATE_SERVICE_DOWN,
76
77   /**
78    * statistics service went up
79    */
80   STATE_SERVICE_UP,
81
82   /**
83    * Testing completed successfully
84    */
85   STATE_OK
86 } state;
87
88 /**
89  * Fail testcase
90  */
91 #define FAIL_TEST(cond, ret) do {                               \
92     if (! (cond)) {                                              \
93       GNUNET_break (0);                                          \
94       if (NULL != abort_task)               \
95         GNUNET_SCHEDULER_cancel (abort_task);                   \
96       abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);  \
97       ret;                                                      \
98     }                                                           \
99 } while (0)
100
101
102 /**
103  * Abort task
104  *
105  * @param cls NULL
106  */
107 static void
108 do_abort (void *cls)
109 {
110   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Aborting\n");
111   abort_task = NULL;
112   if (NULL != op)
113   {
114     GNUNET_TESTBED_operation_done (op);
115     op = NULL;
116   }
117   GNUNET_SCHEDULER_shutdown ();
118 }
119
120
121 /**
122  * Callback to be called when an operation is completed
123  *
124  * @param cls the callback closure from functions generating an operation
125  * @param op the operation that has been finished
126  * @param emsg error message in case the operation has failed; will be NULL if
127  *          operation has executed successfully.
128  */
129 static void
130 op_comp_cb (void *cls,
131             struct GNUNET_TESTBED_Operation *op,
132             const char *emsg)
133 {
134   FAIL_TEST (cls == dummy_cls, return );
135   FAIL_TEST (NULL == emsg, return );
136   GNUNET_TESTBED_operation_done (op);
137   op = NULL;
138   switch (state)
139   {
140   case STATE_PEERS_STARTED:
141     state = STATE_SERVICE_DOWN;
142     op = GNUNET_TESTBED_peer_manage_service (dummy_cls,
143                                              peers[1],
144                                              "topology",
145                                              op_comp_cb,
146                                              dummy_cls,
147                                              0);
148     GNUNET_assert (NULL != op);
149     break;
150
151   case STATE_SERVICE_DOWN:
152     state = STATE_SERVICE_UP;
153     GNUNET_SCHEDULER_cancel (abort_task);
154     abort_task = NULL;
155     state = STATE_OK;
156     GNUNET_SCHEDULER_shutdown ();
157     break;
158
159   default:
160     FAIL_TEST (0, return );
161   }
162 }
163
164
165 /**
166  * Signature of a main function for a testcase.
167  *
168  * @param cls closure
169  * @param h the run handle
170  * @param num_peers number of peers in 'peers'
171  * @param peers_ handle to peers run in the testbed
172  * @param links_succeeded the number of overlay link connection attempts that
173  *          succeeded
174  * @param links_failed the number of overlay link connection attempts that
175  *          failed
176  */
177 static void
178 test_master (void *cls,
179              struct GNUNET_TESTBED_RunHandle *h,
180              unsigned int num_peers,
181              struct GNUNET_TESTBED_Peer **peers_,
182              unsigned int links_succeeded,
183              unsigned int links_failed)
184 {
185   FAIL_TEST (NUM_PEERS == num_peers, return );
186   state = STATE_PEERS_STARTED;
187   peers = peers_;
188   op = GNUNET_TESTBED_peer_manage_service (dummy_cls,
189                                            peers[1],
190                                            "topology",
191                                            op_comp_cb,
192                                            dummy_cls,
193                                            1);
194   FAIL_TEST (NULL != op, return );
195   abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
196                                                (GNUNET_TIME_UNIT_MINUTES, 1),
197                                              &do_abort, NULL);
198 }
199
200
201 /**
202  * Main function
203  */
204 int
205 main (int argc, char **argv)
206 {
207   state = STATE_INIT;
208   (void) GNUNET_TESTBED_test_run ("test_testbed_api_peers_manage_services",
209                                   "test_testbed_api.conf",
210                                   NUM_PEERS,
211                                   1LL, NULL, NULL,
212                                   &test_master, NULL);
213   if (STATE_OK != state)
214     return 1;
215   return 0;
216 }