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