- expand $ variables in filenames
[oweals/gnunet.git] / src / testbed / test_testbed_api_peer_reconfiguration.c
1 /*
2       This file is part of GNUnet
3       (C) 2008--2013 Christian Grothoff (and other contributing authors)
4
5       GNUnet is free software; you can redistribute it and/or modify
6       it under the terms of the GNU General Public License as published
7       by the Free Software Foundation; either version 3, or (at your
8       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       General Public License for more details.
14
15       You should have received a copy of the GNU General Public License
16       along with GNUnet; see the file COPYING.  If not, write to the
17       Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18       Boston, MA 02111-1307, USA.
19  */
20
21 /**
22  * @file testbed/test_testbed_api_peer_reconfiguration.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_common.h"
30 #include "gnunet_testbed_service.h"
31
32 /**
33  * Number of peers we want to start
34  */
35 #define NUM_PEERS 1
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  * Abort task identifier
49  */
50 static GNUNET_SCHEDULER_TaskIdentifier 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_PEER_STARTED,
66   
67   /**
68    * Peer has been reconfigured.  Test completed successfully
69    */
70   STATE_PEER_RECONFIGURED
71
72 } state;
73
74 /**
75  * Fail testcase
76  */
77 #define FAIL_TEST(cond, ret) do {                               \
78     if (!(cond)) {                                              \
79       GNUNET_break(0);                                          \
80       if (GNUNET_SCHEDULER_NO_TASK != abort_task)               \
81         GNUNET_SCHEDULER_cancel (abort_task);                   \
82       abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);  \
83       ret;                                                      \
84     }                                                           \
85   } while (0)
86
87
88 /**
89  * Abort task
90  *
91  * @param cls NULL
92  * @param tc scheduler task context
93  */
94 static void
95 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
96 {
97   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Aborting\n");
98   abort_task = GNUNET_SCHEDULER_NO_TASK;
99   if (NULL != op)
100   {  
101     GNUNET_TESTBED_operation_done (op);
102     op = NULL;
103   }
104   GNUNET_SCHEDULER_shutdown();
105 }
106
107
108 /**
109  * Signature of the event handler function called by the
110  * respective event controller.
111  *
112  * @param cls closure
113  * @param event information about the event
114  */
115 static void
116 controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
117 {
118   if (STATE_PEER_STARTED != state)
119     return;
120   if (GNUNET_TESTBED_ET_OPERATION_FINISHED != event->type)
121   {
122     GNUNET_TESTBED_operation_done (op);
123     op = NULL;
124     FAIL_TEST (0, return);
125   }
126   if (NULL != event->details.operation_finished.emsg)
127   {
128     fprintf (stderr, "Operation failed: %s\n",
129              event->details.operation_finished.emsg);
130     GNUNET_TESTBED_operation_done (op);
131     op = NULL;    
132     FAIL_TEST (0, return);
133   }
134   GNUNET_TESTBED_operation_done (op);
135   state = STATE_PEER_RECONFIGURED;
136   GNUNET_SCHEDULER_cancel (abort_task);
137   abort_task = GNUNET_SCHEDULER_NO_TASK;
138   GNUNET_SCHEDULER_shutdown ();
139 }
140
141
142 /**
143  * Signature of a main function for a testcase.
144  *
145  * @param cls closure
146  * @param num_peers number of peers in 'peers'
147  * @param peers_ handle to peers run in the testbed
148  * @param links_succeeded the number of overlay link connection attempts that
149  *          succeeded
150  * @param links_failed the number of overlay link connection attempts that
151  *          failed
152  */
153 static void
154 test_master (void *cls, unsigned int num_peers,
155              struct GNUNET_TESTBED_Peer **peers_,
156              unsigned int links_succeeded,
157              unsigned int links_failed)
158 {
159   struct GNUNET_CONFIGURATION_Handle *cfg;
160
161   FAIL_TEST (NUM_PEERS == num_peers, return);
162   state = STATE_PEER_STARTED;
163   peers = peers_;
164   cfg = GNUNET_CONFIGURATION_create ();
165   FAIL_TEST (GNUNET_OK == GNUNET_CONFIGURATION_parse 
166              (cfg, "test_testbed_api_testbed_run_topologyrandom.conf"), return);
167   op = GNUNET_TESTBED_peer_update_configuration (peers[0], cfg);
168   GNUNET_CONFIGURATION_destroy (cfg);
169   FAIL_TEST (NULL != op, return);
170   abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
171                                              (GNUNET_TIME_UNIT_SECONDS, 30),
172                                              &do_abort, NULL);
173 }
174
175
176 /**
177  * Main function
178  */
179 int
180 main (int argc, char **argv)
181 {
182   state = STATE_INIT;
183   (void) GNUNET_TESTBED_test_run ("test_testbed_api_peer_reconfiguration",
184                                   "test_testbed_api.conf",
185                                   NUM_PEERS,
186                                   1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED,
187                                   &controller_cb, NULL,
188                                   &test_master, NULL);
189   if (STATE_PEER_RECONFIGURED != state)
190     return 1;
191   return 0;
192 }