fixing block reconstruction start/shutdown code
[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_TIME_Absolute expiration;
44
45 static struct GNUNET_CONTAINER_MetaData *meta;
46
47 static struct GNUNET_FS_Uri *uri_this;
48
49 static struct GNUNET_FS_Uri *uri_next;
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   GNUNET_assert (NULL == emsg);
176   err = 1;
177   GNUNET_FS_namespace_list_updateable (ns,
178                                        NULL,
179                                        &check_this,
180                                        NULL);
181   GNUNET_FS_publish_sks (fs,
182                          ns,
183                          "next",
184                          "future",
185                          meta,
186                          uri_next,
187                          expiration,
188                          1, 1,
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   expiration = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
205   meta = GNUNET_CONTAINER_meta_data_create ();
206
207   uri_this =
208     GNUNET_FS_uri_parse ("gnunet://fs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000.42", NULL);
209   uri_next =
210     GNUNET_FS_uri_parse ("gnunet://fs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000.43", NULL);
211   GNUNET_FS_publish_sks (fs,
212                          ns,
213                          "this",
214                          "next",
215                          meta,
216                          uri_this,
217                          expiration,
218                          1, 1,
219                          GNUNET_FS_PUBLISH_OPTION_NONE,
220                          &sks_cont_this,
221                          NULL);
222 }
223
224
225 static void
226 run (void *cls,
227      char *const *args,
228      const char *cfgfile,
229      const struct GNUNET_CONFIGURATION_Handle *cfg)
230 {
231   setup_peer (&p1, "test_fs_namespace_data.conf");
232   fs = GNUNET_FS_start (cfg,
233                         "test-fs-namespace",
234                         &progress_cb,
235                         NULL,
236                         GNUNET_FS_FLAGS_NONE,
237                         GNUNET_FS_OPTIONS_END);
238   testNamespace ();
239 }
240
241
242 int
243 main (int argc, char *argv[])
244 {
245   char *const argvx[] = { 
246     "test-fs-namespace",
247     "-c",
248     "test_fs_namespace_data.conf",
249 #if VERBOSE
250     "-L", "DEBUG",
251 #endif
252     NULL
253   };
254   struct GNUNET_GETOPT_CommandLineOption options[] = {
255     GNUNET_GETOPT_OPTION_END
256   };
257
258   GNUNET_log_setup ("test_fs_namespace_list_updateable", 
259 #if VERBOSE
260                     "DEBUG",
261 #else
262                     "WARNING",
263 #endif
264                     NULL);
265   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
266                       argvx, "test-fs-namespace",
267                       "nohelp", options, &run, NULL);
268   stop_arm (&p1);
269   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-namespace/");
270   return err;
271 }
272
273
274 /* end of test_fs_namespace_list_updateable.c */