Merge branch 'master' of git+ssh://gnunet.org/gnunet
[oweals/gnunet.git] / src / fs / test_fs.c
index 5eedda9824282708d9a68f38cb8f421b4b05399d..6fdc589bfa9118975f403d41e82cc10343882900 100644 (file)
@@ -1,10 +1,10 @@
 /*
      This file is part of GNUnet.
 /*
      This file is part of GNUnet.
-     (C) 2004, 2005, 2006, 2008 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2004, 2005, 2006, 2008 GNUnet e.V.
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
 
      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
      option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
 
      You should have received a copy of the GNU General Public License
      along with GNUnet; see the file COPYING.  If not, write to the
 
      You should have received a copy of the GNU General Public License
      along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
 */
 
 /**
 */
 
 /**
- * @file applications/fs/fsui/basic_fsui_test.c
- * @brief testcase for fsui (upload-search-download-unindex)
+ * @file fs/test_fs.c
+ * @brief testcase for FS (upload-search-download-unindex)
  * @author Christian Grothoff
  */
 
  * @author Christian Grothoff
  */
 
@@ -37,17 +37,16 @@ makeName (unsigned int i)
 {
   char *fn;
 
 {
   char *fn;
 
-  fn =
-    GNUNET_malloc (strlen ("/tmp/gnunet-basic_fsui_test/BASIC_FSUI_TEST") +
-                   14);
+  fn = GNUNET_malloc (strlen ("/tmp/gnunet-basic_fsui_test/BASIC_FSUI_TEST") +
+                      14);
   GNUNET_snprintf (fn,
   GNUNET_snprintf (fn,
-                   strlen ("/tmp/gnunet-basic_fsui_test/BASIC_FSUI_TEST") +
-                   14, "/tmp/gnunet-basic_fsui_test/BASIC_FSUI_TEST%u", i);
+                   strlen ("/tmp/gnunet-basic_fsui_test/BASIC_FSUI_TEST") + 14,
+                   "/tmp/gnunet-basic_fsui_test/BASIC_FSUI_TEST%u", i);
   GNUNET_disk_directory_create_for_file (NULL, fn);
   return fn;
 }
 
   GNUNET_disk_directory_create_for_file (NULL, fn);
   return fn;
 }
 
-static volatile enum GNUNET_FSUI_EventType lastEvent;
+static enum GNUNET_FSUI_EventType lastEvent;
 
 static struct GNUNET_MetaData *search_meta;
 
 
 static struct GNUNET_MetaData *search_meta;
 
@@ -61,39 +60,37 @@ eventCallback (void *cls, const GNUNET_FSUI_Event * event)
   static char unused;
 
   switch (event->type)
   static char unused;
 
   switch (event->type)
-    {
-    case GNUNET_FSUI_search_resumed:
-    case GNUNET_FSUI_download_resumed:
-    case GNUNET_FSUI_upload_resumed:
-    case GNUNET_FSUI_unindex_resumed:
-      return &unused;
-    case GNUNET_FSUI_search_result:
+  {
+  case GNUNET_FSUI_search_resumed:
+  case GNUNET_FSUI_download_resumed:
+  case GNUNET_FSUI_upload_resumed:
+  case GNUNET_FSUI_unindex_resumed:
+    return &unused;
+  case GNUNET_FSUI_search_result:
 #if DEBUG_VERBOSE
 #if DEBUG_VERBOSE
-      printf ("Received search result\n");
+    printf ("Received search result\n");
 #endif
 #endif
-      search_uri =
-        GNUNET_ECRS_uri_duplicate (event->data.SearchResult.fi.uri);
-      search_meta =
-        GNUNET_meta_data_duplicate (event->data.SearchResult.fi.meta);
-      break;
-    case GNUNET_FSUI_upload_completed:
+    search_uri = GNUNET_ECRS_uri_duplicate (event->data.SearchResult.fi.uri);
+    search_meta = GNUNET_meta_data_duplicate (event->data.SearchResult.fi.meta);
+    break;
+  case GNUNET_FSUI_upload_completed:
 #if DEBUG_VERBOSE
 #if DEBUG_VERBOSE
-      printf ("Upload complete.\n");
+    printf ("Upload complete.\n");
 #endif
 #endif
-      break;
-    case GNUNET_FSUI_download_completed:
+    break;
+  case GNUNET_FSUI_download_completed:
 #if DEBUG_VERBOSE
 #if DEBUG_VERBOSE
-      printf ("Download complete.\n");
+    printf ("Download complete.\n");
 #endif
 #endif
-      break;
-    case GNUNET_FSUI_unindex_completed:
+    break;
+  case GNUNET_FSUI_unindex_completed:
 #if DEBUG_VERBOSE
 #if DEBUG_VERBOSE
-      printf ("Unindex complete.\n");
+    printf ("Unindex complete.\n");
 #endif
 #endif
-      break;
-    default:
-      break;
-    }
+    break;
+  default:
+    break;
+  }
   lastEvent = event->type;
   return NULL;
 }
   lastEvent = event->type;
   return NULL;
 }
