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