testbed helper testcase
[oweals/gnunet.git] / src / testbed / test_gnunet_testbed_helper.c
1 /*
2       This file is part of GNUnet
3       (C) 2012 Christian Grothoff (and other contributing authors)
4
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.
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       General Public License for more details.
14
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.
19  */
20
21 /**
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> 
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "testbed_helper.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 /**
50  * Message send handle
51  */
52 static struct GNUNET_HELPER_SendHandle *shandle;
53
54 /**
55  * Abort task identifier
56  */
57 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
58
59 /**
60  * Shutdown task identifier
61  */
62 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
63
64
65 /**
66  * Shutdown nicely
67  *
68  * @param cls NULL
69  * @param tc the task context
70  */
71 static void
72 do_shutdown (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
73 {  
74   if (GNUNET_SCHEDULER_NO_TASK != abort_task)
75     GNUNET_SCHEDULER_cancel (abort_task);
76   GNUNET_HELPER_stop (helper);  
77 }
78
79
80 /**
81  * abort task to run on test timed out
82  *
83  * @param cls NULL
84  * @param tc the task context
85  */
86 static void
87 do_abort (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
88 {
89   LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
90   abort_task = GNUNET_SCHEDULER_NO_TASK;
91   GNUNET_HELPER_send_cancel (shandle);
92   if (GNUNET_SCHEDULER_NO_TASK == shutdown_task)
93     shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
94 }
95
96
97 /**
98  * Continuation function.
99  * 
100  * @param cls closure
101  * @param result GNUNET_OK on success,
102  *               GNUNET_NO if helper process died
103  *               GNUNET_SYSERR during GNUNET_HELPER_stop
104  */
105 static void 
106 cont_cb (void *cls, int result)
107 {
108   shandle = NULL;
109   LOG (GNUNET_ERROR_TYPE_DEBUG, "Message sent\n");
110   GNUNET_assert (GNUNET_OK == result);
111   if (GNUNET_SCHEDULER_NO_TASK == shutdown_task)
112     shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
113 }
114
115
116
117 /**
118  * Main function that will be run.
119  *
120  * @param cls closure
121  * @param args remaining command-line arguments
122  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
123  * @param cfg configuration
124  */
125 static void 
126 run (void *cls, char *const *args, const char *cfgfile,
127      const struct GNUNET_CONFIGURATION_Handle * cfg)
128 {
129   char * const binary_argv[] = {
130     "gnunet-testbed-helper",
131     NULL
132     };
133
134   helper = GNUNET_HELPER_start ("gnunet-testbed-helper", binary_argv,
135                                 NULL, NULL);
136   GNUNET_assert (NULL != helper);
137   msg.header.size = htons (sizeof (struct GNUNET_TESTBED_HelperInit));
138   msg.header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_INIT);
139   msg.cname_size = htons (0);
140   msg.config_size = htons (0);
141   shandle = GNUNET_HELPER_send (helper,
142                                 (const struct GNUNET_MessageHeader *) &msg,
143                                 GNUNET_NO, cont_cb, NULL);
144   GNUNET_assert (NULL != shandle);
145   abort_task = GNUNET_SCHEDULER_add_delayed 
146     (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 1), &do_abort, NULL);
147 }
148
149
150 /**
151  * Main function
152  *
153  * @param argc the number of command line arguments
154  * @param argv command line arg array
155  * @return return code
156  */
157 int main (int argc, char **argv)
158 {
159   struct GNUNET_GETOPT_CommandLineOption options[] = {
160     GNUNET_GETOPT_OPTION_END
161   };
162
163    if (GNUNET_OK != 
164        GNUNET_PROGRAM_run (argc, argv, "test_gnunet_testbed_helper",
165                            "Testcase for testing gnunet-testbed-helper.c",
166                            options, &run, NULL))
167      return 1;
168   else return 0;
169 }