@@ -104,11 +101,12 @@ int
 main (int argc, char *argv[])
 {
 #if START_DAEMON
 main (int argc, char *argv[])
 {
 #if START_DAEMON
-  pid_t daemon;
+  struct GNUNET_OS_Process *daemon;
 #endif
   int ok;
   struct GNUNET_ECRS_URI *uri;
   char *filename = NULL;
 #endif
   int ok;
   struct GNUNET_ECRS_URI *uri;
   char *filename = NULL;
+
   char *keywords[] = {
     "fsui_foo",
     "fsui_bar",
   char *keywords[] = {
     "fsui_foo",
     "fsui_bar",
@@ -126,16 +124,15 @@ main (int argc, char *argv[])
 
   cfg = GNUNET_GC_create ();
   if (-1 == GNUNET_GC_parse_configuration (cfg, "check.conf"))
 
   cfg = GNUNET_GC_create ();
   if (-1 == GNUNET_GC_parse_configuration (cfg, "check.conf"))
-    {
-      GNUNET_GC_free (cfg);
-      return -1;
-    }
+  {
+    GNUNET_GC_free (cfg);
+    return -1;
+  }
 #if START_DAEMON
   daemon = GNUNET_daemon_start (NULL, cfg, "peer.conf", GNUNET_NO);
 #if START_DAEMON
   daemon = GNUNET_daemon_start (NULL, cfg, "peer.conf", GNUNET_NO);
-  GNUNET_GE_ASSERT (NULL, daemon > 0);
+  GNUNET_GE_ASSERT (NULL, daemon != NULL);
   CHECK (GNUNET_OK ==
   CHECK (GNUNET_OK ==
-         GNUNET_wait_for_daemon_running (NULL, cfg,
-                                         60 * GNUNET_CRON_SECONDS));
+         GNUNET_wait_for_daemon_running (NULL, cfg, 60 * GNUNET_CRON_SECONDS));
 #endif
   GNUNET_thread_sleep (5 * GNUNET_CRON_SECONDS);        /* give apps time to start */
   ok = GNUNET_YES;
 #endif
   GNUNET_thread_sleep (5 * GNUNET_CRON_SECONDS);        /* give apps time to start */
   ok = GNUNET_YES;
@@ -146,33 +143,29 @@ main (int argc, char *argv[])
                            &eventCallback, NULL);
   CHECK (ctx != NULL);
   filename = makeName (42);
                            &eventCallback, NULL);
   CHECK (ctx != NULL);
   filename = makeName (42);
-  GNUNET_disk_file_write (NULL,
-                          filename,
-                          "foo bar test!", strlen ("foo bar test!"), "600");
+  GNUNET_disk_file_write (NULL, filename, "foo bar test!",
+                          strlen ("foo bar test!"), "600");
   meta = GNUNET_meta_data_create ();
   kuri =
   meta = GNUNET_meta_data_create ();
   kuri =
-    GNUNET_ECRS_keyword_command_line_to_uri (NULL, 2,
-                                             (const char **) keywords);
+      GNUNET_ECRS_keyword_command_line_to_uri (NULL, 2,
+                                               (const char **) keywords);
   /* upload */
   upload = GNUNET_FSUI_upload_start (ctx, filename, (GNUNET_FSUI_DirectoryScanCallback) & GNUNET_disk_directory_scan, NULL, 0,  /* anonymity */
                                      0, /* priority */
   /* upload */
   upload = GNUNET_FSUI_upload_start (ctx, filename, (GNUNET_FSUI_DirectoryScanCallback) & GNUNET_disk_directory_scan, NULL, 0,  /* anonymity */
                                      0, /* priority */
-                                     GNUNET_YES,
-                                     GNUNET_NO,
-                                     GNUNET_NO,
-                                     GNUNET_get_time () +
-                                     5 * GNUNET_CRON_HOURS, meta, kuri, kuri);
+                                     GNUNET_YES, GNUNET_NO, GNUNET_NO,
+                                     GNUNET_get_time () + 5 * GNUNET_CRON_HOURS,
+                                     meta, kuri, kuri);
   CHECK (upload != NULL);
   GNUNET_ECRS_uri_destroy (kuri);
   GNUNET_meta_data_destroy (meta);
   prog = 0;
   while (lastEvent != GNUNET_FSUI_upload_completed)
   CHECK (upload != NULL);
   GNUNET_ECRS_uri_destroy (kuri);
   GNUNET_meta_data_destroy (meta);
   prog = 0;
   while (lastEvent != GNUNET_FSUI_upload_completed)
-    {
-      prog++;
-      CHECK (prog <
-             10000) GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
-      if (GNUNET_shutdown_test () == GNUNET_YES)
-        break;
-    }
+  {
+    prog++;
+    CHECK (prog < 10000) GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
+    if (GNUNET_shutdown_test () == GNUNET_YES)
+      break;
+  }
 
   /* search */
   GNUNET_snprintf (keyword, 40, "+%s +%s", keywords[0], keywords[1]);
 
   /* search */
   GNUNET_snprintf (keyword, 40, "+%s +%s", keywords[0], keywords[1]);
@@ -182,33 +175,31 @@ main (int argc, char *argv[])
   CHECK (search != NULL);
   prog = 0;
   while (lastEvent != GNUNET_FSUI_search_result)
   CHECK (search != NULL);
   prog = 0;
   while (lastEvent != GNUNET_FSUI_search_result)
-    {
-      prog++;
-      CHECK (prog < 10000);
-      GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
-      if (GNUNET_shutdown_test () == GNUNET_YES)
-        break;
-    }
+  {
+    prog++;
+    CHECK (prog < 10000);
+    GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
+    if (GNUNET_shutdown_test () == GNUNET_YES)
+      break;
+  }
   GNUNET_FSUI_search_abort (search);
   GNUNET_FSUI_search_stop (search);
 
   /* download */
   fn = makeName (43);
   GNUNET_FSUI_search_abort (search);
   GNUNET_FSUI_search_stop (search);
 
   /* download */
   fn = makeName (43);
-  download = GNUNET_FSUI_download_start (ctx,
-                                         0,
-                                         GNUNET_NO,
-                                         search_uri,
-                                         search_meta, fn, NULL, NULL);
+  download =
+      GNUNET_FSUI_download_start (ctx, 0, GNUNET_NO, search_uri, search_meta,
+                                  fn, NULL, NULL);
   GNUNET_free (fn);
   prog = 0;
   while (lastEvent != GNUNET_FSUI_download_completed)
   GNUNET_free (fn);
   prog = 0;
   while (lastEvent != GNUNET_FSUI_download_completed)
-    {
-      prog++;
-      CHECK (prog < 10000);
-      GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
-      if (GNUNET_shutdown_test () == GNUNET_YES)
-        break;
-    }
+  {
+    prog++;
+    CHECK (prog < 10000);
+    GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
+    if (GNUNET_shutdown_test () == GNUNET_YES)
+      break;
+  }
   GNUNET_FSUI_download_stop (download);
   download = NULL;
   GNUNET_ECRS_uri_destroy (search_uri);
   GNUNET_FSUI_download_stop (download);
   download = NULL;
   GNUNET_ECRS_uri_destroy (search_uri);
@@ -217,13 +208,13 @@ main (int argc, char *argv[])
   unindex = GNUNET_FSUI_unindex_start (ctx, filename);
   prog = 0;
   while (lastEvent != GNUNET_FSUI_unindex_completed)
   unindex = GNUNET_FSUI_unindex_start (ctx, filename);
   prog = 0;
   while (lastEvent != GNUNET_FSUI_unindex_completed)
-    {
-      prog++;
-      CHECK (prog < 10000);
-      GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
-      if (GNUNET_shutdown_test () == GNUNET_YES)
-        break;
-    }
+  {
+    prog++;
+    CHECK (prog < 10000);
+    GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
+    if (GNUNET_shutdown_test () == GNUNET_YES)
+      break;
+  }
   if (lastEvent != GNUNET_FSUI_unindex_completed)
     GNUNET_FSUI_unindex_abort (unindex);
   GNUNET_FSUI_unindex_stop (unindex);
   if (lastEvent != GNUNET_FSUI_unindex_completed)
     GNUNET_FSUI_unindex_abort (unindex);
   GNUNET_FSUI_unindex_stop (unindex);
@@ -234,15 +225,15 @@ FAILURE:
   if (ctx != NULL)
     GNUNET_FSUI_stop (ctx);
   if (filename != NULL)
   if (ctx != NULL)
     GNUNET_FSUI_stop (ctx);
   if (filename != NULL)
-    {
-      UNLINK (filename);
-      GNUNET_free (filename);
-    }
+  {
+    UNLINK (filename);
+    GNUNET_free (filename);
+  }
   if (download != NULL)
   if (download != NULL)
-    {
-      GNUNET_FSUI_download_abort (download);
-      GNUNET_FSUI_download_stop (download);
-    }
+  {
+    GNUNET_FSUI_download_abort (download);
+    GNUNET_FSUI_download_stop (download);
+  }
   filename = makeName (43);
   /* TODO: verify file 'filename(42)' == file 'filename(43)' */
   UNLINK (filename);
   filename = makeName (43);
   /* TODO: verify file 'filename(42)' == file 'filename(43)' */
   UNLINK (filename);
@@ -250,10 +241,11 @@ FAILURE:
 
 #if START_DAEMON
   GNUNET_GE_ASSERT (NULL, GNUNET_OK == GNUNET_daemon_stop (NULL, daemon));
 
 #if START_DAEMON
   GNUNET_GE_ASSERT (NULL, GNUNET_OK == GNUNET_daemon_stop (NULL, daemon));
+  GNUNET_OS_process_destroy (daemon);
 #endif
   GNUNET_GC_free (cfg);
 
   return (ok == GNUNET_YES) ? 0 : 1;
 }
 
 #endif
   GNUNET_GC_free (cfg);
 
   return (ok == GNUNET_YES) ? 0 : 1;
 }
 
-/* end of basic_fsui_test.c */
+/* end of test_fs.c */