Some fix and simple optimizations.
[bertos.git] / bertos / net / http_test.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2011 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief HTTP Server test
34  *
35  * \author Daniele Basile <asterix@develer.com>
36  *
37  * notest: avr
38  */
39
40 #include <cfg/compiler.h>
41 #include <cfg/test.h>
42 #include <cfg/debug.h>
43
44 #include <net/http.h>
45
46 #include <string.h>
47
48 static const char get_str[] = "\
49 GET /test/page1 HTTP/1.1 Host: 10.3.3.199 Connection: keep-alive \
50 User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.1 \
51 (KHTML, like Gecko) Chrome/14.0.835.186 Safari/535.1 \
52 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 \
53 Accept-Encoding: gzip,deflate,sdch Accept-Language: it-IT,it;q=0.8,en-US;\
54 q=0.6,en;q=0.4 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3";
55
56
57 static const char get_str1[] = "\
58 GET /test/page1";
59
60 static const char get_str2[] = "\
61 GET ";
62
63 static const char get_str3[] = "\
64 GAT /test/page1 HTTP/1.1 Host: 10.3.3.199 Connection: keep-alive \
65 User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.1 \
66 (KHTML, like Gecko) Chrome/14.0.835.186 Safari/535.1 \
67 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 \
68 Accept-Encoding: gzip,deflate,sdch Accept-Language: it-IT,it;q=0.8,en-US;\
69 q=0.6,en;q=0.4 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3";
70
71
72 static const char uri[] = "test%5B%5D!@;'%22%5C.%20";
73 static const char uri_check[] = "test[]!@;'\"\\. ";
74
75 static const char uri0[] = "12345";
76 static const char uri_check0[] = "1234";
77
78 static const char uri1[] = "!*'();:@&=%2B%24%2C/?#%5B%5D%3C%3E%7E.%22%7B%7D%7C%5C-%60_%5E%25";
79 static const char uri_check1[] = "!*'();:@&=+$,/?#[]<>~.\"{}|\\-`_^%";
80
81 static const char uri2[] = "test+test1+test2";
82 static const char uri_check2[] = "test test1 test2";
83
84 static char token_str[] = "var1=1&var2=2&var3=3&var4=4";
85 static char token_str1[] = "var1=1&var2=2&=3&var4=";
86 static char token_str2[] = "var1=test+test&var2=2&var3=test%5B%5D!@;'%22%5C.%20&var4=4";
87
88 static struct {const char *content;} contents[] =
89 {
90         {"one/two/three/test"},
91         {"one/two/three.htm"},
92         {"one/test.css"},
93         {"one/two/test.js"},
94         {"one/two/test.png"},
95         {"one/two/test.ico"},
96         {"one/two/test.jpg"},
97         {"one/two/test.gif"},
98 };
99
100
101 static int contents_check_type[] =
102 {
103         HTTP_CONTENT_JSON,
104         HTTP_CONTENT_HTML,
105         HTTP_CONTENT_CSS,
106         HTTP_CONTENT_JS,
107         HTTP_CONTENT_PNG,
108         HTTP_CONTENT_JPEG,
109         HTTP_CONTENT_JPEG,
110         HTTP_CONTENT_GIF,
111 };
112
113 #define CONTENT_TEST_CNT  8
114
115
116 int http_testSetup(void)
117 {
118         kdbg_init();
119         return 0;
120 }
121
122 int http_testRun(void)
123 {
124         char name[80];
125         memset(name, 0, sizeof(name));
126         http_getPageName(get_str, sizeof(get_str), name, sizeof(name));
127
128         if (strcmp("test/page1", name))
129         {
130                 kprintf("error 0 %s\n", name);
131                 goto error;
132         }
133
134         http_getPageName(get_str1, sizeof(get_str1), name, sizeof(name));
135
136         if (strcmp("test/page1", name))
137         {
138                 kprintf("error 1 %s\n", name);
139                 goto error;
140         }
141
142
143         http_getPageName(get_str2, sizeof(get_str2), name, sizeof(name));
144
145         if (name[0] != '\0')
146         {
147                 kprintf("error 2 %s\n", name);
148                 goto error;
149         }
150
151
152         http_getPageName(get_str2, sizeof(get_str2), name, sizeof(name));
153
154         if (name[0] != '\0')
155         {
156                 kprintf("error 3 %s\n", name);
157                 goto error;
158         }
159
160
161         char decoded0[5];
162         http_decodeUrl(uri0, 6, decoded0, 5);
163
164         if (strcmp(decoded0, uri_check0))
165         {
166                 kprintf("error 04 %s\n", decoded0);
167                 goto error;
168         }
169
170         char decoded[sizeof(uri)];
171         http_decodeUrl(uri,sizeof(uri), decoded, sizeof(uri));
172
173         if (strcmp(decoded, uri_check))
174         {
175                 kprintf("error 4 %s\n", decoded);
176                 goto error;
177         }
178
179         char decoded1[sizeof(uri1)];
180         http_decodeUrl(uri1,sizeof(uri1), decoded1, sizeof(uri1));
181
182         if (strcmp(decoded1, uri_check1))
183         {
184                 kprintf("error 5 %s\n", decoded1);
185                 goto error;
186         }
187
188         char decoded2[sizeof(uri2)];
189         http_decodeUrl(uri2,sizeof(uri2), decoded2, sizeof(uri2));
190
191         if (strcmp(decoded2, uri_check2))
192         {
193                 kprintf("error 5 %s\n", decoded2);
194                 goto error;
195         }
196
197         int len = http_tokenizeGetRequest(token_str, sizeof(token_str));
198         if (len != 4)
199         {
200                 kprintf("error 6 len %d expect %d\n", len, 4);
201                 goto error;
202         }
203
204         char value[80];
205         http_getValue(token_str, sizeof(token_str), "var1", value, sizeof(value));
206         if (strcmp(value, "1"))
207         {
208                 kprintf("error 6 value %s expect %s\n", value, "1");
209                 goto error;
210
211         }
212
213         http_getValue(token_str, sizeof(token_str), "var4", value, sizeof(value));
214         if (strcmp(value, "4"))
215         {
216                 kprintf("error 6 value %s expect %s\n", value, "4");
217                 goto error;
218
219         }
220
221
222         len = http_tokenizeGetRequest(token_str1, sizeof(token_str1));
223         if (len != 4)
224         {
225                 kprintf("error 7 len %d expect %d\n", len, 4);
226                 goto error;
227         }
228
229         if (http_getValue(token_str1, sizeof(token_str1), "var1", value, sizeof(value)) < 0)
230         {
231                 kprintf("error 7 during get key %s\n", "var1");
232                 goto error;
233         }
234         if (strcmp(value, "1"))
235         {
236                 kprintf("error 7 value %s expect %s\n", value, "1");
237                 goto error;
238
239         }
240
241         if (http_getValue(token_str1, sizeof(token_str1), "var4", value, sizeof(value)) < 0)
242         {
243                 kprintf("error 7 during get key %s\n", "var4");
244                 goto error;
245         }
246
247         if (strcmp(value, ""))
248         {
249                 kprintf("error 7 value %s expect %s\n", value, "");
250                 goto error;
251
252         }
253
254         len = http_tokenizeGetRequest(token_str2, sizeof(token_str2));
255         if (len != 4)
256         {
257                 kprintf("error 8 len %d expect %d\n", len, 4);
258                 goto error;
259         }
260
261         if (http_getValue(token_str2, sizeof(token_str2), "var3", value, sizeof(value)) < 0)
262         {
263                 kprintf("error 8 during get key %s\n", "var3");
264                 goto error;
265         }
266
267         if (strcmp(value, "test[]!@;'\"\\. "))
268         {
269                 kprintf("error 8 value %s expect %s\n", value, "test[]!@;'\"\\. ");
270                 goto error;
271
272         }
273
274         if (http_getValue(token_str2, sizeof(token_str2), "var1", value, sizeof(value)) < 0)
275         {
276                 kprintf("error 7 during get key %s\n", "var1");
277                 goto error;
278         }
279         if (strcmp(value, "test test"))
280         {
281                 kprintf("error 7 value %s expect %s\n", value, "test test");
282                 goto error;
283
284         }
285
286
287         for (int i = 0; i < CONTENT_TEST_CNT; i++)
288         {
289                 int type = http_searchContentType(contents[i].content);
290                 if (type != contents_check_type[i])
291                 {
292                         kprintf("error 8-%d return type %d expect %d\n", i, type, contents_check_type[i]);
293                         kprintf("error 8-%d ext %s\n", i, contents[i].content);
294                         goto error;
295                 }
296         }
297
298         return 0;
299
300 error:
301         kprintf("Error!\n");
302         return -1;
303 }
304
305 int http_testTearDown(void)
306 {
307         return 0;
308 }
309
310 TEST_MAIN(http);