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