]> git.saurik.com Git - wxWidgets.git/blob - tests/uris/uris.cpp
changed wxMSW's default toolbar bitmaps size from obsolete 16x15 to 24x24 used by...
[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 wxUSE_URL
29
30 // Define this as 1 to test network connections, this is disabled by default as
31 // some machines running automatic builds don't allow outgoing connections and
32 // so the tests fail
33 #define TEST_NETWORK 0
34
35 // ----------------------------------------------------------------------------
36 // test class
37 // ----------------------------------------------------------------------------
38
39 class URITestCase : public CppUnit::TestCase
40 {
41 public:
42 URITestCase();
43
44 private:
45 CPPUNIT_TEST_SUITE( URITestCase );
46 CPPUNIT_TEST( IPv4 );
47 CPPUNIT_TEST( IPv6 );
48 CPPUNIT_TEST( Server );
49 CPPUNIT_TEST( Paths );
50 CPPUNIT_TEST( UserAndPass );
51 CPPUNIT_TEST( NormalResolving );
52 CPPUNIT_TEST( ComplexResolving );
53 CPPUNIT_TEST( ReallyComplexResolving );
54 CPPUNIT_TEST( QueryFragmentResolving );
55 CPPUNIT_TEST( BackwardsResolving );
56 CPPUNIT_TEST( Assignment );
57 CPPUNIT_TEST( Comparison );
58 CPPUNIT_TEST( Unescaping );
59 CPPUNIT_TEST( FileScheme );
60 #if TEST_URL
61 CPPUNIT_TEST( URLCompat );
62 #if 0 && wxUSE_PROTOCOL_HTTP
63 CPPUNIT_TEST( URLProxy );
64 #endif
65 #endif
66 CPPUNIT_TEST_SUITE_END();
67
68 void IPv4();
69 void IPv6();
70 void Server();
71 void Paths();
72 void UserAndPass();
73 void NormalResolving();
74 void ComplexResolving();
75 void ReallyComplexResolving();
76 void QueryFragmentResolving();
77 void BackwardsResolving();
78 void Assignment();
79 void Comparison();
80 void Unescaping();
81 void FileScheme();
82
83 #if TEST_URL
84 void URLCompat();
85 #if 0 && wxUSE_PROTOCOL_HTTP
86 void URLProxy();
87 #endif
88 #endif
89
90 DECLARE_NO_COPY_CLASS(URITestCase)
91 };
92
93 // register in the unnamed registry so that these tests are run by default
94 CPPUNIT_TEST_SUITE_REGISTRATION( URITestCase );
95
96 // also include in it's own registry so that these tests can be run alone
97 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( URITestCase, "URITestCase" );
98
99 URITestCase::URITestCase()
100 {
101 }
102
103 // apply the given accessor to the URI, check that the result is as expected
104 #define URI_ASSERT_PART_EQUAL(uri, expected, accessor) \
105 CPPUNIT_ASSERT_EQUAL(expected, wxURI(uri).accessor)
106
107 #define URI_ASSERT_HOSTTYPE_EQUAL(uri, expected) \
108 URI_ASSERT_PART_EQUAL((uri), (expected), GetHostType())
109
110 #define URI_ASSERT_SERVER_EQUAL(uri, expected) \
111 URI_ASSERT_PART_EQUAL((uri), (expected), GetServer())
112
113 #define URI_ASSERT_PATH_EQUAL(uri, expected) \
114 URI_ASSERT_PART_EQUAL((uri), (expected), GetPath())
115
116 #define URI_ASSERT_USER_EQUAL(uri, expected) \
117 URI_ASSERT_PART_EQUAL((uri), (expected), GetUser())
118
119 void URITestCase::IPv4()
120 {
121 URI_ASSERT_HOSTTYPE_EQUAL("http://user:password@192.168.1.100:5050/path",
122 wxURI_IPV4ADDRESS);
123
124 URI_ASSERT_HOSTTYPE_EQUAL("http://user:password@192.255.1.100:5050/path",
125 wxURI_IPV4ADDRESS);
126
127 // bogus ipv4
128 CPPUNIT_ASSERT( wxURI("http://user:password@192.256.1.100:5050/path").
129 GetHostType() != wxURI_IPV4ADDRESS);
130 }
131
132 void URITestCase::IPv6()
133 {
134 // IPv6address = 6( h16 ":" ) ls32
135 // / "::" 5( h16 ":" ) ls32
136 // / [ h16 ] "::" 4( h16 ":" ) ls32
137 // / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
138 // / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
139 // / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
140 // / [ *4( h16 ":" ) h16 ] "::" ls32
141 // / [ *5( h16 ":" ) h16 ] "::" h16
142 // / [ *6( h16 ":" ) h16 ] "::"
143 // ls32 = ( h16 ":" h16 ) / IPv4address
144
145 URI_ASSERT_HOSTTYPE_EQUAL
146 (
147 "http://user:password@[aa:aa:aa:aa:aa:aa:192.168.1.100]:5050/path",
148 wxURI_IPV6ADDRESS
149 );
150
151 URI_ASSERT_HOSTTYPE_EQUAL
152 (
153 "http://user:password@[aa:aa:aa:aa:aa:aa:aa:aa]:5050/path",
154 wxURI_IPV6ADDRESS
155 );
156
157 URI_ASSERT_HOSTTYPE_EQUAL
158 (
159 "http://user:password@[aa:aa:aa:aa::192.168.1.100]:5050/path",
160 wxURI_IPV6ADDRESS
161 );
162
163 URI_ASSERT_HOSTTYPE_EQUAL
164 (
165 "http://user:password@[aa:aa:aa:aa::aa:aa]:5050/path",
166 wxURI_IPV6ADDRESS
167 );
168 }
169
170 void URITestCase::Server()
171 {
172 URI_ASSERT_SERVER_EQUAL("http://foo/", "foo");
173 URI_ASSERT_SERVER_EQUAL("http://foo-bar/", "foo-bar");
174 URI_ASSERT_SERVER_EQUAL("http://foo/bar/", "foo");
175 URI_ASSERT_SERVER_EQUAL("http://192.168.1.0/", "192.168.1.0");
176 URI_ASSERT_SERVER_EQUAL("http://192.168.1.17/", "192.168.1.17");
177 URI_ASSERT_SERVER_EQUAL("http://192.168.1.255/", "192.168.1.255");
178 URI_ASSERT_SERVER_EQUAL("http://192.168.1.1/index.html", "192.168.1.1");
179 URI_ASSERT_SERVER_EQUAL("http://[aa:aa:aa:aa::aa:aa]/foo", "aa:aa:aa:aa::aa:aa");
180 }
181
182 void URITestCase::Paths()
183 {
184 URI_ASSERT_PATH_EQUAL("http://user:password@192.256.1.100:5050/../path",
185 "/path");
186
187 URI_ASSERT_PATH_EQUAL("http://user:password@192.256.1.100:5050/path/../",
188 "/");
189
190 URI_ASSERT_PATH_EQUAL("http://user:password@192.256.1.100:5050/path/.",
191 "/path/");
192
193 URI_ASSERT_PATH_EQUAL("http://user:password@192.256.1.100:5050/path/./",
194 "/path/");
195
196 URI_ASSERT_PART_EQUAL("path/john/../../../joe",
197 "../joe", BuildURI());
198 }
199
200 void URITestCase::UserAndPass()
201 {
202 URI_ASSERT_USER_EQUAL("http://user:pass@host/path/", "user");
203 URI_ASSERT_USER_EQUAL("http://user@host/path/", "user");
204 URI_ASSERT_USER_EQUAL("http://host/path/", "");
205 }
206
207 #define URI_TEST_RESOLVE_IMPL(string, eq, strict) \
208 { \
209 wxURI uri(string); \
210 uri.Resolve(masteruri, strict); \
211 CPPUNIT_ASSERT_EQUAL(eq, uri.BuildURI()); \
212 }
213
214 #define URI_TEST_RESOLVE(string, eq) \
215 URI_TEST_RESOLVE_IMPL(string, eq, true);
216
217 #define URI_TEST_RESOLVE_LAX(string, eq) \
218 URI_TEST_RESOLVE_IMPL(string, eq, false);
219
220
221 //examples taken from RFC 2396.bis
222
223 void URITestCase::NormalResolving()
224 {
225 wxURI masteruri("http://a/b/c/d;p?q");
226
227 URI_TEST_RESOLVE("g:h" ,"g:h")
228 URI_TEST_RESOLVE("g" ,"http://a/b/c/g")
229 URI_TEST_RESOLVE("./g" ,"http://a/b/c/g")
230 URI_TEST_RESOLVE("g/" ,"http://a/b/c/g/")
231 URI_TEST_RESOLVE("/g" ,"http://a/g")
232 URI_TEST_RESOLVE("//g" ,"http://g")
233 URI_TEST_RESOLVE("?y" ,"http://a/b/c/d;p?y")
234 URI_TEST_RESOLVE("g?y" ,"http://a/b/c/g?y")
235 URI_TEST_RESOLVE("#s" ,"http://a/b/c/d;p?q#s")
236 URI_TEST_RESOLVE("g#s" ,"http://a/b/c/g#s")
237 URI_TEST_RESOLVE("g?y#s","http://a/b/c/g?y#s")
238 URI_TEST_RESOLVE(";x" ,"http://a/b/c/;x")
239 URI_TEST_RESOLVE("g;x" ,"http://a/b/c/g;x")
240 URI_TEST_RESOLVE("g;x?y#s","http://a/b/c/g;x?y#s")
241
242 URI_TEST_RESOLVE("" ,"http://a/b/c/d;p?q")
243 URI_TEST_RESOLVE("." ,"http://a/b/c/")
244 URI_TEST_RESOLVE("./" ,"http://a/b/c/")
245 URI_TEST_RESOLVE(".." ,"http://a/b/")
246 URI_TEST_RESOLVE("../" ,"http://a/b/")
247 URI_TEST_RESOLVE("../g" ,"http://a/b/g")
248 URI_TEST_RESOLVE("../..","http://a/")
249 URI_TEST_RESOLVE("../../" , "http://a/")
250 URI_TEST_RESOLVE("../../g" , "http://a/g")
251 }
252
253 void URITestCase::ComplexResolving()
254 {
255 wxURI masteruri("http://a/b/c/d;p?q");
256
257 //odd path examples
258 URI_TEST_RESOLVE("../../../g" , "http://a/g")
259 URI_TEST_RESOLVE("../../../../g", "http://a/g")
260
261 URI_TEST_RESOLVE("/./g" ,"http://a/g")
262 URI_TEST_RESOLVE("/../g" ,"http://a/g")
263 URI_TEST_RESOLVE("g." ,"http://a/b/c/g.")
264 URI_TEST_RESOLVE(".g" ,"http://a/b/c/.g")
265 URI_TEST_RESOLVE("g.." ,"http://a/b/c/g..")
266 URI_TEST_RESOLVE("..g" ,"http://a/b/c/..g")
267 }
268
269 void URITestCase::ReallyComplexResolving()
270 {
271 wxURI masteruri("http://a/b/c/d;p?q");
272
273 //even more odder path examples
274 URI_TEST_RESOLVE("./../g" ,"http://a/b/g")
275 URI_TEST_RESOLVE("./g/." ,"http://a/b/c/g/")
276 URI_TEST_RESOLVE("g/./h" ,"http://a/b/c/g/h")
277 URI_TEST_RESOLVE("g/../h" ,"http://a/b/c/h")
278 URI_TEST_RESOLVE("g;x=1/./y" , "http://a/b/c/g;x=1/y")
279 URI_TEST_RESOLVE("g;x=1/../y" , "http://a/b/c/y")
280 }
281
282 void URITestCase::QueryFragmentResolving()
283 {
284 wxURI masteruri("http://a/b/c/d;p?q");
285
286 //query/fragment ambigiousness
287 URI_TEST_RESOLVE("g?y/./x","http://a/b/c/g?y/./x")
288 URI_TEST_RESOLVE("g?y/../x" , "http://a/b/c/g?y/../x")
289 URI_TEST_RESOLVE("g#s/./x","http://a/b/c/g#s/./x")
290 URI_TEST_RESOLVE("g#s/../x" , "http://a/b/c/g#s/../x")
291 }
292
293 void URITestCase::BackwardsResolving()
294 {
295 wxURI masteruri("http://a/b/c/d;p?q");
296
297 //"NEW"
298 URI_TEST_RESOLVE("http:g" , "http:g") //strict
299 //bw compat
300 URI_TEST_RESOLVE_LAX("http:g", "http://a/b/c/g");
301 }
302
303 void URITestCase::Assignment()
304 {
305 wxURI uri1("http://mysite.com"),
306 uri2("http://mysite2.com");
307
308 uri2 = uri1;
309
310 CPPUNIT_ASSERT_EQUAL(uri1.BuildURI(), uri2.BuildURI());
311 }
312
313 void URITestCase::Comparison()
314 {
315 CPPUNIT_ASSERT(wxURI("http://mysite.com") == wxURI("http://mysite.com"));
316 }
317
318 void URITestCase::Unescaping()
319 {
320 wxString escaped,
321 unescaped;
322
323 escaped = "http://test.com/of/file%3A%2F%2FC%3A%5Curi%5C"
324 "escaping%5Cthat%5Cseems%5Cbroken%5Csadly%5B1%5D.rss";
325
326 unescaped = wxURI(escaped).BuildUnescapedURI();
327
328 CPPUNIT_ASSERT_EQUAL( "http://test.com/of/file://C:\\uri\\"
329 "escaping\\that\\seems\\broken\\sadly[1].rss",
330 unescaped );
331
332 CPPUNIT_ASSERT_EQUAL( unescaped, wxURI::Unescape(escaped) );
333
334
335 escaped = "http://ru.wikipedia.org/wiki/"
336 "%D0%A6%D0%B5%D0%BB%D0%BE%D0%B5_%D1%87%D0%B8%D1%81%D0%BB%D0%BE";
337
338 unescaped = wxURI::Unescape(escaped);
339
340 CPPUNIT_ASSERT_EQUAL( wxString::FromUTF8(
341 "http://ru.wikipedia.org/wiki/"
342 "\xD0\xA6\xD0\xB5\xD0\xBB\xD0\xBE\xD0\xB5_"
343 "\xD1\x87\xD0\xB8\xD1\x81\xD0\xBB\xD0\xBE"
344 ),
345 unescaped );
346 }
347
348 void URITestCase::FileScheme()
349 {
350 //file:// variety (NOT CONFORMING TO THE RFC)
351 URI_ASSERT_PATH_EQUAL( "file://e:/wxcode/script1.xml",
352 "e:/wxcode/script1.xml" );
353
354 //file:/// variety
355 URI_ASSERT_PATH_EQUAL( "file:///e:/wxcode/script1.xml",
356 "/e:/wxcode/script1.xml" );
357
358 //file:/ variety
359 URI_ASSERT_PATH_EQUAL( "file:/e:/wxcode/script1.xml",
360 "/e:/wxcode/script1.xml" );
361
362 //file: variety
363 URI_ASSERT_PATH_EQUAL( "file:e:/wxcode/script1.xml",
364 "e:/wxcode/script1.xml" );
365 }
366
367 #if TEST_URL
368
369 #include "wx/url.h"
370 #include "wx/file.h"
371
372 void URITestCase::URLCompat()
373 {
374 wxURL url("http://user:password@wxwidgets.org");
375
376 CPPUNIT_ASSERT(url.GetError() == wxURL_NOERR);
377
378 #if TEST_NETWORK
379 wxInputStream* pInput = url.GetInputStream();
380
381 CPPUNIT_ASSERT( pInput != NULL );
382 #endif
383
384 CPPUNIT_ASSERT( url == wxURL("http://user:password@wxwidgets.org") );
385
386 wxURI uri("http://user:password@wxwidgets.org");
387
388 CPPUNIT_ASSERT( url == uri );
389
390 wxURL urlcopy(uri);
391
392 CPPUNIT_ASSERT( urlcopy == url );
393 CPPUNIT_ASSERT( urlcopy == uri );
394
395 wxURI uricopy(url);
396
397 CPPUNIT_ASSERT( uricopy == url );
398 CPPUNIT_ASSERT( uricopy == urlcopy );
399 CPPUNIT_ASSERT( uricopy == uri );
400 CPPUNIT_ASSERT_EQUAL( " A ", wxURI::Unescape("%20%41%20") );
401
402 wxURI test("file:\"myf\"ile.txt");
403
404 CPPUNIT_ASSERT_EQUAL( "file:%22myf%22ile.txt" , test.BuildURI() );
405 CPPUNIT_ASSERT_EQUAL( "file", test.GetScheme() );
406 CPPUNIT_ASSERT_EQUAL( "%22myf%22ile.txt", test.GetPath() );
407
408 // these could be put under a named registry since they take some
409 // time to complete
410 #if 0
411 // Test problem urls (reported not to work some time ago by a user...)
412 const wxChar* pszProblemUrls[] = { "http://www.csdn.net",
413 "http://www.163.com",
414 "http://www.sina.com.cn" };
415
416 for ( size_t i = 0; i < WXSIZEOF(pszProblemUrls); ++i )
417 {
418 wxURL urlProblem(pszProblemUrls[i]);
419 CPPUNIT_ASSERT(urlProblem.GetError() == wxURL_NOERR);
420
421 wxInputStream* is = urlProblem.GetInputStream();
422 CPPUNIT_ASSERT(is != NULL);
423
424 wxFile fOut(_T("test.html"), wxFile::write);
425 wxASSERT(fOut.IsOpened());
426
427 char buf[1001];
428 for( ;; )
429 {
430 is->Read(buf, 1000);
431 size_t n = is->LastRead();
432 if ( n == 0 )
433 break;
434 buf[n] = 0;
435 fOut.Write(buf, n);
436 }
437
438 delete is;
439 }
440 #endif
441 }
442
443 // the purpose of this test is unclear, it seems to be unfinished so disabling
444 // it for now
445 #if 0 && wxUSE_PROTOCOL_HTTP
446 void URITestCase::URLProxy()
447 {
448 wxURL url(wxT("http://www.asite.com/index.html"));
449 url.SetProxy(wxT("pserv:3122"));
450
451 wxURL::SetDefaultProxy(wxT("fol.singnet.com.sg:8080"));
452 wxURL url2(wxT("http://server-name/path/to/file?query_data=value"));
453 wxInputStream *data = url2.GetInputStream();
454 CPPUNIT_ASSERT(data != NULL);
455 }
456 #endif // wxUSE_PROTOCOL_HTTP
457
458 #endif // TEST_URL