adding a GNUNET_memcmp_priv for constant-time comparing of data; fixes #6152 (modulo...
[oweals/gnunet.git] / src / transport / test_http_common.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009, 2010 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 /**
21  * @file transport/test_http_common.c
22  * @brief base test case for common http functionality
23  */
24 #include "platform.h"
25 #include "gnunet_transport_service.h"
26 #include "transport-testing.h"
27 #include "plugin_transport_http_common.h"
28
29
30 static void
31 clean (struct SplittedHTTPAddress *addr)
32 {
33   if (NULL == addr)
34     return;
35   GNUNET_free_non_null (addr->host);
36   GNUNET_free_non_null (addr->path);
37   GNUNET_free_non_null (addr->protocol);
38   GNUNET_free (addr);
39 }
40
41
42 static int
43 check (struct SplittedHTTPAddress *addr,
44        const char *protocol,
45        const char *host,
46        int port,
47        const char *path)
48 {
49   if (NULL == addr)
50     return GNUNET_NO;
51   if (((NULL == addr->protocol) && (NULL != protocol)) ||
52       ((NULL != addr->protocol) && (NULL == protocol)))
53   {
54     GNUNET_break (0);
55     return GNUNET_NO;
56   }
57   else if ((NULL != addr->protocol) && (NULL != protocol))
58   {
59     if (0 != strcmp (addr->protocol, protocol))
60     {
61       GNUNET_break (0);
62       return GNUNET_NO;
63     }
64   }
65
66   if (((NULL == addr->host) && (NULL != host)) ||
67       ((NULL != addr->host) && (NULL == host)))
68   {
69     GNUNET_break (0);
70     return GNUNET_NO;
71   }
72   else if ((NULL != addr->host) && (NULL != host))
73   {
74     if (0 != strcmp (addr->host, host))
75     {
76       GNUNET_break (0);
77       return GNUNET_NO;
78     }
79   }
80
81   if (((NULL == addr->path) && (NULL != path)) ||
82       ((NULL != addr->path) && (NULL == path)))
83   {
84     GNUNET_break (0);
85     return GNUNET_NO;
86   }
87   else if ((NULL != addr->path) && (NULL != path))
88   {
89     if (0 != strcmp (addr->path, path))
90     {
91       GNUNET_break (0);
92       return GNUNET_NO;
93     }
94   }
95
96   if ((addr->port != port))
97   {
98     GNUNET_break (0);
99     return GNUNET_NO;
100   }
101   return GNUNET_OK;
102 }
103
104
105 static int
106 check_pass (const char *src,
107             const char *protocol,
108             const char *host,
109             int port,
110             const char *path)
111 {
112   struct SplittedHTTPAddress *spa;
113
114   spa = http_split_address (src);
115   if (NULL == spa)
116   {
117     GNUNET_break (0);
118     return GNUNET_SYSERR;
119   }
120   if (GNUNET_OK != check (spa, protocol, host, port, path))
121   {
122     clean (spa);
123     GNUNET_break (0);
124     return GNUNET_SYSERR;
125   }
126   clean (spa);
127   return GNUNET_OK;
128 }
129
130
131 static int
132 check_fail (const char *src)
133 {
134   struct SplittedHTTPAddress *spa;
135
136   spa = http_split_address (src);
137   if (NULL != spa)
138   {
139     GNUNET_break (0);
140     clean (spa);
141     return GNUNET_SYSERR;
142   }
143   return GNUNET_OK;
144 }
145
146
147 static void
148 test_pass_hostname ()
149 {
150   check_pass ("http://test.local", "http", "test.local", HTTP_DEFAULT_PORT, "");
151   check_pass ("http://test.local/", "http", "test.local", HTTP_DEFAULT_PORT,
152               "/");
153   check_pass ("http://test.local/path", "http", "test.local", HTTP_DEFAULT_PORT,
154               "/path");
155   check_pass ("http://test.local/path/", "http", "test.local",
156               HTTP_DEFAULT_PORT, "/path/");
157   check_pass ("http://test.local/path/more", "http", "test.local",
158               HTTP_DEFAULT_PORT, "/path/more");
159   check_pass ("http://test.local:81", "http", "test.local", 81, "");
160   check_pass ("http://test.local:81/", "http", "test.local", 81, "/");
161   check_pass ("http://test.local:81/path", "http", "test.local", 81, "/path");
162   check_pass ("http://test.local:81/path/", "http", "test.local", 81, "/path/");
163   check_pass ("http://test.local:81/path/more", "http", "test.local", 81,
164               "/path/more");
165 }
166
167
168 static void
169 test_pass_ipv4 ()
170 {
171   check_pass ("http://127.0.0.1", "http", "127.0.0.1", HTTP_DEFAULT_PORT, "");
172   check_pass ("http://127.0.0.1/", "http", "127.0.0.1", HTTP_DEFAULT_PORT, "/");
173   check_pass ("http://127.0.0.1/path", "http", "127.0.0.1", HTTP_DEFAULT_PORT,
174               "/path");
175   check_pass ("http://127.0.0.1/path/", "http", "127.0.0.1", HTTP_DEFAULT_PORT,
176               "/path/");
177   check_pass ("http://127.0.0.1:81", "http", "127.0.0.1", 81, "");
178   check_pass ("http://127.0.0.1:81/", "http", "127.0.0.1", 81, "/");
179   check_pass ("http://127.0.0.1:81/path", "http", "127.0.0.1", 81, "/path");
180   check_pass ("http://127.0.0.1:81/path/", "http", "127.0.0.1", 81, "/path/");
181   check_pass ("http://127.0.0.1:81/path/more", "http", "127.0.0.1", 81,
182               "/path/more");
183 }
184
185
186 static void
187 test_fail_ipv6 ()
188 {
189   check_pass ("http://[::1]", "http", "[::1]", HTTP_DEFAULT_PORT, "");
190   check_pass ("http://[::1]/", "http", "[::1]", HTTP_DEFAULT_PORT, "/");
191   check_pass ("http://[::1]/path", "http", "[::1]", HTTP_DEFAULT_PORT, "/path");
192   check_pass ("http://[::1]/path/", "http", "[::1]", HTTP_DEFAULT_PORT,
193               "/path/");
194   check_pass ("http://[::1]:81", "http", "[::1]", 81, "");
195   check_pass ("http://[::1]:81/", "http", "[::1]", 81, "/");
196   check_pass ("http://[::1]:81/path", "http", "[::1]", 81, "/path");
197   check_pass ("http://[::1]:81/path/", "http", "[::1]", 81, "/path/");
198   check_pass ("http://[::1]:81/path/more", "http", "[::1]", 81, "/path/more");
199 }
200
201
202 static void
203 test_fail ()
204 {
205   if (GNUNET_SYSERR == check_fail (""))
206     GNUNET_break (0);
207   if (GNUNET_SYSERR == check_fail ("http"))
208     GNUNET_break (0);
209   if (GNUNET_SYSERR == check_fail ("://"))
210     GNUNET_break (0);
211   if (GNUNET_SYSERR == check_fail ("http://"))
212     GNUNET_break (0);
213   if (GNUNET_SYSERR == check_fail ("//localhost"))
214     GNUNET_break (0);
215   if (GNUNET_SYSERR == check_fail ("//:80"))
216     GNUNET_break (0);
217   if (GNUNET_SYSERR == check_fail ("//:80/"))
218     GNUNET_break (0);
219   if (GNUNET_SYSERR == check_fail ("//:80:"))
220     GNUNET_break (0);
221   if (GNUNET_SYSERR == check_fail ("http://localhost:a/"))
222     GNUNET_break (0);
223   if (GNUNET_SYSERR == check_fail ("http://127.0.0.1:a/"))
224     GNUNET_break (0);
225 }
226
227
228 int
229 main (int argc, char *argv[])
230 {
231   int ret = 0;
232   struct SplittedHTTPAddress *spa;
233
234   GNUNET_log_setup ("test", "DEBUG", NULL);
235   spa = http_split_address ("");
236   if (NULL != spa)
237   {
238     clean (spa);
239     spa = NULL;
240     GNUNET_break (0);
241   }
242
243   spa = http_split_address ("http://");
244   if (NULL != spa)
245   {
246     clean (spa);
247     GNUNET_break (0);
248   }
249
250   spa = http_split_address ("://");
251   if (NULL != spa)
252   {
253     clean (spa);
254     GNUNET_break (0);
255   }
256
257   test_pass_hostname ();
258   test_pass_ipv4 ();
259   test_fail_ipv6 ();
260   test_fail ();
261
262   return ret;
263 }
264
265
266 /* end of test_http_common.c */