Merge branch 'license/spdx'
[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   check_pass("http://test.local/path", "http", "test.local", HTTP_DEFAULT_PORT, "/path");
153   check_pass("http://test.local/path/", "http", "test.local", HTTP_DEFAULT_PORT, "/path/");
154   check_pass("http://test.local/path/more", "http", "test.local", HTTP_DEFAULT_PORT, "/path/more");
155   check_pass("http://test.local:81", "http", "test.local", 81, "");
156   check_pass("http://test.local:81/", "http", "test.local", 81, "/");
157   check_pass("http://test.local:81/path", "http", "test.local", 81, "/path");
158   check_pass("http://test.local:81/path/", "http", "test.local", 81, "/path/");
159   check_pass("http://test.local:81/path/more", "http", "test.local", 81, "/path/more");
160
161 }
162
163
164 static void
165 test_pass_ipv4 ()
166 {
167   check_pass("http://127.0.0.1", "http", "127.0.0.1", HTTP_DEFAULT_PORT, "");
168   check_pass("http://127.0.0.1/", "http", "127.0.0.1", HTTP_DEFAULT_PORT, "/");
169   check_pass("http://127.0.0.1/path", "http", "127.0.0.1", HTTP_DEFAULT_PORT, "/path");
170   check_pass("http://127.0.0.1/path/", "http", "127.0.0.1", HTTP_DEFAULT_PORT, "/path/");
171   check_pass("http://127.0.0.1:81", "http", "127.0.0.1", 81, "");
172   check_pass("http://127.0.0.1:81/", "http", "127.0.0.1", 81, "/");
173   check_pass("http://127.0.0.1:81/path", "http", "127.0.0.1", 81, "/path");
174   check_pass("http://127.0.0.1:81/path/", "http", "127.0.0.1", 81, "/path/");
175   check_pass("http://127.0.0.1:81/path/more", "http", "127.0.0.1", 81, "/path/more");
176 }
177
178
179 static void
180 test_fail_ipv6 ()
181 {
182   check_pass("http://[::1]", "http", "[::1]", HTTP_DEFAULT_PORT, "");
183   check_pass("http://[::1]/", "http", "[::1]", HTTP_DEFAULT_PORT, "/");
184   check_pass("http://[::1]/path", "http", "[::1]", HTTP_DEFAULT_PORT, "/path");
185   check_pass("http://[::1]/path/", "http", "[::1]", HTTP_DEFAULT_PORT, "/path/");
186   check_pass("http://[::1]:81", "http", "[::1]", 81, "");
187   check_pass("http://[::1]:81/", "http", "[::1]", 81, "/");
188   check_pass("http://[::1]:81/path", "http", "[::1]", 81, "/path");
189   check_pass("http://[::1]:81/path/", "http", "[::1]", 81, "/path/");
190   check_pass("http://[::1]:81/path/more", "http", "[::1]", 81, "/path/more");
191 }
192
193
194 static void
195 test_fail ()
196 {
197   if (GNUNET_SYSERR == check_fail (""))
198     GNUNET_break (0);
199   if (GNUNET_SYSERR == check_fail ("http"))
200     GNUNET_break (0);
201   if (GNUNET_SYSERR == check_fail ("://"))
202     GNUNET_break (0);
203   if (GNUNET_SYSERR == check_fail ("http://"))
204     GNUNET_break (0);
205   if (GNUNET_SYSERR == check_fail ("//localhost"))
206     GNUNET_break (0);
207   if (GNUNET_SYSERR == check_fail ("//:80"))
208     GNUNET_break (0);
209   if (GNUNET_SYSERR == check_fail ("//:80/"))
210     GNUNET_break (0);
211   if (GNUNET_SYSERR == check_fail ("//:80:"))
212     GNUNET_break (0);
213   if (GNUNET_SYSERR == check_fail ("http://localhost:a/"))
214     GNUNET_break (0);
215   if (GNUNET_SYSERR == check_fail ("http://127.0.0.1:a/"))
216     GNUNET_break (0);
217 }
218
219
220 int
221 main (int argc, char *argv[])
222 {
223   int ret = 0;
224   struct SplittedHTTPAddress * spa;
225
226   GNUNET_log_setup ("test", "DEBUG", NULL);
227   spa = http_split_address ("");
228   if (NULL != spa)
229   {
230     clean (spa);
231     spa = NULL;
232     GNUNET_break (0);
233   }
234
235   spa = http_split_address ("http://");
236   if (NULL != spa)
237   {
238     clean (spa);
239     GNUNET_break (0);
240   }
241
242   spa = http_split_address ("://");
243   if (NULL != spa)
244   {
245     clean (spa);
246     GNUNET_break (0);
247   }
248
249   test_pass_hostname ();
250   test_pass_ipv4 ();
251   test_fail_ipv6 ();
252   test_fail ();
253
254   return ret;
255 }
256
257 /* end of test_http_common.c */