add $(GN_LIBINTL) to Makefile.am (fixes 0005902)
[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      SPDX-License-Identifier: AGPL3.0-or-later
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 /**
63  * The shutdown task
64  *
65  * @param cls NULL
66  */
67 static void
68 do_shutdown (void *cls)
69 {
70   GNUNET_TESTBED_host_destroy (host);
71   while (0 != num_hosts)
72   {
73     GNUNET_TESTBED_host_destroy (hosts[num_hosts - 1]);
74     num_hosts--;
75   }
76   GNUNET_free (hosts);
77   if (NULL != cfg)
78   {
79     GNUNET_CONFIGURATION_destroy (cfg);
80     cfg = NULL;
81   }
82 }
83
84
85 /**
86  * Main run function.
87  *
88  * @param cls NULL
89  * @param args arguments passed to GNUNET_PROGRAM_run
90  * @param cfgfile the path to configuration file
91  * @param cfg the configuration file handle
92  */
93 static void
94 run (void *cls, char *const *args, const char *cfgfile,
95      const struct GNUNET_CONFIGURATION_Handle *config)
96 {
97   unsigned int cnt;
98
99   cfg = GNUNET_CONFIGURATION_dup (config);
100   host = GNUNET_TESTBED_host_create ("localhost", NULL, cfg, 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, cfg, 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   hosts = NULL;
109   num_hosts = GNUNET_TESTBED_hosts_load_from_file ("sample_hosts.txt", cfg,
110                                                    &hosts);
111   GNUNET_assert (7 == num_hosts);
112   GNUNET_assert (NULL != hosts);
113   for (cnt = 0; cnt < num_hosts; cnt++)
114   {
115     if (cnt < 3)
116     {
117       GNUNET_assert (0 == strcmp ("totakura",
118                                   GNUNET_TESTBED_host_get_username_
119                                     (hosts[cnt])));
120       GNUNET_assert (NULL != GNUNET_TESTBED_host_get_hostname (hosts[cnt]));
121       GNUNET_assert (22 == GNUNET_TESTBED_host_get_ssh_port_ (hosts[cnt]));
122     }
123     if (3 == cnt)
124     {
125       GNUNET_assert (0 == strcmp ("totakura",
126                                   GNUNET_TESTBED_host_get_username_
127                                     (hosts[cnt])));
128       GNUNET_assert (NULL != GNUNET_TESTBED_host_get_hostname (hosts[cnt]));
129       GNUNET_assert (2022 == GNUNET_TESTBED_host_get_ssh_port_ (hosts[cnt]));
130     }
131     if (4 == cnt)
132     {
133       GNUNET_assert (0 == strcmp ("totakura",
134                                   GNUNET_TESTBED_host_get_username_
135                                     (hosts[cnt])));
136       GNUNET_assert (0 == strcmp ("asgard.realm",
137                                   GNUNET_TESTBED_host_get_hostname
138                                     (hosts[cnt])));
139       GNUNET_assert (22 == GNUNET_TESTBED_host_get_ssh_port_ (hosts[cnt]));
140     }
141     if (5 == cnt)
142     {
143       GNUNET_assert (NULL == GNUNET_TESTBED_host_get_username_ (hosts[cnt]));
144       GNUNET_assert (0 == strcmp ("rivendal",
145                                   GNUNET_TESTBED_host_get_hostname
146                                     (hosts[cnt])));
147       GNUNET_assert (22 == GNUNET_TESTBED_host_get_ssh_port_ (hosts[cnt]));
148     }
149     if (6 == cnt)
150     {
151       GNUNET_assert (NULL == GNUNET_TESTBED_host_get_username_ (hosts[cnt]));
152       GNUNET_assert (0 == strcmp ("rohan",
153                                   GNUNET_TESTBED_host_get_hostname
154                                     (hosts[cnt])));
155       GNUNET_assert (561 == GNUNET_TESTBED_host_get_ssh_port_ (hosts[cnt]));
156     }
157   }
158   status = GNUNET_YES;
159   GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
160 }
161
162
163 int
164 main (int argc, char **argv)
165 {
166   char *const argv2[] = { "test_testbed_api_hosts",
167                           "-c", "test_testbed_api.conf",
168                           NULL };
169   struct GNUNET_GETOPT_CommandLineOption options[] = {
170     GNUNET_GETOPT_OPTION_END
171   };
172
173   status = GNUNET_SYSERR;
174   if (GNUNET_OK !=
175       GNUNET_PROGRAM_run ((sizeof(argv2) / sizeof(char *)) - 1, argv2,
176                           "test_testbed_api_hosts", "nohelp", options, &run,
177                           NULL))
178     return 1;
179   return (GNUNET_OK == status) ? 0 : 1;
180 }
181
182
183 /* end of test_testbed_api_hosts.c */