- docs
[oweals/gnunet.git] / src / testbed / test_testbed_api_hosts.c
1 /*
2       This file is part of GNUnet
3       (C) 2008--2013 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  * configuration handle to use as template configuration while creating hosts
38  */
39 static struct GNUNET_CONFIGURATION_Handle *cfg;
40
41 /**
42  * Host we are creating and using
43  */
44 static struct GNUNET_TESTBED_Host *host;
45
46 /**
47  * An array of hosts which are loaded from a file
48  */
49 static struct GNUNET_TESTBED_Host **hosts;
50
51 /**
52  * Number of hosts in the above list
53  */
54 static unsigned int num_hosts;
55
56 /**
57  * Global test status
58  */
59 static int status;
60
61 /**
62  * Shutdown task identifier
63  */
64 GNUNET_SCHEDULER_TaskIdentifier shutdown_id;
65
66 /**
67  * The shutdown task
68  *
69  * @param cls NULL
70  * @param tc the task context
71  */
72 static void
73 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
74 {
75   GNUNET_TESTBED_host_destroy (host);
76   while (0 != num_hosts)
77   {
78     GNUNET_TESTBED_host_destroy (hosts[num_hosts - 1]);
79     num_hosts--;
80   }
81   GNUNET_free (hosts);
82   if (NULL != cfg)
83   {
84     GNUNET_CONFIGURATION_destroy (cfg);
85     cfg = NULL;
86   }
87 }
88
89
90 /**
91  * Main run function.
92  *
93  * @param cls NULL
94  * @param args arguments passed to GNUNET_PROGRAM_run
95  * @param cfgfile the path to configuration file
96  * @param cfg the configuration file handle
97  */
98 static void
99 run (void *cls, char *const *args, const char *cfgfile,
100      const struct GNUNET_CONFIGURATION_Handle *config)
101 {
102   cfg = GNUNET_CONFIGURATION_dup (config);
103   host = GNUNET_TESTBED_host_create ("localhost", NULL, cfg, 0);
104   GNUNET_assert (NULL != host);
105   GNUNET_assert (0 != GNUNET_TESTBED_host_get_id_ (host));
106   GNUNET_TESTBED_host_destroy (host);
107   host = GNUNET_TESTBED_host_create (NULL, NULL, cfg, 0);
108   GNUNET_assert (NULL != host);
109   GNUNET_assert (0 == GNUNET_TESTBED_host_get_id_ (host));
110   GNUNET_assert (host == GNUNET_TESTBED_host_lookup_by_id_ (0));
111   hosts = NULL;
112   num_hosts = GNUNET_TESTBED_hosts_load_from_file ("sample_hosts.txt", cfg, &hosts);
113   GNUNET_assert (17 == num_hosts);
114   GNUNET_assert (NULL != hosts);
115   status = GNUNET_YES;
116   shutdown_id =
117       GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (0), &do_shutdown, NULL);
118 }
119
120
121 int
122 main (int argc, char **argv)
123 {
124   char *const argv2[] = { "test_testbed_api_hosts",
125     "-c", "test_testbed_api.conf",
126     NULL
127   };
128   struct GNUNET_GETOPT_CommandLineOption options[] = {
129     GNUNET_GETOPT_OPTION_END
130   };
131
132   status = GNUNET_SYSERR;
133   if (GNUNET_OK !=
134       GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
135                           "test_testbed_api_hosts", "nohelp", options, &run,
136                           NULL))
137     return 1;
138   return (GNUNET_OK == status) ? 0 : 1;
139 }
140
141 /* end of test_testbed_api_hosts.c */