first batch of license fixes (boring)
[oweals/gnunet.git] / src / testing / gnunet-testing.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU 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
16 /**
17  * @file testing/gnunet-testing.c
18  * @brief tool to use testing functionality from cmd line
19  * @author Christian Grothoff
20  */
21 #include "platform.h"
22 #include "gnunet_util_lib.h"
23 #include "gnunet_testing_lib.h"
24
25
26 #define LOG(kind,...)                                           \
27   GNUNET_log_from (kind, "gnunet-testing", __VA_ARGS__)
28
29
30 /**
31  * Final status code.
32  */
33 static int ret;
34
35 /**
36  * Filename of the hostkey file we should write,
37  * null if we should not write a hostkey file.
38  */
39 static char *create_hostkey;
40
41 /**
42  * Non-zero if we should create config files.
43  */
44 static int create_cfg;
45
46 /**
47  * Number of config files to create.
48  */
49 static unsigned int create_no;
50
51 /**
52  * Filename of the config template to be written.
53  */
54 static char *create_cfg_template;
55
56 /**
57  * Service we are supposed to run.
58  */
59 static char *run_service_name;
60
61 /**
62  * File handle to STDIN, for reading restart/quit commands.
63  */
64 static struct GNUNET_DISK_FileHandle *fh;
65
66 /**
67  * Temporary filename, used with '-r' to write the configuration to.
68  */
69 static char *tmpfilename;
70
71 /**
72  * Task identifier of the task that waits for stdin.
73  */
74 static struct GNUNET_SCHEDULER_Task *tid;
75
76 /**
77  * Peer started for '-r'.
78  */
79 static struct GNUNET_TESTING_Peer *my_peer;
80
81
82
83 static int
84 create_unique_cfgs (const char * template, const unsigned int no)
85 {
86   struct GNUNET_TESTING_System *system;
87   int fail;
88   unsigned int cur;
89   char *cur_file;
90   struct GNUNET_CONFIGURATION_Handle *cfg_new;
91   struct GNUNET_CONFIGURATION_Handle *cfg_tmpl;
92
93   if (GNUNET_NO == GNUNET_DISK_file_test(template))
94   {
95     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Configuration template `%s': file not found\n", create_cfg_template);
96     return 1;
97   }
98   cfg_tmpl = GNUNET_CONFIGURATION_create();
99
100   /* load template */
101   if ((create_cfg_template != NULL) && (GNUNET_OK != GNUNET_CONFIGURATION_load(cfg_tmpl, create_cfg_template)))
102   {
103     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not load template `%s'\n", create_cfg_template);
104     GNUNET_CONFIGURATION_destroy (cfg_tmpl);
105
106     return 1;
107   }
108   /* load defaults */
109   if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg_tmpl,  NULL))
110   {
111     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not load template `%s'\n", create_cfg_template);
112     GNUNET_CONFIGURATION_destroy (cfg_tmpl);
113     return 1;
114   }
115
116   fail = GNUNET_NO;
117   system = GNUNET_TESTING_system_create ("testing", NULL /* controller */,
118                                          NULL, NULL);
119   for (cur = 0; cur < no; cur++)
120   {
121     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating configuration no. %u \n", cur);
122     if (create_cfg_template != NULL)
123       GNUNET_asprintf (&cur_file,"%04u-%s",cur, create_cfg_template);
124     else
125       GNUNET_asprintf (&cur_file,"%04u%s",cur, ".conf");
126
127     cfg_new = GNUNET_CONFIGURATION_dup (cfg_tmpl);
128     if (GNUNET_OK !=
129         GNUNET_TESTING_configuration_create (system, cfg_new))
130     {
131       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create another configuration\n");
132       GNUNET_CONFIGURATION_destroy (cfg_new);
133       fail = GNUNET_YES;
134       break;
135     }
136     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
137                 "Writing configuration no. %u to file `%s' \n", cur, cur_file);
138     if (GNUNET_OK != GNUNET_CONFIGURATION_write(cfg_new, cur_file))
139     {
140       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to write configuration no. %u \n", cur);
141       fail = GNUNET_YES;
142     }
143     GNUNET_CONFIGURATION_destroy (cfg_new);
144     GNUNET_free (cur_file);
145     if (GNUNET_YES == fail)
146       break;
147   }
148   GNUNET_CONFIGURATION_destroy(cfg_tmpl);
149   GNUNET_TESTING_system_destroy (system, GNUNET_NO);
150   if (GNUNET_YES == fail)
151     return 1;
152   return 0;
153 }
154
155
156 static int
157 create_hostkeys (const unsigned int no)
158 {
159   struct GNUNET_TESTING_System *system;
160   struct GNUNET_PeerIdentity id;
161   struct GNUNET_DISK_FileHandle *fd;
162   struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
163
164   system = GNUNET_TESTING_system_create ("testing", NULL, NULL, NULL);
165   pk = GNUNET_TESTING_hostkey_get (system, create_no, &id);
166   if (NULL == pk)
167   {
168     fprintf (stderr, _("Could not extract hostkey %u (offset too large?)\n"), create_no);
169     GNUNET_TESTING_system_destroy (system, GNUNET_YES);
170     return 1;
171   }
172   (void) GNUNET_DISK_directory_create_for_file (create_hostkey);
173   fd = GNUNET_DISK_file_open (create_hostkey,
174                               GNUNET_DISK_OPEN_READWRITE |
175                               GNUNET_DISK_OPEN_CREATE,
176                               GNUNET_DISK_PERM_USER_READ |
177                               GNUNET_DISK_PERM_USER_WRITE);
178   GNUNET_assert (fd != NULL);
179   ret = GNUNET_DISK_file_write (fd, pk,
180                                 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey));
181   GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
182   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
183                    "Wrote hostkey to file: `%s'\n", create_hostkey);
184   GNUNET_free (pk);
185   GNUNET_TESTING_system_destroy (system, GNUNET_YES);
186   return 0;
187 }
188
189
190 /**
191  * Cleanup called by signal handlers and when stdin is closed.
192  * Removes the temporary file.
193  *
194  * @param cls unused
195  */
196 static void
197 cleanup (void *cls)
198 {
199   if (NULL != tmpfilename)
200   {
201     if (0 != UNLINK (tmpfilename))
202       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", tmpfilename);
203   }
204   if (NULL != tid)
205   {
206     GNUNET_SCHEDULER_cancel (tid);
207     tid = NULL;
208   }
209   if (NULL != fh)
210   {
211     GNUNET_DISK_file_close (fh);
212     fh = NULL;
213   }
214 }
215
216
217 /**
218  * Called whenever we can read stdin non-blocking
219  *
220  * @param cls unused
221  */
222 static void
223 stdin_cb (void *cls)
224 {
225   int c;
226
227   tid = NULL;
228   c = getchar ();
229   switch (c)
230   {
231   case EOF:
232   case 'q':
233     GNUNET_SCHEDULER_shutdown ();
234     return;
235   case 'r':
236     if (GNUNET_OK != GNUNET_TESTING_peer_stop (my_peer))
237       LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to stop the peer\n");
238     if (GNUNET_OK != GNUNET_TESTING_peer_start (my_peer))
239       LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to start the peer\n");
240     printf ("restarted\n");
241     fflush (stdout);
242     break;
243   case '\n':
244   case '\r':
245     /* ignore whitespace */
246     break;
247   default:
248     fprintf (stderr, _("Unknown command, use 'q' to quit or 'r' to restart peer\n"));
249     break;
250   }
251   tid = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
252                                         fh,
253                                         &stdin_cb, NULL);
254 }
255
256
257 /**
258  * Main function called by the testing library.
259  * Executed inside a running scheduler.
260  *
261  * @param cls unused
262  * @param cfg configuration of the peer that was started
263  * @param peer handle to the peer
264  */
265 static void
266 testing_main (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
267               struct GNUNET_TESTING_Peer *peer)
268 {
269   my_peer = peer;
270   if (NULL == (tmpfilename = GNUNET_DISK_mktemp ("gnunet-testing")))
271   {
272     GNUNET_break (0);
273     GNUNET_SCHEDULER_shutdown ();
274     return;
275   }
276   if (GNUNET_SYSERR ==
277       GNUNET_CONFIGURATION_write ((struct GNUNET_CONFIGURATION_Handle *) cfg,
278                                   tmpfilename))
279   {
280     GNUNET_break (0);
281     return;
282   }
283   printf("ok\n%s\n", tmpfilename);
284   fflush(stdout);
285   GNUNET_SCHEDULER_add_shutdown (&cleanup, NULL);
286   fh = GNUNET_DISK_get_handle_from_native (stdin);
287   tid = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
288                                         fh,
289                                         &stdin_cb, NULL);
290 }
291
292
293
294 /**
295  * Main function that will be running without scheduler.
296  *
297  * @param cls closure
298  * @param args remaining command-line arguments
299  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
300  * @param cfg configuration
301  */
302 static void
303 run_no_scheduler (void *cls, char *const *args, const char *cfgfile,
304      const struct GNUNET_CONFIGURATION_Handle *cfg)
305 {
306   if (NULL != run_service_name)
307   {
308     ret = GNUNET_TESTING_service_run ("gnunet_service_test", run_service_name,
309                                       cfgfile, &testing_main, NULL);
310     return;
311   }
312
313   if (GNUNET_YES == create_cfg)
314   {
315     if (create_no > 0)
316     {
317       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
318                   "Creating %u configuration files based on template `%s'\n", create_no, create_cfg_template);
319       ret = create_unique_cfgs (create_cfg_template, create_no);
320     }
321     else
322     {
323       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Missing arguments! \n");
324       ret = 1;
325     }
326   }
327   if (NULL != create_hostkey)
328   {
329     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Extracting hostkey %u\n", create_no);
330     ret = create_hostkeys (create_no);
331   }
332   GNUNET_free_non_null (create_cfg_template);
333 }
334
335
336 /**
337  * The main function.
338  *
339  * @param argc number of arguments from the command line
340  * @param argv command line arguments
341  * @return 0 ok, 1 on error
342  */
343 int
344 main (int argc, char *const *argv)
345 {
346   struct GNUNET_GETOPT_CommandLineOption options[] = {
347     GNUNET_GETOPT_option_flag ('C',
348                                   "cfg",
349                                   gettext_noop ("create unique configuration files"),
350                                   &create_cfg),
351     GNUNET_GETOPT_option_string ('k',
352                                  "key",
353                                  "FILENAME",
354                                  gettext_noop ("extract hostkey file from pre-computed hostkey list"),
355                                  &create_hostkey),
356
357     GNUNET_GETOPT_option_uint ('n',
358                                    "number",
359                                    "NUMBER",
360                                    gettext_noop ("number of unique configuration files to create, or number of the hostkey to extract"),
361                                    &create_no),
362
363
364     GNUNET_GETOPT_option_string ('t',
365                                  "template",
366                                  "FILENAME",
367                                  gettext_noop ("configuration template"),
368                                  &create_cfg_template),
369
370     GNUNET_GETOPT_option_string ('r',
371                                  "run",
372                                  "SERVICE",
373                                  gettext_noop ("run the given service, wait on stdin for 'r' (restart) or 'q' (quit)"),
374                                  &run_service_name),
375     GNUNET_GETOPT_OPTION_END
376   };
377   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
378     return 2;
379
380   /* Run without scheduler, because we may want to call
381    * GNUNET_TESTING_service_run, which starts the scheduler on its own.
382    * Furthermore, the other functionality currently does not require the scheduler, too,
383    * but beware when extending gnunet-testing. */
384   ret = (GNUNET_OK ==
385          GNUNET_PROGRAM_run2 (argc, argv, "gnunet-testing",
386                              gettext_noop ("Command line tool to access the testing library"), options, &run_no_scheduler,
387                              NULL, GNUNET_YES)) ? ret : 1;
388   GNUNET_free ((void*) argv);
389   return ret;
390 }
391
392 /* end of gnunet-testing.c */