plane hacking
[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   pid_t arm_pid;
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_pid = 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 != PLIBC_KILL (p->arm_pid, SIGTERM))
92     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
93   if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
94     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
95   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
96               "ARM process %u stopped\n", p->arm_pid);
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      struct GNUNET_SCHEDULER_Handle *s,
228      char *const *args,
229      const char *cfgfile,
230      const struct GNUNET_CONFIGURATION_Handle *cfg)
231 {
232   sched = s;
233   setup_peer (&p1, "test_fs_namespace_data.conf");
234   fs = GNUNET_FS_start (sched,
235                         cfg,
236                         "test-fs-namespace",
237                         &progress_cb,
238                         NULL,
239                         GNUNET_FS_FLAGS_NONE,
240                         GNUNET_FS_OPTIONS_END);
241   testNamespace ();
242 }
243
244
245 int
246 main (int argc, char *argv[])
247 {
248   char *const argvx[] = { 
249     "test-fs-namespace",
250     "-c",
251     "test_fs_namespace_data.conf",
252 #if VERBOSE
253     "-L", "DEBUG",
254 #endif
255     NULL
256   };
257   struct GNUNET_GETOPT_CommandLineOption options[] = {
258     GNUNET_GETOPT_OPTION_END
259   };
260
261   GNUNET_log_setup ("test_fs_namespace_list_updateable", 
262 #if VERBOSE
263                     "DEBUG",
264 #else
265                     "WARNING",
266 #endif
267                     NULL);
268   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
269                       argvx, "test-fs-namespace",
270                       "nohelp", options, &run, NULL);
271   stop_arm (&p1);
272   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-namespace/");
273   return err;
274 }
275
276
277 /* end of test_fs_namespace_list_updateable.c */