612b30347340641135c2f0c208a303171299c955
[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 <sreeharsha@totakura.in> 
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 /**
41  * Our localhost
42  */
43 static struct GNUNET_TESTBED_Host *host;
44
45 /**
46  * The controller handle
47  */
48 static struct GNUNET_TESTBED_Controller *c;
49
50 /**
51  * A neighbouring host
52  */
53 static struct GNUNET_TESTBED_Host *neighbour;
54
55 /**
56  * Handle for neighbour registration
57  */
58 static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
59
60 /**
61  * Abort task identifier
62  */
63 static GNUNET_SCHEDULER_TaskIdentifier abort_task_id;
64
65 /**
66  * The testing result
67  */
68 static int result;
69
70
71 /**
72  * Shutdown nicely
73  *
74  * @param cls NULL
75  * @param tc the task context
76  */
77 static void
78 do_shutdown (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
79 {
80   if (GNUNET_SCHEDULER_NO_TASK != abort_task_id)
81     GNUNET_SCHEDULER_cancel (abort_task_id);
82   if (NULL != reg_handle)
83     GNUNET_TESTBED_cancel_registration (reg_handle);
84   GNUNET_TESTBED_controller_disconnect (c);
85   GNUNET_TESTBED_host_destroy (neighbour);
86   GNUNET_TESTBED_host_destroy (host);
87 }
88
89
90 /**
91  * abort task to run on test timed out
92  *
93  * @param cls NULL
94  * @param tc the task context
95  */
96 static void
97 do_abort (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
98 {
99   LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
100   abort_task_id = GNUNET_SCHEDULER_NO_TASK;
101   do_shutdown (cls, tc);
102 }
103
104
105 /**
106  * Signature of the event handler function called by the
107  * respective event controller.
108  *
109  * @param cls closure
110  * @param event information about the event
111  */
112 static void 
113 controller_cb(void *cls, const struct GNUNET_TESTBED_EventInformation *event)
114 {
115   GNUNET_break (0);
116 }
117
118
119 /**
120  * Callback which will be called to after a host registration succeeded or failed
121  *
122  * @param cls the host which has been registered
123  * @param emsg the error message; NULL if host registration is successful
124  */
125 static void 
126 registration_comp (void *cls, const char *emsg)
127 {
128   GNUNET_assert (cls == neighbour);
129   reg_handle = NULL;
130   result = GNUNET_YES;
131   GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
132 }
133
134
135 /**
136  * Main point of test execution
137  */
138 static void
139 run (void *cls,
140      const struct GNUNET_CONFIGURATION_Handle *cfg,
141      struct GNUNET_TESTING_Peer *peer)
142 {
143   uint64_t event_mask;
144
145   host = GNUNET_TESTBED_host_create (NULL, NULL, 0);
146   GNUNET_assert (NULL != host);
147   event_mask ^= event_mask;     /* NULL out */
148   event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
149   event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
150   event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
151   c = GNUNET_TESTBED_controller_connect (cfg, host, event_mask,
152                                          &controller_cb, NULL);
153   GNUNET_assert (NULL != c);
154   neighbour = GNUNET_TESTBED_host_create ("localhost", NULL, 0);
155   GNUNET_assert (NULL != neighbour);
156   reg_handle = 
157     GNUNET_TESTBED_register_host (c, neighbour, &registration_comp, neighbour);
158   GNUNET_assert (NULL != reg_handle);
159   
160   abort_task_id = GNUNET_SCHEDULER_add_delayed 
161     (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 30), &do_abort, NULL);
162 }
163
164
165 /**
166  * Main function
167  */
168 int main (int argc, char **argv)
169 {
170   result = GNUNET_SYSERR;
171   if (0 != GNUNET_TESTING_service_run ("test_testbed_api",
172                                        "testbed",
173                                        "test_testbed_api.conf",
174                                        &run, NULL))
175     return 1;
176   else return (GNUNET_OK == result) ? 0 : 1;
177 }