1cf8653bbc72519b9fc1ef3b6075e59602f1ae62
[oweals/gnunet.git] / src / fs / test_fs_namespace_list_updateable.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 3, 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_list_updateable.c
23  * @brief Test for fs_namespace_list_updateable.c
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_arm_service.h"
29 #include "gnunet_fs_service.h"
30
31 #define VERBOSE GNUNET_NO
32
33 #define START_ARM GNUNET_YES
34
35 static struct GNUNET_SCHEDULER_Handle *sched;
36
37 static struct PeerContext p1;
38
39 static struct GNUNET_FS_Handle *fs;
40
41 static int err;
42
43 static struct GNUNET_FS_Namespace *ns;
44
45 static struct GNUNET_TIME_Absolute expiration;
46
47 static struct GNUNET_CONTAINER_MetaData *meta;
48
49 static struct GNUNET_FS_Uri *uri_this;
50
51 static struct GNUNET_FS_Uri *uri_next;
52
53
54 struct PeerContext
55 {
56   struct GNUNET_CONFIGURATION_Handle *cfg;
57 #if START_ARM
58   GNUNET_OS_Process *arm_proc;
59 #endif
60 };
61
62
63 static void *
64 progress_cb (void *cls, 
65              const struct GNUNET_FS_ProgressInfo *event)
66 {
67   return NULL;
68 }
69
70
71 static void
72 setup_peer (struct PeerContext *p, const char *cfgname)
73 {
74   p->cfg = GNUNET_CONFIGURATION_create ();
75 #if START_ARM
76   p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
77                                         "gnunet-service-arm",
78 #if VERBOSE
79                                         "-L", "DEBUG",
80 #endif
81                                         "-c", cfgname, NULL);
82 #endif
83   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
84 }
85
86
87 static void
88 stop_arm (struct PeerContext *p)
89 {
90 #if START_ARM
91   if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
92     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
93   if (GNUNET_OS_process_wait(p->arm_proc) != GNUNET_OK)
94     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
95   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
96               "ARM process %u stopped\n", GNUNET_OS_process_get_pid (p->arm_proc));
97   GNUNET_OS_process_close (p->arm_proc);
98   p->arm_proc = NULL;
99 #endif
100   if (uri_this != NULL)
101     GNUNET_FS_uri_destroy (uri_this);
102   if (uri_next != NULL)
103     GNUNET_FS_uri_destroy (uri_next);
104   if (ns != NULL)
105     GNUNET_FS_namespace_delete (ns, GNUNET_NO);
106   if (meta != NULL)
107     GNUNET_CONTAINER_meta_data_destroy (meta);
108   GNUNET_CONFIGURATION_destroy (p->cfg);
109 }
110
111
112
113 static void
114 check_next (void *cls,
115             const char *last_id, 
116             const struct GNUNET_FS_Uri *last_uri,
117             const struct GNUNET_CONTAINER_MetaData *last_meta,
118             const char *next_id)
119 {
120   GNUNET_break (0 == strcmp (last_id, "next"));
121   GNUNET_break (0 == strcmp (next_id, "future"));
122   err -= 4;
123 }
124
125
126 static void
127 check_this_next (void *cls,
128                  const char *last_id, 
129                  const struct GNUNET_FS_Uri *last_uri,
130                  const struct GNUNET_CONTAINER_MetaData *last_meta,
131                  const char *next_id)
132 {
133   GNUNET_break (0 == strcmp (last_id, "this"));
134   GNUNET_break (0 == strcmp (next_id, "next"));
135   err -= 2;
136   err += 4;
137   GNUNET_FS_namespace_list_updateable (ns,
138                                        next_id,
139                                        &check_next,
140                                        NULL);
141 }
142
143
144 static void
145 sks_cont_next (void *cls,
146                const struct GNUNET_FS_Uri *uri,
147                const char *emsg)
148 {
149   GNUNET_assert (NULL == emsg);
150   err += 2;
151   GNUNET_FS_namespace_list_updateable (ns,
152                                        NULL,
153                                        &check_this_next,
154                                        NULL);
155
156 }
157
158
159 static void
160 check_this (void *cls,
161             const char *last_id, 
162             const struct GNUNET_FS_Uri *last_uri,
163             const struct GNUNET_CONTAINER_MetaData *last_meta,
164             const char *next_id)
165 {
166   GNUNET_break (0 == strcmp (last_id, "this"));
167   GNUNET_break (0 == strcmp (next_id, "next"));
168   err -= 1;
169 }
170
171
172 static void
173 sks_cont_this (void *cls,
174                const struct GNUNET_FS_Uri *uri,
175                const char *emsg)
176 {
177   GNUNET_assert (NULL == emsg);
178   err = 1;
179   GNUNET_FS_namespace_list_updateable (ns,
180                                        NULL,
181                                        &check_this,
182                                        NULL);
183   GNUNET_FS_publish_sks (fs,
184                          ns,
185                          "next",
186                          "future",
187                          meta,
188                          uri_next,
189                          expiration,
190                          1, 1,
191                          GNUNET_FS_PUBLISH_OPTION_NONE,
192                          &sks_cont_next,
193                          NULL);
194
195 }
196
197
198
199 static void
200 testNamespace ()
201 {
202
203   ns = GNUNET_FS_namespace_create (fs,
204                                    "testNamespace");
205   GNUNET_assert (NULL != ns);
206   expiration = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
207   meta = GNUNET_CONTAINER_meta_data_create ();
208
209   uri_this =
210     GNUNET_FS_uri_parse ("gnunet://fs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000.42", NULL);
211   uri_next =
212     GNUNET_FS_uri_parse ("gnunet://fs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000.43", NULL);
213   GNUNET_FS_publish_sks (fs,
214                          ns,
215                          "this",
216                          "next",
217                          meta,
218                          uri_this,
219                          expiration,
220                          1, 1,
221                          GNUNET_FS_PUBLISH_OPTION_NONE,
222                          &sks_cont_this,
223                          NULL);
224 }
225
226
227 static void
228 run (void *cls,
229      struct GNUNET_SCHEDULER_Handle *s,
230      char *const *args,
231      const char *cfgfile,
232      const struct GNUNET_CONFIGURATION_Handle *cfg)
233 {
234   sched = s;
235   setup_peer (&p1, "test_fs_namespace_data.conf");
236   fs = GNUNET_FS_start (sched,
237                         cfg,
238                         "test-fs-namespace",
239                         &progress_cb,
240                         NULL,
241                         GNUNET_FS_FLAGS_NONE,
242                         GNUNET_FS_OPTIONS_END);
243   testNamespace ();
244 }
245
246
247 int
248 main (int argc, char *argv[])
249 {
250   char *const argvx[] = { 
251     "test-fs-namespace",
252     "-c",
253     "test_fs_namespace_data.conf",
254 #if VERBOSE
255     "-L", "DEBUG",
256 #endif
257     NULL
258   };
259   struct GNUNET_GETOPT_CommandLineOption options[] = {
260     GNUNET_GETOPT_OPTION_END
261   };
262
263   GNUNET_log_setup ("test_fs_namespace_list_updateable", 
264 #if VERBOSE
265                     "DEBUG",
266 #else
267                     "WARNING",
268 #endif
269                     NULL);
270   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
271                       argvx, "test-fs-namespace",
272                       "nohelp", options, &run, NULL);
273   stop_arm (&p1);
274   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-namespace/");
275   return err;
276 }
277
278
279 /* end of test_fs_namespace_list_updateable.c */