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