aboutsummaryrefslogtreecommitdiff
path: root/src/testing
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2012-06-10 00:45:37 +0000
committerChristian Grothoff <christian@grothoff.org>2012-06-10 00:45:37 +0000
commitdfb3c59120f834a579d74391ec67391e1411b4cf (patch)
tree2239506d061ba4ee9bf634b5afa4963f767befb2 /src/testing
parentfef2de1075e1a294de3675dbd1f71a073e6ae49d (diff)
downloadgnunet-dfb3c59120f834a579d74391ec67391e1411b4cf.tar.gz
gnunet-dfb3c59120f834a579d74391ec67391e1411b4cf.zip
-auto-load hostkeys, minor bugfixes
Diffstat (limited to 'src/testing')
-rw-r--r--src/testing/test_testing_peerstartup.c9
-rw-r--r--src/testing/test_testing_portreservation.c1
-rw-r--r--src/testing/testing.c186
3 files changed, 90 insertions, 106 deletions
diff --git a/src/testing/test_testing_peerstartup.c b/src/testing/test_testing_peerstartup.c
index 8021a763c..13853fe68 100644
--- a/src/testing/test_testing_peerstartup.c
+++ b/src/testing/test_testing_peerstartup.c
@@ -72,7 +72,6 @@ do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
72 GNUNET_assert (GNUNET_OK == GNUNET_TESTING_peer_stop (test_ctx->peer)); 72 GNUNET_assert (GNUNET_OK == GNUNET_TESTING_peer_stop (test_ctx->peer));
73 GNUNET_TESTING_peer_destroy (test_ctx->peer); 73 GNUNET_TESTING_peer_destroy (test_ctx->peer);
74 GNUNET_CONFIGURATION_destroy (test_ctx->cfg); 74 GNUNET_CONFIGURATION_destroy (test_ctx->cfg);
75 GNUNET_TESTING_hostkeys_unload (test_ctx->system);
76 GNUNET_TESTING_system_destroy (test_ctx->system, GNUNET_YES); 75 GNUNET_TESTING_system_destroy (test_ctx->system, GNUNET_YES);
77 GNUNET_free (test_ctx); 76 GNUNET_free (test_ctx);
78} 77}
@@ -89,8 +88,6 @@ run (void *cls, char *const *args, const char *cfgfile,
89 struct GNUNET_TESTING_Peer *peer; 88 struct GNUNET_TESTING_Peer *peer;
90 struct GNUNET_CONFIGURATION_Handle *new_cfg; 89 struct GNUNET_CONFIGURATION_Handle *new_cfg;
91 struct TestingContext *test_ctx; 90 struct TestingContext *test_ctx;
92 char *data_dir;
93 char *hostkeys_file;
94 char *emsg; 91 char *emsg;
95 char *_tmpdir; 92 char *_tmpdir;
96 char *tmpdir; 93 char *tmpdir;
@@ -123,12 +120,6 @@ run (void *cls, char *const *args, const char *cfgfile,
123 "127.0.0.1"); 120 "127.0.0.1");
124 GNUNET_assert (NULL != system); 121 GNUNET_assert (NULL != system);
125 GNUNET_free (tmpdir); 122 GNUNET_free (tmpdir);
126 data_dir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
127 GNUNET_asprintf (&hostkeys_file, "%s/testing_hostkeys.dat", data_dir);
128 GNUNET_free (data_dir);
129 GNUNET_assert (GNUNET_OK ==
130 GNUNET_TESTING_hostkeys_load (system, hostkeys_file));
131 GNUNET_free (hostkeys_file);
132 new_cfg = GNUNET_CONFIGURATION_dup (cfg); 123 new_cfg = GNUNET_CONFIGURATION_dup (cfg);
133 emsg = NULL; 124 emsg = NULL;
134 peer = GNUNET_TESTING_peer_configure (system, new_cfg, 0, &id, &emsg); 125 peer = GNUNET_TESTING_peer_configure (system, new_cfg, 0, &id, &emsg);
diff --git a/src/testing/test_testing_portreservation.c b/src/testing/test_testing_portreservation.c
index 40109446b..02d6e1686 100644
--- a/src/testing/test_testing_portreservation.c
+++ b/src/testing/test_testing_portreservation.c
@@ -69,6 +69,7 @@ run (void *cls, char *const *args, const char *cfgfile,
69 GNUNET_TESTING_system_destroy (system, GNUNET_NO); 69 GNUNET_TESTING_system_destroy (system, GNUNET_NO);
70} 70}
71 71
72
72int main (int argc, char *argv[]) 73int main (int argc, char *argv[])
73{ 74{
74 struct GNUNET_GETOPT_CommandLineOption options[] = { 75 struct GNUNET_GETOPT_CommandLineOption options[] = {
diff --git a/src/testing/testing.c b/src/testing/testing.c
index c4da3a973..ac5edab34 100644
--- a/src/testing/testing.c
+++ b/src/testing/testing.c
@@ -149,6 +149,85 @@ struct GNUNET_TESTING_Peer
149 149
150 150
151/** 151/**
152 * Testing includes a number of pre-created hostkeys for faster peer
153 * startup. This function loads such keys into memory from a file.
154 *
155 * @param system the testing system handle
156 * @return GNUNET_OK on success; GNUNET_SYSERR on error
157 */
158static int
159hostkeys_load (struct GNUNET_TESTING_System *system)
160{
161 struct GNUNET_DISK_FileHandle *fd;
162 uint64_t fs;
163 char *data_dir;
164 char *filename;
165
166 data_dir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
167 GNUNET_asprintf (&filename, "%s/testing_hostkeys.dat", data_dir);
168 GNUNET_free (data_dir);
169
170 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
171 {
172 LOG (GNUNET_ERROR_TYPE_ERROR,
173 _("Hostkeys file not found: %s\n"), filename);
174 GNUNET_free (filename);
175 return GNUNET_SYSERR;
176 }
177 /* Check hostkey file size, read entire thing into memory */
178 fd = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
179 GNUNET_DISK_PERM_NONE);
180 if (NULL == fd)
181 {
182 LOG (GNUNET_ERROR_TYPE_ERROR,
183 _("Could not open hostkeys file: %s\n"), filename);
184 GNUNET_free (filename);
185 return GNUNET_SYSERR;
186 }
187 if (GNUNET_OK !=
188 GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
189 fs = 0;
190 if (0 == fs)
191 {
192 GNUNET_DISK_file_close (fd);
193 GNUNET_free (filename);
194 return GNUNET_SYSERR; /* File is empty */
195 }
196 if (0 != (fs % HOSTKEYFILESIZE))
197 {
198 GNUNET_DISK_file_close (fd);
199 LOG (GNUNET_ERROR_TYPE_ERROR,
200 _("Incorrect hostkey file format: %s\n"), filename);
201 GNUNET_free (filename);
202 return GNUNET_SYSERR;
203 }
204 GNUNET_break (NULL == system->hostkeys_data);
205 system->total_hostkeys = fs / HOSTKEYFILESIZE;
206 /* FIXME: mmap instead? */
207 system->hostkeys_data = GNUNET_malloc_large (fs); /* free in hostkeys_unload */
208 GNUNET_assert (fs == GNUNET_DISK_file_read (fd, system->hostkeys_data, fs));
209 GNUNET_DISK_file_close (fd);
210 GNUNET_free (filename);
211 return GNUNET_OK;
212}
213
214
215/**
216 * Function to remove the loaded hostkeys
217 *
218 * @param system the testing system handle
219 */
220static void
221hostkeys_unload (struct GNUNET_TESTING_System *system)
222{
223 GNUNET_break (NULL != system->hostkeys_data);
224 GNUNET_free_non_null (system->hostkeys_data);
225 system->hostkeys_data = NULL;
226 system->total_hostkeys = 0;
227}
228
229
230/**
152 * Create a system handle. There must only be one system 231 * Create a system handle. There must only be one system
153 * handle per operating system. 232 * handle per operating system.
154 * 233 *
@@ -164,15 +243,16 @@ GNUNET_TESTING_system_create (const char *tmppath,
164{ 243{
165 struct GNUNET_TESTING_System *system; 244 struct GNUNET_TESTING_System *system;
166 245
167 if (NULL == tmppath) 246 GNUNET_assert (NULL != tmppath);
168 {
169 LOG (GNUNET_ERROR_TYPE_ERROR, _("tmppath cannot be NULL\n"));
170 return NULL;
171 }
172 system = GNUNET_malloc (sizeof (struct GNUNET_TESTING_System)); 247 system = GNUNET_malloc (sizeof (struct GNUNET_TESTING_System));
173 system->tmppath = GNUNET_strdup (tmppath); 248 system->tmppath = GNUNET_strdup (tmppath);
174 if (NULL != controller) 249 if (NULL != controller)
175 system->controller = GNUNET_strdup (controller); 250 system->controller = GNUNET_strdup (controller);
251 if (GNUNET_OK != hostkeys_load (system))
252 {
253 GNUNET_TESTING_system_destroy (system, GNUNET_YES);
254 return NULL;
255 }
176 return system; 256 return system;
177} 257}
178 258
@@ -189,10 +269,7 @@ GNUNET_TESTING_system_destroy (struct GNUNET_TESTING_System *system,
189 int remove_paths) 269 int remove_paths)
190{ 270{
191 if (NULL != system->hostkeys_data) 271 if (NULL != system->hostkeys_data)
192 { 272 hostkeys_unload (system);
193 GNUNET_break (0); /* Use GNUNET_TESTING_hostkeys_unload() */
194 GNUNET_TESTING_hostkeys_unload (system);
195 }
196 if (GNUNET_YES == remove_paths) 273 if (GNUNET_YES == remove_paths)
197 GNUNET_DISK_directory_remove (system->tmppath); 274 GNUNET_DISK_directory_remove (system->tmppath);
198 GNUNET_free (system->tmppath); 275 GNUNET_free (system->tmppath);
@@ -336,75 +413,6 @@ reserve_path (struct GNUNET_TESTING_System *system)
336 413
337 414
338/** 415/**
339 * Testing includes a number of pre-created hostkeys for faster peer
340 * startup. This function loads such keys into memory from a file.
341 *
342 * @param system the testing system handle
343 * @param filename the path of the hostkeys file
344 * @return GNUNET_OK on success; GNUNET_SYSERR on error
345 */
346int
347GNUNET_TESTING_hostkeys_load (struct GNUNET_TESTING_System *system,
348 const char *filename)
349{
350 struct GNUNET_DISK_FileHandle *fd;
351 uint64_t fs;
352
353 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
354 {
355 LOG (GNUNET_ERROR_TYPE_ERROR,
356 _("Hostkeys file not found: %s\n"), filename);
357 return GNUNET_SYSERR;
358 }
359 /* Check hostkey file size, read entire thing into memory */
360 fd = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
361 GNUNET_DISK_PERM_NONE);
362 if (NULL == fd)
363 {
364 LOG (GNUNET_ERROR_TYPE_ERROR,
365 _("Could not open hostkeys file: %s\n"), filename);
366 return GNUNET_SYSERR;
367 }
368 if (GNUNET_OK !=
369 GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
370 fs = 0;
371 if (0 == fs)
372 {
373 GNUNET_DISK_file_close (fd);
374 return GNUNET_SYSERR; /* File is empty */
375 }
376 if (0 != (fs % HOSTKEYFILESIZE))
377 {
378 GNUNET_DISK_file_close (fd);
379 LOG (GNUNET_ERROR_TYPE_ERROR,
380 _("Incorrect hostkey file format: %s\n"), filename);
381 return GNUNET_SYSERR;
382 }
383 GNUNET_break (NULL == system->hostkeys_data);
384 system->total_hostkeys = fs / HOSTKEYFILESIZE;
385 system->hostkeys_data = GNUNET_malloc_large (fs); /* free in hostkeys_unload */
386 GNUNET_assert (fs == GNUNET_DISK_file_read (fd, system->hostkeys_data, fs));
387 GNUNET_DISK_file_close (fd);
388 return GNUNET_OK;
389}
390
391
392/**
393 * Function to remove the loaded hostkeys
394 *
395 * @param system the testing system handle
396 */
397void
398GNUNET_TESTING_hostkeys_unload (struct GNUNET_TESTING_System *system)
399{
400 GNUNET_break (NULL != system->hostkeys_data);
401 GNUNET_free_non_null (system->hostkeys_data);
402 system->hostkeys_data = NULL;
403 system->total_hostkeys = 0;
404}
405
406
407/**
408 * Testing includes a number of pre-created hostkeys for 416 * Testing includes a number of pre-created hostkeys for
409 * faster peer startup. This function can be used to 417 * faster peer startup. This function can be used to
410 * access the n-th key of those pre-created hostkeys; note 418 * access the n-th key of those pre-created hostkeys; note
@@ -695,7 +703,7 @@ GNUNET_TESTING_peer_configure (struct GNUNET_TESTING_System *system,
695 GNUNET_asprintf (&emsg_, 703 GNUNET_asprintf (&emsg_,
696 _("You attempted to create a testbed with more than %u hosts. Please precompute more hostkeys first.\n"), 704 _("You attempted to create a testbed with more than %u hosts. Please precompute more hostkeys first.\n"),
697 (unsigned int) system->total_hostkeys); 705 (unsigned int) system->total_hostkeys);
698 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s", *emsg_); 706 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s", emsg_);
699 if (NULL != emsg) 707 if (NULL != emsg)
700 *emsg = emsg_; 708 *emsg = emsg_;
701 else 709 else
@@ -943,25 +951,10 @@ GNUNET_TESTING_service_run (const char *tmppath,
943 struct GNUNET_TESTING_System *system; 951 struct GNUNET_TESTING_System *system;
944 struct GNUNET_TESTING_Peer *peer; 952 struct GNUNET_TESTING_Peer *peer;
945 struct GNUNET_CONFIGURATION_Handle *cfg; 953 struct GNUNET_CONFIGURATION_Handle *cfg;
946 char *data_dir; 954
947 char *hostkeys_file;
948
949 data_dir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
950 GNUNET_asprintf (&hostkeys_file, "%s/testing_hostkeys.dat", data_dir);
951 GNUNET_free (data_dir);
952 system = GNUNET_TESTING_system_create (tmppath, "127.0.0.1"); 955 system = GNUNET_TESTING_system_create (tmppath, "127.0.0.1");
953 if (NULL == system) 956 if (NULL == system)
954 {
955 GNUNET_free (hostkeys_file);
956 return 1; 957 return 1;
957 }
958 if (GNUNET_OK != GNUNET_TESTING_hostkeys_load (system, hostkeys_file))
959 {
960 GNUNET_free (hostkeys_file);
961 GNUNET_TESTING_system_destroy (system, GNUNET_YES);
962 return 1;
963 }
964 GNUNET_free (hostkeys_file);
965 cfg = GNUNET_CONFIGURATION_create (); 958 cfg = GNUNET_CONFIGURATION_create ();
966 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, cfgfilename)) 959 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, cfgfilename))
967 { 960 {
@@ -975,11 +968,10 @@ GNUNET_TESTING_service_run (const char *tmppath,
975 if (NULL == peer) 968 if (NULL == peer)
976 { 969 {
977 GNUNET_CONFIGURATION_destroy (cfg); 970 GNUNET_CONFIGURATION_destroy (cfg);
978 GNUNET_TESTING_hostkeys_unload (system); 971 hostkeys_unload (system);
979 GNUNET_TESTING_system_destroy (system, GNUNET_YES); 972 GNUNET_TESTING_system_destroy (system, GNUNET_YES);
980 return 1; 973 return 1;
981 } 974 }
982 GNUNET_TESTING_hostkeys_unload (system);
983 GNUNET_free (peer->main_binary); 975 GNUNET_free (peer->main_binary);
984 GNUNET_asprintf (&peer->main_binary, "gnunet-service-%s", service_name); 976 GNUNET_asprintf (&peer->main_binary, "gnunet-service-%s", service_name);
985 if (GNUNET_OK != GNUNET_TESTING_peer_start (peer)) 977 if (GNUNET_OK != GNUNET_TESTING_peer_start (peer))