6da56a742813a6de378e0b4d61b79cf840dd7f65
[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://ecrs/ksk/++", &emsg))
42     ABORT ();
43   GNUNET_free (emsg);
44   ret = GNUNET_FS_uri_parse ("gnunet://ecrs/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://ecrs/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://ecrs/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       return 1;
101     }
102   if (!GNUNET_FS_uri_test_loc (uri))
103     {
104       GNUNET_break (0);
105       GNUNET_FS_uri_destroy (uri);
106       GNUNET_FS_uri_destroy (baseURI);
107       return 1;
108     }
109   uri2 = GNUNET_FS_uri_loc_get_uri (uri);
110   if (!GNUNET_FS_uri_test_equal (baseURI, uri2))
111     {
112       GNUNET_break (0);
113       GNUNET_FS_uri_destroy (uri);
114       GNUNET_FS_uri_destroy (uri2);
115       GNUNET_FS_uri_destroy (baseURI);
116       return 1;
117     }
118   GNUNET_FS_uri_destroy (uri2);
119   GNUNET_FS_uri_destroy (baseURI);
120   uric = GNUNET_FS_uri_to_string (uri);
121 #if 0
122   /* not for the faint of heart: */
123   printf ("URI: `%s'\n", uric);
124 #endif
125   uri2 = GNUNET_FS_uri_parse (uric, &emsg);
126   GNUNET_free (uric);
127   if (uri2 == NULL)
128     {
129       GNUNET_break (0);
130       GNUNET_FS_uri_destroy (uri);
131       return 1;
132     }
133   if (GNUNET_YES != GNUNET_FS_uri_test_equal (uri, uri2))
134     {
135       GNUNET_break (0);
136       GNUNET_FS_uri_destroy (uri);
137       GNUNET_FS_uri_destroy (uri2);
138       return 1;
139     }
140   GNUNET_FS_uri_destroy (uri2);
141   GNUNET_FS_uri_destroy (uri);
142   return 0;
143 }
144
145 static int
146 testNamespace (int i)
147 {
148   char *uri;
149   struct GNUNET_FS_Uri *ret;
150   char *emsg;
151
152   if (NULL !=
153       GNUNET_FS_uri_parse ("gnunet://ecrs/sks/D1KJS9H2A82Q65VKQ0ML3RFU6U1D3VUK", &emsg))
154     ABORT ();
155   if (NULL !=
156       GNUNET_FS_uri_parse ("gnunet://ecrs/sks/D1KJS9H2A82Q65VKQ0ML3RFU6U1D3V/test", &emsg))
157     ABORT ();
158   if (NULL != GNUNET_FS_uri_parse ("gnunet://ecrs/sks/test", &emsg))
159     ABORT ();
160   ret =
161     GNUNET_FS_uri_parse ("gnunet://ecrs/sks/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820/test", &emsg);
162   if (ret == NULL)
163     ABORT ();
164   if (GNUNET_FS_uri_test_ksk (ret))
165     {
166       GNUNET_FS_uri_destroy (ret);
167       ABORT ();
168     }
169   if (!GNUNET_FS_uri_test_sks (ret))
170     {
171       GNUNET_FS_uri_destroy (ret);
172       ABORT ();
173     }
174
175   uri = GNUNET_FS_uri_to_string (ret);
176   if (0 != strcmp (uri,
177                    "gnunet://ecrs/sks/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820/test"))
178     {
179       GNUNET_FS_uri_destroy (ret);
180       GNUNET_free (uri);
181       ABORT ();
182     }
183   GNUNET_free (uri);
184   GNUNET_FS_uri_destroy (ret);
185   return 0;
186 }
187
188 static int
189 testFile (int i)
190 {
191   char *uri;
192   struct GNUNET_FS_Uri *ret;
193   char *emsg;
194
195   if (NULL !=
196       GNUNET_FS_uri_parse ("gnunet://ecrs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H00000440000.42", &emsg))
197     ABORT ();
198   GNUNET_free (emsg);
199   if (NULL !=
200       GNUNET_FS_uri_parse ("gnunet://ecrs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000", &emsg))
201     ABORT ();
202   GNUNET_free (emsg);
203   if (NULL !=
204       GNUNET_FS_uri_parse ("gnunet://ecrs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000.FGH", &emsg))
205     ABORT ();
206   GNUNET_free (emsg);
207   ret =
208     GNUNET_FS_uri_parse ("gnunet://ecrs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000.42", &emsg);
209   if (ret == NULL)
210     ABORT ();
211   if (GNUNET_FS_uri_test_ksk (ret))
212     {
213       GNUNET_FS_uri_destroy (ret);
214       ABORT ();
215     }
216   if (GNUNET_FS_uri_test_sks (ret))
217     {
218       GNUNET_FS_uri_destroy (ret);
219       ABORT ();
220     }
221   if (GNUNET_ntohll (ret->data.chk.file_length) != 42)
222     {
223       GNUNET_FS_uri_destroy (ret);
224       ABORT ();
225     }
226
227   uri = GNUNET_FS_uri_to_string (ret);
228   if (0 != strcmp (uri,
229                    "gnunet://ecrs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000.42"))
230     {
231       GNUNET_free (uri);
232       GNUNET_FS_uri_destroy (ret);
233       ABORT ();
234     }
235   GNUNET_free (uri);
236   GNUNET_FS_uri_destroy (ret);
237   return 0;
238 }
239
240 int
241 main (int argc, char *argv[])
242 {
243   int failureCount = 0;
244   int i;
245
246   GNUNET_CRYPTO_random_disable_entropy_gathering ();
247   failureCount += testKeyword ();
248   failureCount += testLocation ();
249   for (i = 0; i < 255; i++)
250     {
251       failureCount += testNamespace (i);
252       failureCount += testFile (i);
253     }
254   if (failureCount != 0)
255     return 1;
256   return 0;
257 }
258
259 /* end of test_fs_uri.c */