splitting 'struct GNUNET_CRYPTO_EccPublicKey' into one struct for signing and another...
[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  * @file fs/test_fs_uri.c
22  * @brief Test for fs_uri.c
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_fs_service.h"
28 #include "fs_api.h"
29
30
31 static int
32 testKeyword ()
33 {
34   char *uri;
35   struct GNUNET_FS_Uri *ret;
36   char *emsg;
37
38   if (NULL != (ret = GNUNET_FS_uri_parse ("gnunet://fs/ksk/++", &emsg)))
39   {
40     GNUNET_FS_uri_destroy (ret);
41     GNUNET_assert (0);
42   }
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     GNUNET_assert (0);
49   }
50   if (!GNUNET_FS_uri_test_ksk (ret))
51   {
52     GNUNET_FS_uri_destroy (ret);
53     GNUNET_assert (0);
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     GNUNET_assert (0);
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     GNUNET_assert (0);
69   }
70   GNUNET_free (uri);
71   GNUNET_FS_uri_destroy (ret);
72   return 0;
73 }
74
75
76 static int
77 testLocation ()
78 {
79   struct GNUNET_FS_Uri *uri;
80   char *uric;
81   struct GNUNET_FS_Uri *uri2;
82   struct GNUNET_FS_Uri *baseURI;
83   char *emsg;
84   struct GNUNET_CONFIGURATION_Handle *cfg;
85
86   baseURI =
87       GNUNET_FS_uri_parse
88       ("gnunet://fs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000.42",
89        &emsg);
90   GNUNET_assert (baseURI != NULL);
91   GNUNET_assert (emsg == NULL);
92   cfg = GNUNET_CONFIGURATION_create ();
93   if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, "test_fs_uri_data.conf"))
94   {
95     FPRINTF (stderr, "%s",  "Failed to parse configuration file\n");
96     GNUNET_FS_uri_destroy (baseURI);
97     GNUNET_CONFIGURATION_destroy (cfg);
98     return 1;
99   }
100   uri = GNUNET_FS_uri_loc_create (baseURI, cfg, GNUNET_TIME_absolute_get ());
101   if (uri == NULL)
102   {
103     GNUNET_break (0);
104     GNUNET_FS_uri_destroy (baseURI);
105     GNUNET_CONFIGURATION_destroy (cfg);
106     return 1;
107   }
108   if (!GNUNET_FS_uri_test_loc (uri))
109   {
110     GNUNET_break (0);
111     GNUNET_FS_uri_destroy (uri);
112     GNUNET_FS_uri_destroy (baseURI);
113     GNUNET_CONFIGURATION_destroy (cfg);
114     return 1;
115   }
116   uri2 = GNUNET_FS_uri_loc_get_uri (uri);
117   if (!GNUNET_FS_uri_test_equal (baseURI, uri2))
118   {
119     GNUNET_break (0);
120     GNUNET_FS_uri_destroy (uri);
121     GNUNET_FS_uri_destroy (uri2);
122     GNUNET_FS_uri_destroy (baseURI);
123     GNUNET_CONFIGURATION_destroy (cfg);
124     return 1;
125   }
126   GNUNET_FS_uri_destroy (uri2);
127   GNUNET_FS_uri_destroy (baseURI);
128   uric = GNUNET_FS_uri_to_string (uri);
129 #if 0
130   /* not for the faint of heart: */
131   printf ("URI: `%s'\n", uric);
132 #endif
133   uri2 = GNUNET_FS_uri_parse (uric, &emsg);
134   GNUNET_free (uric);
135   if (uri2 == NULL)
136   {
137     fprintf (stderr, "URI parsing failed: %s\n", emsg);
138     GNUNET_break (0);
139     GNUNET_FS_uri_destroy (uri);
140     GNUNET_CONFIGURATION_destroy (cfg);
141     GNUNET_free (emsg);
142     return 1;
143   }
144   GNUNET_assert (NULL == emsg);
145   if (GNUNET_YES != GNUNET_FS_uri_test_equal (uri, uri2))
146   {
147     GNUNET_break (0);
148     GNUNET_FS_uri_destroy (uri);
149     GNUNET_FS_uri_destroy (uri2);
150     GNUNET_CONFIGURATION_destroy (cfg);
151     return 1;
152   }
153   GNUNET_FS_uri_destroy (uri2);
154   GNUNET_FS_uri_destroy (uri);
155   GNUNET_CONFIGURATION_destroy (cfg);
156   return 0;
157 }
158
159
160 static int
161 testNamespace (int i)
162 {
163   char *uri;
164   struct GNUNET_FS_Uri *ret;
165   char *emsg;
166   struct GNUNET_CRYPTO_EccPrivateKey *ph;
167   struct GNUNET_CRYPTO_EccPublicSignKey id;
168   char buf[1024];
169   char ubuf[1024];
170   char *sret;
171
172   if (NULL !=
173       (ret =
174        GNUNET_FS_uri_parse ("gnunet://fs/sks/D1KJS9H2A82Q65VKQ0ML3RFU6U1D3VUK",
175                             &emsg)))
176   {
177     GNUNET_FS_uri_destroy (ret);
178     GNUNET_assert (0);
179   }
180   GNUNET_free (emsg);
181   if (NULL !=
182       (ret =
183        GNUNET_FS_uri_parse
184        ("gnunet://fs/sks/D1KJS9H2A82Q65VKQ0ML3RFU6U1D3V/test", &emsg)))
185   {
186     GNUNET_FS_uri_destroy (ret);
187     GNUNET_assert (0);
188   }
189   GNUNET_free (emsg);
190   if (NULL != (ret = GNUNET_FS_uri_parse ("gnunet://fs/sks/test", &emsg)))
191   {
192     GNUNET_FS_uri_destroy (ret);
193     GNUNET_assert (0);
194   }
195   GNUNET_free (emsg);
196   ph = GNUNET_CRYPTO_ecc_key_create ();
197   GNUNET_CRYPTO_ecc_key_get_public_for_signature (ph, &id);
198   sret = GNUNET_STRINGS_data_to_string (&id, sizeof (id),
199                                         ubuf, sizeof (ubuf) - 1);
200   GNUNET_assert (NULL != sret);
201   sret[0] = '\0';
202   GNUNET_snprintf (buf, sizeof (buf),
203                    "gnunet://fs/sks/%s/test",
204                    ubuf);
205   ret = GNUNET_FS_uri_parse (buf, &emsg);
206   if (NULL == ret)
207   {
208     GNUNET_free (emsg);
209     GNUNET_assert (0);
210   }
211   if (GNUNET_FS_uri_test_ksk (ret))
212   {
213     GNUNET_FS_uri_destroy (ret);
214     GNUNET_assert (0);
215   }
216   if (!GNUNET_FS_uri_test_sks (ret))
217   {
218     GNUNET_FS_uri_destroy (ret);
219     GNUNET_assert (0);
220   }
221
222   uri = GNUNET_FS_uri_to_string (ret);
223   if (0 !=
224       strcmp (uri,
225               buf))
226   {
227     GNUNET_FS_uri_destroy (ret);
228     GNUNET_free (uri);
229     GNUNET_assert (0);
230   }
231   GNUNET_free (uri);
232   GNUNET_FS_uri_destroy (ret);
233   return 0;
234 }
235
236
237 static int
238 testFile (int i)
239 {
240   char *uri;
241   struct GNUNET_FS_Uri *ret;
242   char *emsg;
243
244   if (NULL !=
245       (ret =
246        GNUNET_FS_uri_parse
247        ("gnunet://fs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H00000440000.42",
248         &emsg)))
249   {
250     GNUNET_FS_uri_destroy (ret);
251     GNUNET_assert (0);
252   }
253   GNUNET_free (emsg);
254   if (NULL !=
255       (ret =
256        GNUNET_FS_uri_parse
257        ("gnunet://fs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000",
258         &emsg)))
259   {
260     GNUNET_FS_uri_destroy (ret);
261     GNUNET_assert (0);
262   }
263   GNUNET_free (emsg);
264   if (NULL !=
265       (ret =
266        GNUNET_FS_uri_parse
267        ("gnunet://fs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000.FGH",
268         &emsg)))
269   {
270     GNUNET_FS_uri_destroy (ret);
271     GNUNET_assert (0);
272   }
273   GNUNET_free (emsg);
274   ret =
275       GNUNET_FS_uri_parse
276       ("gnunet://fs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000.42",
277        &emsg);
278   if (ret == NULL)
279   {
280     GNUNET_free (emsg);
281     GNUNET_assert (0);
282   }
283   if (GNUNET_FS_uri_test_ksk (ret))
284   {
285     GNUNET_FS_uri_destroy (ret);
286     GNUNET_assert (0);
287   }
288   if (GNUNET_FS_uri_test_sks (ret))
289   {
290     GNUNET_FS_uri_destroy (ret);
291     GNUNET_assert (0);
292   }
293   if (GNUNET_ntohll (ret->data.chk.file_length) != 42)
294   {
295     GNUNET_FS_uri_destroy (ret);
296     GNUNET_assert (0);
297   }
298
299   uri = GNUNET_FS_uri_to_string (ret);
300   if (0 !=
301       strcmp (uri,
302               "gnunet://fs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000.42"))
303   {
304     GNUNET_free (uri);
305     GNUNET_FS_uri_destroy (ret);
306     GNUNET_assert (0);
307   }
308   GNUNET_free (uri);
309   GNUNET_FS_uri_destroy (ret);
310   return 0;
311 }
312
313
314 int
315 main (int argc, char *argv[])
316 {
317   int failureCount = 0;
318   int i;
319
320   GNUNET_log_setup ("test_fs_uri",
321                     "WARNING",
322                     NULL);
323   failureCount += testKeyword ();
324   failureCount += testLocation ();
325   for (i = 0; i < 255; i++)
326   {
327     /* FPRINTF (stderr, "%s",  "."); */
328     failureCount += testNamespace (i);
329     failureCount += testFile (i);
330   }
331   /* FPRINTF (stderr, "%s",  "\n"); */
332   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-uri");
333   if (failureCount != 0)
334     return 1;
335   return 0;
336 }
337
338 /* end of test_fs_uri.c */