2 This file is part of GNUnet
3 Copyright (C) 2008--2013 GNUnet e.V.
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.
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.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file testbed/test_gnunet_helper_testbed.c
23 * @brief Testcase for testing gnunet-helper-testbed.c
24 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testbed_service.h"
32 #include "testbed_api.h"
33 #include "testbed_helper.h"
34 #include "testbed_api_hosts.h"
37 * Generic logging shortcut
39 #define LOG(kind,...) \
40 GNUNET_log (kind, __VA_ARGS__)
44 * Handle to the helper process
46 static struct GNUNET_HELPER_Handle *helper;
51 static struct GNUNET_TESTBED_HelperInit *msg;
56 static struct GNUNET_HELPER_SendHandle *shandle;
59 * Abort task identifier
61 static struct GNUNET_SCHEDULER_Task * abort_task;
64 * Shutdown task identifier
66 static struct GNUNET_SCHEDULER_Task * shutdown_task;
69 * Configuratin handler
71 static struct GNUNET_CONFIGURATION_Handle *cfg;
74 * Global testing status
85 do_shutdown (void *cls)
87 if (NULL != abort_task)
88 GNUNET_SCHEDULER_cancel (abort_task);
90 GNUNET_HELPER_stop (helper, GNUNET_NO);
91 GNUNET_free_non_null (msg);
93 GNUNET_CONFIGURATION_destroy (cfg);
98 * abort task to run on test timed out
106 LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
107 result = GNUNET_SYSERR;
109 GNUNET_HELPER_send_cancel (shandle);
110 if (NULL == shutdown_task)
111 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
116 * Continuation function.
119 * @param result #GNUNET_OK on success,
120 * #GNUNET_NO if helper process died
121 * #GNUNET_SYSERR during GNUNET_HELPER_stop()
124 cont_cb (void *cls, int result)
127 LOG (GNUNET_ERROR_TYPE_DEBUG, "Message sent\n");
128 GNUNET_assert (GNUNET_OK == result);
133 * Functions with this signature are called whenever a
134 * complete message is received by the tokenizer.
136 * Do not call GNUNET_SERVER_mst_destroy in callback
139 * @param client identification of the client
140 * @param message the actual message
142 * @return #GNUNET_OK on success, #GNUNET_SYSERR to stop further processing
145 mst_cb (void *cls, void *client, const struct GNUNET_MessageHeader *message)
147 const struct GNUNET_TESTBED_HelperReply *msg;
152 msg = (const struct GNUNET_TESTBED_HelperReply *) message;
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);
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)
170 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
171 (GNUNET_TIME_UNIT_SECONDS, 1),
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()
181 * @param cls the closure from GNUNET_HELPER_start()
187 result = GNUNET_SYSERR;
192 * Main function that will be run.
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
200 run (void *cls, char *const *args, const char *cfgfile,
201 const struct GNUNET_CONFIGURATION_Handle *cfg2)
203 static char *const binary_argv[] = {
204 "gnunet-helper-testbed",
207 const char *trusted_ip = "127.0.0.1";
210 GNUNET_HELPER_start (GNUNET_YES, "gnunet-helper-testbed", binary_argv,
211 &mst_cb, &exp_cb, NULL);
212 GNUNET_assert (NULL != helper);
213 cfg = GNUNET_CONFIGURATION_dup (cfg2);
214 msg = GNUNET_TESTBED_create_helper_init_msg_ (trusted_ip, NULL, cfg);
216 GNUNET_HELPER_send (helper, &msg->header, GNUNET_NO, &cont_cb, NULL);
217 GNUNET_assert (NULL != shandle);
219 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
220 (GNUNET_TIME_UNIT_MINUTES, 1), &do_abort,
228 * @param argc the number of command line arguments
229 * @param argv command line arg array
230 * @return return code
233 main (int argc, char **argv)
235 struct GNUNET_GETOPT_CommandLineOption options[] = {
236 GNUNET_GETOPT_OPTION_END
241 GNUNET_PROGRAM_run (argc, argv, "test_gnunet_helper_testbed",
242 "Testcase for testing gnunet-helper-testbed.c",
243 options, &run, NULL))
245 return (GNUNET_OK == result) ? 0 : 1;
248 /* end of test_gnunet_helper_testbed.c */