-remove trailing whitespace
[oweals/gnunet.git] / src / testing / gnunet-testing.c
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/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 GNUNET_SCHEDULER_TaskIdentifier 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_EccPrivateKey *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_EccPrivateKey));
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  * @param tc scheduler context
201  */
202 static void
203 cleanup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
204 {
205   if (NULL != tmpfilename)
206   {
207     if (0 != UNLINK (tmpfilename))
208       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", tmpfilename);
209   }
210   if (GNUNET_SCHEDULER_NO_TASK != tid)
211   {
212     GNUNET_SCHEDULER_cancel (tid);
213     tid = GNUNET_SCHEDULER_NO_TASK;
214   }
215   if (NULL != fh)
216   {
217     GNUNET_DISK_file_close (fh);
218     fh = NULL;
219   }
220 }
221
222
223 /**
224  * Called whenever we can read stdin non-blocking
225  *
226  * @param cls unused
227  * @param tc scheduler context
228  */
229 static void
230 stdin_cb (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
231 {
232   int c;
233
234   tid = GNUNET_SCHEDULER_NO_TASK;
235   if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
236     return;
237   GNUNET_assert (0 != (GNUNET_SCHEDULER_REASON_READ_READY & tc->reason));
238   c = getchar ();
239   switch (c)
240   {
241   case EOF:
242   case 'q':
243     GNUNET_SCHEDULER_shutdown ();
244     return;
245   case 'r':
246     if (GNUNET_OK != GNUNET_TESTING_peer_stop (my_peer))
247       LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to stop the peer\n");
248     if (GNUNET_OK != GNUNET_TESTING_peer_start (my_peer))
249       LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to start the peer\n");
250     printf ("restarted\n");
251     fflush (stdout);
252     break;
253   case '\n':
254   case '\r':
255     /* ignore whitespace */
256     break;
257   default:
258     fprintf (stderr, _("Unknown command, use 'q' to quit or 'r' to restart peer\n"));
259     break;
260   }
261   tid = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, fh,
262                                         &stdin_cb, NULL);
263 }
264
265
266 /**
267  * Main function called by the testing library.
268  * Executed inside a running scheduler.
269  *
270  * @param cls unused
271  * @param cfg configuration of the peer that was started
272  * @param peer handle to the peer
273  */
274 static void
275 testing_main (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
276               struct GNUNET_TESTING_Peer *peer)
277 {
278   my_peer = peer;
279   if (NULL == (tmpfilename = GNUNET_DISK_mktemp ("gnunet-testing")))
280   {
281     GNUNET_break (0);
282     GNUNET_SCHEDULER_shutdown ();
283     return;
284   }
285   if (GNUNET_SYSERR ==
286       GNUNET_CONFIGURATION_write ((struct GNUNET_CONFIGURATION_Handle *) cfg,
287                                   tmpfilename))
288   {
289     GNUNET_break (0);
290     return;
291   }
292   printf("ok\n%s\n", tmpfilename);
293   fflush(stdout);
294   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, NULL);
295   fh = GNUNET_DISK_get_handle_from_native (stdin);
296   tid = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, fh,
297                                         &stdin_cb, NULL);
298 }
299
300
301
302 /**
303  * Main function that will be running without scheduler.
304  *
305  * @param cls closure
306  * @param args remaining command-line arguments
307  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
308  * @param cfg configuration
309  */
310 static void
311 run_no_scheduler (void *cls, char *const *args, const char *cfgfile,
312      const struct GNUNET_CONFIGURATION_Handle *cfg)
313 {
314   if (NULL != run_service_name)
315   {
316     ret = GNUNET_TESTING_service_run ("gnunet_service_test", run_service_name,
317                                       cfgfile, &testing_main, NULL);
318     return;
319   }
320
321   if (GNUNET_YES == create_cfg)
322   {
323     if (create_no > 0)
324     {
325       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
326                   "Creating %u configuration files based on template `%s'\n", create_no, create_cfg_template);
327       ret = create_unique_cfgs (create_cfg_template, create_no);
328     }
329     else
330     {
331       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Missing arguments! \n");
332       ret = 1;
333     }
334   }
335   if (NULL != create_hostkey)
336   {
337     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Extracting hostkey %u\n", create_no);
338     ret = create_hostkeys (create_no);
339   }
340   GNUNET_free_non_null (create_cfg_template);
341 }
342
343
344 /**
345  * The main function.
346  *
347  * @param argc number of arguments from the command line
348  * @param argv command line arguments
349  * @return 0 ok, 1 on error
350  */
351 int
352 main (int argc, char *const *argv)
353 {
354   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
355     {'C', "cfg", NULL, gettext_noop ("create unique configuration files"),
356      GNUNET_NO, &GNUNET_GETOPT_set_one, &create_cfg},
357     {'k', "key", "FILENAME", gettext_noop ("extract hostkey file from pre-computed hostkey list"),
358      GNUNET_YES, &GNUNET_GETOPT_set_string, &create_hostkey},
359     {'n', "number", "NUMBER", gettext_noop ("number of unique configuration files to create, or number of the hostkey to extract"),
360      GNUNET_YES, &GNUNET_GETOPT_set_uint, &create_no},
361     {'t', "template", "FILENAME", gettext_noop ("configuration template"),
362      GNUNET_YES, &GNUNET_GETOPT_set_string, &create_cfg_template},
363     {'r', "run", "SERVICE", gettext_noop ("run the given service, wait on stdin for 'r' (restart) or 'q' (quit)"),
364      GNUNET_YES, &GNUNET_GETOPT_set_string, &run_service_name},
365     GNUNET_GETOPT_OPTION_END
366   };
367   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
368     return 2;
369
370   /* Run without scheduler, because we may want to call
371    * GNUNET_TESTING_service_run, which starts the scheduler on its own.
372    * Furthermore, the other functionality currently does not require the scheduler, too,
373    * but beware when extending gnunet-testing. */
374   ret = (GNUNET_OK ==
375          GNUNET_PROGRAM_run2 (argc, argv, "gnunet-testing",
376                              gettext_noop ("Command line tool to access the testing library"), options, &run_no_scheduler,
377                              NULL, GNUNET_YES)) ? ret : 1;
378   GNUNET_free ((void*) argv);
379   return ret;
380 }
381
382 /* end of gnunet-testing.c */