f24f4fe27b1a433391b35c278ca80c689c9f9a36
[oweals/gnunet.git] / src / fs / test_fs_uri.c
1 /*
2      This file is part of GNUnet.
3      (C) 2003, 2004, 2006, 2007 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 applications/fs/ecrs/uritest.c
23  * @brief Test for uri.c
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
28 #include "gnunet_util.h"
29 #include "gnunet_ecrs_lib.h"
30 #include "ecrs.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_ECRS_URI *ret;
39
40   if (NULL != GNUNET_ECRS_string_to_uri (NULL, "gnunet://ecrs/ksk/++"))
41     ABORT ();
42   ret = GNUNET_ECRS_string_to_uri (NULL, "gnunet://ecrs/ksk/foo+bar");
43   if (ret == NULL)
44     ABORT ();
45   if (!GNUNET_ECRS_uri_test_ksk (ret))
46     {
47       GNUNET_ECRS_uri_destroy (ret);
48       ABORT ();
49     }
50   if ((2 != ret->data.ksk.keywordCount) ||
51       (0 != strcmp (" foo", ret->data.ksk.keywords[0])) ||
52       (0 != strcmp (" bar", ret->data.ksk.keywords[1])))
53     {
54       GNUNET_ECRS_uri_destroy (ret);
55       ABORT ();
56     }
57
58   uri = GNUNET_ECRS_uri_to_string (ret);
59   if (0 != strcmp (uri, "gnunet://ecrs/ksk/foo+bar"))
60     {
61       GNUNET_free (uri);
62       GNUNET_ECRS_uri_destroy (ret);
63       ABORT ();
64     }
65   GNUNET_free (uri);
66   GNUNET_ECRS_uri_destroy (ret);
67   return 0;
68 }
69
70 static int
71 testLocation ()
72 {
73   struct GNUNET_ECRS_URI *uri;
74   char *uric;
75   struct GNUNET_ECRS_URI *uri2;
76   GNUNET_RSA_PublicKey pk;
77   struct GNUNET_RSA_PrivateKey *hk;
78   struct GNUNET_ECRS_URI *baseURI;
79
80   baseURI =
81     GNUNET_ECRS_string_to_uri (NULL,
82                                "gnunet://ecrs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000.42");
83   hk = GNUNET_RSA_create_key ();
84   GNUNET_RSA_get_public_key (hk, &pk);
85   uri = GNUNET_ECRS_location_to_uri (baseURI,
86                                      &pk, 43,
87                                      (GNUNET_ECRS_SignFunction) &
88                                      GNUNET_RSA_sign, hk);
89   GNUNET_RSA_free_key (hk);
90   if (uri == NULL)
91     {
92       GNUNET_GE_BREAK (NULL, 0);
93       GNUNET_ECRS_uri_destroy (baseURI);
94       return 1;
95     }
96   if (!GNUNET_ECRS_uri_test_loc (uri))
97     {
98       GNUNET_GE_BREAK (NULL, 0);
99       GNUNET_ECRS_uri_destroy (uri);
100       GNUNET_ECRS_uri_destroy (baseURI);
101       return 1;
102     }
103   uri2 = GNUNET_ECRS_uri_get_content_uri_from_loc (uri);
104   if (!GNUNET_ECRS_uri_test_equal (baseURI, uri2))
105     {
106       GNUNET_GE_BREAK (NULL, 0);
107       GNUNET_ECRS_uri_destroy (uri);
108       GNUNET_ECRS_uri_destroy (uri2);
109       GNUNET_ECRS_uri_destroy (baseURI);
110       return 1;
111     }
112   GNUNET_ECRS_uri_destroy (uri2);
113   GNUNET_ECRS_uri_destroy (baseURI);
114   uric = GNUNET_ECRS_uri_to_string (uri);
115 #if 0
116   /* not for the faint of heart: */
117   printf ("URI: `%s'\n", uric);
118 #endif
119   uri2 = GNUNET_ECRS_string_to_uri (NULL, uric);
120   GNUNET_free (uric);
121   if (uri2 == NULL)
122     {
123       GNUNET_GE_BREAK (NULL, 0);
124       GNUNET_ECRS_uri_destroy (uri);
125       return 1;
126     }
127   if (GNUNET_YES != GNUNET_ECRS_uri_test_equal (uri, uri2))
128     {
129       GNUNET_GE_BREAK (NULL, 0);
130       GNUNET_ECRS_uri_destroy (uri);
131       GNUNET_ECRS_uri_destroy (uri2);
132       return 1;
133     }
134   GNUNET_ECRS_uri_destroy (uri2);
135   GNUNET_ECRS_uri_destroy (uri);
136   return 0;
137 }
138
139 static int
140 testNamespace (int i)
141 {
142   char *uri;
143   struct GNUNET_ECRS_URI *ret;
144
145   if (NULL !=
146       GNUNET_ECRS_string_to_uri (NULL,
147                                  "gnunet://ecrs/sks/D1KJS9H2A82Q65VKQ0ML3RFU6U1D3VUK"))
148     ABORT ();
149   if (NULL !=
150       GNUNET_ECRS_string_to_uri (NULL,
151                                  "gnunet://ecrs/sks/D1KJS9H2A82Q65VKQ0ML3RFU6U1D3V/test"))
152     ABORT ();
153   if (NULL != GNUNET_ECRS_string_to_uri (NULL, "gnunet://ecrs/sks/test"))
154     ABORT ();
155   ret =
156     GNUNET_ECRS_string_to_uri (NULL,
157                                "gnunet://ecrs/sks/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820/test");
158   if (ret == NULL)
159     ABORT ();
160   if (GNUNET_ECRS_uri_test_ksk (ret))
161     {
162       GNUNET_ECRS_uri_destroy (ret);
163       ABORT ();
164     }
165   if (!GNUNET_ECRS_uri_test_sks (ret))
166     {
167       GNUNET_ECRS_uri_destroy (ret);
168       ABORT ();
169     }
170
171   uri = GNUNET_ECRS_uri_to_string (ret);
172   if (0 != strcmp (uri,
173                    "gnunet://ecrs/sks/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820/test"))
174     {
175       GNUNET_ECRS_uri_destroy (ret);
176       GNUNET_free (uri);
177       ABORT ();
178     }
179   GNUNET_free (uri);
180   GNUNET_ECRS_uri_destroy (ret);
181   return 0;
182 }
183
184 static int
185 testFile (int i)
186 {
187   char *uri;
188   struct GNUNET_ECRS_URI *ret;
189
190   if (NULL !=
191       GNUNET_ECRS_string_to_uri (NULL,
192                                  "gnunet://ecrs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H00000440000.42"))
193     ABORT ();
194   if (NULL !=
195       GNUNET_ECRS_string_to_uri (NULL,
196                                  "gnunet://ecrs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000"))
197     ABORT ();
198   if (NULL !=
199       GNUNET_ECRS_string_to_uri (NULL,
200                                  "gnunet://ecrs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000.FGH"))
201     ABORT ();
202   ret =
203     GNUNET_ECRS_string_to_uri (NULL,
204                                "gnunet://ecrs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000.42");
205   if (ret == NULL)
206     ABORT ();
207   if (GNUNET_ECRS_uri_test_ksk (ret))
208     {
209       GNUNET_ECRS_uri_destroy (ret);
210       ABORT ();
211     }
212   if (GNUNET_ECRS_uri_test_sks (ret))
213     {
214       GNUNET_ECRS_uri_destroy (ret);
215       ABORT ();
216     }
217   if (GNUNET_ntohll (ret->data.fi.file_length) != 42)
218     {
219       GNUNET_ECRS_uri_destroy (ret);
220       ABORT ();
221     }
222
223   uri = GNUNET_ECRS_uri_to_string (ret);
224   if (0 != strcmp (uri,
225                    "gnunet://ecrs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000.42"))
226     {
227       GNUNET_free (uri);
228       GNUNET_ECRS_uri_destroy (ret);
229       ABORT ();
230     }
231   GNUNET_free (uri);
232   GNUNET_ECRS_uri_destroy (ret);
233   return 0;
234 }
235
236 int
237 main (int argc, char *argv[])
238 {
239   int failureCount = 0;
240   int i;
241
242   GNUNET_disable_entropy_gathering ();
243   failureCount += testKeyword ();
244   failureCount += testLocation ();
245   for (i = 0; i < 255; i++)
246     {
247       failureCount += testNamespace (i);
248       failureCount += testFile (i);
249     }
250   if (failureCount != 0)
251     return 1;
252   return 0;
253 }
254
255 /* end of uritest.c */