arg
[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 PeerContext p1;
36
37 static struct GNUNET_FS_Handle *fs;
38
39 static int err;
40
41 static struct GNUNET_FS_Namespace *ns;
42
43 static struct GNUNET_CONTAINER_MetaData *meta;
44
45 static struct GNUNET_FS_Uri *uri_this;
46
47 static struct GNUNET_FS_Uri *uri_next;
48
49 static struct GNUNET_FS_BlockOptions bo;
50
51
52 struct PeerContext
53 {
54   struct GNUNET_CONFIGURATION_Handle *cfg;
55 #if START_ARM
56   struct GNUNET_OS_Process *arm_proc;
57 #endif
58 };
59
60
61 static void *
62 progress_cb (void *cls, 
63              const struct GNUNET_FS_ProgressInfo *event)
64 {
65   return NULL;
66 }
67
68
69 static void
70 setup_peer (struct PeerContext *p, const char *cfgname)
71 {
72   p->cfg = GNUNET_CONFIGURATION_create ();
73 #if START_ARM
74   p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
75                                         "gnunet-service-arm",
76 #if VERBOSE
77                                         "-L", "DEBUG",
78 #endif
79                                         "-c", cfgname, NULL);
80 #endif
81   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
82 }
83
84
85 static void
86 stop_arm (struct PeerContext *p)
87 {
88 #if START_ARM
89   if (NULL != p->arm_proc)
90     {
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     }
100 #endif
101   if (uri_this != NULL)
102     GNUNET_FS_uri_destroy (uri_this);
103   if (uri_next != NULL)
104     GNUNET_FS_uri_destroy (uri_next);
105   if (ns != NULL)
106     GNUNET_FS_namespace_delete (ns, GNUNET_NO);
107   if (meta != NULL)
108     GNUNET_CONTAINER_meta_data_destroy (meta);
109   GNUNET_CONFIGURATION_destroy (p->cfg);
110 }
111
112
113
114 static void
115 check_next (void *cls,
116             const char *last_id, 
117             const struct GNUNET_FS_Uri *last_uri,
118             const struct GNUNET_CONTAINER_MetaData *last_meta,
119             const char *next_id)
120 {
121   GNUNET_break (0 == strcmp (last_id, "next"));
122   GNUNET_break (0 == strcmp (next_id, "future"));
123   err -= 4;
124 }
125
126
127 static void
128 check_this_next (void *cls,
129                  const char *last_id, 
130                  const struct GNUNET_FS_Uri *last_uri,
131                  const struct GNUNET_CONTAINER_MetaData *last_meta,
132                  const char *next_id)
133 {
134   GNUNET_break (0 == strcmp (last_id, "this"));
135   GNUNET_break (0 == strcmp (next_id, "next"));
136   err -= 2;
137   err += 4;
138   GNUNET_FS_namespace_list_updateable (ns,
139                                        next_id,
140                                        &check_next,
141                                        NULL);
142 }
143
144
145 static void
146 sks_cont_next (void *cls,
147                const struct GNUNET_FS_Uri *uri,
148                const char *emsg)
149 {
150   GNUNET_assert (NULL == emsg);
151   err += 2;
152   GNUNET_FS_namespace_list_updateable (ns,
153                                        NULL,
154                                        &check_this_next,
155                                        NULL);
156
157 }
158
159
160 static void
161 check_this (void *cls,
162             const char *last_id, 
163             const struct GNUNET_FS_Uri *last_uri,
164             const struct GNUNET_CONTAINER_MetaData *last_meta,
165             const char *next_id)
166 {
167   GNUNET_break (0 == strcmp (last_id, "this"));
168   GNUNET_break (0 == strcmp (next_id, "next"));
169   err -= 1;
170 }
171
172
173 static void
174 sks_cont_this (void *cls,
175                const struct GNUNET_FS_Uri *uri,
176                const char *emsg)
177 {
178
179   GNUNET_assert (NULL == emsg);
180   err = 1;
181   GNUNET_FS_namespace_list_updateable (ns,
182                                        NULL,
183                                        &check_this,
184                                        NULL);
185   GNUNET_FS_publish_sks (fs,
186                          ns,
187                          "next",
188                          "future",
189                          meta,
190                          uri_next,
191                          &bo,
192                          GNUNET_FS_PUBLISH_OPTION_NONE,
193                          &sks_cont_next,
194                          NULL);
195
196 }
197
198
199
200 static void
201 testNamespace ()
202 {
203
204   ns = GNUNET_FS_namespace_create (fs,
205                                    "testNamespace");
206   GNUNET_assert (NULL != ns);
207   bo.content_priority = 1;
208   bo.anonymity_level = 1;
209   bo.replication_level = 0;
210   bo.expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
211   meta = GNUNET_CONTAINER_meta_data_create ();
212
213   uri_this =
214     GNUNET_FS_uri_parse ("gnunet://fs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000.42", NULL);
215   uri_next =
216     GNUNET_FS_uri_parse ("gnunet://fs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000.43", NULL);
217   GNUNET_FS_publish_sks (fs,
218                          ns,
219                          "this",
220                          "next",
221                          meta,
222                          uri_this,
223                          &bo,
224                          GNUNET_FS_PUBLISH_OPTION_NONE,
225                          &sks_cont_this,
226                          NULL);
227 }
228
229
230 static void
231 run (void *cls,
232      char *const *args,
233      const char *cfgfile,
234      const struct GNUNET_CONFIGURATION_Handle *cfg)
235 {
236   setup_peer (&p1, "test_fs_namespace_data.conf");
237   fs = GNUNET_FS_start (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 */