-Merge branch 'master' of ssh://gnunet.org/gnunet into gsoc2018/rest_api
[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 /**
19  * @file transport/test_http_common.c
20  * @brief base test case for common http functionality
21  */
22 #include "platform.h"
23 #include "gnunet_transport_service.h"
24 #include "transport-testing.h"
25 #include "plugin_transport_http_common.h"
26
27
28 static void
29 clean (struct SplittedHTTPAddress *addr)
30 {
31   if (NULL == addr)
32     return;
33   GNUNET_free_non_null (addr->host);
34   GNUNET_free_non_null (addr->path);
35   GNUNET_free_non_null (addr->protocol);
36   GNUNET_free (addr);
37 }
38
39
40 static int
41 check (struct SplittedHTTPAddress *addr,
42        const char *protocol,
43        const char *host,
44        int port,
45        const char *path)
46 {
47   if (NULL == addr)
48     return GNUNET_NO;
49   if (((NULL == addr->protocol) && (NULL != protocol)) ||
50       ((NULL != addr->protocol) && (NULL == protocol)))
51   {
52     GNUNET_break (0);
53     return GNUNET_NO;
54   }
55   else if ((NULL != addr->protocol) && (NULL != protocol))
56   {
57     if (0 != strcmp(addr->protocol, protocol))
58     {
59       GNUNET_break (0);
60       return GNUNET_NO;
61     }
62   }
63
64   if (((NULL == addr->host) && (NULL != host)) ||
65       ((NULL != addr->host) && (NULL == host)))
66   {
67     GNUNET_break (0);
68     return GNUNET_NO;
69   }
70   else if ((NULL != addr->host) && (NULL != host))
71   {
72     if (0 != strcmp(addr->host, host))
73     {
74       GNUNET_break (0);
75       return GNUNET_NO;
76     }
77   }
78
79   if (((NULL == addr->path) && (NULL != path)) ||
80       ((NULL != addr->path) && (NULL == path)))
81   {
82     GNUNET_break (0);
83     return GNUNET_NO;
84   }
85   else if ((NULL != addr->path) && (NULL != path))
86   {
87     if (0 != strcmp(addr->path, path))
88     {
89       GNUNET_break (0);
90       return GNUNET_NO;
91     }
92   }
93
94   if ((addr->port != port))
95   {
96     GNUNET_break (0);
97     return GNUNET_NO;
98   }
99   return GNUNET_OK;
100 }
101
102
103 static int
104 check_pass (const char *src,
105             const char *protocol,
106             const char *host,
107             int port,
108             const char *path)
109 {
110   struct SplittedHTTPAddress *spa;
111
112   spa = http_split_address (src);
113   if (NULL == spa)
114   {
115     GNUNET_break (0);
116     return GNUNET_SYSERR;
117   }
118   if (GNUNET_OK != check(spa, protocol, host, port, path))
119   {
120     clean (spa);
121     GNUNET_break (0);
122     return GNUNET_SYSERR;
123   }
124   clean (spa);
125   return GNUNET_OK;
126 }
127
128
129 static int
130 check_fail (const char *src)
131 {
132   struct SplittedHTTPAddress * spa;
133
134   spa = http_split_address (src);
135   if (NULL != spa)
136   {
137     GNUNET_break (0);
138     clean (spa);
139     return GNUNET_SYSERR;
140   }
141   return GNUNET_OK;
142 }
143
144
145 static void
146 test_pass_hostname ()
147 {
148   check_pass("http://test.local", "http", "test.local", HTTP_DEFAULT_PORT, "");
149   check_pass("http://test.local/", "http", "test.local", HTTP_DEFAULT_PORT, "/");
150   check_pass("http://test.local/path", "http", "test.local", HTTP_DEFAULT_PORT, "/path");
151   check_pass("http://test.local/path/", "http", "test.local", HTTP_DEFAULT_PORT, "/path/");
152   check_pass("http://test.local/path/more", "http", "test.local", HTTP_DEFAULT_PORT, "/path/more");
153   check_pass("http://test.local:81", "http", "test.local", 81, "");
154   check_pass("http://test.local:81/", "http", "test.local", 81, "/");
155   check_pass("http://test.local:81/path", "http", "test.local", 81, "/path");
156   check_pass("http://test.local:81/path/", "http", "test.local", 81, "/path/");
157   check_pass("http://test.local:81/path/more", "http", "test.local", 81, "/path/more");
158
159 }
160
161
162 static void
163 test_pass_ipv4 ()
164 {
165   check_pass("http://127.0.0.1", "http", "127.0.0.1", HTTP_DEFAULT_PORT, "");
166   check_pass("http://127.0.0.1/", "http", "127.0.0.1", HTTP_DEFAULT_PORT, "/");
167   check_pass("http://127.0.0.1/path", "http", "127.0.0.1", HTTP_DEFAULT_PORT, "/path");
168   check_pass("http://127.0.0.1/path/", "http", "127.0.0.1", HTTP_DEFAULT_PORT, "/path/");
169   check_pass("http://127.0.0.1:81", "http", "127.0.0.1", 81, "");
170   check_pass("http://127.0.0.1:81/", "http", "127.0.0.1", 81, "/");
171   check_pass("http://127.0.0.1:81/path", "http", "127.0.0.1", 81, "/path");
172   check_pass("http://127.0.0.1:81/path/", "http", "127.0.0.1", 81, "/path/");
173   check_pass("http://127.0.0.1:81/path/more", "http", "127.0.0.1", 81, "/path/more");
174 }
175
176
177 static void
178 test_fail_ipv6 ()
179 {
180   check_pass("http://[::1]", "http", "[::1]", HTTP_DEFAULT_PORT, "");
181   check_pass("http://[::1]/", "http", "[::1]", HTTP_DEFAULT_PORT, "/");
182   check_pass("http://[::1]/path", "http", "[::1]", HTTP_DEFAULT_PORT, "/path");
183   check_pass("http://[::1]/path/", "http", "[::1]", HTTP_DEFAULT_PORT, "/path/");
184   check_pass("http://[::1]:81", "http", "[::1]", 81, "");
185   check_pass("http://[::1]:81/", "http", "[::1]", 81, "/");
186   check_pass("http://[::1]:81/path", "http", "[::1]", 81, "/path");
187   check_pass("http://[::1]:81/path/", "http", "[::1]", 81, "/path/");
188   check_pass("http://[::1]:81/path/more", "http", "[::1]", 81, "/path/more");
189 }
190
191
192 static void
193 test_fail ()
194 {
195   if (GNUNET_SYSERR == check_fail (""))
196     GNUNET_break (0);
197   if (GNUNET_SYSERR == check_fail ("http"))
198     GNUNET_break (0);
199   if (GNUNET_SYSERR == check_fail ("://"))
200     GNUNET_break (0);
201   if (GNUNET_SYSERR == check_fail ("http://"))
202     GNUNET_break (0);
203   if (GNUNET_SYSERR == check_fail ("//localhost"))
204     GNUNET_break (0);
205   if (GNUNET_SYSERR == check_fail ("//:80"))
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 ("http://localhost:a/"))
212     GNUNET_break (0);
213   if (GNUNET_SYSERR == check_fail ("http://127.0.0.1:a/"))
214     GNUNET_break (0);
215 }
216
217
218 int
219 main (int argc, char *argv[])
220 {
221   int ret = 0;
222   struct SplittedHTTPAddress * spa;
223
224   GNUNET_log_setup ("test", "DEBUG", NULL);
225   spa = http_split_address ("");
226   if (NULL != spa)
227   {
228     clean (spa);
229     spa = NULL;
230     GNUNET_break (0);
231   }
232
233   spa = http_split_address ("http://");
234   if (NULL != spa)
235   {
236     clean (spa);
237     GNUNET_break (0);
238   }
239
240   spa = http_split_address ("://");
241   if (NULL != spa)
242   {
243     clean (spa);
244     GNUNET_break (0);
245   }
246
247   test_pass_hostname ();
248   test_pass_ipv4 ();
249   test_fail_ipv6 ();
250   test_fail ();
251
252   return ret;
253 }
254
255 /* end of test_http_common.c */