aboutsummaryrefslogtreecommitdiff
path: root/src/testing/gnunet-testing.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2012-06-19 07:36:21 +0000
committerChristian Grothoff <christian@grothoff.org>2012-06-19 07:36:21 +0000
commit35430e08f99302862e9569f0d8c4f8528f346ed2 (patch)
tree4bcbe1bc0379b175e1e3948d247bd3761bd8699b /src/testing/gnunet-testing.c
parent15cbe297d66550a646dec5176949f5425746aba7 (diff)
downloadgnunet-35430e08f99302862e9569f0d8c4f8528f346ed2.tar.gz
gnunet-35430e08f99302862e9569f0d8c4f8528f346ed2.zip
-porting gnunet-testing command-line tool to new testing library
Diffstat (limited to 'src/testing/gnunet-testing.c')
-rw-r--r--src/testing/gnunet-testing.c279
1 files changed, 279 insertions, 0 deletions
diff --git a/src/testing/gnunet-testing.c b/src/testing/gnunet-testing.c
new file mode 100644
index 000000000..b7e115761
--- /dev/null
+++ b/src/testing/gnunet-testing.c
@@ -0,0 +1,279 @@
1/*
2 This file is part of GNUnet.
3 (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19*/
20
21/**
22 * @file testing_old/gnunet-testing.c
23 * @brief tool to use testing functionality from cmd line
24 * @author Christian Grothoff
25 */
26#include "platform.h"
27#include "gnunet_util_lib.h"
28#include "gnunet_testing_lib-new.h"
29
30#define HOSTKEYFILESIZE 914
31
32/**
33 * Final status code.
34 */
35static int ret;
36
37static unsigned int create_hostkey;
38
39static unsigned int create_cfg;
40
41static int create_no;
42
43static char * create_cfg_template;
44
45static char * create_hostkey_file;
46
47
48static int
49create_unique_cfgs (const char * template, const unsigned int no)
50{
51 struct GNUNET_TESTING_System *system;
52 int fail;
53 unsigned int cur;
54 char *cur_file;
55 struct GNUNET_CONFIGURATION_Handle *cfg_new;
56 struct GNUNET_CONFIGURATION_Handle *cfg_tmpl;
57
58 if (GNUNET_NO == GNUNET_DISK_file_test(template))
59 {
60 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Configuration template `%s': file not found\n", create_cfg_template);
61 return 1;
62 }
63 cfg_tmpl = GNUNET_CONFIGURATION_create();
64
65 /* load template */
66 if ((create_cfg_template != NULL) && (GNUNET_OK != GNUNET_CONFIGURATION_load(cfg_tmpl, create_cfg_template)))
67 {
68 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not load template `%s'\n", create_cfg_template);
69 GNUNET_CONFIGURATION_destroy(cfg_tmpl);
70
71 return 1;
72 }
73 /* load defaults */
74 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg_tmpl, NULL))
75 {
76 GNUNET_break (0);
77 return 1;
78 }
79
80 fail = GNUNET_NO;
81 system = GNUNET_TESTING_system_create ("testing", NULL /* controller */);
82 for (cur = 0; cur < no; cur++)
83 {
84 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating configuration no. %u \n", cur);
85 if (create_cfg_template != NULL)
86 GNUNET_asprintf (&cur_file,"%04u-%s",cur, create_cfg_template);
87 else
88 GNUNET_asprintf (&cur_file,"%04u%s",cur, ".conf");
89
90 cfg_new = GNUNET_CONFIGURATION_dup (cfg_tmpl);
91 if (GNUNET_OK !=
92 GNUNET_TESTING_configuration_create (system, cfg_new))
93 {
94 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create another configuration\n");
95 GNUNET_CONFIGURATION_destroy (cfg_new);
96 fail = GNUNET_YES;
97 break;
98 }
99 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
100 "Writing configuration no. %u to file `%s' \n", cur, cur_file);
101 if (GNUNET_OK != GNUNET_CONFIGURATION_write(cfg_new, cur_file))
102 {
103 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to write configuration no. %u \n", cur);
104 fail = GNUNET_YES;
105 }
106 GNUNET_CONFIGURATION_destroy (cfg_new);
107 GNUNET_free (cur_file);
108 if (fail == GNUNET_YES)
109 break;
110 }
111 GNUNET_CONFIGURATION_destroy(cfg_tmpl);
112 GNUNET_TESTING_system_destroy (system, GNUNET_NO);
113 if (GNUNET_YES == fail)
114 return 1;
115 return 0;
116}
117
118
119static int
120create_hostkeys (const unsigned int no)
121{
122 struct GNUNET_DISK_FileHandle *fd;
123 int cur = 0;
124 uint64_t fs;
125 uint64_t total_hostkeys;
126 char *hostkey_data;
127 char *hostkey_src_file;
128 char *hostkey_dest_file;
129
130 /* prepare hostkeys */
131 if (create_hostkey_file == NULL)
132 hostkey_src_file = "../../contrib/testing_hostkeys.dat";
133 else
134 {
135 hostkey_src_file = create_hostkey_file;
136 }
137
138 if (GNUNET_YES != GNUNET_DISK_file_test (hostkey_src_file))
139 {
140 if (create_hostkey_file == NULL)
141 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not read hostkeys file, specify hostkey file with -H!\n"));
142 else
143 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Specified hostkey file `%s' not found!\n"), create_hostkey_file);
144 return 1;
145 }
146 else
147 {
148 /* Check hostkey file size, read entire thing into memory */
149 fd = GNUNET_DISK_file_open (hostkey_src_file, GNUNET_DISK_OPEN_READ,
150 GNUNET_DISK_PERM_NONE);
151 if (NULL == fd)
152 {
153 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", hostkey_src_file);
154 return 1;
155 }
156
157 if (GNUNET_OK != GNUNET_DISK_file_size (hostkey_src_file, &fs, GNUNET_YES, GNUNET_YES))
158 fs = 0;
159
160 if (0 != (fs % HOSTKEYFILESIZE))
161 {
162 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
163 "File size %llu seems incorrect for hostkeys...\n", fs);
164 }
165 else
166 {
167 total_hostkeys = fs / HOSTKEYFILESIZE;
168 hostkey_data = GNUNET_malloc_large (fs);
169 GNUNET_assert (fs == GNUNET_DISK_file_read (fd, hostkey_data, fs));
170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
171 "Read %llu hostkeys from file\n", total_hostkeys);
172 }
173 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
174 }
175
176 while (cur < no)
177 {
178 GNUNET_asprintf (&hostkey_dest_file, "%04u-hostkey",cur);
179 GNUNET_assert (GNUNET_OK ==
180 GNUNET_DISK_directory_create_for_file (hostkey_dest_file));
181 fd = GNUNET_DISK_file_open (hostkey_dest_file,
182 GNUNET_DISK_OPEN_READWRITE |
183 GNUNET_DISK_OPEN_CREATE,
184 GNUNET_DISK_PERM_USER_READ |
185 GNUNET_DISK_PERM_USER_WRITE);
186 GNUNET_assert (fd != NULL);
187 GNUNET_assert (HOSTKEYFILESIZE ==
188 GNUNET_DISK_file_write (fd, &hostkey_data[cur * HOSTKEYFILESIZE], HOSTKEYFILESIZE));
189 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
190 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
191 "Wrote hostkey to file: `%s' \n", hostkey_dest_file);
192 GNUNET_free (hostkey_dest_file);
193 cur ++;
194 }
195
196 GNUNET_free (hostkey_data);
197
198 return 0;
199}
200
201/**
202 * Main function that will be run by the scheduler.
203 *
204 * @param cls closure
205 * @param args remaining command-line arguments
206 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
207 * @param cfg configuration
208 */
209static void
210run (void *cls, char *const *args, const char *cfgfile,
211 const struct GNUNET_CONFIGURATION_Handle *cfg)
212{
213 /* main code here */
214 if (create_cfg == GNUNET_YES)
215 {
216 if (create_no > 0)
217 {
218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating %u configuration files based on template `%s'\n", create_no, create_cfg_template);
219 ret = create_unique_cfgs (create_cfg_template, create_no);
220 }
221 else
222 {
223 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Missing arguments! \n");
224 ret = 1;
225 }
226 }
227
228 if (create_hostkey == GNUNET_YES)
229 {
230 if (create_no > 0)
231 {
232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating %u hostkeys \n", create_no);
233 ret = create_hostkeys (create_no);
234 }
235 else
236 {
237 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Missing arguments! \n");
238 ret = 1;
239 }
240 }
241
242 GNUNET_free_non_null (create_cfg_template);
243}
244
245
246/**
247 * The main function.
248 *
249 * @param argc number of arguments from the command line
250 * @param argv command line arguments
251 * @return 0 ok, 1 on error
252 */
253int
254main (int argc, char *const *argv)
255{
256 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
257 {'C', "cfg", NULL, gettext_noop ("create unique configuration files"),
258 GNUNET_NO, &GNUNET_GETOPT_set_one, &create_cfg},
259 {'k', "key", NULL, gettext_noop ("create hostkey files from pre-computed hostkey list"),
260 GNUNET_NO, &GNUNET_GETOPT_set_one, &create_hostkey},
261 {'H', "hostkeys", NULL, gettext_noop ("host key file"),
262 GNUNET_YES, &GNUNET_GETOPT_set_string, &create_hostkey_file},
263 {'n', "number", NULL, gettext_noop ("number of unique configuration files or hostkeys to create"),
264 GNUNET_YES, &GNUNET_GETOPT_set_uint, &create_no},
265 {'t', "template", NULL, gettext_noop ("configuration template"),
266 GNUNET_YES, &GNUNET_GETOPT_set_string, &create_cfg_template},
267 GNUNET_GETOPT_OPTION_END
268 };
269
270 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
271 return 2;
272
273 return (GNUNET_OK ==
274 GNUNET_PROGRAM_run (argc, argv, "gnunet-testing",
275 gettext_noop ("Command line tool to access the testing library"), options, &run,
276 NULL)) ? ret : 1;
277}
278
279/* end of gnunet-testing.c */