Deconding query string when we get a key value from it.
[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 uri1[] = "!*'();:@&=%2B%24%2C/?#%5B%5D%3C%3E%7E.%22%7B%7D%7C%5C-%60_%5E%25";
76 static const char uri_check1[] = "!*'();:@&=+$,/?#[]<>~.\"{}|\\-`_^%";
77
78 static const char uri2[] = "test+test1+test2";
79 static const char uri_check2[] = "test test1 test2";
80
81 static char token_str[] = "var1=1&var2=2&var3=3&var4=4";
82 static char token_str1[] = "var1=1&var2=2&=3&var4=";
83 static char token_str2[] = "var1=test+test&var2=2&var3=test%5B%5D!@;'%22%5C.%20&var4=4";
84
85 int http_testSetup(void)
86 {
87         kdbg_init();
88         return 0;
89 }
90
91 int http_testRun(void)
92 {
93         char name[80];
94         memset(name, 0, sizeof(name));
95         http_getPageName(get_str, sizeof(get_str), name, sizeof(name));
96
97         if (strcmp("test/page1", name))
98         {
99                 kprintf("error 0 %s\n", name);
100                 goto error;
101         }
102
103         http_getPageName(get_str1, sizeof(get_str1), name, sizeof(name));
104
105         if (strcmp("test/page1", name))
106         {
107                 kprintf("error 1 %s\n", name);
108                 goto error;
109         }
110
111
112         http_getPageName(get_str2, sizeof(get_str2), name, sizeof(name));
113
114         if (name[0] != '\0')
115         {
116                 kprintf("error 2 %s\n", name);
117                 goto error;
118         }
119
120
121         http_getPageName(get_str2, sizeof(get_str2), name, sizeof(name));
122
123         if (name[0] != '\0')
124         {
125                 kprintf("error 3 %s\n", name);
126                 goto error;
127         }
128
129
130         char decoded[sizeof(uri)];
131         http_decodeUrl(uri,sizeof(uri), decoded, sizeof(uri));
132
133         if (strcmp(decoded, uri_check))
134         {
135                 kprintf("error 4 %s\n", decoded);
136                 goto error;
137         }
138
139         char decoded1[sizeof(uri1)];
140         http_decodeUrl(uri1,sizeof(uri1), decoded1, sizeof(uri1));
141
142         if (strcmp(decoded1, uri_check1))
143         {
144                 kprintf("error 5 %s\n", decoded1);
145                 goto error;
146         }
147
148         char decoded2[sizeof(uri2)];
149         http_decodeUrl(uri2,sizeof(uri2), decoded2, sizeof(uri2));
150
151         if (strcmp(decoded2, uri_check2))
152         {
153                 kprintf("error 5 %s\n", decoded2);
154                 goto error;
155         }
156
157         int len = http_tokenizeGetRequest(token_str, sizeof(token_str));
158         if (len != 4)
159         {
160                 kprintf("error 6 len %d expect %d\n", len, 4);
161                 goto error;
162         }
163
164         char value[80];
165         http_getValue(token_str, sizeof(token_str), "var1", value, sizeof(value));
166         if (strcmp(value, "1"))
167         {
168                 kprintf("error 6 value %s expect %s\n", value, "1");
169                 goto error;
170
171         }
172
173         http_getValue(token_str, sizeof(token_str), "var4", value, sizeof(value));
174         if (strcmp(value, "4"))
175         {
176                 kprintf("error 6 value %s expect %s\n", value, "4");
177                 goto error;
178
179         }
180
181
182         len = http_tokenizeGetRequest(token_str1, sizeof(token_str1));
183         if (len != 4)
184         {
185                 kprintf("error 7 len %d expect %d\n", len, 4);
186                 goto error;
187         }
188
189         if (http_getValue(token_str1, sizeof(token_str1), "var1", value, sizeof(value)) < 0)
190         {
191                 kprintf("error 7 during get key %s\n", "var1");
192                 goto error;
193         }
194         if (strcmp(value, "1"))
195         {
196                 kprintf("error 7 value %s expect %s\n", value, "1");
197                 goto error;
198
199         }
200
201         if (http_getValue(token_str1, sizeof(token_str1), "var4", value, sizeof(value)) < 0)
202         {
203                 kprintf("error 7 during get key %s\n", "var4");
204                 goto error;
205         }
206
207         if (strcmp(value, ""))
208         {
209                 kprintf("error 7 value %s expect %s\n", value, "");
210                 goto error;
211
212         }
213
214         len = http_tokenizeGetRequest(token_str2, sizeof(token_str2));
215         if (len != 4)
216         {
217                 kprintf("error 8 len %d expect %d\n", len, 4);
218                 goto error;
219         }
220
221         if (http_getValue(token_str2, sizeof(token_str2), "var3", value, sizeof(value)) < 0)
222         {
223                 kprintf("error 8 during get key %s\n", "var3");
224                 goto error;
225         }
226
227         if (strcmp(value, "test[]!@;'\"\\. "))
228         {
229                 kprintf("error 8 value %s expect %s\n", value, "test[]!@;'\"\\. ");
230                 goto error;
231
232         }
233
234         if (http_getValue(token_str2, sizeof(token_str2), "var1", value, sizeof(value)) < 0)
235         {
236                 kprintf("error 7 during get key %s\n", "var1");
237                 goto error;
238         }
239         if (strcmp(value, "test test"))
240         {
241                 kprintf("error 7 value %s expect %s\n", value, "test test");
242                 goto error;
243
244         }
245
246         return 0;
247
248 error:
249         kprintf("Error!\n");
250         return -1;
251 }
252
253 int http_testTearDown(void)
254 {
255         return 0;
256 }
257
258 TEST_MAIN(http);