fixes
[oweals/gnunet.git] / src / fs / test_fs_uri.c
1 /*
2      This file is part of GNUnet.
3      (C) 2003, 2004, 2006, 2007, 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_uri.c
23  * @brief Test for fs_uri.c
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_fs_service.h"
30 #include "fs.h"
31
32 #define ABORT() { fprintf(stderr, "Error at %s:%d\n", __FILE__, __LINE__); return 1; }
33
34 static int
35 testKeyword ()
36 {
37   char *uri;
38   struct GNUNET_FS_Uri *ret;
39   char *emsg;
40
41   if (NULL != GNUNET_FS_uri_parse ("gnunet://fs/ksk/++", &emsg))
42     ABORT ();
43   GNUNET_free (emsg);
44   ret = GNUNET_FS_uri_parse ("gnunet://fs/ksk/foo+bar", &emsg);
45   if (ret == NULL)
46     {
47       GNUNET_free (emsg);
48       ABORT ();
49     }
50   if (!GNUNET_FS_uri_test_ksk (ret))
51     {
52       GNUNET_FS_uri_destroy (ret);
53       ABORT ();
54     }
55   if ((2 != ret->data.ksk.keywordCount) ||
56       (0 != strcmp (" foo", ret->data.ksk.keywords[0])) ||
57       (0 != strcmp (" bar", ret->data.ksk.keywords[1])))
58     {
59       GNUNET_FS_uri_destroy (ret);
60       ABORT ();
61     }
62
63   uri = GNUNET_FS_uri_to_string (ret);
64   if (0 != strcmp (uri, "gnunet://fs/ksk/foo+bar"))
65     {
66       GNUNET_free (uri);
67       GNUNET_FS_uri_destroy (ret);
68       ABORT ();
69     }
70   GNUNET_free (uri);
71   GNUNET_FS_uri_destroy (ret);
72   return 0;
73 }
74
75 static int
76 testLocation ()
77 {
78   struct GNUNET_FS_Uri *uri;
79   char *uric;
80   struct GNUNET_FS_Uri *uri2;
81   struct GNUNET_FS_Uri *baseURI;
82   char *emsg;
83   struct GNUNET_CONFIGURATION_Handle *cfg;
84
85   baseURI =
86     GNUNET_FS_uri_parse ("gnunet://fs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000.42", &emsg);
87   GNUNET_assert (baseURI != NULL);
88   cfg = GNUNET_CONFIGURATION_create ();
89   if (GNUNET_OK !=
90       GNUNET_CONFIGURATION_parse (cfg, "test_fs_uri_data.conf"))
91     {
92       fprintf (stderr, "Failed to parse configuration file\n");
93       GNUNET_FS_uri_destroy (baseURI);
94       GNUNET_CONFIGURATION_destroy (cfg);
95       return 1;
96     }
97   uri = GNUNET_FS_uri_loc_create (baseURI,
98                                   cfg,
99                                   GNUNET_TIME_absolute_get ());
100   if (uri == NULL)
101     {
102       GNUNET_break (0);
103       GNUNET_FS_uri_destroy (baseURI);
104       GNUNET_CONFIGURATION_destroy (cfg);
105       return 1;
106     }
107   if (!GNUNET_FS_uri_test_loc (uri))
108     {
109       GNUNET_break (0);
110       GNUNET_FS_uri_destroy (uri);
111       GNUNET_FS_uri_destroy (baseURI);
112       GNUNET_CONFIGURATION_destroy (cfg);
113       return 1;
114     }
115   uri2 = GNUNET_FS_uri_loc_get_uri (uri);
116   if (!GNUNET_FS_uri_test_equal (baseURI, uri2))
117     {
118       GNUNET_break (0);
119       GNUNET_FS_uri_destroy (uri);
120       GNUNET_FS_uri_destroy (uri2);
121       GNUNET_FS_uri_destroy (baseURI);
122       GNUNET_CONFIGURATION_destroy (cfg);
123       return 1;
124     }
125   GNUNET_FS_uri_destroy (uri2);
126   GNUNET_FS_uri_destroy (baseURI);
127   uric = GNUNET_FS_uri_to_string (uri);
128 #if 0
129   /* not for the faint of heart: */
130   printf ("URI: `%s'\n", uric);
131 #endif
132   uri2 = GNUNET_FS_uri_parse (uric, &emsg);
133   GNUNET_free (uric);
134   if (uri2 == NULL)
135     {
136       GNUNET_break (0);
137       GNUNET_FS_uri_destroy (uri);
138       GNUNET_CONFIGURATION_destroy (cfg);
139       return 1;
140     }
141   if (GNUNET_YES != GNUNET_FS_uri_test_equal (uri, uri2))
142     {
143       GNUNET_break (0);
144       GNUNET_FS_uri_destroy (uri);
145       GNUNET_FS_uri_destroy (uri2);
146       GNUNET_CONFIGURATION_destroy (cfg);
147       return 1;
148     }
149   GNUNET_FS_uri_destroy (uri2);
150   GNUNET_FS_uri_destroy (uri);
151   GNUNET_CONFIGURATION_destroy (cfg);
152   return 0;
153 }
154
155 static int
156 testNamespace (int i)
157 {
158   char *uri;
159   struct GNUNET_FS_Uri *ret;
160   char *emsg;
161
162   if (NULL !=
163       GNUNET_FS_uri_parse ("gnunet://fs/sks/D1KJS9H2A82Q65VKQ0ML3RFU6U1D3VUK", &emsg))
164     ABORT ();
165   GNUNET_free (emsg);
166   if (NULL !=
167       GNUNET_FS_uri_parse ("gnunet://fs/sks/D1KJS9H2A82Q65VKQ0ML3RFU6U1D3V/test", &emsg))    
168       ABORT ();    
169   GNUNET_free (emsg);
170   if (NULL != GNUNET_FS_uri_parse ("gnunet://fs/sks/test", &emsg))
171     ABORT ();
172   GNUNET_free (emsg);
173   ret =
174     GNUNET_FS_uri_parse ("gnunet://fs/sks/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820/test", &emsg);
175   if (ret == NULL)
176     {
177       GNUNET_free (emsg);
178       ABORT ();
179     }
180   if (GNUNET_FS_uri_test_ksk (ret))
181     {
182       GNUNET_FS_uri_destroy (ret);
183       ABORT ();
184     }
185   if (!GNUNET_FS_uri_test_sks (ret))
186     {
187       GNUNET_FS_uri_destroy (ret);
188       ABORT ();
189     }
190
191   uri = GNUNET_FS_uri_to_string (ret);
192   if (0 != strcmp (uri,
193                    "gnunet://fs/sks/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820/test"))
194     {
195       GNUNET_FS_uri_destroy (ret);
196       GNUNET_free (uri);
197       ABORT ();
198     }
199   GNUNET_free (uri);
200   GNUNET_FS_uri_destroy (ret);
201   return 0;
202 }
203
204 static int
205 testFile (int i)
206 {
207   char *uri;
208   struct GNUNET_FS_Uri *ret;
209   char *emsg;
210
211   if (NULL !=
212       GNUNET_FS_uri_parse ("gnunet://fs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H00000440000.42", &emsg))
213     ABORT ();
214   GNUNET_free (emsg);
215   if (NULL !=
216       GNUNET_FS_uri_parse ("gnunet://fs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000", &emsg))
217     ABORT ();
218   GNUNET_free (emsg);
219   if (NULL !=
220       GNUNET_FS_uri_parse ("gnunet://fs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000.FGH", &emsg))
221     ABORT ();
222   GNUNET_free (emsg);
223   ret =
224     GNUNET_FS_uri_parse ("gnunet://fs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000.42", &emsg);
225   if (ret == NULL)
226     {
227       GNUNET_free (emsg);
228       ABORT ();
229     }
230   if (GNUNET_FS_uri_test_ksk (ret))
231     {
232       GNUNET_FS_uri_destroy (ret);
233       ABORT ();
234     }
235   if (GNUNET_FS_uri_test_sks (ret))
236     {
237       GNUNET_FS_uri_destroy (ret);
238       ABORT ();
239     }
240   if (GNUNET_ntohll (ret->data.chk.file_length) != 42)
241     {
242       GNUNET_FS_uri_destroy (ret);
243       ABORT ();
244     }
245
246   uri = GNUNET_FS_uri_to_string (ret);
247   if (0 != strcmp (uri,
248                    "gnunet://fs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000.42"))
249     {
250       GNUNET_free (uri);
251       GNUNET_FS_uri_destroy (ret);
252       ABORT ();
253     }
254   GNUNET_free (uri);
255   GNUNET_FS_uri_destroy (ret);
256   return 0;
257 }
258
259 int
260 main (int argc, char *argv[])
261 {
262   int failureCount = 0;
263   int i;
264
265   GNUNET_log_setup ("test_fs_uri", 
266 #if VERBOSE
267                     "DEBUG",
268 #else
269                     "WARNING",
270 #endif
271                     NULL);
272   GNUNET_CRYPTO_random_disable_entropy_gathering ();
273   failureCount += testKeyword ();
274   failureCount += testLocation ();
275   for (i = 0; i < 255; i++)
276     {
277       /* fprintf (stderr, "."); */
278       failureCount += testNamespace (i);
279       failureCount += testFile (i);
280     }
281   /* fprintf (stderr, "\n"); */
282   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-uri");
283   if (failureCount != 0)
284     return 1;
285   return 0;
286 }
287
288 /* end of test_fs_uri.c */