48442f8555ac4f4d7f7c43b85d688ece2c384c1f
[oweals/gnunet.git] / src / fs / test_fs_namespace.c
1 /*
2      This file is part of GNUnet.
3      (C) 2005, 2006, 2008, 2009 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 fs/test_fs_namespace.c
23  * @brief Test for fs_namespace.c
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_arm_service.h"
30 #include "gnunet_fs_service.h"
31
32 #define START_ARM GNUNET_YES
33
34 static struct GNUNET_SCHEDULER_Handle *sched;
35
36 static struct PeerContext p1;
37
38 static struct GNUNET_FS_Handle *fs;
39
40
41 struct PeerContext
42 {
43   struct GNUNET_CONFIGURATION_Handle *cfg;
44 #if START_ARM
45   pid_t arm_pid;
46 #endif
47 };
48
49
50 static void *
51 progress_cb (void *cls, 
52              const struct GNUNET_FS_ProgressInfo *event)
53 {
54   return NULL;
55 }
56
57
58 static void
59 setup_peer (struct PeerContext *p, const char *cfgname)
60 {
61   p->cfg = GNUNET_CONFIGURATION_create ();
62 #if START_ARM
63   p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
64                                         "gnunet-service-arm",
65 #if VERBOSE
66                                         "-L", "DEBUG",
67 #endif
68                                         "-c", cfgname, NULL);
69 #endif
70   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
71   GNUNET_ARM_start_services (p->cfg, sched, "core", NULL);
72 }
73
74
75 static void
76 stop_arm (struct PeerContext *p)
77 {
78 #if START_ARM
79   if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
80     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
81   if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
82     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
83   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
84               "ARM process %u stopped\n", p->arm_pid);
85 #endif
86   GNUNET_CONFIGURATION_destroy (p->cfg);
87 }
88
89
90 #if 0
91 static void
92 spcb (void *cls,
93       const char *name,
94       const GNUNET_HashCode * key)
95 {
96 }
97 #endif
98
99
100 static void
101 publish_cont (void *cls,
102               const struct GNUNET_FS_Uri *ksk_uri,
103               const char *emsg)
104 {
105   struct GNUNET_FS_SearchContext *search;
106
107   GNUNET_assert (NULL == emsg);
108   fprintf (stderr, "Starting keyword search...\n");
109   search = GNUNET_FS_search_start (fs, ksk_uri, 1, NULL);
110 }
111
112
113 static void
114 sks_cont (void *cls,
115           const struct GNUNET_FS_Uri *uri,
116           const char *emsg)
117 {
118   struct GNUNET_CONTAINER_MetaData *meta;
119   struct GNUNET_TIME_Absolute expiration;
120   struct GNUNET_FS_Uri *ksk_uri;
121   char * msg;
122
123   expiration = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
124   meta = GNUNET_CONTAINER_meta_data_create ();
125   msg = NULL;
126   ksk_uri = GNUNET_FS_uri_parse ("gnunet://fs/ksk/ns-search", &msg);
127   GNUNET_assert (NULL == msg);
128   fprintf (stderr, "Advertising update 'this' namespace entry under keyword...\n");
129   GNUNET_FS_publish_ksk (fs,
130                          ksk_uri,
131                          meta,
132                          uri,
133                          expiration,
134                          1, 1,
135                          GNUNET_FS_PUBLISH_OPTION_NONE,
136                          &publish_cont,
137                          NULL);
138   GNUNET_CONTAINER_meta_data_destroy (meta);
139 }
140
141
142 static void
143 adv_cont (void *cls,
144               const struct GNUNET_FS_Uri *uri,
145               const char *emsg)
146 {
147   struct GNUNET_CONTAINER_MetaData *meta;
148   struct GNUNET_FS_Namespace *ns;
149   struct GNUNET_TIME_Absolute expiration;
150
151   expiration = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
152   ns = GNUNET_FS_namespace_create (fs,
153                                    "testNamespace");
154   meta = GNUNET_CONTAINER_meta_data_create ();
155   GNUNET_assert (NULL == emsg);
156   fprintf (stderr, "Advertising update 'this->next' namespace update...\n");
157   GNUNET_FS_publish_sks (fs,
158                          ns,
159                          "this",
160                          "next",
161                          meta,
162                          uri, /* FIXME: this is non-sense (use CHK URI!) */
163                          expiration,
164                          1, 1,
165                          GNUNET_FS_PUBLISH_OPTION_NONE,
166                          &sks_cont,
167                          NULL);
168   GNUNET_CONTAINER_meta_data_destroy (meta);
169   GNUNET_FS_namespace_delete (ns, GNUNET_NO);
170 }
171
172
173 static void
174 testNamespace ()
175 {
176   struct GNUNET_FS_Namespace *ns;
177   struct GNUNET_TIME_Absolute expiration;
178   struct GNUNET_CONTAINER_MetaData *meta;
179
180   expiration = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
181   meta = GNUNET_CONTAINER_meta_data_create ();
182   ns = GNUNET_FS_namespace_create (fs,
183                                    "testNamespace");
184   fprintf (stderr, "Advertising namespace root...\n");
185   GNUNET_FS_namespace_advertise (fs,
186                                  ns,
187                                  meta,
188                                  1, 1,
189                                  expiration,                                       
190                                  "root",
191                                  &adv_cont, NULL);
192   GNUNET_FS_namespace_delete (ns, GNUNET_NO);
193   GNUNET_CONTAINER_meta_data_destroy (meta);
194 }
195
196 #if 0
197   fprintf (stderr, "Completed namespace search...\n");
198   GNUNET_assert (GNUNET_OK == GNUNET_FS_namespace_delete (NULL, cfg, &pid));
199   GNUNET_assert (GNUNET_SYSERR == GNUNET_FS_namespace_delete (NULL, cfg, &pid));
200   GNUNET_FS_uri_destroy (rootURI);
201   GNUNET_FS_uri_destroy (advURI);
202   GNUNET_assert (match == 1);
203   return 0;
204 }
205 #endif
206
207
208 static void
209 run (void *cls,
210      struct GNUNET_SCHEDULER_Handle *s,
211      char *const *args,
212      const char *cfgfile,
213      const struct GNUNET_CONFIGURATION_Handle *cfg)
214 {
215   sched = s;
216   setup_peer (&p1, "test_fs_download_data.conf");
217   fs = GNUNET_FS_start (sched,
218                         cfg,
219                         "test-fs-namespace",
220                         &progress_cb,
221                         NULL,
222                         GNUNET_FS_FLAGS_NONE,
223                         GNUNET_FS_OPTIONS_END);
224   testNamespace ();
225 }
226
227
228 int
229 main (int argc, char *argv[])
230 {
231   char *const argvx[] = { 
232     "test-fs-namespace",
233     "-c",
234     "test_fs_namespace_data.conf",
235 #if VERBOSE
236     "-L", "DEBUG",
237 #endif
238     NULL
239   };
240   struct GNUNET_GETOPT_CommandLineOption options[] = {
241     GNUNET_GETOPT_OPTION_END
242   };
243
244   GNUNET_log_setup ("test_fs_namespace", 
245 #if VERBOSE
246                     "DEBUG",
247 #else
248                     "WARNING",
249 #endif
250                     NULL);
251   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
252                       argvx, "test-fs-namespace",
253                       "nohelp", options, &run, NULL);
254   stop_arm (&p1);
255   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-namespace/");
256   return 0;
257 }
258
259
260
261 /* end of test_fs_namespace.c */