glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / testbed / test_testbed_api_peer_reconfiguration.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 testbed/test_testbed_api_peer_reconfiguration.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 1
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  * Abort task identifier
44  */
45 static struct GNUNET_SCHEDULER_Task * abort_task;
46
47 /**
48  * States in this test
49  */
50 enum {
51
52   /**
53    * Test has just been initialized
54    */
55   STATE_INIT,
56
57   /**
58    * Peers have been started
59    */
60   STATE_PEER_STARTED,
61
62   /**
63    * Peer has been reconfigured.  Test completed successfully
64    */
65   STATE_PEER_RECONFIGURED
66
67 } state;
68
69 /**
70  * Fail testcase
71  */
72 #define FAIL_TEST(cond, ret) do {                               \
73     if (!(cond)) {                                              \
74       GNUNET_break(0);                                          \
75       if (NULL != abort_task)               \
76         GNUNET_SCHEDULER_cancel (abort_task);                   \
77       abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);  \
78       ret;                                                      \
79     }                                                           \
80   } while (0)
81
82
83 /**
84  * Abort task
85  *
86  * @param cls NULL
87  */
88 static void
89 do_abort (void *cls)
90 {
91   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Aborting\n");
92   abort_task = NULL;
93   if (NULL != op)
94   {
95     GNUNET_TESTBED_operation_done (op);
96     op = NULL;
97   }
98   GNUNET_SCHEDULER_shutdown();
99 }
100
101
102 /**
103  * Signature of the event handler function called by the
104  * respective event controller.
105  *
106  * @param cls closure
107  * @param event information about the event
108  */
109 static void
110 controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
111 {
112   if (STATE_PEER_STARTED != state)
113     return;
114   if (GNUNET_TESTBED_ET_OPERATION_FINISHED != event->type)
115   {
116     GNUNET_TESTBED_operation_done (op);
117     op = NULL;
118     FAIL_TEST (0, return);
119   }
120   if (NULL != event->details.operation_finished.emsg)
121   {
122     fprintf (stderr, "Operation failed: %s\n",
123              event->details.operation_finished.emsg);
124     GNUNET_TESTBED_operation_done (op);
125     op = NULL;
126     FAIL_TEST (0, return);
127   }
128   GNUNET_TESTBED_operation_done (op);
129   state = STATE_PEER_RECONFIGURED;
130   GNUNET_SCHEDULER_cancel (abort_task);
131   abort_task = NULL;
132   GNUNET_SCHEDULER_shutdown ();
133 }
134
135
136 /**
137  * Signature of a main function for a testcase.
138  *
139  * @param cls closure
140  * @param h the run handle
141  * @param num_peers number of peers in 'peers'
142  * @param peers_ handle to peers run in the testbed
143  * @param links_succeeded the number of overlay link connection attempts that
144  *          succeeded
145  * @param links_failed the number of overlay link connection attempts that
146  *          failed
147  */
148 static void
149 test_master (void *cls,
150              struct GNUNET_TESTBED_RunHandle *h,
151              unsigned int num_peers,
152              struct GNUNET_TESTBED_Peer **peers_,
153              unsigned int links_succeeded,
154              unsigned int links_failed)
155 {
156   struct GNUNET_CONFIGURATION_Handle *cfg;
157
158   FAIL_TEST (NUM_PEERS == num_peers, return);
159   state = STATE_PEER_STARTED;
160   peers = peers_;
161   cfg = GNUNET_CONFIGURATION_create ();
162   FAIL_TEST (GNUNET_OK == GNUNET_CONFIGURATION_load
163              (cfg, "test_testbed_api_testbed_run_topologyrandom.conf"), return);
164   op = GNUNET_TESTBED_peer_update_configuration (peers[0], cfg);
165   GNUNET_CONFIGURATION_destroy (cfg);
166   FAIL_TEST (NULL != op, return);
167   abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
168                                              (GNUNET_TIME_UNIT_SECONDS, 30),
169                                              &do_abort, NULL);
170 }
171
172
173 /**
174  * Main function
175  */
176 int
177 main (int argc, char **argv)
178 {
179   state = STATE_INIT;
180   (void) GNUNET_TESTBED_test_run ("test_testbed_api_peer_reconfiguration",
181                                   "test_testbed_api.conf",
182                                   NUM_PEERS,
183                                   1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED,
184                                   &controller_cb, NULL,
185                                   &test_master, NULL);
186   if (STATE_PEER_RECONFIGURED != state)
187     return 1;
188   return 0;
189 }