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