- fix
[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_getopt_lib.h"
28 #include "gnunet_program_lib.h"
29 #include "gnunet_testing_lib.h"
30
31 #define HOSTKEYFILESIZE 914
32
33 /**
34  * Final status code.
35  */
36 static int ret;
37
38 static unsigned int create_hostkey;
39
40 static unsigned int create_cfg;
41
42 static int create_no;
43
44 static char * create_cfg_template;
45
46 static char * create_hostkey_file;
47
48 static int
49 create_unique_cfgs (const char * template, const unsigned int no)
50 {
51   int fail = GNUNET_NO;
52
53   uint16_t port = 20000;
54   uint32_t upnum = 1;
55   uint32_t fdnum = 1;
56
57   if (GNUNET_NO == GNUNET_DISK_file_test(template))
58   {
59     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Configuration template `%s': file not found\n", create_cfg_template);
60     return 1;
61   }
62
63   int cur = 0;
64   char * cur_file;
65   char *service_home = NULL;
66   char *cur_service_home = NULL;
67
68   struct GNUNET_CONFIGURATION_Handle *cfg_new = NULL;
69   struct GNUNET_CONFIGURATION_Handle *cfg_tmpl = GNUNET_CONFIGURATION_create();
70
71   /* load template */
72   if ((create_cfg_template != NULL) && (GNUNET_OK != GNUNET_CONFIGURATION_load(cfg_tmpl, create_cfg_template)))
73   {
74     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not load template `%s'\n", create_cfg_template);
75     GNUNET_CONFIGURATION_destroy(cfg_tmpl);
76
77     return 1;
78   }
79   /* load defaults */
80   else
81     GNUNET_CONFIGURATION_load(cfg_tmpl,  NULL);
82
83   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg_tmpl, "PATHS", "SERVICEHOME", &service_home))
84   {
85     GNUNET_asprintf(&service_home, "%s", "/tmp/testing");
86   }
87   else
88   {
89     int s = strlen (service_home);
90     if (service_home[s-1] == DIR_SEPARATOR)
91       service_home[s-1] = '\0';
92   }
93
94   while (cur < no)
95   {
96     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating configuration no. %u \n", cur);
97     if (create_cfg_template != NULL)
98       GNUNET_asprintf (&cur_file,"%04u-%s",cur, create_cfg_template);
99     else
100       GNUNET_asprintf (&cur_file,"%04u%s",cur, ".conf");
101
102
103     GNUNET_asprintf (&cur_service_home, "%s-%04u%c",service_home, cur, DIR_SEPARATOR);
104     GNUNET_CONFIGURATION_set_value_string (cfg_tmpl,"PATHS","SERVICEHOME", cur_service_home);
105     GNUNET_CONFIGURATION_set_value_string (cfg_tmpl,"PATHS","DEFAULTCONFIG", cur_file);
106     GNUNET_free (cur_service_home);
107
108     cfg_new = GNUNET_TESTING_create_cfg(cfg_tmpl, cur, &port, &upnum, NULL, &fdnum);
109
110     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Writing configuration no. %u to file `%s' \n", cur, cur_file);
111     if (GNUNET_OK != GNUNET_CONFIGURATION_write(cfg_new, cur_file))
112     {
113       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to write configuration no. %u \n", cur);
114       fail = GNUNET_YES;
115     }
116
117     GNUNET_CONFIGURATION_destroy (cfg_new);
118     GNUNET_free (cur_file);
119     if (fail == GNUNET_YES)
120       break;
121     cur ++;
122   }
123
124   GNUNET_CONFIGURATION_destroy(cfg_tmpl);
125   GNUNET_free (service_home);
126   if (fail == GNUNET_NO)
127     return 0;
128   else
129     return 1;
130 }
131
132 static int
133 create_hostkeys (const unsigned int no)
134 {
135   struct GNUNET_DISK_FileHandle *fd;
136   int cur = 0;
137   uint64_t fs;
138   uint64_t total_hostkeys;
139   char *hostkey_data;
140   char *hostkey_src_file;
141   char *hostkey_dest_file;
142
143   /* prepare hostkeys */
144   if (create_hostkey_file == NULL)
145     hostkey_src_file = "../../contrib/testing_hostkeys.dat";
146   else
147   {
148     hostkey_src_file = create_hostkey_file;
149   }
150
151   if (GNUNET_YES != GNUNET_DISK_file_test (hostkey_src_file))
152   {
153     if (create_hostkey_file == NULL)
154       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not read hostkeys file, specify hostkey file with -H!\n"));
155     else
156       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Specified hostkey file `%s' not found!\n"), create_hostkey_file);
157     return 1;
158   }
159   else
160   {
161     /* Check hostkey file size, read entire thing into memory */
162     fd = GNUNET_DISK_file_open (hostkey_src_file, GNUNET_DISK_OPEN_READ,
163                                 GNUNET_DISK_PERM_NONE);
164     if (NULL == fd)
165     {
166       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", hostkey_src_file);
167       return 1;
168     }
169
170     if (GNUNET_YES != GNUNET_DISK_file_size (hostkey_src_file, &fs, GNUNET_YES))
171       fs = 0;
172
173     if (0 != (fs % HOSTKEYFILESIZE))
174     {
175       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
176                   "File size %llu seems incorrect for hostkeys...\n", fs);
177     }
178     else
179     {
180       total_hostkeys = fs / HOSTKEYFILESIZE;
181       hostkey_data = GNUNET_malloc_large (fs);
182       GNUNET_assert (fs == GNUNET_DISK_file_read (fd, hostkey_data, fs));
183       GNUNET_log  (GNUNET_ERROR_TYPE_DEBUG,
184                        "Read %llu hostkeys from file\n", total_hostkeys);
185     }
186     GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
187   }
188
189   while (cur < no)
190   {
191     GNUNET_asprintf (&hostkey_dest_file, "%04u-hostkey",cur);
192     GNUNET_assert (GNUNET_OK ==
193                    GNUNET_DISK_directory_create_for_file (hostkey_dest_file));
194     fd = GNUNET_DISK_file_open (hostkey_dest_file,
195                                 GNUNET_DISK_OPEN_READWRITE |
196                                 GNUNET_DISK_OPEN_CREATE,
197                                 GNUNET_DISK_PERM_USER_READ |
198                                 GNUNET_DISK_PERM_USER_WRITE);
199     GNUNET_assert (fd != NULL);
200     GNUNET_assert (HOSTKEYFILESIZE ==
201                    GNUNET_DISK_file_write (fd, &hostkey_data[cur * HOSTKEYFILESIZE], HOSTKEYFILESIZE));
202     GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
203     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
204                      "Wrote hostkey to file: `%s' \n", hostkey_dest_file);
205     GNUNET_free (hostkey_dest_file);
206     cur ++;
207   }
208
209   GNUNET_free (hostkey_data);
210
211   return 0;
212 }
213
214 /**
215  * Main function that will be run by the scheduler.
216  *
217  * @param cls closure
218  * @param args remaining command-line arguments
219  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
220  * @param cfg configuration
221  */
222 static void
223 run (void *cls, char *const *args, const char *cfgfile,
224      const struct GNUNET_CONFIGURATION_Handle *cfg)
225 {
226   /* main code here */
227   if (create_cfg == GNUNET_YES)
228   {
229     if (create_no > 0)
230     {
231       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating %u configuration files based on template `%s'\n", create_no, create_cfg_template);
232       ret = create_unique_cfgs (create_cfg_template, create_no);
233     }
234     else
235     {
236       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Missing arguments! \n");
237       ret = 1;
238     }
239   }
240
241   if (create_hostkey == GNUNET_YES)
242   {
243     if  (create_no > 0)
244     {
245       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating %u hostkeys \n", create_no);
246       ret = create_hostkeys (create_no);
247     }
248     else
249     {
250       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Missing arguments! \n");
251       ret = 1;
252     }
253   }
254
255   GNUNET_free_non_null (create_cfg_template);
256 }
257
258
259 /**
260  * The main function.
261  *
262  * @param argc number of arguments from the command line
263  * @param argv command line arguments
264  * @return 0 ok, 1 on error
265  */
266 int
267 main (int argc, char *const *argv)
268 {
269   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
270     {'C', "cfg", NULL, gettext_noop ("create unique configuration files"),
271      GNUNET_NO, &GNUNET_GETOPT_set_one, &create_cfg},
272      {'k', "key", NULL, gettext_noop ("create hostkey files from pre-computed hostkey list"),
273      GNUNET_NO, &GNUNET_GETOPT_set_one, &create_hostkey},
274      {'H', "hostkeys", NULL, gettext_noop ("host key file"),
275      GNUNET_YES, &GNUNET_GETOPT_set_string, &create_hostkey_file},
276     {'n', "number", NULL, gettext_noop ("number of unique configuration files or hostkeys to create"),
277      GNUNET_YES, &GNUNET_GETOPT_set_uint, &create_no},
278     {'t', "template", NULL, gettext_noop ("configuration template"),
279      GNUNET_YES, &GNUNET_GETOPT_set_string, &create_cfg_template},
280     GNUNET_GETOPT_OPTION_END
281   };
282   return (GNUNET_OK ==
283           GNUNET_PROGRAM_run (argc, argv, "gnunet-testing",
284                               gettext_noop ("Command line tool to access the testing library"), options, &run,
285                               NULL)) ? ret : 1;
286 }
287
288 /* end of gnunet-testing.c */