diff options
Diffstat (limited to 'src/testing/gnunet-testing.c')
-rw-r--r-- | src/testing/gnunet-testing.c | 212 |
1 files changed, 127 insertions, 85 deletions
diff --git a/src/testing/gnunet-testing.c b/src/testing/gnunet-testing.c index ec611ba4c..bc402227c 100644 --- a/src/testing/gnunet-testing.c +++ b/src/testing/gnunet-testing.c | |||
@@ -28,8 +28,7 @@ | |||
28 | #include "gnunet_testing_lib.h" | 28 | #include "gnunet_testing_lib.h" |
29 | 29 | ||
30 | 30 | ||
31 | #define LOG(kind,...) \ | 31 | #define LOG(kind, ...) GNUNET_log_from (kind, "gnunet-testing", __VA_ARGS__) |
32 | GNUNET_log_from (kind, "gnunet-testing", __VA_ARGS__) | ||
33 | 32 | ||
34 | 33 | ||
35 | /** | 34 | /** |
@@ -84,9 +83,8 @@ static struct GNUNET_SCHEDULER_Task *tid; | |||
84 | static struct GNUNET_TESTING_Peer *my_peer; | 83 | static struct GNUNET_TESTING_Peer *my_peer; |
85 | 84 | ||
86 | 85 | ||
87 | |||
88 | static int | 86 | static int |
89 | create_unique_cfgs (const char * template, const unsigned int no) | 87 | create_unique_cfgs (const char *template, const unsigned int no) |
90 | { | 88 | { |
91 | struct GNUNET_TESTING_System *system; | 89 | struct GNUNET_TESTING_System *system; |
92 | int fail; | 90 | int fail; |
@@ -95,54 +93,67 @@ create_unique_cfgs (const char * template, const unsigned int no) | |||
95 | struct GNUNET_CONFIGURATION_Handle *cfg_new; | 93 | struct GNUNET_CONFIGURATION_Handle *cfg_new; |
96 | struct GNUNET_CONFIGURATION_Handle *cfg_tmpl; | 94 | struct GNUNET_CONFIGURATION_Handle *cfg_tmpl; |
97 | 95 | ||
98 | if (GNUNET_NO == GNUNET_DISK_file_test(template)) | 96 | if (GNUNET_NO == GNUNET_DISK_file_test (template)) |
99 | { | 97 | { |
100 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Configuration template `%s': file not found\n", create_cfg_template); | 98 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
99 | "Configuration template `%s': file not found\n", | ||
100 | create_cfg_template); | ||
101 | return 1; | 101 | return 1; |
102 | } | 102 | } |
103 | cfg_tmpl = GNUNET_CONFIGURATION_create(); | 103 | cfg_tmpl = GNUNET_CONFIGURATION_create (); |
104 | 104 | ||
105 | /* load template */ | 105 | /* load template */ |
106 | if ((create_cfg_template != NULL) && (GNUNET_OK != GNUNET_CONFIGURATION_load(cfg_tmpl, create_cfg_template))) | 106 | if ((create_cfg_template != NULL) && |
107 | (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg_tmpl, create_cfg_template))) | ||
107 | { | 108 | { |
108 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not load template `%s'\n", create_cfg_template); | 109 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
110 | "Could not load template `%s'\n", | ||
111 | create_cfg_template); | ||
109 | GNUNET_CONFIGURATION_destroy (cfg_tmpl); | 112 | GNUNET_CONFIGURATION_destroy (cfg_tmpl); |
110 | 113 | ||
111 | return 1; | 114 | return 1; |
112 | } | 115 | } |
113 | /* load defaults */ | 116 | /* load defaults */ |
114 | if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg_tmpl, NULL)) | 117 | if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg_tmpl, NULL)) |
115 | { | 118 | { |
116 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not load template `%s'\n", create_cfg_template); | 119 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
120 | "Could not load template `%s'\n", | ||
121 | create_cfg_template); | ||
117 | GNUNET_CONFIGURATION_destroy (cfg_tmpl); | 122 | GNUNET_CONFIGURATION_destroy (cfg_tmpl); |
118 | return 1; | 123 | return 1; |
119 | } | 124 | } |
120 | 125 | ||
121 | fail = GNUNET_NO; | 126 | fail = GNUNET_NO; |
122 | system = GNUNET_TESTING_system_create ("testing", NULL /* controller */, | 127 | system = |
123 | NULL, NULL); | 128 | GNUNET_TESTING_system_create ("testing", NULL /* controller */, NULL, NULL); |
124 | for (cur = 0; cur < no; cur++) | 129 | for (cur = 0; cur < no; cur++) |
125 | { | 130 | { |
126 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating configuration no. %u \n", cur); | 131 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
132 | "Creating configuration no. %u \n", | ||
133 | cur); | ||
127 | if (create_cfg_template != NULL) | 134 | if (create_cfg_template != NULL) |
128 | GNUNET_asprintf (&cur_file,"%04u-%s",cur, create_cfg_template); | 135 | GNUNET_asprintf (&cur_file, "%04u-%s", cur, create_cfg_template); |
129 | else | 136 | else |
130 | GNUNET_asprintf (&cur_file,"%04u%s",cur, ".conf"); | 137 | GNUNET_asprintf (&cur_file, "%04u%s", cur, ".conf"); |
131 | 138 | ||
132 | cfg_new = GNUNET_CONFIGURATION_dup (cfg_tmpl); | 139 | cfg_new = GNUNET_CONFIGURATION_dup (cfg_tmpl); |
133 | if (GNUNET_OK != | 140 | if (GNUNET_OK != GNUNET_TESTING_configuration_create (system, cfg_new)) |
134 | GNUNET_TESTING_configuration_create (system, cfg_new)) | ||
135 | { | 141 | { |
136 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create another configuration\n"); | 142 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
143 | "Could not create another configuration\n"); | ||
137 | GNUNET_CONFIGURATION_destroy (cfg_new); | 144 | GNUNET_CONFIGURATION_destroy (cfg_new); |
138 | fail = GNUNET_YES; | 145 | fail = GNUNET_YES; |
139 | break; | 146 | break; |
140 | } | 147 | } |
141 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 148 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
142 | "Writing configuration no. %u to file `%s' \n", cur, cur_file); | 149 | "Writing configuration no. %u to file `%s' \n", |
143 | if (GNUNET_OK != GNUNET_CONFIGURATION_write(cfg_new, cur_file)) | 150 | cur, |
151 | cur_file); | ||
152 | if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg_new, cur_file)) | ||
144 | { | 153 | { |
145 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to write configuration no. %u \n", cur); | 154 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
155 | "Failed to write configuration no. %u \n", | ||
156 | cur); | ||
146 | fail = GNUNET_YES; | 157 | fail = GNUNET_YES; |
147 | } | 158 | } |
148 | GNUNET_CONFIGURATION_destroy (cfg_new); | 159 | GNUNET_CONFIGURATION_destroy (cfg_new); |
@@ -150,7 +161,7 @@ create_unique_cfgs (const char * template, const unsigned int no) | |||
150 | if (GNUNET_YES == fail) | 161 | if (GNUNET_YES == fail) |
151 | break; | 162 | break; |
152 | } | 163 | } |
153 | GNUNET_CONFIGURATION_destroy(cfg_tmpl); | 164 | GNUNET_CONFIGURATION_destroy (cfg_tmpl); |
154 | GNUNET_TESTING_system_destroy (system, GNUNET_NO); | 165 | GNUNET_TESTING_system_destroy (system, GNUNET_NO); |
155 | if (GNUNET_YES == fail) | 166 | if (GNUNET_YES == fail) |
156 | return 1; | 167 | return 1; |
@@ -170,22 +181,27 @@ create_hostkeys (const unsigned int no) | |||
170 | pk = GNUNET_TESTING_hostkey_get (system, create_no, &id); | 181 | pk = GNUNET_TESTING_hostkey_get (system, create_no, &id); |
171 | if (NULL == pk) | 182 | if (NULL == pk) |
172 | { | 183 | { |
173 | fprintf (stderr, _("Could not extract hostkey %u (offset too large?)\n"), create_no); | 184 | fprintf (stderr, |
185 | _ ("Could not extract hostkey %u (offset too large?)\n"), | ||
186 | create_no); | ||
174 | GNUNET_TESTING_system_destroy (system, GNUNET_YES); | 187 | GNUNET_TESTING_system_destroy (system, GNUNET_YES); |
175 | return 1; | 188 | return 1; |
176 | } | 189 | } |
177 | (void) GNUNET_DISK_directory_create_for_file (create_hostkey); | 190 | (void) GNUNET_DISK_directory_create_for_file (create_hostkey); |
178 | fd = GNUNET_DISK_file_open (create_hostkey, | 191 | fd = |
179 | GNUNET_DISK_OPEN_READWRITE | | 192 | GNUNET_DISK_file_open (create_hostkey, |
180 | GNUNET_DISK_OPEN_CREATE, | 193 | GNUNET_DISK_OPEN_READWRITE | GNUNET_DISK_OPEN_CREATE, |
181 | GNUNET_DISK_PERM_USER_READ | | 194 | GNUNET_DISK_PERM_USER_READ | |
182 | GNUNET_DISK_PERM_USER_WRITE); | 195 | GNUNET_DISK_PERM_USER_WRITE); |
183 | GNUNET_assert (fd != NULL); | 196 | GNUNET_assert (fd != NULL); |
184 | ret = GNUNET_DISK_file_write (fd, pk, | 197 | ret = GNUNET_DISK_file_write (fd, |
185 | sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey)); | 198 | pk, |
199 | sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey)); | ||
186 | GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd)); | 200 | GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd)); |
187 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing", | 201 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, |
188 | "Wrote hostkey to file: `%s'\n", create_hostkey); | 202 | "transport-testing", |
203 | "Wrote hostkey to file: `%s'\n", | ||
204 | create_hostkey); | ||
189 | GNUNET_free (pk); | 205 | GNUNET_free (pk); |
190 | GNUNET_TESTING_system_destroy (system, GNUNET_YES); | 206 | GNUNET_TESTING_system_destroy (system, GNUNET_YES); |
191 | return 0; | 207 | return 0; |
@@ -203,8 +219,10 @@ cleanup (void *cls) | |||
203 | { | 219 | { |
204 | if (NULL != tmpfilename) | 220 | if (NULL != tmpfilename) |
205 | { | 221 | { |
206 | if (0 != UNLINK (tmpfilename)) | 222 | if (0 != unlink (tmpfilename)) |
207 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", tmpfilename); | 223 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, |
224 | "unlink", | ||
225 | tmpfilename); | ||
208 | } | 226 | } |
209 | if (NULL != tid) | 227 | if (NULL != tid) |
210 | { | 228 | { |
@@ -250,12 +268,14 @@ stdin_cb (void *cls) | |||
250 | /* ignore whitespace */ | 268 | /* ignore whitespace */ |
251 | break; | 269 | break; |
252 | default: | 270 | default: |
253 | fprintf (stderr, _("Unknown command, use 'q' to quit or 'r' to restart peer\n")); | 271 | fprintf (stderr, |
272 | _ ("Unknown command, use 'q' to quit or 'r' to restart peer\n")); | ||
254 | break; | 273 | break; |
255 | } | 274 | } |
256 | tid = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, | 275 | tid = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, |
257 | fh, | 276 | fh, |
258 | &stdin_cb, NULL); | 277 | &stdin_cb, |
278 | NULL); | ||
259 | } | 279 | } |
260 | 280 | ||
261 | 281 | ||
@@ -268,7 +288,8 @@ stdin_cb (void *cls) | |||
268 | * @param peer handle to the peer | 288 | * @param peer handle to the peer |
269 | */ | 289 | */ |
270 | static void | 290 | static void |
271 | testing_main (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg, | 291 | testing_main (void *cls, |
292 | const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
272 | struct GNUNET_TESTING_Peer *peer) | 293 | struct GNUNET_TESTING_Peer *peer) |
273 | { | 294 | { |
274 | my_peer = peer; | 295 | my_peer = peer; |
@@ -285,17 +306,17 @@ testing_main (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
285 | GNUNET_break (0); | 306 | GNUNET_break (0); |
286 | return; | 307 | return; |
287 | } | 308 | } |
288 | printf("ok\n%s\n", tmpfilename); | 309 | printf ("ok\n%s\n", tmpfilename); |
289 | fflush(stdout); | 310 | fflush (stdout); |
290 | GNUNET_SCHEDULER_add_shutdown (&cleanup, NULL); | 311 | GNUNET_SCHEDULER_add_shutdown (&cleanup, NULL); |
291 | fh = GNUNET_DISK_get_handle_from_native (stdin); | 312 | fh = GNUNET_DISK_get_handle_from_native (stdin); |
292 | tid = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, | 313 | tid = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, |
293 | fh, | 314 | fh, |
294 | &stdin_cb, NULL); | 315 | &stdin_cb, |
316 | NULL); | ||
295 | } | 317 | } |
296 | 318 | ||
297 | 319 | ||
298 | |||
299 | /** | 320 | /** |
300 | * Main function that will be running without scheduler. | 321 | * Main function that will be running without scheduler. |
301 | * | 322 | * |
@@ -305,13 +326,18 @@ testing_main (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
305 | * @param cfg configuration | 326 | * @param cfg configuration |
306 | */ | 327 | */ |
307 | static void | 328 | static void |
308 | run_no_scheduler (void *cls, char *const *args, const char *cfgfile, | 329 | run_no_scheduler (void *cls, |
309 | const struct GNUNET_CONFIGURATION_Handle *cfg) | 330 | char *const *args, |
331 | const char *cfgfile, | ||
332 | const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
310 | { | 333 | { |
311 | if (NULL != run_service_name) | 334 | if (NULL != run_service_name) |
312 | { | 335 | { |
313 | ret = GNUNET_TESTING_service_run ("gnunet_service_test", run_service_name, | 336 | ret = GNUNET_TESTING_service_run ("gnunet_service_test", |
314 | cfgfile, &testing_main, NULL); | 337 | run_service_name, |
338 | cfgfile, | ||
339 | &testing_main, | ||
340 | NULL); | ||
315 | return; | 341 | return; |
316 | } | 342 | } |
317 | 343 | ||
@@ -320,7 +346,9 @@ run_no_scheduler (void *cls, char *const *args, const char *cfgfile, | |||
320 | if (create_no > 0) | 346 | if (create_no > 0) |
321 | { | 347 | { |
322 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 348 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
323 | "Creating %u configuration files based on template `%s'\n", create_no, create_cfg_template); | 349 | "Creating %u configuration files based on template `%s'\n", |
350 | create_no, | ||
351 | create_cfg_template); | ||
324 | ret = create_unique_cfgs (create_cfg_template, create_no); | 352 | ret = create_unique_cfgs (create_cfg_template, create_no); |
325 | } | 353 | } |
326 | else | 354 | else |
@@ -348,37 +376,42 @@ run_no_scheduler (void *cls, char *const *args, const char *cfgfile, | |||
348 | int | 376 | int |
349 | main (int argc, char *const *argv) | 377 | main (int argc, char *const *argv) |
350 | { | 378 | { |
351 | struct GNUNET_GETOPT_CommandLineOption options[] = { | 379 | struct GNUNET_GETOPT_CommandLineOption options[] = |
352 | GNUNET_GETOPT_option_flag ('C', | 380 | {GNUNET_GETOPT_option_flag ('C', |
353 | "cfg", | 381 | "cfg", |
354 | gettext_noop ("create unique configuration files"), | 382 | gettext_noop ( |
355 | &create_cfg), | 383 | "create unique configuration files"), |
356 | GNUNET_GETOPT_option_string ('k', | 384 | &create_cfg), |
357 | "key", | 385 | GNUNET_GETOPT_option_string ( |
358 | "FILENAME", | 386 | 'k', |
359 | gettext_noop ("extract hostkey file from pre-computed hostkey list"), | 387 | "key", |
360 | &create_hostkey), | 388 | "FILENAME", |
361 | 389 | gettext_noop ("extract hostkey file from pre-computed hostkey list"), | |
362 | GNUNET_GETOPT_option_uint ('n', | 390 | &create_hostkey), |
363 | "number", | 391 | |
364 | "NUMBER", | 392 | GNUNET_GETOPT_option_uint ( |
365 | gettext_noop ("number of unique configuration files to create, or number of the hostkey to extract"), | 393 | 'n', |
366 | &create_no), | 394 | "number", |
367 | 395 | "NUMBER", | |
368 | 396 | gettext_noop ( | |
369 | GNUNET_GETOPT_option_string ('t', | 397 | "number of unique configuration files to create, or number of the hostkey to extract"), |
370 | "template", | 398 | &create_no), |
371 | "FILENAME", | 399 | |
372 | gettext_noop ("configuration template"), | 400 | |
373 | &create_cfg_template), | 401 | GNUNET_GETOPT_option_string ('t', |
374 | 402 | "template", | |
375 | GNUNET_GETOPT_option_string ('r', | 403 | "FILENAME", |
376 | "run", | 404 | gettext_noop ("configuration template"), |
377 | "SERVICE", | 405 | &create_cfg_template), |
378 | gettext_noop ("run the given service, wait on stdin for 'r' (restart) or 'q' (quit)"), | 406 | |
379 | &run_service_name), | 407 | GNUNET_GETOPT_option_string ( |
380 | GNUNET_GETOPT_OPTION_END | 408 | 'r', |
381 | }; | 409 | "run", |
410 | "SERVICE", | ||
411 | gettext_noop ( | ||
412 | "run the given service, wait on stdin for 'r' (restart) or 'q' (quit)"), | ||
413 | &run_service_name), | ||
414 | GNUNET_GETOPT_OPTION_END}; | ||
382 | if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) | 415 | if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) |
383 | return 2; | 416 | return 2; |
384 | 417 | ||
@@ -386,11 +419,20 @@ main (int argc, char *const *argv) | |||
386 | * GNUNET_TESTING_service_run, which starts the scheduler on its own. | 419 | * GNUNET_TESTING_service_run, which starts the scheduler on its own. |
387 | * Furthermore, the other functionality currently does not require the scheduler, too, | 420 | * Furthermore, the other functionality currently does not require the scheduler, too, |
388 | * but beware when extending gnunet-testing. */ | 421 | * but beware when extending gnunet-testing. */ |
389 | ret = (GNUNET_OK == | 422 | ret = |
390 | GNUNET_PROGRAM_run2 (argc, argv, "gnunet-testing", | 423 | (GNUNET_OK == |
391 | gettext_noop ("Command line tool to access the testing library"), options, &run_no_scheduler, | 424 | GNUNET_PROGRAM_run2 (argc, |
392 | NULL, GNUNET_YES)) ? ret : 1; | 425 | argv, |
393 | GNUNET_free ((void*) argv); | 426 | "gnunet-testing", |
427 | gettext_noop ( | ||
428 | "Command line tool to access the testing library"), | ||
429 | options, | ||
430 | &run_no_scheduler, | ||
431 | NULL, | ||
432 | GNUNET_YES)) | ||
433 | ? ret | ||
434 | : 1; | ||
435 | GNUNET_free ((void *) argv); | ||
394 | return ret; | 436 | return ret; |
395 | } | 437 | } |
396 | 438 | ||