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_util_lib.h"
28 #include "gnunet_testing_lib.h"
36 static char *create_hostkey;
38 static int create_cfg;
40 static unsigned int create_no;
42 static char *create_cfg_template;
46 create_unique_cfgs (const char * template, const unsigned int no)
48 struct GNUNET_TESTING_System *system;
52 struct GNUNET_CONFIGURATION_Handle *cfg_new;
53 struct GNUNET_CONFIGURATION_Handle *cfg_tmpl;
55 if (GNUNET_NO == GNUNET_DISK_file_test(template))
57 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Configuration template `%s': file not found\n", create_cfg_template);
60 cfg_tmpl = GNUNET_CONFIGURATION_create();
63 if ((create_cfg_template != NULL) && (GNUNET_OK != GNUNET_CONFIGURATION_load(cfg_tmpl, create_cfg_template)))
65 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not load template `%s'\n", create_cfg_template);
66 GNUNET_CONFIGURATION_destroy (cfg_tmpl);
71 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg_tmpl, NULL))
73 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not load template `%s'\n", create_cfg_template);
74 GNUNET_CONFIGURATION_destroy (cfg_tmpl);
79 system = GNUNET_TESTING_system_create ("testing", NULL /* controller */,
81 for (cur = 0; cur < no; cur++)
83 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating configuration no. %u \n", cur);
84 if (create_cfg_template != NULL)
85 GNUNET_asprintf (&cur_file,"%04u-%s",cur, create_cfg_template);
87 GNUNET_asprintf (&cur_file,"%04u%s",cur, ".conf");
89 cfg_new = GNUNET_CONFIGURATION_dup (cfg_tmpl);
91 GNUNET_TESTING_configuration_create (system, cfg_new))
93 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create another configuration\n");
94 GNUNET_CONFIGURATION_destroy (cfg_new);
98 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
99 "Writing configuration no. %u to file `%s' \n", cur, cur_file);
100 if (GNUNET_OK != GNUNET_CONFIGURATION_write(cfg_new, cur_file))
102 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to write configuration no. %u \n", cur);
105 GNUNET_CONFIGURATION_destroy (cfg_new);
106 GNUNET_free (cur_file);
107 if (GNUNET_YES == fail)
110 GNUNET_CONFIGURATION_destroy(cfg_tmpl);
111 GNUNET_TESTING_system_destroy (system, GNUNET_NO);
112 if (GNUNET_YES == fail)
119 create_hostkeys (const unsigned int no)
121 static char pad[GNUNET_TESTING_HOSTKEYFILESIZE];
122 struct GNUNET_TESTING_System *system;
123 struct GNUNET_PeerIdentity id;
124 struct GNUNET_DISK_FileHandle *fd;
125 struct GNUNET_CRYPTO_EccPrivateKey *pk;
126 struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded *pkb;
129 system = GNUNET_TESTING_system_create ("testing", NULL, NULL, NULL);
130 pk = GNUNET_TESTING_hostkey_get (system, create_no, &id);
133 fprintf (stderr, _("Could not extract hostkey %u (offset too large?)\n"), create_no);
134 GNUNET_TESTING_system_destroy (system, GNUNET_YES);
137 (void) GNUNET_DISK_directory_create_for_file (create_hostkey);
138 fd = GNUNET_DISK_file_open (create_hostkey,
139 GNUNET_DISK_OPEN_READWRITE |
140 GNUNET_DISK_OPEN_CREATE,
141 GNUNET_DISK_PERM_USER_READ |
142 GNUNET_DISK_PERM_USER_WRITE);
143 GNUNET_assert (fd != NULL);
144 pkb = GNUNET_CRYPTO_ecc_encode_key (pk);
145 ret = GNUNET_DISK_file_write (fd, pkb,
147 GNUNET_assert (ntohs (pkb->size) == ret);
148 GNUNET_assert (ntohs (pkb->size) < GNUNET_TESTING_HOSTKEYFILESIZE);
149 GNUNET_assert (GNUNET_TESTING_HOSTKEYFILESIZE - ret ==
150 GNUNET_DISK_file_write (fd, pad,
151 GNUNET_TESTING_HOSTKEYFILESIZE - ret));
153 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
154 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
155 "Wrote hostkey to file: `%s'\n", create_hostkey);
157 GNUNET_CRYPTO_ecc_key_free (pk);
158 GNUNET_TESTING_system_destroy (system, GNUNET_YES);
164 * Main function that will be run by the scheduler.
167 * @param args remaining command-line arguments
168 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
169 * @param cfg configuration
172 run (void *cls, char *const *args, const char *cfgfile,
173 const struct GNUNET_CONFIGURATION_Handle *cfg)
176 if (GNUNET_YES == create_cfg)
180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
181 "Creating %u configuration files based on template `%s'\n", create_no, create_cfg_template);
182 ret = create_unique_cfgs (create_cfg_template, create_no);
186 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Missing arguments! \n");
190 if (NULL != create_hostkey)
192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Extracting hostkey %u\n", create_no);
193 ret = create_hostkeys (create_no);
195 GNUNET_free_non_null (create_cfg_template);
202 * @param argc number of arguments from the command line
203 * @param argv command line arguments
204 * @return 0 ok, 1 on error
207 main (int argc, char *const *argv)
209 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
210 {'C', "cfg", NULL, gettext_noop ("create unique configuration files"),
211 GNUNET_NO, &GNUNET_GETOPT_set_one, &create_cfg},
212 {'k', "key", "FILENAME", gettext_noop ("extract hostkey file from pre-computed hostkey list"),
213 GNUNET_YES, &GNUNET_GETOPT_set_string, &create_hostkey},
214 {'n', "number", "NUMBER", gettext_noop ("number of unique configuration files to create, or number of the hostkey to extract"),
215 GNUNET_YES, &GNUNET_GETOPT_set_uint, &create_no},
216 {'t', "template", "FILENAME", gettext_noop ("configuration template"),
217 GNUNET_YES, &GNUNET_GETOPT_set_string, &create_cfg_template},
218 GNUNET_GETOPT_OPTION_END
220 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
224 GNUNET_PROGRAM_run (argc, argv, "gnunet-testing",
225 gettext_noop ("Command line tool to access the testing library"), options, &run,
227 GNUNET_free ((void*) argv);
231 /* end of gnunet-testing.c */