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