+/**
+ * Clean up the transport testing
+ * @param tth transport testing handle
+ */
+void
+GNUNET_TRANSPORT_TESTING_done (struct GNUNET_TRANSPORT_TESTING_handle *tth)
+{
+ struct ConnectingContext *cc = tth->cc_head;
+ struct ConnectingContext *ct = NULL;
+ struct PeerContext *p = tth->p_head;
+ struct PeerContext *t = NULL;
+
+ GNUNET_assert (tth != NULL);
+
+ while (cc != tth->cc_tail)
+ {
+ ct = cc->next;
+ GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
+ "Developer forgot to cancel connect request %X!\n", cc);
+ GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
+ cc = ct;
+ }
+
+ while (p != NULL)
+ {
+ t = p->next;
+ GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
+ "Developer forgot to stop peer!\n");
+ GNUNET_TRANSPORT_TESTING_stop_peer (tth, p);
+ p = t;
+ }
+
+ GNUNET_free_non_null (tth->hostkey_data);
+
+ GNUNET_free (tth);
+ tth = NULL;
+}
+
+/**
+ * Initialize the transport testing
+ * @return transport testing handle
+ */
+struct GNUNET_TRANSPORT_TESTING_handle *
+GNUNET_TRANSPORT_TESTING_init ()
+{
+ struct GNUNET_TRANSPORT_TESTING_handle *tth;
+ struct GNUNET_DISK_FileHandle *fd;
+ uint64_t fs;
+ uint64_t total_hostkeys;
+
+
+ /* prepare hostkeys */
+ tth = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_TESTING_handle));
+ tth->hostkey_data = NULL;
+ const char *hostkeys_file = "../../contrib/testing_hostkeys.dat";
+
+ if (GNUNET_YES != GNUNET_DISK_file_test (hostkeys_file))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not read hostkeys file!\n"));
+ }
+ 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);
+ GNUNET_free (tth);
+ return NULL;
+ }
+
+ if (GNUNET_YES != GNUNET_DISK_file_size (hostkeys_file, &fs, GNUNET_YES))
+ fs = 0;
+
+ if (0 != (fs % HOSTKEYFILESIZE))
+ {
+ GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
+ "File size %llu seems incorrect for hostkeys...\n", fs);
+ }
+ else
+ {
+ total_hostkeys = fs / HOSTKEYFILESIZE;
+ tth->hostkey_data = GNUNET_malloc_large (fs);
+ GNUNET_assert (fs == GNUNET_DISK_file_read (fd, tth->hostkey_data, fs));
+ GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
+ "Read %llu hostkeys from file\n", total_hostkeys);
+ tth->hostkeys_total = total_hostkeys;
+ }
+ GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
+ }
+
+ return tth;
+}
+
+
+/*
+ * Some utility functions
+ */
+
+/**
+ * Removes all directory separators from absolute filename
+ * @param file the absolute file name, e.g. as found in argv[0]
+ * @return extracted file name, has to be freed by caller
+ */
+static char *
+extract_filename (const char *file)
+{
+ char *pch = GNUNET_strdup (file);
+ char *backup = pch;
+ char *filename = NULL;
+ char *res;
+
+ if (NULL != strstr (pch, "/"))
+ {
+ pch = strtok (pch, "/");
+ while (pch != NULL)
+ {
+ pch = strtok (NULL, "/");
+ if (pch != NULL)
+ {
+ filename = pch;
+ }
+ }
+ }
+ else
+ filename = pch;
+
+ res = GNUNET_strdup (filename);
+ GNUNET_free (backup);
+ return res;
+}
+
+/**
+ * Extracts the test filename from an absolute file name and removes the extension
+ * @param file absolute file name
+ * @param dest where to store result
+ */
+void
+GNUNET_TRANSPORT_TESTING_get_test_name (const char *file, char **dest)
+{
+ char *filename = extract_filename (file);
+ char *backup = filename;
+ char *dotexe;
+
+ if (filename == NULL)
+ goto fail;
+
+ /* remove "lt-" */
+ filename = strstr (filename, "tes");
+ if (filename == NULL)
+ goto fail;
+
+ /* remove ".exe" */
+ if (NULL != (dotexe = strstr (filename, ".exe")))
+ dotexe[0] = '\0';
+
+ goto suc;
+
+fail:
+ (*dest) = NULL;
+ return;
+
+suc:
+ /* create filename */
+ GNUNET_asprintf (dest, "%s", filename);
+ GNUNET_free (backup);
+}
+
+
+/**
+ * Extracts the filename from an absolute file name and removes the extension
+ * @param file absolute file name
+ * @param dest where to store result
+ */
+void
+GNUNET_TRANSPORT_TESTING_get_test_source_name (const char *file, char **dest)
+{
+ char *src = extract_filename (file);
+ char *split;
+
+ split = strstr (src, ".");
+ if (split != NULL)
+ {
+ split[0] = '\0';
+ }
+ GNUNET_asprintf (dest, "%s", src);
+ GNUNET_free (src);
+}
+
+
+/**
+ * Extracts the plugin anme from an absolute file name and the test name
+ * @param file absolute file name
+ * @param test test name
+ * @param dest where to store result
+ */
+void
+GNUNET_TRANSPORT_TESTING_get_test_plugin_name (const char *file,
+ const char *test, char **dest)
+{
+ char *e = extract_filename (file);
+ char *t = extract_filename (test);
+
+ char *filename = NULL;
+ char *dotexe;
+
+ if (e == NULL)
+ goto fail;
+
+ /* remove "lt-" */
+ filename = strstr (e, "tes");
+ if (filename == NULL)
+ goto fail;
+
+ /* remove ".exe" */
+ if (NULL != (dotexe = strstr (filename, ".exe")))
+ dotexe[0] = '\0';
+
+ /* find last _ */
+ filename = strstr (filename, t);
+ if (filename == NULL)
+ goto fail;
+
+ /* copy plugin */
+ filename += strlen (t);
+ filename++;
+ GNUNET_asprintf (dest, "%s", filename);
+ goto suc;
+
+fail:
+ (*dest) = NULL;
+suc:
+ GNUNET_free (t);
+ GNUNET_free (e);
+
+}
+
+/**
+ * This function takes the filename (e.g. argv[0), removes a "lt-"-prefix and
+ * if existing ".exe"-prefix and adds the peer-number
+ *
+ * @param file filename of the test, e.g. argv[0]
+ * @param dest where to write the filename
+ * @param count peer number
+ */
+void
+GNUNET_TRANSPORT_TESTING_get_config_name (const char *file, char **dest,
+ int count)
+{
+ char *filename = extract_filename (file);
+ char *backup = filename;
+ char *dotexe;
+
+ if (filename == NULL)
+ goto fail;
+
+ /* remove "lt-" */
+ filename = strstr (filename, "tes");
+ if (filename == NULL)
+ goto fail;
+
+ /* remove ".exe" */
+ if (NULL != (dotexe = strstr (filename, ".exe")))
+ dotexe[0] = '\0';
+
+ goto suc;
+
+fail:
+ (*dest) = NULL;
+ return;
+
+suc:
+ /* create cfg filename */
+ GNUNET_asprintf (dest, "%s_peer%u.conf", filename, count);
+ GNUNET_free (backup);
+}
+
+
+