2 This file is part of GNUnet
3 (C) 2012 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file testbed/test_gnunet_testbed_helper.c
23 * @brief Testcase for testing gnunet-testbed-helper.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 GNUNET_SCHEDULER_TaskIdentifier abort_task;
64 * Shutdown task identifier
66 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
69 * Configuratin handler
71 static struct GNUNET_CONFIGURATION_Handle *cfg;
74 * Global testing status
83 * @param tc the task context
86 do_shutdown (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
88 if (GNUNET_SCHEDULER_NO_TASK != abort_task)
89 GNUNET_SCHEDULER_cancel (abort_task);
90 GNUNET_HELPER_stop (helper);
91 GNUNET_free_non_null (msg);
93 GNUNET_CONFIGURATION_destroy (cfg);
98 * abort task to run on test timed out
101 * @param tc the task context
104 do_abort (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
106 abort_task = GNUNET_SCHEDULER_NO_TASK;
107 LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
108 result = GNUNET_SYSERR;
110 GNUNET_HELPER_send_cancel (shandle);
111 if (GNUNET_SCHEDULER_NO_TASK == shutdown_task)
112 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
117 * Continuation function.
120 * @param result GNUNET_OK on success,
121 * GNUNET_NO if helper process died
122 * GNUNET_SYSERR during GNUNET_HELPER_stop
125 cont_cb (void *cls, int result)
128 LOG (GNUNET_ERROR_TYPE_DEBUG, "Message sent\n");
129 GNUNET_assert (GNUNET_OK == result);
134 * Functions with this signature are called whenever a
135 * complete message is received by the tokenizer.
137 * Do not call GNUNET_SERVER_mst_destroy in callback
140 * @param client identification of the client
141 * @param message the actual message
143 * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
146 mst_cb (void *cls, void *client, const struct GNUNET_MessageHeader *message)
148 const struct GNUNET_TESTBED_HelperReply *msg;
153 msg = (const struct GNUNET_TESTBED_HelperReply *) message;
156 GNUNET_assert (sizeof (struct GNUNET_TESTBED_HelperReply)
157 < ntohs (msg->header.size));
158 GNUNET_assert (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_REPLY
159 == ntohs (msg->header.type));
160 config_size = (uLongf) ntohs (msg->config_size);
161 xconfig_size = (uLongf) (ntohs (msg->header.size)
162 - sizeof (struct GNUNET_TESTBED_HelperReply));
163 config = GNUNET_malloc (config_size);
164 GNUNET_assert (Z_OK == uncompress ((Bytef *) config, &config_size,
165 (const Bytef *) &msg[1], xconfig_size));
166 GNUNET_free (config);
167 if (GNUNET_SCHEDULER_NO_TASK == shutdown_task)
168 shutdown_task = GNUNET_SCHEDULER_add_delayed
169 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1),
176 * Callback that will be called when the helper process dies. This is not called
177 * when the helper process is stoped using GNUNET_HELPER_stop()
179 * @param cls the closure from GNUNET_HELPER_start()
185 result = GNUNET_SYSERR;
190 * Main function that will be run.
193 * @param args remaining command-line arguments
194 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
195 * @param cfg configuration
198 run (void *cls, char *const *args, const char *cfgfile,
199 const struct GNUNET_CONFIGURATION_Handle * cfg2)
201 static char * const binary_argv[] = {
202 "gnunet-testbed-helper",
205 const char *controller_name = "127.0.0.1";
207 helper = GNUNET_HELPER_start ("gnunet-testbed-helper",
209 &mst_cb, &exp_cb, NULL);
210 GNUNET_assert (NULL != helper);
211 cfg = GNUNET_CONFIGURATION_dup (cfg2);
212 msg = GNUNET_TESTBED_create_helper_init_msg_ (controller_name, cfg);
213 shandle = GNUNET_HELPER_send (helper,
215 GNUNET_NO, &cont_cb, NULL);
216 GNUNET_assert (NULL != shandle);
217 abort_task = GNUNET_SCHEDULER_add_delayed
218 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 1), &do_abort, NULL);
225 * @param argc the number of command line arguments
226 * @param argv command line arg array
227 * @return return code
229 int main (int argc, char **argv)
231 struct GNUNET_GETOPT_CommandLineOption options[] = {
232 GNUNET_GETOPT_OPTION_END
237 GNUNET_PROGRAM_run (argc, argv, "test_gnunet_testbed_helper",
238 "Testcase for testing gnunet-testbed-helper.c",
239 options, &run, NULL))
241 return (GNUNET_OK == result) ? 0 : 1;
244 /* end of test_gnunet_testbed_helper.c */