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 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.
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.
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>
23 #include "gnunet_util_lib.h"
24 #include "gnunet_testbed_service.h"
27 #include "testbed_api.h"
28 #include "testbed_helper.h"
29 #include "testbed_api_hosts.h"
32 * Generic logging shortcut
34 #define LOG(kind,...) \
35 GNUNET_log (kind, __VA_ARGS__)
39 * Handle to the helper process
41 static struct GNUNET_HELPER_Handle *helper;
46 static struct GNUNET_TESTBED_HelperInit *msg;
51 static struct GNUNET_HELPER_SendHandle *shandle;
54 * Abort task identifier
56 static struct GNUNET_SCHEDULER_Task * abort_task;
59 * Shutdown task identifier
61 static struct GNUNET_SCHEDULER_Task * shutdown_task;
64 * Configuratin handler
66 static struct GNUNET_CONFIGURATION_Handle *cfg;
69 * Global testing status
80 do_shutdown (void *cls)
82 if (NULL != abort_task)
83 GNUNET_SCHEDULER_cancel (abort_task);
85 GNUNET_HELPER_stop (helper, GNUNET_NO);
86 GNUNET_free_non_null (msg);
88 GNUNET_CONFIGURATION_destroy (cfg);
93 * abort task to run on test timed out
101 LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
102 result = GNUNET_SYSERR;
104 GNUNET_HELPER_send_cancel (shandle);
105 if (NULL == shutdown_task)
106 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
111 * Continuation function.
114 * @param result #GNUNET_OK on success,
115 * #GNUNET_NO if helper process died
116 * #GNUNET_SYSERR during GNUNET_HELPER_stop()
123 LOG (GNUNET_ERROR_TYPE_DEBUG,
125 GNUNET_assert (GNUNET_OK == result);
130 * Functions with this signature are called whenever a
131 * complete message is received by the tokenizer.
133 * Do not call GNUNET_SERVER_mst_destroy in callback
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
142 const struct GNUNET_MessageHeader *message)
144 const struct GNUNET_TESTBED_HelperReply *msg;
149 msg = (const struct GNUNET_TESTBED_HelperReply *) message;
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);
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)
167 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
168 (GNUNET_TIME_UNIT_SECONDS, 1),
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()
178 * @param cls the closure from GNUNET_HELPER_start()
184 result = GNUNET_SYSERR;
189 * Main function that will be run.
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
197 run (void *cls, char *const *args, const char *cfgfile,
198 const struct GNUNET_CONFIGURATION_Handle *cfg2)
200 static char *const binary_argv[] = {
201 "gnunet-helper-testbed",
204 const char *trusted_ip = "127.0.0.1";
207 GNUNET_HELPER_start (GNUNET_YES,
208 "gnunet-helper-testbed",
213 GNUNET_assert (NULL != helper);
214 cfg = GNUNET_CONFIGURATION_dup (cfg2);
215 msg = GNUNET_TESTBED_create_helper_init_msg_ (trusted_ip, NULL, cfg);
217 GNUNET_HELPER_send (helper, &msg->header, GNUNET_NO, &cont_cb, NULL);
218 GNUNET_assert (NULL != shandle);
220 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
221 (GNUNET_TIME_UNIT_MINUTES, 1), &do_abort,
229 * @param argc the number of command line arguments
230 * @param argv command line arg array
231 * @return return code
234 main (int argc, char **argv)
236 struct GNUNET_GETOPT_CommandLineOption options[] = {
237 GNUNET_GETOPT_OPTION_END
242 GNUNET_PROGRAM_run (argc, argv, "test_gnunet_helper_testbed",
243 "Testcase for testing gnunet-helper-testbed.c",
244 options, &run, NULL))
246 return (GNUNET_OK == result) ? 0 : 1;
249 /* end of test_gnunet_helper_testbed.c */