-inlined peer destroy in testcase
[oweals/gnunet.git] / src / testbed / test_testbed_api.c
1 /*
2       This file is part of GNUnet
3       (C) 2008--2012 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.c
23  * @brief testcases for the testbed api
24  * @author Sree Harsha Totakura
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testing_lib-new.h"
30 #include "gnunet_testbed_service.h"
31
32
33 /**
34  * Generic logging shortcut
35  */
36 #define LOG(kind,...)                           \
37   GNUNET_log (kind, __VA_ARGS__)
38
39 /**
40  * Relative time seconds shorthand
41  */
42 #define TIME_REL_SECS(sec) \
43   GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
44
45 /**
46  * Our localhost
47  */
48 static struct GNUNET_TESTBED_Host *host;
49
50 /**
51  * The controller handle
52  */
53 static struct GNUNET_TESTBED_Controller *controller;
54
55 /**
56  * A neighbouring host
57  */
58 static struct GNUNET_TESTBED_Host *neighbour;
59
60 /**
61  * Handle for neighbour registration
62  */
63 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
64
65 /**
66  * Handle for a peer
67  */
68 static struct GNUNET_TESTBED_Peer *peer;
69
70 /**
71  * Handle to configuration
72  */
73 static const struct GNUNET_CONFIGURATION_Handle *cfg;
74
75 /**
76  * Handle to operation
77  */
78 static struct GNUNET_TESTBED_Operation *operation;
79
80 /**
81  * Abort task identifier
82  */
83 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
84
85 /**
86  * The testing result
87  */
88 static int result;
89
90
91 /**
92  * Shutdown nicely
93  *
94  * @param cls NULL
95  * @param tc the task context
96  */
97 static void
98 do_shutdown (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
99 {
100   if (GNUNET_SCHEDULER_NO_TASK != abort_task)
101     GNUNET_SCHEDULER_cancel (abort_task);
102   if (NULL != reg_handle)
103     GNUNET_TESTBED_cancel_registration (reg_handle);
104   GNUNET_TESTBED_controller_disconnect (controller);
105   GNUNET_TESTBED_host_destroy (neighbour);
106   GNUNET_TESTBED_host_destroy (host);
107 }
108
109
110 /**
111  * abort task to run on test timed out
112  *
113  * @param cls NULL
114  * @param tc the task context
115  */
116 static void
117 do_abort (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
118 {
119   LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
120   abort_task = GNUNET_SCHEDULER_NO_TASK;
121   do_shutdown (cls, tc);
122 }
123
124
125 /**
126  * Signature of the event handler function called by the
127  * respective event controller.
128  *
129  * @param cls closure
130  * @param event information about the event
131  */
132 static void 
133 controller_cb(void *cls, const struct GNUNET_TESTBED_EventInformation *event)
134 {
135   GNUNET_assert (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
136   GNUNET_assert (event->details.operation_finished.operation == operation);
137   GNUNET_assert (NULL == event->details.operation_finished.op_cls);
138   GNUNET_assert (NULL == event->details.operation_finished.emsg);
139   GNUNET_assert (GNUNET_TESTBED_PIT_GENERIC ==
140                  event->details.operation_finished.pit);
141   GNUNET_assert (NULL == event->details.operation_finished.op_result.generic);
142   result = GNUNET_YES;  
143   GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
144 }
145
146
147 /**
148  * Callback which will be called to after a host registration succeeded or failed
149  *
150  * @param cls the host which has been registered
151  * @param emsg the error message; NULL if host registration is successful
152  */
153 static void 
154 registration_comp (void *cls, const char *emsg)
155 {
156   GNUNET_assert (cls == neighbour);
157   reg_handle = NULL;  
158   peer = GNUNET_TESTBED_peer_create (controller, host, cfg);
159   GNUNET_assert (NULL != peer);
160   operation = GNUNET_TESTBED_peer_destroy (peer);
161   GNUNET_assert (NULL != operation);
162 }
163
164
165 /**
166  * Main point of test execution
167  */
168 static void
169 run (void *cls,
170      const struct GNUNET_CONFIGURATION_Handle *config,
171      struct GNUNET_TESTING_Peer *peer)
172 {
173   uint64_t event_mask;
174
175   cfg = config;
176   host = GNUNET_TESTBED_host_create (NULL, NULL, 0);
177   GNUNET_assert (NULL != host);
178   event_mask ^= event_mask;     /* NULL out */
179   event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
180   event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
181   event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
182   event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
183   controller = GNUNET_TESTBED_controller_connect (config, host, event_mask,
184                                                   &controller_cb, NULL);
185   GNUNET_assert (NULL != controller);
186   neighbour = GNUNET_TESTBED_host_create ("localhost", NULL, 0);
187   GNUNET_assert (NULL != neighbour);
188   reg_handle = 
189     GNUNET_TESTBED_register_host (controller, neighbour, &registration_comp,
190                                   neighbour);
191   GNUNET_assert (NULL != reg_handle);  
192   abort_task = GNUNET_SCHEDULER_add_delayed 
193     (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 30), &do_abort, NULL);
194 }
195
196
197 /**
198  * Main function
199  */
200 int main (int argc, char **argv)
201 {
202   result = GNUNET_SYSERR;
203   if (0 != GNUNET_TESTING_peer_run ("test_testbed_api",
204                                     //                                 "arm",
205                                     "test_testbed_api.conf",
206                                     &run, NULL))
207     return 1;
208   else return (GNUNET_OK == result) ? 0 : 1;
209 }