/*
This file is part of GNUnet.
- (C) 2004, 2005, 2006 Christian Grothoff (and other contributing authors)
+ (C) 2004, 2005, 2006, 2008, 2009, 2010 Christian Grothoff (and other contributing authors)
GNUnet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
- by the Free Software Foundation; either version 2, or (at your
+ by the Free Software Foundation; either version 3, or (at your
option) any later version.
GNUnet is distributed in the hope that it will be useful, but
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
-
/**
- * @file applications/fs/fsui/search_persistence_test.c
- * @brief testcase for fsui download persistence for search
+ * @file fs/test_fs_search_persistence.c
+ * @brief simple testcase for persistence of search operation
* @author Christian Grothoff
*/
-
#include "platform.h"
-#include "gnunet_util.h"
-#include "gnunet_fsui_lib.h"
+#include "gnunet_util_lib.h"
+#include "gnunet_testing_lib.h"
+#include "gnunet_fs_service.h"
+
+
+/**
+ * File-size we use for testing.
+ */
+#define FILESIZE 1024
+
+/**
+ * How long until we give up on transmitting the message?
+ */
+#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
+
+/**
+ * How long should our test-content live?
+ */
+#define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
+
+
+static struct GNUNET_TIME_Absolute start;
+
+static struct GNUNET_FS_Handle *fs;
-#define DEBUG_VERBOSE GNUNET_NO
+static struct GNUNET_FS_SearchContext *search;
-#define UPLOAD_PREFIX "/tmp/gnunet-fsui-search_persistence_test"
+static struct GNUNET_FS_PublishContext *publish;
-#define CHECK(a) if (!(a)) { ok = GNUNET_NO; GNUNET_GE_BREAK(ectx, 0); goto FAILURE; }
+static const struct GNUNET_CONFIGURATION_Handle *cfg;
-static struct GNUNET_GE_Context *ectx;
+static GNUNET_SCHEDULER_TaskIdentifier timeout_task;
+
+static int err;
+
+
+static void
+abort_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ timeout_task = GNUNET_SCHEDULER_NO_TASK;
+ fprintf (stderr,
+ "Timeout\n");
+ if (NULL != search)
+ {
+ GNUNET_FS_search_stop (search);
+ search = NULL;
+ }
+ if (NULL != publish)
+ {
+ GNUNET_FS_publish_stop (publish);
+ publish = NULL;
+ }
+ err = 1;
+}
+
+static void
+abort_publish_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ if (NULL != publish)
+ {
+ GNUNET_FS_publish_stop (publish);
+ publish = NULL;
+ }
+ if (GNUNET_SCHEDULER_NO_TASK != timeout_task)
+ {
+ GNUNET_SCHEDULER_cancel (timeout_task);
+ timeout_task = GNUNET_SCHEDULER_NO_TASK;
+ }
+}
+
+
+static void
+abort_search_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ if (NULL != search)
+ {
+ GNUNET_FS_search_stop (search);
+ search = NULL;
+ }
+}
-static struct GNUNET_FSUI_Context *ctx;
-static struct GNUNET_FSUI_SearchList *search;
-static int have_error;
static void *
-eventCallback (void *cls, const GNUNET_FSUI_Event * event)
+progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event);
+
+
+static void
+restart_fs_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
- switch (event->type)
- {
- case GNUNET_FSUI_search_suspended:
+ GNUNET_FS_stop (fs);
+ fs = GNUNET_FS_start (cfg, "test-fs-search-persistence", &progress_cb, NULL,
+ GNUNET_FS_FLAGS_PERSISTENCE, GNUNET_FS_OPTIONS_END);
+}
+
+
+/**
+ * Consider scheduling the restart-task.
+ * Only runs the restart task once per event
+ * category.
+ *
+ * @param ev type of the event to consider
+ */
+static void
+consider_restart (int ev)
+{
+ static int prev[32];
+ static int off;
+ int i;
+
+ for (i = 0; i < off; i++)
+ if (prev[i] == ev)
+ return;
+ prev[off++] = ev;
+ GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_URGENT,
+ &restart_fs_task, NULL);
+}
+
+
+static void *
+progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
+{
+ const char *keywords[] = {
+ "down_foo"
+ };
+ struct GNUNET_FS_Uri *kuri;
+
+ switch (event->status)
+ {
+ case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Publish is progressing (%llu/%llu at level %u off %llu)...\n",
+ (unsigned long long) event->value.publish.completed,
+ (unsigned long long) event->value.publish.size,
+ event->value.publish.specifics.progress.depth,
+ (unsigned long long) event->value.publish.specifics.
+ progress.offset);
+ break;
+ case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
+ kuri = GNUNET_FS_uri_ksk_create_from_args (1, keywords);
+ start = GNUNET_TIME_absolute_get ();
+ GNUNET_FS_search_start (fs, kuri, 1, GNUNET_FS_SEARCH_OPTION_NONE,
+ "search");
+ GNUNET_FS_uri_destroy (kuri);
+ GNUNET_assert (search != NULL);
+ break;
+ case GNUNET_FS_STATUS_PUBLISH_SUSPEND:
+ if (event->value.publish.pc == publish)
+ publish = NULL;
+ break;
+ case GNUNET_FS_STATUS_PUBLISH_RESUME:
+ if (NULL == publish)
+ publish = event->value.publish.pc;
+ break;
+ case GNUNET_FS_STATUS_SEARCH_RESULT:
+ /* FIXME: consider_restart (event->status); cannot be tested with
+ * search result since we exit here after the first one... */
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Search complete.\n");
+ GNUNET_SCHEDULER_add_continuation (&abort_search_task, NULL,
+ GNUNET_SCHEDULER_REASON_PREREQ_DONE);
+ break;
+ case GNUNET_FS_STATUS_PUBLISH_ERROR:
+ FPRINTF (stderr, "Error publishing file: %s\n",
+ event->value.publish.specifics.error.message);
+ GNUNET_break (0);
+ GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
+ GNUNET_SCHEDULER_REASON_PREREQ_DONE);
+ break;
+ case GNUNET_FS_STATUS_SEARCH_ERROR:
+ FPRINTF (stderr, "Error searching file: %s\n",
+ event->value.search.specifics.error.message);
+ GNUNET_SCHEDULER_add_continuation (&abort_search_task, NULL,
+ GNUNET_SCHEDULER_REASON_PREREQ_DONE);
+ break;
+ case GNUNET_FS_STATUS_SEARCH_SUSPEND:
+ if (event->value.search.sc == search)
search = NULL;
- break;
- case GNUNET_FSUI_search_resumed:
-#if DEBUG_VERBOSE
- printf ("Search resuming\n");
-#endif
- search = event->data.SearchResumed.sc.pos;
- break;
- case GNUNET_FSUI_search_result:
-#if DEBUG_VERBOSE
- printf ("Received search result\n");
-#endif
- break;
- case GNUNET_FSUI_upload_progress:
-#if DEBUG_VERBOSE
- printf ("Upload is progressing (%llu/%llu)...\n",
- event->data.UploadProgress.completed,
- event->data.UploadProgress.total);
-#endif
- break;
- case GNUNET_FSUI_upload_completed:
-#if DEBUG_VERBOSE
- printf ("Upload complete.\n");
-#endif
- break;
- case GNUNET_FSUI_unindex_progress:
-#if DEBUG_VERBOSE
- printf ("Unindex is progressing (%llu/%llu)...\n",
- event->data.UnindexProgress.completed,
- event->data.UnindexProgress.total);
-#endif
- break;
- case GNUNET_FSUI_unindex_completed:
-#if DEBUG_VERBOSE
- printf ("Unindex complete.\n");
-#endif
- break;
- case GNUNET_FSUI_unindex_error:
- case GNUNET_FSUI_upload_error:
- case GNUNET_FSUI_download_error:
- fprintf (stderr, "Received ERROR: %d\n", event->type);
- GNUNET_GE_BREAK (ectx, 0);
- break;
- case GNUNET_FSUI_download_aborted:
-#if DEBUG_VERBOSE
- printf ("Received download aborted event.\n");
-#endif
- break;
- case GNUNET_FSUI_unindex_suspended:
- case GNUNET_FSUI_upload_suspended:
-#if DEBUG_VERBOSE
- fprintf (stderr, "Received SUSPENDING: %d\n", event->type);
-#endif
- break;
- case GNUNET_FSUI_upload_started:
- case GNUNET_FSUI_upload_stopped:
- case GNUNET_FSUI_search_started:
- case GNUNET_FSUI_search_aborted:
- case GNUNET_FSUI_search_stopped:
- case GNUNET_FSUI_search_update:
- case GNUNET_FSUI_unindex_started:
- case GNUNET_FSUI_unindex_stopped:
- break;
- default:
- printf ("Unexpected event: %d\n", event->type);
- break;
+ break;
+ case GNUNET_FS_STATUS_SEARCH_RESUME:
+ if (NULL == search)
+ {
+ search = event->value.search.sc;
+ return "search";
}
+ break;
+ case GNUNET_FS_STATUS_PUBLISH_START:
+ GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
+ GNUNET_assert (NULL == event->value.publish.pctx);
+ GNUNET_assert (FILESIZE == event->value.publish.size);
+ GNUNET_assert (0 == event->value.publish.completed);
+ GNUNET_assert (1 == event->value.publish.anonymity);
+ break;
+ case GNUNET_FS_STATUS_PUBLISH_STOPPED:
+ GNUNET_assert (publish == event->value.publish.pc);
+ GNUNET_assert (FILESIZE == event->value.publish.size);
+ GNUNET_assert (1 == event->value.publish.anonymity);
+ GNUNET_FS_stop (fs);
+ fs = NULL;
+ break;
+ case GNUNET_FS_STATUS_SEARCH_START:
+ consider_restart (event->status);
+ GNUNET_assert (search == NULL);
+ search = event->value.search.sc;
+ GNUNET_assert (0 == strcmp ("search", event->value.search.cctx));
+ GNUNET_assert (1 == event->value.search.anonymity);
+ break;
+ case GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED:
+ break;
+ case GNUNET_FS_STATUS_SEARCH_STOPPED:
+ GNUNET_assert (search == event->value.search.sc);
+ GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
+ GNUNET_SCHEDULER_REASON_PREREQ_DONE);
+ search = NULL;
+ break;
+ default:
+ FPRINTF (stderr, "Unexpected event: %d\n", event->status);
+ break;
+ }
return NULL;
}
-#define FILESIZE (1024)
-#define START_DAEMON 1
-
-int
-main (int argc, char *argv[])
+static void
+run (void *cls,
+ const struct GNUNET_CONFIGURATION_Handle *c,
+ struct GNUNET_TESTING_Peer *peer)
{
-#if START_DAEMON
- pid_t daemon;
-#endif
- int ok;
- struct GNUNET_ECRS_URI *uri = NULL;
- char *keywords[] = {
+ const char *keywords[] = {
"down_foo",
- "down_bar",
+ "down_bar"
};
- char keyword[40];
- int prog;
- struct GNUNET_GC_Configuration *cfg;
- int suspendRestart = 0;
+ char *buf;
+ struct GNUNET_CONTAINER_MetaData *meta;
+ struct GNUNET_FS_Uri *kuri;
+ struct GNUNET_FS_FileInformation *fi;
+ size_t i;
+ struct GNUNET_FS_BlockOptions bo;
+ cfg = c;
+ fs = GNUNET_FS_start (cfg, "test-fs-search-persistence", &progress_cb, NULL,
+ GNUNET_FS_FLAGS_PERSISTENCE, GNUNET_FS_OPTIONS_END);
+ GNUNET_assert (NULL != fs);
+ buf = GNUNET_malloc (FILESIZE);
+ for (i = 0; i < FILESIZE; i++)
+ buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
+ meta = GNUNET_CONTAINER_meta_data_create ();
+ kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
+ bo.content_priority = 42;
+ bo.anonymity_level = 1;
+ bo.replication_level = 0;
+ bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
+ fi = GNUNET_FS_file_information_create_from_data (fs, "publish-context",
+ FILESIZE, buf, kuri, meta,
+ GNUNET_NO, &bo);
+ GNUNET_FS_uri_destroy (kuri);
+ GNUNET_CONTAINER_meta_data_destroy (meta);
+ GNUNET_assert (NULL != fi);
+ start = GNUNET_TIME_absolute_get ();
+ publish =
+ GNUNET_FS_publish_start (fs, fi, NULL, NULL, NULL,
+ GNUNET_FS_PUBLISH_OPTION_NONE);
+ GNUNET_assert (publish != NULL);
+ timeout_task = GNUNET_SCHEDULER_add_delayed (LIFETIME,
+ &abort_error, NULL);
+}
- ok = GNUNET_YES;
- cfg = GNUNET_GC_create ();
- if (-1 == GNUNET_GC_parse_configuration (cfg, "check.conf"))
- {
- GNUNET_GC_free (cfg);
- return -1;
- }
-#if START_DAEMON
- daemon = GNUNET_daemon_start (NULL, cfg, "peer.conf", GNUNET_NO);
- GNUNET_GE_ASSERT (NULL, daemon > 0);
- CHECK (GNUNET_OK ==
- GNUNET_wait_for_daemon_running (NULL, cfg,
- 30 * GNUNET_CRON_SECONDS));
- GNUNET_thread_sleep (5 * GNUNET_CRON_SECONDS); /* give apps time to start */
- /* ACTUAL TEST CODE */
-#endif
- ctx = GNUNET_FSUI_start (NULL,
- cfg, "search_persistence_test", 32, GNUNET_YES,
- &eventCallback, NULL);
- CHECK (ctx != NULL);
- GNUNET_snprintf (keyword, 40, "+%s +%s", keywords[0], keywords[1]);
- uri = GNUNET_ECRS_keyword_string_to_uri (ectx, keyword);
- search = GNUNET_FSUI_search_start (ctx, 0, uri);
- CHECK (search != NULL);
- prog = 0;
- suspendRestart = 10;
- while (prog < 100)
- {
- prog++;
- GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
- if ((suspendRestart > 0)
- && (GNUNET_random_u32 (GNUNET_RANDOM_QUALITY_WEAK, 10) == 0))
- {
-#if 1
-#if DEBUG_VERBOSE
- printf ("Testing FSUI suspend-resume\n");
-#endif
- GNUNET_FSUI_stop (ctx); /* download possibly incomplete
- at this point, thus testing resume */
- CHECK (search == NULL);
- ctx = GNUNET_FSUI_start (NULL,
- cfg,
- "search_persistence_test", 32, GNUNET_YES,
- &eventCallback, NULL);
-#if DEBUG_VERBOSE
- printf ("Resumed...\n");
-#endif
-#endif
- suspendRestart--;
- }
- if (GNUNET_shutdown_test () == GNUNET_YES)
- break;
- }
- GNUNET_FSUI_search_abort (search);
- GNUNET_FSUI_search_stop (search);
- search = NULL;
- /* END OF TEST CODE */
-FAILURE:
- if (ctx != NULL)
- GNUNET_FSUI_stop (ctx);
- if (uri != NULL)
- GNUNET_ECRS_uri_destroy (uri);
-
-#if START_DAEMON
- GNUNET_GE_BREAK (NULL, GNUNET_OK == GNUNET_daemon_stop (NULL, daemon));
-#endif
- GNUNET_GC_free (cfg);
- if (have_error)
- ok = GNUNET_NO;
- return (ok == GNUNET_YES) ? 0 : 1;
+
+int
+main (int argc, char *argv[])
+{
+ if (0 != GNUNET_TESTING_peer_run ("test-fs-search-persistence",
+ "test_fs_search_data.conf",
+ &run, NULL))
+ return 1;
+ return err;
}
-/* end of search_persistence_test.c */
+/* end of test_fs_search_persistence.c */