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