]> git.saurik.com Git - wxWidgets.git/blob - tests/uris/uris.cpp
simplify the test, don't reuse the same macro for different things
[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 uri = new wxURI(wxT(string));\
162 uri->Resolve(masteruri, strict);\
163 CPPUNIT_ASSERT(uri->BuildURI() == wxT(eq));\
164 delete uri;
165
166 #define URI_TEST_RESOLVE(string, eq) \
167 URI_TEST_RESOLVE_IMPL(string, eq, true);
168
169 #define URI_TEST_RESOLVE_LAX(string, eq) \
170 URI_TEST_RESOLVE_IMPL(string, eq, false);
171
172
173 //examples taken from RFC 2396.bis
174
175 void URITestCase::NormalResolving()
176 {
177 wxURI masteruri(wxT("http://a/b/c/d;p?q"));
178 wxURI* uri;
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(wxT("http://a/b/c/d;p?q"));
209 wxURI* uri;
210
211 //odd path examples
212 URI_TEST_RESOLVE("/./g" ,"http://a/g")
213 URI_TEST_RESOLVE("/../g" ,"http://a/g")
214 URI_TEST_RESOLVE("g." ,"http://a/b/c/g.")
215 URI_TEST_RESOLVE(".g" ,"http://a/b/c/.g")
216 URI_TEST_RESOLVE("g.." ,"http://a/b/c/g..")
217 URI_TEST_RESOLVE("..g" ,"http://a/b/c/..g")
218 }
219 //Should Fail
220 //"../../../g" = "http://a/g"
221 //"../../../../g" = "http://a/g"
222
223 void URITestCase::ReallyComplexResolving()
224 {
225 wxURI masteruri(wxT("http://a/b/c/d;p?q"));
226 wxURI* uri;
227
228 //even more odder path examples
229 URI_TEST_RESOLVE("./../g" ,"http://a/b/g")
230 URI_TEST_RESOLVE("./g/." ,"http://a/b/c/g/")
231 URI_TEST_RESOLVE("g/./h" ,"http://a/b/c/g/h")
232 URI_TEST_RESOLVE("g/../h" ,"http://a/b/c/h")
233 URI_TEST_RESOLVE("g;x=1/./y" , "http://a/b/c/g;x=1/y")
234 URI_TEST_RESOLVE("g;x=1/../y" , "http://a/b/c/y")
235 }
236
237 void URITestCase::QueryFragmentResolving()
238 {
239 wxURI masteruri(wxT("http://a/b/c/d;p?q"));
240 wxURI* uri;
241
242 //query/fragment ambigiousness
243 URI_TEST_RESOLVE("g?y/./x","http://a/b/c/g?y/./x")
244 URI_TEST_RESOLVE("g?y/../x" , "http://a/b/c/g?y/../x")
245 URI_TEST_RESOLVE("g#s/./x","http://a/b/c/g#s/./x")
246 URI_TEST_RESOLVE("g#s/../x" , "http://a/b/c/g#s/../x")
247 }
248
249 void URITestCase::BackwardsResolving()
250 {
251 wxURI masteruri(wxT("http://a/b/c/d;p?q"));
252 wxURI* uri;
253
254 //"NEW"
255 URI_TEST_RESOLVE("http:g" , "http:g") //strict
256 //bw compat
257 URI_TEST_RESOLVE_LAX("http:g", "http://a/b/c/g");
258 }
259
260 void URITestCase::Assignment()
261 {
262 wxURI uri1(wxT("http://mysite.com")),
263 uri2(wxT("http://mysite2.com"));
264
265 uri2 = uri1;
266
267 CPPUNIT_ASSERT(uri1.BuildURI() == uri2.BuildURI());
268 }
269
270 void URITestCase::Comparison()
271 {
272 CPPUNIT_ASSERT(wxURI(wxT("http://mysite.com")) == wxURI(wxT("http://mysite.com")));
273 }
274
275 void URITestCase::Unescaping()
276 {
277 wxString orig = wxT("http://test.com/of/file%3A%2F%2FC%3A%5Curi%5C")
278 wxT("escaping%5Cthat%5Cseems%5Cbroken%5Csadly%5B1%5D.rss");
279
280 wxString works= wxURI(orig).BuildUnescapedURI();
281
282 CPPUNIT_ASSERT(orig.IsSameAs(works) == false);
283
284 wxString orig2 = wxT("http://test.com/of/file%3A%2F%")
285 wxT("2FC%3A%5Curi%5Cescaping%5Cthat%5Cseems%")
286 wxT("5Cbroken%5Csadly%5B1%5D.rss");
287
288 wxString works2 = wxURI::Unescape(orig2);
289 wxString broken2 = wxURI(orig2).BuildUnescapedURI();
290
291 CPPUNIT_ASSERT(works2.IsSameAs(broken2));
292
293 }
294
295 void URITestCase::FileScheme()
296 {
297 //file:// variety (NOT CONFORMANT TO THE RFC)
298 CPPUNIT_ASSERT(wxURI(wxString(wxT("file://e:/wxcode/script1.xml"))).GetPath()
299 == wxT("e:/wxcode/script1.xml") );
300
301 //file:/// variety
302 CPPUNIT_ASSERT(wxURI(wxString(wxT("file:///e:/wxcode/script1.xml"))).GetPath()
303 == wxT("/e:/wxcode/script1.xml") );
304
305 //file:/ variety
306 CPPUNIT_ASSERT(wxURI(wxString(wxT("file:/e:/wxcode/script1.xml"))).GetPath()
307 == wxT("/e:/wxcode/script1.xml") );
308
309 //file: variety
310 CPPUNIT_ASSERT(wxURI(wxString(wxT("file:e:/wxcode/script1.xml"))).GetPath()
311 == wxT("e:/wxcode/script1.xml") );
312 }
313
314 #if TEST_URL
315
316 const wxChar* pszProblemUrls[] = { wxT("http://www.csdn.net"),
317 wxT("http://www.163.com"),
318 wxT("http://www.sina.com.cn") };
319
320 #include "wx/url.h"
321 #include "wx/file.h"
322
323 void URITestCase::URLCompat()
324 {
325 wxURL url(wxT("http://user:password@wxwidgets.org"));
326
327 CPPUNIT_ASSERT(url.GetError() == wxURL_NOERR);
328
329 #if TEST_NETWORK
330 wxInputStream* pInput = url.GetInputStream();
331
332 CPPUNIT_ASSERT( pInput != NULL );
333 #endif
334
335 CPPUNIT_ASSERT( url == wxURL(wxT("http://user:password@wxwidgets.org")) );
336
337 wxURI uri(wxT("http://user:password@wxwidgets.org"));
338
339 CPPUNIT_ASSERT( url == uri );
340
341 wxURL urlcopy(uri);
342
343 CPPUNIT_ASSERT( urlcopy == url );
344 CPPUNIT_ASSERT( urlcopy == uri );
345
346 wxURI uricopy(url);
347
348 CPPUNIT_ASSERT( uricopy == url );
349 CPPUNIT_ASSERT( uricopy == urlcopy );
350 CPPUNIT_ASSERT( uricopy == uri );
351 CPPUNIT_ASSERT( wxURI::Unescape(wxT("%20%41%20")) == wxT(" A ") );
352
353 wxURI test(wxT("file:\"myf\"ile.txt"));
354
355 CPPUNIT_ASSERT( test.BuildURI() == wxT("file:%22myf%22ile.txt") );
356 CPPUNIT_ASSERT( test.GetScheme() == wxT("file") );
357 CPPUNIT_ASSERT( test.GetPath() == wxT("%22myf%22ile.txt") );
358
359 // these could be put under a named registry since they take some
360 // time to complete
361 #if 0
362 // Test problem urls (reported not to work some time ago by a user...)
363 for ( size_t i = 0; i < WXSIZEOF(pszProblemUrls); ++i )
364 {
365 wxURL urlProblem(pszProblemUrls[i]);
366 CPPUNIT_ASSERT(urlProblem.GetError() == wxURL_NOERR);
367
368 wxInputStream* is = urlProblem.GetInputStream();
369 CPPUNIT_ASSERT(is != NULL);
370
371 wxFile fOut(_T("test.html"), wxFile::write);
372 wxASSERT(fOut.IsOpened());
373
374 char buf[1001];
375 for( ;; )
376 {
377 is->Read(buf, 1000);
378 size_t n = is->LastRead();
379 if ( n == 0 )
380 break;
381 buf[n] = 0;
382 fOut.Write(buf, n);
383 }
384
385 delete is;
386 }
387 #endif
388 }
389
390 // the purpose of this test is unclear, it seems to be unfinished so disabling
391 // it for now
392 #if 0 && wxUSE_PROTOCOL_HTTP
393 void URITestCase::URLProxy()
394 {
395 wxURL url(wxT("http://www.asite.com/index.html"));
396 url.SetProxy(wxT("pserv:3122"));
397
398 wxURL::SetDefaultProxy(wxT("fol.singnet.com.sg:8080"));
399 wxURL url2(wxT("http://server-name/path/to/file?query_data=value"));
400 wxInputStream *data = url2.GetInputStream();
401 CPPUNIT_ASSERT(data != NULL);
402 }
403 #endif // wxUSE_PROTOCOL_HTTP
404
405 #endif // TEST_URL