more FS coding
[oweals/gnunet.git] / src / fs / test_namespace.c
1 /*
2      This file is part of GNUnet.
3      (C) 2005, 2006, 2008 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 2, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file applications/fs/ecrs/namespacetest.c
23  * @brief Test for namespace.c
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
28 #include "gnunet_util.h"
29 #include "gnunet_ecrs_lib.h"
30 #include "ecrs.h"
31
32 #define ABORT() { fprintf(stderr, "Error at %s:%d\n", __FILE__, __LINE__); return 1; }
33 #define CHECK(c) { do { if (!(c)) ABORT(); } while(0); }
34
35 #define CHECKNAME "gnunet-namespace-test"
36
37 static struct GNUNET_GC_Configuration *cfg;
38
39 static int match;
40
41 static int
42 spcb (const GNUNET_ECRS_FileInfo * fi,
43       const GNUNET_HashCode * key, int isRoot, void *closure)
44 {
45   struct GNUNET_ECRS_URI *want = closure;
46
47   if (GNUNET_ECRS_uri_test_equal (want, fi->uri))
48     match = 1;
49   else
50     fprintf (stderr,
51              "Namespace search returned unexpected result: \nHAVE: %s\nWANT: %s...\n",
52              GNUNET_ECRS_uri_to_string (fi->uri),
53              GNUNET_ECRS_uri_to_string (want));
54   return GNUNET_OK;
55 }
56
57 static int
58 tt (void *unused)
59 {
60   if (match == 1)
61     return GNUNET_SYSERR;
62   return GNUNET_OK;
63 }
64
65 static int
66 testNamespace ()
67 {
68   GNUNET_HashCode pid;
69   struct GNUNET_ECRS_URI *adv;
70   struct GNUNET_ECRS_URI *advURI;
71   struct GNUNET_ECRS_URI *rootURI;
72   struct GNUNET_MetaData *meta;
73
74   meta = GNUNET_meta_data_create ();
75   adv = GNUNET_ECRS_keyword_string_to_uri (NULL, "testNamespace");
76   rootURI =
77     GNUNET_ECRS_namespace_create (NULL,
78                                   cfg,
79                                   meta,
80                                   0, 0,
81                                   GNUNET_get_time () +
82                                   15 * GNUNET_CRON_MINUTES, adv, "root");
83   CHECK (NULL != rootURI);
84   GNUNET_ECRS_uri_get_namespace_from_sks (rootURI, &pid);
85   advURI = GNUNET_ECRS_namespace_add_content (NULL, cfg, &pid, 1,       /* anonymity */
86                                               1000,     /* priority */
87                                               5 * GNUNET_CRON_MINUTES +
88                                               GNUNET_get_time (),
89                                               "this", "next", rootURI, meta);
90   CHECK (NULL != advURI);
91   fprintf (stderr, "Starting namespace search...\n");
92   CHECK (GNUNET_OK == GNUNET_ECRS_search (NULL,
93                                           cfg,
94                                           advURI, 1, &spcb, rootURI, &tt,
95                                           NULL));
96   fprintf (stderr, "Completed namespace search...\n");
97   CHECK (GNUNET_OK == GNUNET_ECRS_namespace_delete (NULL, cfg, &pid));
98   CHECK (GNUNET_SYSERR == GNUNET_ECRS_namespace_delete (NULL, cfg, &pid));
99   GNUNET_meta_data_destroy (meta);
100   GNUNET_ECRS_uri_destroy (rootURI);
101   GNUNET_ECRS_uri_destroy (advURI);
102   CHECK (match == 1);
103   return 0;
104 }
105
106 int
107 main (int argc, char *argv[])
108 {
109   pid_t daemon;
110   int failureCount = 0;
111
112   GNUNET_disable_entropy_gathering ();
113   cfg = GNUNET_GC_create ();
114   if (-1 == GNUNET_GC_parse_configuration (cfg, "check.conf"))
115     {
116       GNUNET_GC_free (cfg);
117       return -1;
118     }
119   daemon = GNUNET_daemon_start (NULL, cfg, "peer.conf", GNUNET_NO);
120   GNUNET_GE_ASSERT (NULL, daemon > 0);
121   if (GNUNET_OK !=
122       GNUNET_wait_for_daemon_running (NULL, cfg, 60 * GNUNET_CRON_SECONDS))
123     {
124       failureCount++;
125     }
126   else
127     {
128       GNUNET_thread_sleep (5 * GNUNET_CRON_SECONDS);
129       failureCount += testNamespace ();
130     }
131   GNUNET_GE_ASSERT (NULL, GNUNET_OK == GNUNET_daemon_stop (NULL, daemon));
132
133   return (failureCount == 0) ? 0 : 1;
134 }
135
136 /* end of namespacetest.c */