helper integration to GNUNET_TESTBED_host_run_()
[oweals/gnunet.git] / src / testbed / test_testbed_api_hosts.c
1 /*
2       This file is part of GNUnet
3       (C) 2008--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_testbed_api_hosts.c
23  * @brief tests cases for testbed_api_hosts.c
24  * @author Sree Harsha Totakura
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testbed_service.h"
30 #include "testbed_api_hosts.h"
31
32
33 #define TIME_REL_SECS(sec)                                              \
34   GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
35
36 /**
37  * Host we are creating and using
38  */
39 static struct GNUNET_TESTBED_Host *host;
40
41 /**
42  * The host helper handle
43  */
44 static struct GNUNET_TESTBED_HelperHandle *helper_handle;
45
46 /**
47  * Global test status
48  */
49 static int status;
50
51 /**
52  * Shutdown task identifier
53  */
54 GNUNET_SCHEDULER_TaskIdentifier shutdown_id;
55
56 /**
57  * The shutdown task
58  *
59  * @param cls NULL
60  * @param tc the task context
61  */
62 static void
63 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
64 {
65   GNUNET_TESTBED_host_stop_ (helper_handle);
66   GNUNET_TESTBED_host_destroy (host);
67 }
68
69
70 /**
71  * Callback that will be called when the helper process dies. This is not called
72  * when the helper process is stoped using GNUNET_HELPER_stop()
73  *
74  * @param cls the closure from GNUNET_HELPER_start()
75  * @param h the handle representing the helper process. This handle is invalid
76  *          in this callback. It is only presented for reference. No operations
77  *          can be performed using it.
78  */
79 static void 
80 exp_cb (void *cls, const struct GNUNET_HELPER_Handle *h)
81 {
82   status = GNUNET_SYSERR;
83   GNUNET_SCHEDULER_cancel (shutdown_id);
84   GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
85 }
86
87
88 /**
89  * Main run function. 
90  *
91  * @param cls NULL
92  * @param args arguments passed to GNUNET_PROGRAM_run
93  * @param cfgfile the path to configuration file
94  * @param cfg the configuration file handle
95  */
96 static void
97 run (void *cls, char *const *args, const char *cfgfile,
98      const struct GNUNET_CONFIGURATION_Handle *cfg)
99 {
100   host = GNUNET_TESTBED_host_create ("localhost", NULL, 0);
101   GNUNET_assert (NULL != host);
102   GNUNET_assert (0 != GNUNET_TESTBED_host_get_id_ (host));
103   GNUNET_TESTBED_host_destroy (host);
104   host = GNUNET_TESTBED_host_create (NULL, NULL, 0);
105   GNUNET_assert (NULL != host);
106   GNUNET_assert (0 == GNUNET_TESTBED_host_get_id_ (host));
107   GNUNET_assert (host == GNUNET_TESTBED_host_lookup_by_id_ (0));
108   helper_handle = GNUNET_TESTBED_host_run_ ("127.0.0.1", host, cfg, &exp_cb, NULL);
109   GNUNET_assert (NULL != helper_handle);
110   shutdown_id = 
111     GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (2), &do_shutdown, NULL);
112 }
113
114
115 int main (int argc, char **argv)
116 {
117   char *const argv2[] = { "test_testbed_api_hosts",
118                           "-c", "test_testbed_api.conf",
119                           NULL
120   };
121   struct GNUNET_GETOPT_CommandLineOption options[] = {
122     GNUNET_GETOPT_OPTION_END
123   };
124
125   status = GNUNET_YES;
126   if (GNUNET_OK != 
127       GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
128                           "test_testbed_api_hosts", "nohelp", 
129                           options, &run, NULL))
130     return 1;
131   return (GNUNET_OK == status) ? 0 : 1;
132 }