From 26e893cb7ce6050354c44d32430d5be2df13f6a7 Mon Sep 17 00:00:00 2001 From: "Nathan S. Evans" Date: Fri, 21 Jan 2011 15:19:25 +0000 Subject: enable reading of hostkeys from a file for testing, also loading of topology from topology file --- src/include/gnunet_testing_lib.h | 8 +- src/testing/test_testing.c | 2 +- src/testing/test_testing_connect.c | 4 +- src/testing/test_testing_data_topology_clique.conf | 1 + src/testing/test_testing_topology.c | 2 +- src/testing/testing.c | 28 +++ src/testing/testing_group.c | 237 +++++++++++++-------- 7 files changed, 184 insertions(+), 98 deletions(-) (limited to 'src') diff --git a/src/include/gnunet_testing_lib.h b/src/include/gnunet_testing_lib.h index 6ad3e408c..7830ac226 100644 --- a/src/include/gnunet_testing_lib.h +++ b/src/include/gnunet_testing_lib.h @@ -43,6 +43,8 @@ extern "C" #endif #endif +#define HOSTKEYFILESIZE 914 + /** * Handle for a GNUnet daemon (technically a set of * daemons; the handle is really for the master ARM @@ -409,6 +411,7 @@ typedef void (*GNUNET_TESTING_NotifyTopology)(void *cls, * (use NULL for localhost). * @param ssh_username ssh username to use when connecting to hostname * @param sshport port to pass to ssh process when connecting to hostname + * @param hostkey pointer to a hostkey to be written to disk (instead of being generated) * @param hostkey_callback function to call once the hostkey has been * generated for this peer, but it hasn't yet been started * (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start) @@ -423,8 +426,9 @@ GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *hostname, const char *ssh_username, uint16_t sshport, - GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback, - void *hostkey_cls, + const char *hostkey, + GNUNET_TESTING_NotifyHostkeyCreated + hostkey_callback, void *hostkey_cls, GNUNET_TESTING_NotifyDaemonRunning cb, void *cb_cls); diff --git a/src/testing/test_testing.c b/src/testing/test_testing.c index 64bf7572a..880ecb399 100644 --- a/src/testing/test_testing.c +++ b/src/testing/test_testing.c @@ -76,7 +76,7 @@ run (void *cls, GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting daemon.\n"); #endif d = - GNUNET_TESTING_daemon_start (cfg, TIMEOUT, NULL, NULL, 0, NULL, NULL, + GNUNET_TESTING_daemon_start (cfg, TIMEOUT, NULL, NULL, 0, NULL, NULL, NULL, &my_cb, NULL); GNUNET_assert (d != NULL); } diff --git a/src/testing/test_testing_connect.c b/src/testing/test_testing_connect.c index 2c3014635..f42142319 100644 --- a/src/testing/test_testing_connect.c +++ b/src/testing/test_testing_connect.c @@ -133,7 +133,7 @@ my_cb1 (void *cls, "Daemon `%s' started.\n", GNUNET_i2s (id)); #endif d2 = - GNUNET_TESTING_daemon_start (c2, TIMEOUT, NULL, NULL, 0, NULL, NULL, + GNUNET_TESTING_daemon_start (c2, TIMEOUT, NULL, NULL, 0, NULL, NULL, NULL, &my_cb2, NULL); GNUNET_assert (d2 != NULL); @@ -154,7 +154,7 @@ run (void *cls, c2 = GNUNET_CONFIGURATION_create (); GNUNET_CONFIGURATION_parse (c2, "test_testing_connect_peer2.conf"); d1 = - GNUNET_TESTING_daemon_start (c1, TIMEOUT, NULL, NULL, 0, NULL, NULL, + GNUNET_TESTING_daemon_start (c1, TIMEOUT, NULL, NULL, 0, NULL, NULL, NULL, &my_cb1, NULL); GNUNET_assert (d1 != NULL); } diff --git a/src/testing/test_testing_data_topology_clique.conf b/src/testing/test_testing_data_topology_clique.conf index 012c2cab4..7858d7661 100644 --- a/src/testing/test_testing_data_topology_clique.conf +++ b/src/testing/test_testing_data_topology_clique.conf @@ -23,6 +23,7 @@ PORT = 2567 [transport-tcp] PORT = 2568 +ENABLE_UPNP = NO [transport-udp] PORT = 2568 diff --git a/src/testing/test_testing_topology.c b/src/testing/test_testing_topology.c index cf1b3232c..8ad864be7 100644 --- a/src/testing/test_testing_topology.c +++ b/src/testing/test_testing_topology.c @@ -26,7 +26,7 @@ #include "gnunet_core_service.h" #include "gnunet_os_lib.h" -#define VERBOSE GNUNET_NO +#define VERBOSE GNUNET_YES #define DELAY_FOR_LOGGING GNUNET_NO diff --git a/src/testing/testing.c b/src/testing/testing.c index 75eab1a06..a24c1feae 100644 --- a/src/testing/testing.c +++ b/src/testing/testing.c @@ -385,6 +385,7 @@ start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) return; } d->hostkeybuf[103] = '\0'; + if ((bytes_read < 0) || (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (d->hostkeybuf, &d->id.hashPubKey))) @@ -789,6 +790,7 @@ GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon, * (use NULL for localhost). * @param ssh_username ssh username to use when connecting to hostname * @param sshport port to pass to ssh process when connecting to hostname + * @param hostkey pointer to a hostkey to be written to disk (instead of being generated) * @param hostkey_callback function to call once the hostkey has been * generated for this peer, but it hasn't yet been started * (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start) @@ -803,6 +805,7 @@ GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *hostname, const char *ssh_username, uint16_t sshport, + const char *hostkey, GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback, void *hostkey_cls, GNUNET_TESTING_NotifyDaemonRunning cb, @@ -811,6 +814,9 @@ GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg, struct GNUNET_TESTING_Daemon *ret; char *arg; char *username; + char *servicehome; + char *hostkeyfile; + struct GNUNET_DISK_FileHandle *fn; ret = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Daemon)); ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname); @@ -842,6 +848,28 @@ GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_CONFIGURATION_set_value_string (ret->cfg, "PATHS", "DEFAULTCONFIG", ret->cfgfile); + if (hostkey != NULL) + { + GNUNET_assert(GNUNET_OK == + GNUNET_CONFIGURATION_get_value_string (ret->cfg, + "PATHS", + "SERVICEHOME", + &servicehome)); + GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_create (servicehome)); + GNUNET_asprintf(&hostkeyfile, "%s/.hostkey", servicehome); + fn = + GNUNET_DISK_file_open (hostkeyfile, + GNUNET_DISK_OPEN_READWRITE + | GNUNET_DISK_OPEN_CREATE, + GNUNET_DISK_PERM_USER_READ | + GNUNET_DISK_PERM_USER_WRITE); + GNUNET_assert(fn != NULL); + GNUNET_assert(HOSTKEYFILESIZE == GNUNET_DISK_file_write(fn, hostkey, HOSTKEYFILESIZE)); + GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fn)); + GNUNET_free(servicehome); + GNUNET_free(hostkeyfile); + } + /* 1) write configuration to temporary file */ if (GNUNET_OK != GNUNET_CONFIGURATION_write (ret->cfg, ret->cfgfile)) { diff --git a/src/testing/testing_group.c b/src/testing/testing_group.c index f3eeb9069..78de644f4 100644 --- a/src/testing/testing_group.c +++ b/src/testing/testing_group.c @@ -303,6 +303,11 @@ struct InternalStartContext */ const char *username; + /** + * Pointer to starting memory location of a hostkey + */ + const char *hostkey; + /** * Port to use for ssh. */ @@ -629,6 +634,96 @@ struct UnblacklistContext uint32_t first_uid; }; +struct RandomContext +{ + /** + * The peergroup + */ + struct GNUNET_TESTING_PeerGroup *pg; + + /** + * uid of the first peer + */ + uint32_t first_uid; + + /** + * Peer data for first peer. + */ + struct PeerData *first; + + /** + * Random percentage to use + */ + double percentage; +}; + +struct MinimumContext +{ + /** + * The peergroup + */ + struct GNUNET_TESTING_PeerGroup *pg; + + /** + * uid of the first peer + */ + uint32_t first_uid; + + /** + * Peer data for first peer. + */ + struct PeerData *first; + + /** + * Number of conns per peer + */ + unsigned int num_to_add; + + /** + * Permuted array of all possible connections. Only add the Nth + * peer if it's in the Nth position. + */ + unsigned int *pg_array; + + /** + * What number is the current element we are iterating over? + */ + unsigned int current; +}; + +struct DFSContext +{ + /** + * The peergroup + */ + struct GNUNET_TESTING_PeerGroup *pg; + + /** + * uid of the first peer + */ + uint32_t first_uid; + + /** + * uid of the second peer + */ + uint32_t second_uid; + + /** + * Peer data for first peer. + */ + struct PeerData *first; + + /** + * Which peer has been chosen as the one to add? + */ + unsigned int chosen; + + /** + * What number is the current element we are iterating over? + */ + unsigned int current; +}; + /** * Convert unique ID to hash code. * @@ -2100,6 +2195,7 @@ create_from_file (struct GNUNET_TESTING_PeerGroup *pg, return connect_attempts; } GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Read %u total peers in topology\n", total_peers); + GNUNET_assert(total_peers == pg->total); curr_state = PEER_INDEX; while((buf[count] != '\n') && (count < frstat.st_size - 1)) count++; @@ -2131,18 +2227,19 @@ create_from_file (struct GNUNET_TESTING_PeerGroup *pg, return connect_attempts; } GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Read second peer index %u\n", second_peer_index); - while((buf[count] != '\n') && (buf[count] != ' ') && (count < frstat.st_size - 1)) + /* Assume file is written with first peer 1, but array index is 0 */ + connect_attempts += proc (pg, first_peer_index - 1, second_peer_index - 1); + while((buf[count] != '\n') && (buf[count] != ',') && (count < frstat.st_size - 1)) count++; if (buf[count] == '\n') { curr_state = PEER_INDEX; } - else if (buf[count] != ' ') + else if (buf[count] != ',') { curr_state = OTHER_PEER_INDEX; } count++; - curr_state = OTHER_PEER_INDEX; break; default: GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Found bad data in topology file while in state %d!\n", curr_state); @@ -3078,95 +3175,6 @@ GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg, return num_connections; } -struct RandomContext -{ - /** - * The peergroup - */ - struct GNUNET_TESTING_PeerGroup *pg; - - /** - * uid of the first peer - */ - uint32_t first_uid; - - /** - * Peer data for first peer. - */ - struct PeerData *first; - - /** - * Random percentage to use - */ - double percentage; -}; - -struct MinimumContext -{ - /** - * The peergroup - */ - struct GNUNET_TESTING_PeerGroup *pg; - - /** - * uid of the first peer - */ - uint32_t first_uid; - - /** - * Peer data for first peer. - */ - struct PeerData *first; - - /** - * Number of conns per peer - */ - unsigned int num_to_add; - - /** - * Permuted array of all possible connections. Only add the Nth - * peer if it's in the Nth position. - */ - unsigned int *pg_array; - - /** - * What number is the current element we are iterating over? - */ - unsigned int current; -}; - -struct DFSContext -{ - /** - * The peergroup - */ - struct GNUNET_TESTING_PeerGroup *pg; - - /** - * uid of the first peer - */ - uint32_t first_uid; - - /** - * uid of the second peer - */ - uint32_t second_uid; - - /** - * Peer data for first peer. - */ - struct PeerData *first; - - /** - * Which peer has been chosen as the one to add? - */ - unsigned int chosen; - - /** - * What number is the current element we are iterating over? - */ - unsigned int current; -}; /** * Iterator for choosing random peers to connect. @@ -4324,6 +4332,7 @@ internal_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) internal_context->hostname, internal_context->username, internal_context->sshport, + internal_context->hostkey, &internal_hostkey_callback, internal_context, &internal_startup_callback, @@ -4405,6 +4414,9 @@ GNUNET_TESTING_daemons_start (const struct GNUNET_CONFIGURATION_Handle *cfg, char *baseservicehome; char *newservicehome; char *tmpdir; + char *hostkeys_file; + char *hostkey_data; + struct GNUNET_DISK_FileHandle *fd; struct GNUNET_CONFIGURATION_Handle *pcfg; unsigned int off; unsigned int hostcnt; @@ -4412,12 +4424,15 @@ GNUNET_TESTING_daemons_start (const struct GNUNET_CONFIGURATION_Handle *cfg, uint16_t sshport; uint32_t upnum; uint32_t fdnum; + uint64_t fs; + uint64_t total_hostkeys; if (0 == total) { GNUNET_break (0); return NULL; } + hostkey_data = NULL; upnum = 0; fdnum = 0; pg = GNUNET_malloc (sizeof (struct GNUNET_TESTING_PeerGroup)); @@ -4509,6 +4524,42 @@ GNUNET_TESTING_daemons_start (const struct GNUNET_CONFIGURATION_Handle *cfg, hostcnt = 0; minport = LOW_PORT; } + + if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "HOSTKEYSFILE", + &hostkeys_file)) + { + if (GNUNET_YES != GNUNET_DISK_file_test (hostkeys_file)) + GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Couldn't read hostkeys file!"); + else + { + /* Check hostkey file size, read entire thing into memory */ + fd = GNUNET_DISK_file_open (hostkeys_file, GNUNET_DISK_OPEN_READ, + GNUNET_DISK_PERM_NONE); + if (NULL == fd) + { + GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", hostkeys_file); + return NULL; + } + + if (GNUNET_YES != GNUNET_DISK_file_size (hostkeys_file, &fs, GNUNET_YES)) + fs = 0; + + GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Found file size %llu for hostkeys, expect hostkeys to be size %d\n", fs, HOSTKEYFILESIZE); + + if (fs % HOSTKEYFILESIZE != 0) + { + GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "File size %llu seems incorrect for hostkeys...\n", fs); + } + else + { + total_hostkeys = fs / HOSTKEYFILESIZE; + GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Will read %llu hostkeys from file\n", total_hostkeys); + hostkey_data = GNUNET_malloc_large (fs); + GNUNET_assert (fs == GNUNET_DISK_file_read (fd, hostkey_data, fs)); + } + } + } + for (off = 0; off < total; off++) { if (hostcnt > 0) @@ -4570,6 +4621,8 @@ GNUNET_TESTING_daemons_start (const struct GNUNET_CONFIGURATION_Handle *cfg, pg->peers[off].internal_context.hostname = hostname; pg->peers[off].internal_context.username = username; pg->peers[off].internal_context.sshport = sshport; + if (hostkey_data != NULL) + pg->peers[off].internal_context.hostkey = &hostkey_data[off * HOSTKEYFILESIZE]; pg->peers[off].internal_context.hostkey_callback = hostkey_callback; pg->peers[off].internal_context.hostkey_cls = hostkey_cls; pg->peers[off].internal_context.start_cb = cb; -- cgit v1.2.3