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