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