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