2 This file is part of GNUnet.
3 (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file testing/gnunet-testing.c
23 * @brief tool to use testing functionality from cmd line
24 * @author Christian Grothoff
27 #include "gnunet_getopt_lib.h"
28 #include "gnunet_program_lib.h"
29 #include "gnunet_testing_lib.h"
31 #define HOSTKEYFILESIZE 914
38 static unsigned int create_hostkey;
40 static unsigned int create_cfg;
44 static char * create_cfg_template;
46 static char * create_hostkey_file;
49 create_unique_cfgs (const char * template, const unsigned int no)
53 uint16_t port = 20000;
57 if (GNUNET_NO == GNUNET_DISK_file_test(template))
59 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Configuration template `%s': file not found\n", create_cfg_template);
65 char *service_home = NULL;
66 char *cur_service_home = NULL;
68 struct GNUNET_CONFIGURATION_Handle *cfg_new = NULL;
69 struct GNUNET_CONFIGURATION_Handle *cfg_tmpl = GNUNET_CONFIGURATION_create();
72 if ((create_cfg_template != NULL) && (GNUNET_OK != GNUNET_CONFIGURATION_load(cfg_tmpl, create_cfg_template)))
74 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not load template `%s'\n", create_cfg_template);
75 GNUNET_CONFIGURATION_destroy(cfg_tmpl);
80 else if (GNUNET_OK != GNUNET_CONFIGURATION_load(cfg_tmpl, NULL))
86 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg_tmpl, "PATHS", "SERVICEHOME", &service_home))
88 GNUNET_asprintf(&service_home, "%s", "/tmp/testing");
92 int s = strlen (service_home);
93 if (service_home[s-1] == DIR_SEPARATOR)
94 service_home[s-1] = '\0';
99 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating configuration no. %u \n", cur);
100 if (create_cfg_template != NULL)
101 GNUNET_asprintf (&cur_file,"%04u-%s",cur, create_cfg_template);
103 GNUNET_asprintf (&cur_file,"%04u%s",cur, ".conf");
106 GNUNET_asprintf (&cur_service_home, "%s-%04u%c",service_home, cur, DIR_SEPARATOR);
107 GNUNET_CONFIGURATION_set_value_string (cfg_tmpl,"PATHS","SERVICEHOME", cur_service_home);
108 GNUNET_CONFIGURATION_set_value_string (cfg_tmpl,"PATHS","DEFAULTCONFIG", cur_file);
109 GNUNET_free (cur_service_home);
111 cfg_new = GNUNET_TESTING_create_cfg(cfg_tmpl, cur, &port, &upnum, NULL, &fdnum);
113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Writing configuration no. %u to file `%s' \n", cur, cur_file);
114 if (GNUNET_OK != GNUNET_CONFIGURATION_write(cfg_new, cur_file))
116 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to write configuration no. %u \n", cur);
120 GNUNET_CONFIGURATION_destroy (cfg_new);
121 GNUNET_free (cur_file);
122 if (fail == GNUNET_YES)
127 GNUNET_CONFIGURATION_destroy(cfg_tmpl);
128 GNUNET_free (service_home);
129 if (fail == GNUNET_NO)
136 create_hostkeys (const unsigned int no)
138 struct GNUNET_DISK_FileHandle *fd;
141 uint64_t total_hostkeys;
143 char *hostkey_src_file;
144 char *hostkey_dest_file;
146 /* prepare hostkeys */
147 if (create_hostkey_file == NULL)
148 hostkey_src_file = "../../contrib/testing_hostkeys.dat";
151 hostkey_src_file = create_hostkey_file;
154 if (GNUNET_YES != GNUNET_DISK_file_test (hostkey_src_file))
156 if (create_hostkey_file == NULL)
157 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not read hostkeys file, specify hostkey file with -H!\n"));
159 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Specified hostkey file `%s' not found!\n"), create_hostkey_file);
164 /* Check hostkey file size, read entire thing into memory */
165 fd = GNUNET_DISK_file_open (hostkey_src_file, GNUNET_DISK_OPEN_READ,
166 GNUNET_DISK_PERM_NONE);
169 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", hostkey_src_file);
173 if (GNUNET_OK != GNUNET_DISK_file_size (hostkey_src_file, &fs, GNUNET_YES, GNUNET_YES))
176 if (0 != (fs % HOSTKEYFILESIZE))
178 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
179 "File size %llu seems incorrect for hostkeys...\n", fs);
183 total_hostkeys = fs / HOSTKEYFILESIZE;
184 hostkey_data = GNUNET_malloc_large (fs);
185 GNUNET_assert (fs == GNUNET_DISK_file_read (fd, hostkey_data, fs));
186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
187 "Read %llu hostkeys from file\n", total_hostkeys);
189 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
194 GNUNET_asprintf (&hostkey_dest_file, "%04u-hostkey",cur);
195 GNUNET_assert (GNUNET_OK ==
196 GNUNET_DISK_directory_create_for_file (hostkey_dest_file));
197 fd = GNUNET_DISK_file_open (hostkey_dest_file,
198 GNUNET_DISK_OPEN_READWRITE |
199 GNUNET_DISK_OPEN_CREATE,
200 GNUNET_DISK_PERM_USER_READ |
201 GNUNET_DISK_PERM_USER_WRITE);
202 GNUNET_assert (fd != NULL);
203 GNUNET_assert (HOSTKEYFILESIZE ==
204 GNUNET_DISK_file_write (fd, &hostkey_data[cur * HOSTKEYFILESIZE], HOSTKEYFILESIZE));
205 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
206 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
207 "Wrote hostkey to file: `%s' \n", hostkey_dest_file);
208 GNUNET_free (hostkey_dest_file);
212 GNUNET_free (hostkey_data);
218 * Main function that will be run by the scheduler.
221 * @param args remaining command-line arguments
222 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
223 * @param cfg configuration
226 run (void *cls, char *const *args, const char *cfgfile,
227 const struct GNUNET_CONFIGURATION_Handle *cfg)
230 if (create_cfg == GNUNET_YES)
234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating %u configuration files based on template `%s'\n", create_no, create_cfg_template);
235 ret = create_unique_cfgs (create_cfg_template, create_no);
239 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Missing arguments! \n");
244 if (create_hostkey == GNUNET_YES)
248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating %u hostkeys \n", create_no);
249 ret = create_hostkeys (create_no);
253 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Missing arguments! \n");
258 GNUNET_free_non_null (create_cfg_template);
265 * @param argc number of arguments from the command line
266 * @param argv command line arguments
267 * @return 0 ok, 1 on error
270 main (int argc, char *const *argv)
272 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
273 {'C', "cfg", NULL, gettext_noop ("create unique configuration files"),
274 GNUNET_NO, &GNUNET_GETOPT_set_one, &create_cfg},
275 {'k', "key", NULL, gettext_noop ("create hostkey files from pre-computed hostkey list"),
276 GNUNET_NO, &GNUNET_GETOPT_set_one, &create_hostkey},
277 {'H', "hostkeys", NULL, gettext_noop ("host key file"),
278 GNUNET_YES, &GNUNET_GETOPT_set_string, &create_hostkey_file},
279 {'n', "number", NULL, gettext_noop ("number of unique configuration files or hostkeys to create"),
280 GNUNET_YES, &GNUNET_GETOPT_set_uint, &create_no},
281 {'t', "template", NULL, gettext_noop ("configuration template"),
282 GNUNET_YES, &GNUNET_GETOPT_set_string, &create_cfg_template},
283 GNUNET_GETOPT_OPTION_END
286 GNUNET_PROGRAM_run (argc, argv, "gnunet-testing",
287 gettext_noop ("Command line tool to access the testing library"), options, &run,
291 /* end of gnunet-testing.c */