-cleaner version of sharetree trim code; should be fast enough to run synchrnously
[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_EXTRA_LOGGING
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, const struct GNUNET_FS_ProgressInfo *event)
63 {
64   return NULL;
65 }
66
67
68 static void
69 setup_peer (struct PeerContext *p, const char *cfgname)
70 {
71   p->cfg = GNUNET_CONFIGURATION_create ();
72 #if START_ARM
73   p->arm_proc =
74       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, "ARM process %u stopped\n",
96                 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, 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, const char *last_id,
128                  const struct GNUNET_FS_Uri *last_uri,
129                  const struct GNUNET_CONTAINER_MetaData *last_meta,
130                  const char *next_id)
131 {
132   GNUNET_break (0 == strcmp (last_id, "this"));
133   GNUNET_break (0 == strcmp (next_id, "next"));
134   err -= 2;
135   err += 4;
136   GNUNET_FS_namespace_list_updateable (ns, next_id, &check_next, NULL);
137 }
138
139
140 static void
141 sks_cont_next (void *cls, const struct GNUNET_FS_Uri *uri, const char *emsg)
142 {
143   GNUNET_assert (NULL == emsg);
144   err += 2;
145   GNUNET_FS_namespace_list_updateable (ns, NULL, &check_this_next, NULL);
146
147 }
148
149
150 static void
151 check_this (void *cls, const char *last_id,
152             const struct GNUNET_FS_Uri *last_uri,
153             const struct GNUNET_CONTAINER_MetaData *last_meta,
154             const char *next_id)
155 {
156   GNUNET_break (0 == strcmp (last_id, "this"));
157   GNUNET_break (0 == strcmp (next_id, "next"));
158   err -= 1;
159 }
160
161
162 static void
163 sks_cont_this (void *cls, const struct GNUNET_FS_Uri *uri, const char *emsg)
164 {
165
166   GNUNET_assert (NULL == emsg);
167   err = 1;
168   GNUNET_FS_namespace_list_updateable (ns, NULL, &check_this, NULL);
169   GNUNET_FS_publish_sks (fs, ns, "next", "future", meta, uri_next, &bo,
170                          GNUNET_FS_PUBLISH_OPTION_NONE, &sks_cont_next, NULL);
171
172 }
173
174
175
176 static void
177 testNamespace ()
178 {
179
180   ns = GNUNET_FS_namespace_create (fs, "testNamespace");
181   GNUNET_assert (NULL != ns);
182   bo.content_priority = 1;
183   bo.anonymity_level = 1;
184   bo.replication_level = 0;
185   bo.expiration_time =
186       GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_MINUTES);
187   meta = GNUNET_CONTAINER_meta_data_create ();
188
189   uri_this =
190       GNUNET_FS_uri_parse
191       ("gnunet://fs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000.42",
192        NULL);
193   uri_next =
194       GNUNET_FS_uri_parse
195       ("gnunet://fs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000.43",
196        NULL);
197   GNUNET_FS_publish_sks (fs, ns, "this", "next", meta, uri_this, &bo,
198                          GNUNET_FS_PUBLISH_OPTION_NONE, &sks_cont_this, NULL);
199 }
200
201
202 static void
203 run (void *cls, char *const *args, const char *cfgfile,
204      const struct GNUNET_CONFIGURATION_Handle *cfg)
205 {
206   setup_peer (&p1, "test_fs_namespace_data.conf");
207   fs = GNUNET_FS_start (cfg, "test-fs-namespace", &progress_cb, NULL,
208                         GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
209   testNamespace ();
210 }
211
212
213 int
214 main (int argc, char *argv[])
215 {
216   char *const argvx[] = {
217     "test-fs-namespace",
218     "-c",
219     "test_fs_namespace_data.conf",
220 #if VERBOSE
221     "-L", "DEBUG",
222 #endif
223     NULL
224   };
225   struct GNUNET_GETOPT_CommandLineOption options[] = {
226     GNUNET_GETOPT_OPTION_END
227   };
228
229   GNUNET_log_setup ("test_fs_namespace_list_updateable",
230 #if VERBOSE
231                     "DEBUG",
232 #else
233                     "WARNING",
234 #endif
235                     NULL);
236   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1, argvx,
237                       "test-fs-namespace", "nohelp", options, &run, NULL);
238   stop_arm (&p1);
239   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-namespace/");
240   return err;
241 }
242
243
244 /* end of test_fs_namespace_list_updateable.c */