fix
[oweals/gnunet.git] / src / testbed / test_gnunet_helper_testbed.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       You should have received a copy of the GNU Affero General Public License
16       along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 /**
20  * @file testbed/test_gnunet_helper_testbed.c
21  * @brief Testcase for testing gnunet-helper-testbed.c
22  * @author Sree Harsha Totakura <sreeharsha@totakura.in>
23  */
24
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_testbed_service.h"
28 #include <zlib.h>
29
30 #include "testbed_api.h"
31 #include "testbed_helper.h"
32 #include "testbed_api_hosts.h"
33
34 /**
35  * Generic logging shortcut
36  */
37 #define LOG(kind,...)                           \
38   GNUNET_log (kind, __VA_ARGS__)
39
40
41 /**
42  * Handle to the helper process
43  */
44 static struct GNUNET_HELPER_Handle *helper;
45
46 /**
47  * Message to helper
48  */
49 static struct GNUNET_TESTBED_HelperInit *msg;
50
51 /**
52  * Message send handle
53  */
54 static struct GNUNET_HELPER_SendHandle *shandle;
55
56 /**
57  * Abort task identifier
58  */
59 static struct GNUNET_SCHEDULER_Task * abort_task;
60
61 /**
62  * Shutdown task identifier
63  */
64 static struct GNUNET_SCHEDULER_Task * shutdown_task;
65
66 /**
67  * Configuratin handler
68  */
69 static struct GNUNET_CONFIGURATION_Handle *cfg;
70
71 /**
72  * Global testing status
73  */
74 static int result;
75
76
77 /**
78  * Shutdown nicely
79  *
80  * @param cls NULL
81  */
82 static void
83 do_shutdown (void *cls)
84 {
85   if (NULL != abort_task)
86     GNUNET_SCHEDULER_cancel (abort_task);
87   if (NULL != helper)
88     GNUNET_HELPER_stop (helper, GNUNET_NO);
89   GNUNET_free_non_null (msg);
90   if (NULL != cfg)
91     GNUNET_CONFIGURATION_destroy (cfg);
92 }
93
94
95 /**
96  * abort task to run on test timed out
97  *
98  * @param cls NULL
99  */
100 static void
101 do_abort (void *cls)
102 {
103   abort_task = NULL;
104   LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
105   result = GNUNET_SYSERR;
106   if (NULL != shandle)
107     GNUNET_HELPER_send_cancel (shandle);
108   if (NULL == shutdown_task)
109     shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
110 }
111
112
113 /**
114  * Continuation function.
115  *
116  * @param cls closure
117  * @param result #GNUNET_OK on success,
118  *               #GNUNET_NO if helper process died
119  *               #GNUNET_SYSERR during GNUNET_HELPER_stop()
120  */
121 static void
122 cont_cb (void *cls,
123          int result)
124 {
125   shandle = NULL;
126   LOG (GNUNET_ERROR_TYPE_DEBUG,
127        "Message sent\n");
128   GNUNET_assert (GNUNET_OK == result);
129 }
130
131
132 /**
133  * Functions with this signature are called whenever a
134  * complete message is received by the tokenizer.
135  *
136  * Do not call GNUNET_SERVER_mst_destroy in callback
137  *
138  * @param cls closure
139  * @param client identification of the client
140  * @param message the actual message
141  * @return #GNUNET_OK on success, #GNUNET_SYSERR to stop further processing
142  */
143 static int
144 mst_cb (void *cls,
145         const struct GNUNET_MessageHeader *message)
146 {
147   const struct GNUNET_TESTBED_HelperReply *msg;
148   char *config;
149   uLongf config_size;
150   uLongf xconfig_size;
151
152   msg = (const struct GNUNET_TESTBED_HelperReply *) message;
153   config_size = 0;
154   xconfig_size = 0;
155   GNUNET_assert (sizeof (struct GNUNET_TESTBED_HelperReply) <
156                  ntohs (msg->header.size));
157   GNUNET_assert (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_REPLY ==
158                  ntohs (msg->header.type));
159   config_size = (uLongf) ntohs (msg->config_size);
160   xconfig_size =
161       (uLongf) (ntohs (msg->header.size) -
162                 sizeof (struct GNUNET_TESTBED_HelperReply));
163   config = GNUNET_malloc (config_size);
164   GNUNET_assert (Z_OK ==
165                  uncompress ((Bytef *) config, &config_size,
166                              (const Bytef *) &msg[1], xconfig_size));
167   GNUNET_free (config);
168   if (NULL == shutdown_task)
169     shutdown_task =
170         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
171                                       (GNUNET_TIME_UNIT_SECONDS, 1),
172                                       &do_shutdown, NULL);
173   return GNUNET_OK;
174 }
175
176
177 /**
178  * Callback that will be called when the helper process dies. This is not called
179  * when the helper process is stoped using GNUNET_HELPER_stop()
180  *
181  * @param cls the closure from GNUNET_HELPER_start()
182  */
183 static void
184 exp_cb (void *cls)
185 {
186   helper = NULL;
187   result = GNUNET_SYSERR;
188 }
189
190
191 /**
192  * Main function that will be run.
193  *
194  * @param cls closure
195  * @param args remaining command-line arguments
196  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
197  * @param cfg configuration
198  */
199 static void
200 run (void *cls, char *const *args, const char *cfgfile,
201      const struct GNUNET_CONFIGURATION_Handle *cfg2)
202 {
203   static char *const binary_argv[] = {
204     "gnunet-helper-testbed",
205     NULL
206   };
207   const char *trusted_ip = "127.0.0.1";
208
209   helper =
210       GNUNET_HELPER_start (GNUNET_YES,
211                            "gnunet-helper-testbed",
212                            binary_argv,
213                            &mst_cb,
214                            &exp_cb,
215                            NULL);
216   GNUNET_assert (NULL != helper);
217   cfg = GNUNET_CONFIGURATION_dup (cfg2);
218   msg = GNUNET_TESTBED_create_helper_init_msg_ (trusted_ip, NULL, cfg);
219   shandle =
220       GNUNET_HELPER_send (helper, &msg->header, GNUNET_NO, &cont_cb, NULL);
221   GNUNET_assert (NULL != shandle);
222   abort_task =
223       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
224                                     (GNUNET_TIME_UNIT_MINUTES, 1), &do_abort,
225                                     NULL);
226 }
227
228
229 /**
230  * Main function
231  *
232  * @param argc the number of command line arguments
233  * @param argv command line arg array
234  * @return return code
235  */
236 int
237 main (int argc, char **argv)
238 {
239   struct GNUNET_GETOPT_CommandLineOption options[] = {
240     GNUNET_GETOPT_OPTION_END
241   };
242
243   result = GNUNET_OK;
244   if (GNUNET_OK !=
245       GNUNET_PROGRAM_run (argc, argv, "test_gnunet_helper_testbed",
246                           "Testcase for testing gnunet-helper-testbed.c",
247                           options, &run, NULL))
248     return 1;
249   return (GNUNET_OK == result) ? 0 : 1;
250 }
251
252 /* end of test_gnunet_helper_testbed.c */