small API change: do no longer pass rarely needed GNUNET_SCHEDULER_TaskContext to...
[oweals/gnunet.git] / src / testbed / test_testbed_api_hosts.c
1 /*
2       This file is part of GNUnet
3       Copyright (C) 2008--2013 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18       Boston, MA 02110-1301, 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 struct GNUNET_SCHEDULER_Task * shutdown_id;
65
66 /**
67  * The shutdown task
68  *
69  * @param cls NULL
70  */
71 static void
72 do_shutdown (void *cls)
73 {
74   GNUNET_TESTBED_host_destroy (host);
75   while (0 != num_hosts)
76   {
77     GNUNET_TESTBED_host_destroy (hosts[num_hosts - 1]);
78     num_hosts--;
79   }
80   GNUNET_free (hosts);
81   if (NULL != cfg)
82   {
83     GNUNET_CONFIGURATION_destroy (cfg);
84     cfg = NULL;
85   }
86 }
87
88
89 /**
90  * Main run function.
91  *
92  * @param cls NULL
93  * @param args arguments passed to GNUNET_PROGRAM_run
94  * @param cfgfile the path to configuration file
95  * @param cfg the configuration file handle
96  */
97 static void
98 run (void *cls, char *const *args, const char *cfgfile,
99      const struct GNUNET_CONFIGURATION_Handle *config)
100 {
101   unsigned int cnt;
102
103   cfg = GNUNET_CONFIGURATION_dup (config);
104   host = GNUNET_TESTBED_host_create ("localhost", NULL, cfg, 0);
105   GNUNET_assert (NULL != host);
106   GNUNET_assert (0 != GNUNET_TESTBED_host_get_id_ (host));
107   GNUNET_TESTBED_host_destroy (host);
108   host = GNUNET_TESTBED_host_create (NULL, NULL, cfg, 0);
109   GNUNET_assert (NULL != host);
110   GNUNET_assert (0 == GNUNET_TESTBED_host_get_id_ (host));
111   GNUNET_assert (host == GNUNET_TESTBED_host_lookup_by_id_ (0));
112   hosts = NULL;
113   num_hosts = GNUNET_TESTBED_hosts_load_from_file ("sample_hosts.txt", cfg, &hosts);
114   GNUNET_assert (7 == num_hosts);
115   GNUNET_assert (NULL != hosts);
116   for (cnt = 0; cnt < num_hosts; cnt++)
117   {
118     if (cnt < 3)
119     {
120       GNUNET_assert (0 == strcmp ("totakura",
121                                   GNUNET_TESTBED_host_get_username_
122                                   (hosts[cnt])));
123       GNUNET_assert (NULL != GNUNET_TESTBED_host_get_hostname (hosts[cnt]));
124       GNUNET_assert (22 == GNUNET_TESTBED_host_get_ssh_port_ (hosts[cnt]));
125     }
126     if (3 == cnt)
127     {
128       GNUNET_assert (0 == strcmp ("totakura",
129                                   GNUNET_TESTBED_host_get_username_
130                                   (hosts[cnt])));
131       GNUNET_assert (NULL != GNUNET_TESTBED_host_get_hostname (hosts[cnt]));
132       GNUNET_assert (2022 == GNUNET_TESTBED_host_get_ssh_port_ (hosts[cnt]));
133     }
134     if (4 == cnt)
135     {
136       GNUNET_assert (0 == strcmp ("totakura",
137                                   GNUNET_TESTBED_host_get_username_
138                                   (hosts[cnt])));
139       GNUNET_assert (0 == strcmp ("asgard.realm",
140                                   GNUNET_TESTBED_host_get_hostname
141                                   (hosts[cnt])));
142       GNUNET_assert (22 == GNUNET_TESTBED_host_get_ssh_port_ (hosts[cnt]));
143     }
144     if (5 == cnt)
145     {
146       GNUNET_assert (NULL == GNUNET_TESTBED_host_get_username_ (hosts[cnt]));
147       GNUNET_assert (0 == strcmp ("rivendal",
148                                   GNUNET_TESTBED_host_get_hostname
149                                   (hosts[cnt])));
150       GNUNET_assert (22 == GNUNET_TESTBED_host_get_ssh_port_ (hosts[cnt]));
151     }
152     if (6 == cnt)
153     {
154       GNUNET_assert (NULL == GNUNET_TESTBED_host_get_username_ (hosts[cnt]));
155       GNUNET_assert (0 == strcmp ("rohan",
156                                   GNUNET_TESTBED_host_get_hostname
157                                   (hosts[cnt])));
158       GNUNET_assert (561 == GNUNET_TESTBED_host_get_ssh_port_ (hosts[cnt]));
159     }
160   }
161   status = GNUNET_YES;
162   shutdown_id =
163       GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (0), &do_shutdown, NULL);
164 }
165
166
167 int
168 main (int argc, char **argv)
169 {
170   char *const argv2[] = { "test_testbed_api_hosts",
171     "-c", "test_testbed_api.conf",
172     NULL
173   };
174   struct GNUNET_GETOPT_CommandLineOption options[] = {
175     GNUNET_GETOPT_OPTION_END
176   };
177
178   status = GNUNET_SYSERR;
179   if (GNUNET_OK !=
180       GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
181                           "test_testbed_api_hosts", "nohelp", options, &run,
182                           NULL))
183     return 1;
184   return (GNUNET_OK == status) ? 0 : 1;
185 }
186
187 /* end of test_testbed_api_hosts.c */