]> git.saurik.com Git - wxWidgets.git/blob - tests/uris/uris.cpp
Applied patch [ 1232933 ] [WinCE] wxDateTime incorrect reading
[wxWidgets.git] / tests / uris / uris.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/uris/uris.cpp
3 // Purpose: wxURI unit test
4 // Author: Ryan Norton
5 // Created: 2004-08-14
6 // RCS-ID: $Id$
7 // Copyright: (c) 2004 Ryan Norton
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "testprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #ifndef WX_PRECOMP
21 #include "wx/wx.h"
22 #endif // WX_PRECOMP
23
24 #include "wx/uri.h"
25 #include "wx/url.h"
26
27 // Test wxURL & wxURI compat?
28 #define TEST_URL ( 1 && wxUSE_URL )
29
30 // ----------------------------------------------------------------------------
31 // test class
32 // ----------------------------------------------------------------------------
33
34 class URITestCase : public CppUnit::TestCase
35 {
36 public:
37 URITestCase();
38
39 private:
40 CPPUNIT_TEST_SUITE( URITestCase );
41 CPPUNIT_TEST( IPv4 );
42 CPPUNIT_TEST( IPv6 );
43 CPPUNIT_TEST( Paths );
44 CPPUNIT_TEST( NormalResolving );
45 CPPUNIT_TEST( ComplexResolving );
46 CPPUNIT_TEST( ReallyComplexResolving );
47 CPPUNIT_TEST( QueryFragmentResolving );
48 CPPUNIT_TEST( BackwardsResolving );
49 CPPUNIT_TEST( Assignment );
50 CPPUNIT_TEST( Comparison );
51 CPPUNIT_TEST( Unescaping );
52 #if TEST_URL
53 CPPUNIT_TEST( URLCompat );
54 #if wxUSE_PROTOCOL_HTTP
55 CPPUNIT_TEST( URLProxy );
56 #endif
57 #endif
58 CPPUNIT_TEST_SUITE_END();
59
60 void IPv4();
61 void IPv6();
62 void Paths();
63 void NormalResolving();
64 void ComplexResolving();
65 void ReallyComplexResolving();
66 void QueryFragmentResolving();
67 void BackwardsResolving();
68 void Assignment();
69 void Comparison();
70 void Unescaping();
71
72 #if TEST_URL
73 void URLCompat();
74 void URLProxy();
75 #endif
76
77 DECLARE_NO_COPY_CLASS(URITestCase)
78 };
79
80 // register in the unnamed registry so that these tests are run by default
81 CPPUNIT_TEST_SUITE_REGISTRATION( URITestCase );
82
83 // also include in it's own registry so that these tests can be run alone
84 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( URITestCase, "URITestCase" );
85
86 URITestCase::URITestCase()
87 {
88 }
89
90
91 #define URI_TEST(uristring, cond) \
92 uri = new wxURI(wxT(uristring));\
93 CPPUNIT_ASSERT(cond);\
94 delete uri;
95
96 #define URI_PRINT(uri)\
97 wxPrintf(wxT("SCHEME:%s\n"), uri.GetScheme());\
98 wxPrintf(wxT("USER:%s\n"), uri.GetUser());\
99 wxPrintf(wxT("SERVER:%s\n"), uri.GetServer());\
100 wxPrintf(wxT("PORT:%s\n"), uri.GetPort());\
101 wxPrintf(wxT("PATH:%s\n"), uri.GetPath());\
102 wxPrintf(wxT("QUERY:%s\n"), uri.GetQuery());\
103 wxPrintf(wxT("FRAGMENT:%s\n"), uri.GetFragment());
104
105 void URITestCase::IPv4()
106 {
107 wxURI* uri;
108
109
110 URI_TEST("http://user:password@192.168.1.100:5050/path",
111 uri->GetHostType() == wxURI_IPV4ADDRESS);
112
113 URI_TEST("http://user:password@192.255.1.100:5050/path",
114 uri->GetHostType() == wxURI_IPV4ADDRESS);
115
116 //bogus ipv4
117 URI_TEST("http://user:password@192.256.1.100:5050/path",
118 uri->GetHostType() != wxURI_IPV4ADDRESS);
119 }
120
121 void URITestCase::IPv6()
122 {
123 wxURI* uri;
124
125 // IPv6address = 6( h16 ":" ) ls32
126 // / "::" 5( h16 ":" ) ls32
127 // / [ h16 ] "::" 4( h16 ":" ) ls32
128 // / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
129 // / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
130 // / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
131 // / [ *4( h16 ":" ) h16 ] "::" ls32
132 // / [ *5( h16 ":" ) h16 ] "::" h16
133 // / [ *6( h16 ":" ) h16 ] "::"
134 // ls32 = ( h16 ":" h16 ) / IPv4address
135
136 URI_TEST("http://user:password@[aa:aa:aa:aa:aa:aa:192.168.1.100]:5050/path",
137 uri->GetHostType() == wxURI_IPV6ADDRESS);
138
139 URI_TEST("http://user:password@[aa:aa:aa:aa:aa:aa:aa:aa]:5050/path",
140 uri->GetHostType() == wxURI_IPV6ADDRESS);
141
142 URI_TEST("http://user:password@[aa:aa:aa:aa::192.168.1.100]:5050/path",
143 uri->GetHostType() == wxURI_IPV6ADDRESS);
144
145 URI_TEST("http://user:password@[aa:aa:aa:aa::aa:aa]:5050/path",
146 uri->GetHostType() == wxURI_IPV6ADDRESS);
147 }
148
149 void URITestCase::Paths()
150 {
151 wxURI* uri;
152
153 //path tests
154 URI_TEST("http://user:password@192.256.1.100:5050/../path",
155 uri->GetPath() == wxT("/path"));
156
157 URI_TEST("http://user:password@192.256.1.100:5050/path/../",
158 uri->GetPath() == wxT("/"));
159
160 URI_TEST("http://user:password@192.256.1.100:5050/path/.",
161 uri->GetPath() == wxT("/path/"));
162
163 URI_TEST("http://user:password@192.256.1.100:5050/path/./",
164 uri->GetPath() == wxT("/path/"));
165
166 URI_TEST("path/john/../../../joe",
167 uri->BuildURI() == wxT("../joe"));
168 }
169 #undef URI_TEST
170
171 #define URI_TEST_RESOLVE(string, eq, strict) \
172 uri = new wxURI(wxT(string));\
173 uri->Resolve(masteruri, strict);\
174 CPPUNIT_ASSERT(uri->BuildURI() == wxT(eq));\
175 delete uri;
176
177 #define URI_TEST(string, eq) \
178 URI_TEST_RESOLVE(string, eq, true);
179
180
181 //examples taken from RFC 2396.bis
182
183 void URITestCase::NormalResolving()
184 {
185 wxURI masteruri(wxT("http://a/b/c/d;p?q"));
186 wxURI* uri;
187
188 URI_TEST("g:h" ,"g:h")
189 URI_TEST("g" ,"http://a/b/c/g")
190 URI_TEST("./g" ,"http://a/b/c/g")
191 URI_TEST("g/" ,"http://a/b/c/g/")
192 URI_TEST("/g" ,"http://a/g")
193 URI_TEST("//g" ,"http://g")
194 URI_TEST("?y" ,"http://a/b/c/d;p?y")
195 URI_TEST("g?y" ,"http://a/b/c/g?y")
196 URI_TEST("#s" ,"http://a/b/c/d;p?q#s")
197 URI_TEST("g#s" ,"http://a/b/c/g#s")
198 URI_TEST("g?y#s","http://a/b/c/g?y#s")
199 URI_TEST(";x" ,"http://a/b/c/;x")
200 URI_TEST("g;x" ,"http://a/b/c/g;x")
201 URI_TEST("g;x?y#s","http://a/b/c/g;x?y#s")
202
203 URI_TEST("" ,"http://a/b/c/d;p?q")
204 URI_TEST("." ,"http://a/b/c/")
205 URI_TEST("./" ,"http://a/b/c/")
206 URI_TEST(".." ,"http://a/b/")
207 URI_TEST("../" ,"http://a/b/")
208 URI_TEST("../g" ,"http://a/b/g")
209 URI_TEST("../..","http://a/")
210 URI_TEST("../../" , "http://a/")
211 URI_TEST("../../g" , "http://a/g")
212 }
213
214 void URITestCase::ComplexResolving()
215 {
216 wxURI masteruri(wxT("http://a/b/c/d;p?q"));
217 wxURI* uri;
218
219 //odd path examples
220 URI_TEST("/./g" ,"http://a/g")
221 URI_TEST("/../g" ,"http://a/g")
222 URI_TEST("g." ,"http://a/b/c/g.")
223 URI_TEST(".g" ,"http://a/b/c/.g")
224 URI_TEST("g.." ,"http://a/b/c/g..")
225 URI_TEST("..g" ,"http://a/b/c/..g")
226 }
227 //Should Fail
228 //"../../../g" = "http://a/g"
229 //"../../../../g" = "http://a/g"
230
231 void URITestCase::ReallyComplexResolving()
232 {
233 wxURI masteruri(wxT("http://a/b/c/d;p?q"));
234 wxURI* uri;
235
236 //even more odder path examples
237 URI_TEST("./../g" ,"http://a/b/g")
238 URI_TEST("./g/." ,"http://a/b/c/g/")
239 URI_TEST("g/./h" ,"http://a/b/c/g/h")
240 URI_TEST("g/../h" ,"http://a/b/c/h")
241 URI_TEST("g;x=1/./y" , "http://a/b/c/g;x=1/y")
242 URI_TEST("g;x=1/../y" , "http://a/b/c/y")
243 }
244
245 void URITestCase::QueryFragmentResolving()
246 {
247 wxURI masteruri(wxT("http://a/b/c/d;p?q"));
248 wxURI* uri;
249
250 //query/fragment ambigiousness
251 URI_TEST("g?y/./x","http://a/b/c/g?y/./x")
252 URI_TEST("g?y/../x" , "http://a/b/c/g?y/../x")
253 URI_TEST("g#s/./x","http://a/b/c/g#s/./x")
254 URI_TEST("g#s/../x" , "http://a/b/c/g#s/../x")
255 }
256
257 void URITestCase::BackwardsResolving()
258 {
259 wxURI masteruri(wxT("http://a/b/c/d;p?q"));
260 wxURI* uri;
261
262 //"NEW"
263 URI_TEST("http:g" , "http:g") //strict
264 //bw compat
265 URI_TEST_RESOLVE("http:g", "http://a/b/c/g", false);
266 }
267
268 void URITestCase::Assignment()
269 {
270 wxURI uri1(wxT("http://mysite.com")),
271 uri2(wxT("http://mysite2.com"));
272
273 uri2 = uri1;
274
275 CPPUNIT_ASSERT(uri1.BuildURI() == uri2.BuildURI());
276 }
277
278 void URITestCase::Comparison()
279 {
280 CPPUNIT_ASSERT(wxURI(wxT("http://mysite.com")) == wxURI(wxT("http://mysite.com")));
281 }
282
283 void URITestCase::Unescaping()
284 {
285 wxString orig = wxT("http://test.com/of/file%3A%2F%2FC%3A%5Curi%5C")
286 wxT("escaping%5Cthat%5Cseems%5Cbroken%5Csadly%5B1%5D.rss");
287
288 wxString works= wxURI(orig).BuildUnescapedURI();
289
290 CPPUNIT_ASSERT(orig.IsSameAs(works) == false);
291
292 wxString orig2 = wxT("http://test.com/of/file%3A%2F%")
293 wxT("2FC%3A%5Curi%5Cescaping%5Cthat%5Cseems%")
294 wxT("5Cbroken%5Csadly%5B1%5D.rss");
295
296 wxString works2 = wxURI::Unescape(orig2);
297 wxString broken2 = wxURI(orig2).BuildUnescapedURI();
298
299 CPPUNIT_ASSERT(works2.IsSameAs(broken2));
300
301 }
302 #if TEST_URL
303
304 const wxChar* pszProblemUrls[] = { wxT("http://www.csdn.net"),
305 wxT("http://www.163.com"),
306 wxT("http://www.sina.com.cn") };
307
308 #include "wx/url.h"
309 #include "wx/file.h"
310
311 void URITestCase::URLCompat()
312 {
313 wxURL url(wxT("http://user:password@wxwidgets.org"));
314
315 CPPUNIT_ASSERT(url.GetError() == wxURL_NOERR);
316
317 wxInputStream* pInput = url.GetInputStream();
318
319 CPPUNIT_ASSERT( pInput != NULL );
320
321 CPPUNIT_ASSERT( url == wxURL(wxT("http://user:password@wxwidgets.org")) );
322
323 wxURI uri(wxT("http://user:password@wxwidgets.org"));
324
325 CPPUNIT_ASSERT( url == uri );
326
327 wxURL urlcopy(uri);
328
329 CPPUNIT_ASSERT( urlcopy == url );
330 CPPUNIT_ASSERT( urlcopy == uri );
331
332 wxURI uricopy(url);
333
334 CPPUNIT_ASSERT( uricopy == url );
335 CPPUNIT_ASSERT( uricopy == urlcopy );
336 CPPUNIT_ASSERT( uricopy == uri );
337 #if WXWIN_COMPATIBILITY_2_4
338 CPPUNIT_ASSERT( wxURL::ConvertFromURI(wxT("%20%41%20")) == wxT(" A ") );
339 #endif
340 CPPUNIT_ASSERT( wxURI::Unescape(wxT("%20%41%20")) == wxT(" A ") );
341
342 wxURI test(wxT("file:\"myf\"ile.txt"));
343
344 CPPUNIT_ASSERT( test.BuildURI() == wxT("file:%22myf%22ile.txt") );
345 CPPUNIT_ASSERT( test.GetScheme() == wxT("file") );
346 CPPUNIT_ASSERT( test.GetPath() == wxT("%22myf%22ile.txt") );
347
348 // Test problem urls (reported not to work some time ago by a user...)
349 for ( size_t i = 0; i < WXSIZEOF(pszProblemUrls); ++i )
350 {
351 wxURL urlProblem(pszProblemUrls[i]);
352 CPPUNIT_ASSERT(urlProblem.GetError() == wxURL_NOERR);
353
354 wxInputStream* is = urlProblem.GetInputStream();
355 CPPUNIT_ASSERT(is != NULL);
356
357 wxFile fOut(_T("test.html"), wxFile::write);
358 wxASSERT(fOut.IsOpened());
359
360 char buf[1001];
361 for( ;; )
362 {
363 is->Read(buf, 1000);
364 size_t n = is->LastRead();
365 if ( n == 0 )
366 break;
367 buf[n] = 0;
368 fOut.Write(buf, n);
369 }
370
371 delete is;
372 }
373 }
374
375 #if wxUSE_PROTOCOL_HTTP
376 void URITestCase::URLProxy()
377 {
378 wxURL url(wxT("http://www.asite.com/index.html"));
379 url.SetProxy(wxT("pserv:3122"));
380
381 wxURL::SetDefaultProxy(wxT("fol.singnet.com.sg:8080"));
382 wxURL url2(wxT("http://server-name/path/to/file?query_data=value"));
383 wxInputStream *data = url2.GetInputStream();
384 CPPUNIT_ASSERT(data != NULL);
385 }
386 #endif // wxUSE_PROTOCOL_HTTP
387
388 #endif // TEST_URL