changing datastore API to support replication level --- not fully implemented --...
[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 (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
90     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
91   if (GNUNET_OS_process_wait(p->arm_proc) != GNUNET_OK)
92     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
93   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
94               "ARM process %u stopped\n", GNUNET_OS_process_get_pid (p->arm_proc));
95   GNUNET_OS_process_close (p->arm_proc);
96   p->arm_proc = NULL;
97 #endif
98   if (uri_this != NULL)
99     GNUNET_FS_uri_destroy (uri_this);
100   if (uri_next != NULL)
101     GNUNET_FS_uri_destroy (uri_next);
102   if (ns != NULL)
103     GNUNET_FS_namespace_delete (ns, GNUNET_NO);
104   if (meta != NULL)
105     GNUNET_CONTAINER_meta_data_destroy (meta);
106   GNUNET_CONFIGURATION_destroy (p->cfg);
107 }
108
109
110
111 static void
112 check_next (void *cls,
113             const char *last_id, 
114             const struct GNUNET_FS_Uri *last_uri,
115             const struct GNUNET_CONTAINER_MetaData *last_meta,
116             const char *next_id)
117 {
118   GNUNET_break (0 == strcmp (last_id, "next"));
119   GNUNET_break (0 == strcmp (next_id, "future"));
120   err -= 4;
121 }
122
123
124 static void
125 check_this_next (void *cls,
126                  const char *last_id, 
127                  const struct GNUNET_FS_Uri *last_uri,
128                  const struct GNUNET_CONTAINER_MetaData *last_meta,
129                  const char *next_id)
130 {
131   GNUNET_break (0 == strcmp (last_id, "this"));
132   GNUNET_break (0 == strcmp (next_id, "next"));
133   err -= 2;
134   err += 4;
135   GNUNET_FS_namespace_list_updateable (ns,
136                                        next_id,
137                                        &check_next,
138                                        NULL);
139 }
140
141
142 static void
143 sks_cont_next (void *cls,
144                const struct GNUNET_FS_Uri *uri,
145                const char *emsg)
146 {
147   GNUNET_assert (NULL == emsg);
148   err += 2;
149   GNUNET_FS_namespace_list_updateable (ns,
150                                        NULL,
151                                        &check_this_next,
152                                        NULL);
153
154 }
155
156
157 static void
158 check_this (void *cls,
159             const char *last_id, 
160             const struct GNUNET_FS_Uri *last_uri,
161             const struct GNUNET_CONTAINER_MetaData *last_meta,
162             const char *next_id)
163 {
164   GNUNET_break (0 == strcmp (last_id, "this"));
165   GNUNET_break (0 == strcmp (next_id, "next"));
166   err -= 1;
167 }
168
169
170 static void
171 sks_cont_this (void *cls,
172                const struct GNUNET_FS_Uri *uri,
173                const char *emsg)
174 {
175
176   GNUNET_assert (NULL == emsg);
177   err = 1;
178   GNUNET_FS_namespace_list_updateable (ns,
179                                        NULL,
180                                        &check_this,
181                                        NULL);
182   GNUNET_FS_publish_sks (fs,
183                          ns,
184                          "next",
185                          "future",
186                          meta,
187                          uri_next,
188                          &bo,
189                          GNUNET_FS_PUBLISH_OPTION_NONE,
190                          &sks_cont_next,
191                          NULL);
192
193 }
194
195
196
197 static void
198 testNamespace ()
199 {
200
201   ns = GNUNET_FS_namespace_create (fs,
202                                    "testNamespace");
203   GNUNET_assert (NULL != ns);
204   bo.content_priority = 1;
205   bo.anonymity_level = 1;
206   bo.replication_level = 0;
207   bo.expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
208   meta = GNUNET_CONTAINER_meta_data_create ();
209
210   uri_this =
211     GNUNET_FS_uri_parse ("gnunet://fs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000.42", NULL);
212   uri_next =
213     GNUNET_FS_uri_parse ("gnunet://fs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000.43", NULL);
214   GNUNET_FS_publish_sks (fs,
215                          ns,
216                          "this",
217                          "next",
218                          meta,
219                          uri_this,
220                          &bo,
221                          GNUNET_FS_PUBLISH_OPTION_NONE,
222                          &sks_cont_this,
223                          NULL);
224 }
225
226
227 static void
228 run (void *cls,
229      char *const *args,
230      const char *cfgfile,
231      const struct GNUNET_CONFIGURATION_Handle *cfg)
232 {
233   setup_peer (&p1, "test_fs_namespace_data.conf");
234   fs = GNUNET_FS_start (cfg,
235                         "test-fs-namespace",
236                         &progress_cb,
237                         NULL,
238                         GNUNET_FS_FLAGS_NONE,
239                         GNUNET_FS_OPTIONS_END);
240   testNamespace ();
241 }
242
243
244 int
245 main (int argc, char *argv[])
246 {
247   char *const argvx[] = { 
248     "test-fs-namespace",
249     "-c",
250     "test_fs_namespace_data.conf",
251 #if VERBOSE
252     "-L", "DEBUG",
253 #endif
254     NULL
255   };
256   struct GNUNET_GETOPT_CommandLineOption options[] = {
257     GNUNET_GETOPT_OPTION_END
258   };
259
260   GNUNET_log_setup ("test_fs_namespace_list_updateable", 
261 #if VERBOSE
262                     "DEBUG",
263 #else
264                     "WARNING",
265 #endif
266                     NULL);
267   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
268                       argvx, "test-fs-namespace",
269                       "nohelp", options, &run, NULL);
270   stop_arm (&p1);
271   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-namespace/");
272   return err;
273 }
274
275
276 /* end of test_fs_namespace_list_updateable.c */