]> git.saurik.com Git - wxWidgets.git/blob - tests/uris/uris.cpp
Compilation fixes for mingw-w64.
[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
100 #define URI_TEST(uristring, cond) \
101 uri = new wxURI(wxT(uristring));\
102 CPPUNIT_ASSERT(cond);\
103 delete uri;
104
105 #define URI_PRINT(uri)\
106 wxPrintf(wxT("SCHEME:%s\n"), uri.GetScheme());\
107 wxPrintf(wxT("USER:%s\n"), uri.GetUser());\
108 wxPrintf(wxT("SERVER:%s\n"), uri.GetServer());\
109 wxPrintf(wxT("PORT:%s\n"), uri.GetPort());\
110 wxPrintf(wxT("PATH:%s\n"), uri.GetPath());\
111 wxPrintf(wxT("QUERY:%s\n"), uri.GetQuery());\
112 wxPrintf(wxT("FRAGMENT:%s\n"), uri.GetFragment());
113
114 void URITestCase::IPv4()
115 {
116 wxURI* uri;
117
118
119 URI_TEST("http://user:password@192.168.1.100:5050/path",
120 uri->GetHostType() == wxURI_IPV4ADDRESS);
121
122 URI_TEST("http://user:password@192.255.1.100:5050/path",
123 uri->GetHostType() == wxURI_IPV4ADDRESS);
124
125 //bogus ipv4
126 URI_TEST("http://user:password@192.256.1.100:5050/path",
127 uri->GetHostType() != wxURI_IPV4ADDRESS);
128 }
129
130 void URITestCase::IPv6()
131 {
132 wxURI* uri;
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_TEST("http://user:password@[aa:aa:aa:aa:aa:aa:192.168.1.100]:5050/path",
146 uri->GetHostType() == wxURI_IPV6ADDRESS);
147
148 URI_TEST("http://user:password@[aa:aa:aa:aa:aa:aa:aa:aa]:5050/path",
149 uri->GetHostType() == wxURI_IPV6ADDRESS);
150
151 URI_TEST("http://user:password@[aa:aa:aa:aa::192.168.1.100]:5050/path",
152 uri->GetHostType() == wxURI_IPV6ADDRESS);
153
154 URI_TEST("http://user:password@[aa:aa:aa:aa::aa:aa]:5050/path",
155 uri->GetHostType() == wxURI_IPV6ADDRESS);
156 }
157
158 void URITestCase::Paths()
159 {
160 wxURI* uri;
161
162 //path tests
163 URI_TEST("http://user:password@192.256.1.100:5050/../path",
164 uri->GetPath() == wxT("/path"));
165
166 URI_TEST("http://user:password@192.256.1.100:5050/path/../",
167 uri->GetPath() == wxT("/"));
168
169 URI_TEST("http://user:password@192.256.1.100:5050/path/.",
170 uri->GetPath() == wxT("/path/"));
171
172 URI_TEST("http://user:password@192.256.1.100:5050/path/./",
173 uri->GetPath() == wxT("/path/"));
174
175 URI_TEST("path/john/../../../joe",
176 uri->BuildURI() == wxT("../joe"));
177 }
178 #undef URI_TEST
179
180 #define URI_TEST_RESOLVE(string, eq, strict) \
181 uri = new wxURI(wxT(string));\
182 uri->Resolve(masteruri, strict);\
183 CPPUNIT_ASSERT(uri->BuildURI() == wxT(eq));\
184 delete uri;
185
186 #define URI_TEST(string, eq) \
187 URI_TEST_RESOLVE(string, eq, true);
188
189
190 //examples taken from RFC 2396.bis
191
192 void URITestCase::NormalResolving()
193 {
194 wxURI masteruri(wxT("http://a/b/c/d;p?q"));
195 wxURI* uri;
196
197 URI_TEST("g:h" ,"g:h")
198 URI_TEST("g" ,"http://a/b/c/g")
199 URI_TEST("./g" ,"http://a/b/c/g")
200 URI_TEST("g/" ,"http://a/b/c/g/")
201 URI_TEST("/g" ,"http://a/g")
202 URI_TEST("//g" ,"http://g")
203 URI_TEST("?y" ,"http://a/b/c/d;p?y")
204 URI_TEST("g?y" ,"http://a/b/c/g?y")
205 URI_TEST("#s" ,"http://a/b/c/d;p?q#s")
206 URI_TEST("g#s" ,"http://a/b/c/g#s")
207 URI_TEST("g?y#s","http://a/b/c/g?y#s")
208 URI_TEST(";x" ,"http://a/b/c/;x")
209 URI_TEST("g;x" ,"http://a/b/c/g;x")
210 URI_TEST("g;x?y#s","http://a/b/c/g;x?y#s")
211
212 URI_TEST("" ,"http://a/b/c/d;p?q")
213 URI_TEST("." ,"http://a/b/c/")
214 URI_TEST("./" ,"http://a/b/c/")
215 URI_TEST(".." ,"http://a/b/")
216 URI_TEST("../" ,"http://a/b/")
217 URI_TEST("../g" ,"http://a/b/g")
218 URI_TEST("../..","http://a/")
219 URI_TEST("../../" , "http://a/")
220 URI_TEST("../../g" , "http://a/g")
221 }
222
223 void URITestCase::ComplexResolving()
224 {
225 wxURI masteruri(wxT("http://a/b/c/d;p?q"));
226 wxURI* uri;
227
228 //odd path examples
229 URI_TEST("/./g" ,"http://a/g")
230 URI_TEST("/../g" ,"http://a/g")
231 URI_TEST("g." ,"http://a/b/c/g.")
232 URI_TEST(".g" ,"http://a/b/c/.g")
233 URI_TEST("g.." ,"http://a/b/c/g..")
234 URI_TEST("..g" ,"http://a/b/c/..g")
235 }
236 //Should Fail
237 //"../../../g" = "http://a/g"
238 //"../../../../g" = "http://a/g"
239
240 void URITestCase::ReallyComplexResolving()
241 {
242 wxURI masteruri(wxT("http://a/b/c/d;p?q"));
243 wxURI* uri;
244
245 //even more odder path examples
246 URI_TEST("./../g" ,"http://a/b/g")
247 URI_TEST("./g/." ,"http://a/b/c/g/")
248 URI_TEST("g/./h" ,"http://a/b/c/g/h")
249 URI_TEST("g/../h" ,"http://a/b/c/h")
250 URI_TEST("g;x=1/./y" , "http://a/b/c/g;x=1/y")
251 URI_TEST("g;x=1/../y" , "http://a/b/c/y")
252 }
253
254 void URITestCase::QueryFragmentResolving()
255 {
256 wxURI masteruri(wxT("http://a/b/c/d;p?q"));
257 wxURI* uri;
258
259 //query/fragment ambigiousness
260 URI_TEST("g?y/./x","http://a/b/c/g?y/./x")
261 URI_TEST("g?y/../x" , "http://a/b/c/g?y/../x")
262 URI_TEST("g#s/./x","http://a/b/c/g#s/./x")
263 URI_TEST("g#s/../x" , "http://a/b/c/g#s/../x")
264 }
265
266 void URITestCase::BackwardsResolving()
267 {
268 wxURI masteruri(wxT("http://a/b/c/d;p?q"));
269 wxURI* uri;
270
271 //"NEW"
272 URI_TEST("http:g" , "http:g") //strict
273 //bw compat
274 URI_TEST_RESOLVE("http:g", "http://a/b/c/g", false);
275 }
276
277 void URITestCase::Assignment()
278 {
279 wxURI uri1(wxT("http://mysite.com")),
280 uri2(wxT("http://mysite2.com"));
281
282 uri2 = uri1;
283
284 CPPUNIT_ASSERT(uri1.BuildURI() == uri2.BuildURI());
285 }
286
287 void URITestCase::Comparison()
288 {
289 CPPUNIT_ASSERT(wxURI(wxT("http://mysite.com")) == wxURI(wxT("http://mysite.com")));
290 }
291
292 void URITestCase::Unescaping()
293 {
294 wxString orig = wxT("http://test.com/of/file%3A%2F%2FC%3A%5Curi%5C")
295 wxT("escaping%5Cthat%5Cseems%5Cbroken%5Csadly%5B1%5D.rss");
296
297 wxString works= wxURI(orig).BuildUnescapedURI();
298
299 CPPUNIT_ASSERT(orig.IsSameAs(works) == false);
300
301 wxString orig2 = wxT("http://test.com/of/file%3A%2F%")
302 wxT("2FC%3A%5Curi%5Cescaping%5Cthat%5Cseems%")
303 wxT("5Cbroken%5Csadly%5B1%5D.rss");
304
305 wxString works2 = wxURI::Unescape(orig2);
306 wxString broken2 = wxURI(orig2).BuildUnescapedURI();
307
308 CPPUNIT_ASSERT(works2.IsSameAs(broken2));
309
310 }
311
312 void URITestCase::FileScheme()
313 {
314 //file:// variety (NOT CONFORMANT TO THE RFC)
315 CPPUNIT_ASSERT(wxURI(wxString(wxT("file://e:/wxcode/script1.xml"))).GetPath()
316 == wxT("e:/wxcode/script1.xml") );
317
318 //file:/// variety
319 CPPUNIT_ASSERT(wxURI(wxString(wxT("file:///e:/wxcode/script1.xml"))).GetPath()
320 == wxT("/e:/wxcode/script1.xml") );
321
322 //file:/ variety
323 CPPUNIT_ASSERT(wxURI(wxString(wxT("file:/e:/wxcode/script1.xml"))).GetPath()
324 == wxT("/e:/wxcode/script1.xml") );
325
326 //file: variety
327 CPPUNIT_ASSERT(wxURI(wxString(wxT("file:e:/wxcode/script1.xml"))).GetPath()
328 == wxT("e:/wxcode/script1.xml") );
329 }
330
331 #if TEST_URL
332
333 const wxChar* pszProblemUrls[] = { wxT("http://www.csdn.net"),
334 wxT("http://www.163.com"),
335 wxT("http://www.sina.com.cn") };
336
337 #include "wx/url.h"
338 #include "wx/file.h"
339
340 void URITestCase::URLCompat()
341 {
342 wxURL url(wxT("http://user:password@wxwidgets.org"));
343
344 CPPUNIT_ASSERT(url.GetError() == wxURL_NOERR);
345
346 #if TEST_NETWORK
347 wxInputStream* pInput = url.GetInputStream();
348
349 CPPUNIT_ASSERT( pInput != NULL );
350 #endif
351
352 CPPUNIT_ASSERT( url == wxURL(wxT("http://user:password@wxwidgets.org")) );
353
354 wxURI uri(wxT("http://user:password@wxwidgets.org"));
355
356 CPPUNIT_ASSERT( url == uri );
357
358 wxURL urlcopy(uri);
359
360 CPPUNIT_ASSERT( urlcopy == url );
361 CPPUNIT_ASSERT( urlcopy == uri );
362
363 wxURI uricopy(url);
364
365 CPPUNIT_ASSERT( uricopy == url );
366 CPPUNIT_ASSERT( uricopy == urlcopy );
367 CPPUNIT_ASSERT( uricopy == uri );
368 CPPUNIT_ASSERT( wxURI::Unescape(wxT("%20%41%20")) == wxT(" A ") );
369
370 wxURI test(wxT("file:\"myf\"ile.txt"));
371
372 CPPUNIT_ASSERT( test.BuildURI() == wxT("file:%22myf%22ile.txt") );
373 CPPUNIT_ASSERT( test.GetScheme() == wxT("file") );
374 CPPUNIT_ASSERT( test.GetPath() == wxT("%22myf%22ile.txt") );
375
376 // these could be put under a named registry since they take some
377 // time to complete
378 #if 0
379 // Test problem urls (reported not to work some time ago by a user...)
380 for ( size_t i = 0; i < WXSIZEOF(pszProblemUrls); ++i )
381 {
382 wxURL urlProblem(pszProblemUrls[i]);
383 CPPUNIT_ASSERT(urlProblem.GetError() == wxURL_NOERR);
384
385 wxInputStream* is = urlProblem.GetInputStream();
386 CPPUNIT_ASSERT(is != NULL);
387
388 wxFile fOut(_T("test.html"), wxFile::write);
389 wxASSERT(fOut.IsOpened());
390
391 char buf[1001];
392 for( ;; )
393 {
394 is->Read(buf, 1000);
395 size_t n = is->LastRead();
396 if ( n == 0 )
397 break;
398 buf[n] = 0;
399 fOut.Write(buf, n);
400 }
401
402 delete is;
403 }
404 #endif
405 }
406
407 // the purpose of this test is unclear, it seems to be unfinished so disabling
408 // it for now
409 #if 0 && wxUSE_PROTOCOL_HTTP
410 void URITestCase::URLProxy()
411 {
412 wxURL url(wxT("http://www.asite.com/index.html"));
413 url.SetProxy(wxT("pserv:3122"));
414
415 wxURL::SetDefaultProxy(wxT("fol.singnet.com.sg:8080"));
416 wxURL url2(wxT("http://server-name/path/to/file?query_data=value"));
417 wxInputStream *data = url2.GetInputStream();
418 CPPUNIT_ASSERT(data != NULL);
419 }
420 #endif // wxUSE_PROTOCOL_HTTP
421
422 #endif // TEST_URL