fixing common off-by-one error with respect to maximum message size
[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     {
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_CONFIGURATION_destroy (cfg);
94       return 1;
95     }
96   uri = GNUNET_FS_uri_loc_create (baseURI,
97                                   cfg,
98                                   GNUNET_TIME_absolute_get ());
99   if (uri == NULL)
100     {
101       GNUNET_break (0);
102       GNUNET_FS_uri_destroy (baseURI);
103       GNUNET_CONFIGURATION_destroy (cfg);
104       return 1;
105     }
106   if (!GNUNET_FS_uri_test_loc (uri))
107     {
108       GNUNET_break (0);
109       GNUNET_FS_uri_destroy (uri);
110       GNUNET_FS_uri_destroy (baseURI);
111       GNUNET_CONFIGURATION_destroy (cfg);
112       return 1;
113     }
114   uri2 = GNUNET_FS_uri_loc_get_uri (uri);
115   if (!GNUNET_FS_uri_test_equal (baseURI, uri2))
116     {
117       GNUNET_break (0);
118       GNUNET_FS_uri_destroy (uri);
119       GNUNET_FS_uri_destroy (uri2);
120       GNUNET_FS_uri_destroy (baseURI);
121       GNUNET_CONFIGURATION_destroy (cfg);
122       return 1;
123     }
124   GNUNET_FS_uri_destroy (uri2);
125   GNUNET_FS_uri_destroy (baseURI);
126   uric = GNUNET_FS_uri_to_string (uri);
127 #if 0
128   /* not for the faint of heart: */
129   printf ("URI: `%s'\n", uric);
130 #endif
131   uri2 = GNUNET_FS_uri_parse (uric, &emsg);
132   GNUNET_free (uric);
133   if (uri2 == NULL)
134     {
135       GNUNET_break (0);
136       GNUNET_FS_uri_destroy (uri);
137       GNUNET_CONFIGURATION_destroy (cfg);
138       return 1;
139     }
140   if (GNUNET_YES != GNUNET_FS_uri_test_equal (uri, uri2))
141     {
142       GNUNET_break (0);
143       GNUNET_FS_uri_destroy (uri);
144       GNUNET_FS_uri_destroy (uri2);
145       GNUNET_CONFIGURATION_destroy (cfg);
146       return 1;
147     }
148   GNUNET_FS_uri_destroy (uri2);
149   GNUNET_FS_uri_destroy (uri);
150   GNUNET_CONFIGURATION_destroy (cfg);
151   return 0;
152 }
153
154 static int
155 testNamespace (int i)
156 {
157   char *uri;
158   struct GNUNET_FS_Uri *ret;
159   char *emsg;
160
161   if (NULL !=
162       GNUNET_FS_uri_parse ("gnunet://fs/sks/D1KJS9H2A82Q65VKQ0ML3RFU6U1D3VUK", &emsg))
163     ABORT ();
164   GNUNET_free (emsg);
165   if (NULL !=
166       GNUNET_FS_uri_parse ("gnunet://fs/sks/D1KJS9H2A82Q65VKQ0ML3RFU6U1D3V/test", &emsg))    
167       ABORT ();    
168   GNUNET_free (emsg);
169   if (NULL != GNUNET_FS_uri_parse ("gnunet://fs/sks/test", &emsg))
170     ABORT ();
171   GNUNET_free (emsg);
172   ret =
173     GNUNET_FS_uri_parse ("gnunet://fs/sks/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820/test", &emsg);
174   if (ret == NULL)
175     {
176       GNUNET_free (emsg);
177       ABORT ();
178     }
179   if (GNUNET_FS_uri_test_ksk (ret))
180     {
181       GNUNET_FS_uri_destroy (ret);
182       ABORT ();
183     }
184   if (!GNUNET_FS_uri_test_sks (ret))
185     {
186       GNUNET_FS_uri_destroy (ret);
187       ABORT ();
188     }
189
190   uri = GNUNET_FS_uri_to_string (ret);
191   if (0 != strcmp (uri,
192                    "gnunet://fs/sks/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820/test"))
193     {
194       GNUNET_FS_uri_destroy (ret);
195       GNUNET_free (uri);
196       ABORT ();
197     }
198   GNUNET_free (uri);
199   GNUNET_FS_uri_destroy (ret);
200   return 0;
201 }
202
203 static int
204 testFile (int i)
205 {
206   char *uri;
207   struct GNUNET_FS_Uri *ret;
208   char *emsg;
209
210   if (NULL !=
211       GNUNET_FS_uri_parse ("gnunet://fs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H00000440000.42", &emsg))
212     ABORT ();
213   GNUNET_free (emsg);
214   if (NULL !=
215       GNUNET_FS_uri_parse ("gnunet://fs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000", &emsg))
216     ABORT ();
217   GNUNET_free (emsg);
218   if (NULL !=
219       GNUNET_FS_uri_parse ("gnunet://fs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000.FGH", &emsg))
220     ABORT ();
221   GNUNET_free (emsg);
222   ret =
223     GNUNET_FS_uri_parse ("gnunet://fs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000.42", &emsg);
224   if (ret == NULL)
225     {
226       GNUNET_free (emsg);
227       ABORT ();
228     }
229   if (GNUNET_FS_uri_test_ksk (ret))
230     {
231       GNUNET_FS_uri_destroy (ret);
232       ABORT ();
233     }
234   if (GNUNET_FS_uri_test_sks (ret))
235     {
236       GNUNET_FS_uri_destroy (ret);
237       ABORT ();
238     }
239   if (GNUNET_ntohll (ret->data.chk.file_length) != 42)
240     {
241       GNUNET_FS_uri_destroy (ret);
242       ABORT ();
243     }
244
245   uri = GNUNET_FS_uri_to_string (ret);
246   if (0 != strcmp (uri,
247                    "gnunet://fs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000.42"))
248     {
249       GNUNET_free (uri);
250       GNUNET_FS_uri_destroy (ret);
251       ABORT ();
252     }
253   GNUNET_free (uri);
254   GNUNET_FS_uri_destroy (ret);
255   return 0;
256 }
257
258 int
259 main (int argc, char *argv[])
260 {
261   int failureCount = 0;
262   int i;
263
264   GNUNET_log_setup ("test_fs_uri", 
265 #if VERBOSE
266                     "DEBUG",
267 #else
268                     "WARNING",
269 #endif
270                     NULL);
271   GNUNET_CRYPTO_random_disable_entropy_gathering ();
272   failureCount += testKeyword ();
273   failureCount += testLocation ();
274   for (i = 0; i < 255; i++)
275     {
276       /* fprintf (stderr, "."); */
277       failureCount += testNamespace (i);
278       failureCount += testFile (i);
279     }
280   /* fprintf (stderr, "\n"); */
281   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-uri");
282   if (failureCount != 0)
283     return 1;
284   return 0;
285 }
286
287 /* end of test_fs_uri.c */