55933950a6832f4860e696d9b9935be1a08a6867
[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 it
6       under the terms of the GNU Affero General Public License as published
7       by the Free Software Foundation, either version 3 of the License,
8       or (at your 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       Affero General Public License for more details.
14      
15       You should have received a copy of the GNU Affero General Public License
16       along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 /**
20  * @file testbed/test_testbed_api_hosts.c
21  * @brief tests cases for testbed_api_hosts.c
22  * @author Sree Harsha Totakura
23  */
24
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_testbed_service.h"
28 #include "testbed_api_hosts.h"
29
30
31 #define TIME_REL_SECS(sec)                                              \
32   GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
33
34 /**
35  * configuration handle to use as template configuration while creating hosts
36  */
37 static struct GNUNET_CONFIGURATION_Handle *cfg;
38
39 /**
40  * Host we are creating and using
41  */
42 static struct GNUNET_TESTBED_Host *host;
43
44 /**
45  * An array of hosts which are loaded from a file
46  */
47 static struct GNUNET_TESTBED_Host **hosts;
48
49 /**
50  * Number of hosts in the above list
51  */
52 static unsigned int num_hosts;
53
54 /**
55  * Global test status
56  */
57 static int status;
58
59
60 /**
61  * The shutdown task
62  *
63  * @param cls NULL
64  */
65 static void
66 do_shutdown (void *cls)
67 {
68   GNUNET_TESTBED_host_destroy (host);
69   while (0 != num_hosts)
70   {
71     GNUNET_TESTBED_host_destroy (hosts[num_hosts - 1]);
72     num_hosts--;
73   }
74   GNUNET_free (hosts);
75   if (NULL != cfg)
76   {
77     GNUNET_CONFIGURATION_destroy (cfg);
78     cfg = NULL;
79   }
80 }
81
82
83 /**
84  * Main run function.
85  *
86  * @param cls NULL
87  * @param args arguments passed to GNUNET_PROGRAM_run
88  * @param cfgfile the path to configuration file
89  * @param cfg the configuration file handle
90  */
91 static void
92 run (void *cls, char *const *args, const char *cfgfile,
93      const struct GNUNET_CONFIGURATION_Handle *config)
94 {
95   unsigned int cnt;
96
97   cfg = GNUNET_CONFIGURATION_dup (config);
98   host = GNUNET_TESTBED_host_create ("localhost", NULL, cfg, 0);
99   GNUNET_assert (NULL != host);
100   GNUNET_assert (0 != GNUNET_TESTBED_host_get_id_ (host));
101   GNUNET_TESTBED_host_destroy (host);
102   host = GNUNET_TESTBED_host_create (NULL, NULL, cfg, 0);
103   GNUNET_assert (NULL != host);
104   GNUNET_assert (0 == GNUNET_TESTBED_host_get_id_ (host));
105   GNUNET_assert (host == GNUNET_TESTBED_host_lookup_by_id_ (0));
106   hosts = NULL;
107   num_hosts = GNUNET_TESTBED_hosts_load_from_file ("sample_hosts.txt", cfg, &hosts);
108   GNUNET_assert (7 == num_hosts);
109   GNUNET_assert (NULL != hosts);
110   for (cnt = 0; cnt < num_hosts; cnt++)
111   {
112     if (cnt < 3)
113     {
114       GNUNET_assert (0 == strcmp ("totakura",
115                                   GNUNET_TESTBED_host_get_username_
116                                   (hosts[cnt])));
117       GNUNET_assert (NULL != GNUNET_TESTBED_host_get_hostname (hosts[cnt]));
118       GNUNET_assert (22 == GNUNET_TESTBED_host_get_ssh_port_ (hosts[cnt]));
119     }
120     if (3 == cnt)
121     {
122       GNUNET_assert (0 == strcmp ("totakura",
123                                   GNUNET_TESTBED_host_get_username_
124                                   (hosts[cnt])));
125       GNUNET_assert (NULL != GNUNET_TESTBED_host_get_hostname (hosts[cnt]));
126       GNUNET_assert (2022 == GNUNET_TESTBED_host_get_ssh_port_ (hosts[cnt]));
127     }
128     if (4 == cnt)
129     {
130       GNUNET_assert (0 == strcmp ("totakura",
131                                   GNUNET_TESTBED_host_get_username_
132                                   (hosts[cnt])));
133       GNUNET_assert (0 == strcmp ("asgard.realm",
134                                   GNUNET_TESTBED_host_get_hostname
135                                   (hosts[cnt])));
136       GNUNET_assert (22 == GNUNET_TESTBED_host_get_ssh_port_ (hosts[cnt]));
137     }
138     if (5 == cnt)
139     {
140       GNUNET_assert (NULL == GNUNET_TESTBED_host_get_username_ (hosts[cnt]));
141       GNUNET_assert (0 == strcmp ("rivendal",
142                                   GNUNET_TESTBED_host_get_hostname
143                                   (hosts[cnt])));
144       GNUNET_assert (22 == GNUNET_TESTBED_host_get_ssh_port_ (hosts[cnt]));
145     }
146     if (6 == cnt)
147     {
148       GNUNET_assert (NULL == GNUNET_TESTBED_host_get_username_ (hosts[cnt]));
149       GNUNET_assert (0 == strcmp ("rohan",
150                                   GNUNET_TESTBED_host_get_hostname
151                                   (hosts[cnt])));
152       GNUNET_assert (561 == GNUNET_TESTBED_host_get_ssh_port_ (hosts[cnt]));
153     }
154   }
155   status = GNUNET_YES;
156   GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
157 }
158
159
160 int
161 main (int argc, char **argv)
162 {
163   char *const argv2[] = { "test_testbed_api_hosts",
164     "-c", "test_testbed_api.conf",
165     NULL
166   };
167   struct GNUNET_GETOPT_CommandLineOption options[] = {
168     GNUNET_GETOPT_OPTION_END
169   };
170
171   status = GNUNET_SYSERR;
172   if (GNUNET_OK !=
173       GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
174                           "test_testbed_api_hosts", "nohelp", options, &run,
175                           NULL))
176     return 1;
177   return (GNUNET_OK == status) ? 0 : 1;
178 }
179
180 /* end of test_testbed_api_hosts.c */