- fix 2699
[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 /**
32  * Final status code.
33  */
34 static int ret;
35
36 static char *create_hostkey;
37
38 static int create_cfg;
39
40 static unsigned int create_no;
41
42 static char *create_cfg_template;
43
44
45 static int
46 create_unique_cfgs (const char * template, const unsigned int no)
47 {
48   struct GNUNET_TESTING_System *system;
49   int fail;
50   unsigned int cur;
51   char *cur_file;
52   struct GNUNET_CONFIGURATION_Handle *cfg_new;
53   struct GNUNET_CONFIGURATION_Handle *cfg_tmpl;
54
55   if (GNUNET_NO == GNUNET_DISK_file_test(template))
56   {
57     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Configuration template `%s': file not found\n", create_cfg_template);
58     return 1;
59   }
60   cfg_tmpl = GNUNET_CONFIGURATION_create();
61
62   /* load template */
63   if ((create_cfg_template != NULL) && (GNUNET_OK != GNUNET_CONFIGURATION_load(cfg_tmpl, create_cfg_template)))
64   {
65     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not load template `%s'\n", create_cfg_template);
66     GNUNET_CONFIGURATION_destroy (cfg_tmpl);
67
68     return 1;
69   }
70   /* load defaults */
71   if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg_tmpl,  NULL))
72   {
73     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not load template `%s'\n", create_cfg_template);
74     GNUNET_CONFIGURATION_destroy (cfg_tmpl);
75     return 1;
76   }
77
78   fail = GNUNET_NO;
79   system = GNUNET_TESTING_system_create ("testing", NULL /* controller */, NULL);
80   for (cur = 0; cur < no; cur++)
81   {
82     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Creating configuration no. %u \n", cur);
83     if (create_cfg_template != NULL)
84       GNUNET_asprintf (&cur_file,"%04u-%s",cur, create_cfg_template);
85     else
86       GNUNET_asprintf (&cur_file,"%04u%s",cur, ".conf");
87
88     cfg_new = GNUNET_CONFIGURATION_dup (cfg_tmpl);
89     if (GNUNET_OK !=
90         GNUNET_TESTING_configuration_create (system, cfg_new))
91     {
92       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not create another configuration\n");
93       GNUNET_CONFIGURATION_destroy (cfg_new);
94       fail = GNUNET_YES;
95       break;
96     }
97     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
98                 "Writing configuration no. %u to file `%s' \n", cur, cur_file);
99     if (GNUNET_OK != GNUNET_CONFIGURATION_write(cfg_new, cur_file))
100     {
101       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to write configuration no. %u \n", cur);
102       fail = GNUNET_YES;
103     }
104     GNUNET_CONFIGURATION_destroy (cfg_new);
105     GNUNET_free (cur_file);
106     if (GNUNET_YES == fail)
107       break;
108   }
109   GNUNET_CONFIGURATION_destroy(cfg_tmpl);
110   GNUNET_TESTING_system_destroy (system, GNUNET_NO);
111   if (GNUNET_YES == fail)
112     return 1;
113   return 0;
114 }
115
116
117 static int
118 create_hostkeys (const unsigned int no)
119 {
120   static char pad[GNUNET_TESTING_HOSTKEYFILESIZE];
121   struct GNUNET_TESTING_System *system;
122   struct GNUNET_PeerIdentity id;
123   struct GNUNET_DISK_FileHandle *fd;
124   struct GNUNET_CRYPTO_EccPrivateKey *pk;
125   struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded *pkb;
126   ssize_t ret;
127
128   system = GNUNET_TESTING_system_create ("testing", NULL, NULL);
129   pk = GNUNET_TESTING_hostkey_get (system, create_no, &id);
130   if (NULL == pk)
131   {
132     fprintf (stderr, _("Could not extract hostkey %u (offset too large?)\n"), create_no);
133     GNUNET_TESTING_system_destroy (system, GNUNET_YES);
134     return 1;
135   }
136   (void) GNUNET_DISK_directory_create_for_file (create_hostkey);
137   fd = GNUNET_DISK_file_open (create_hostkey,
138                               GNUNET_DISK_OPEN_READWRITE |
139                               GNUNET_DISK_OPEN_CREATE,
140                               GNUNET_DISK_PERM_USER_READ |
141                               GNUNET_DISK_PERM_USER_WRITE);
142   GNUNET_assert (fd != NULL);
143   pkb = GNUNET_CRYPTO_ecc_encode_key (pk);
144   ret = GNUNET_DISK_file_write (fd, pkb, 
145                                 ntohs (pkb->size));
146   GNUNET_assert (ntohs (pkb->size) == ret);
147   GNUNET_assert (ntohs (pkb->size) < GNUNET_TESTING_HOSTKEYFILESIZE);
148   GNUNET_assert (GNUNET_TESTING_HOSTKEYFILESIZE - ret ==
149                  GNUNET_DISK_file_write (fd, pad, 
150                                          GNUNET_TESTING_HOSTKEYFILESIZE - ret));
151   
152   GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
153   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
154                    "Wrote hostkey to file: `%s'\n", create_hostkey);
155   GNUNET_free (pkb);
156   GNUNET_CRYPTO_ecc_key_free (pk);
157   GNUNET_TESTING_system_destroy (system, GNUNET_YES);
158   return 0;
159 }
160
161
162 /**
163  * Main function that will be run by the scheduler.
164  *
165  * @param cls closure
166  * @param args remaining command-line arguments
167  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
168  * @param cfg configuration
169  */
170 static void
171 run (void *cls, char *const *args, const char *cfgfile,
172      const struct GNUNET_CONFIGURATION_Handle *cfg)
173 {
174   /* main code here */
175   if (GNUNET_YES == create_cfg)
176   {
177     if (create_no > 0)
178     {
179       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
180                   "Creating %u configuration files based on template `%s'\n", create_no, create_cfg_template);
181       ret = create_unique_cfgs (create_cfg_template, create_no);
182     }
183     else
184     {
185       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Missing arguments! \n");
186       ret = 1;
187     }
188   }
189   if (NULL != create_hostkey)
190   {
191     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Extracting hostkey %u\n", create_no);
192     ret = create_hostkeys (create_no);
193   }
194   GNUNET_free_non_null (create_cfg_template);
195 }
196
197
198 /**
199  * The main function.
200  *
201  * @param argc number of arguments from the command line
202  * @param argv command line arguments
203  * @return 0 ok, 1 on error
204  */
205 int
206 main (int argc, char *const *argv)
207 {
208   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
209     {'C', "cfg", NULL, gettext_noop ("create unique configuration files"),
210      GNUNET_NO, &GNUNET_GETOPT_set_one, &create_cfg},
211     {'k', "key", "FILENAME", gettext_noop ("extract hostkey file from pre-computed hostkey list"),
212      GNUNET_YES, &GNUNET_GETOPT_set_string, &create_hostkey},
213     {'n', "number", "NUMBER", gettext_noop ("number of unique configuration files to create, or number of the hostkey to extract"),
214      GNUNET_YES, &GNUNET_GETOPT_set_uint, &create_no},
215     {'t', "template", "FILENAME", gettext_noop ("configuration template"),
216      GNUNET_YES, &GNUNET_GETOPT_set_string, &create_cfg_template},
217     GNUNET_GETOPT_OPTION_END
218   };
219   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
220     return 2;
221
222   ret = (GNUNET_OK ==
223          GNUNET_PROGRAM_run (argc, argv, "gnunet-testing",
224                              gettext_noop ("Command line tool to access the testing library"), options, &run,
225                              NULL)) ? ret : 1;
226   GNUNET_free ((void*) argv);
227   return ret;
228 }
229
230 /* end of gnunet-testing.c */