]> git.saurik.com Git - wxWidgets.git/blame - tests/uris/uris.cpp
already escaped backslash shouldn't escape the following quote in ConvertStringToArgs()
[wxWidgets.git] / tests / uris / uris.cpp
CommitLineData
dd65d8c8
RN
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
8899b155 14#include "testprec.h"
dd65d8c8
RN
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"
2b9afe40 25#include "wx/url.h"
dd65d8c8 26
b60b2ec8 27// Test wxURL & wxURI compat?
9fc7a1d2
VZ
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
b60b2ec8 34
dd65d8c8
RN
35// ----------------------------------------------------------------------------
36// test class
37// ----------------------------------------------------------------------------
38
39class URITestCase : public CppUnit::TestCase
40{
41public:
42 URITestCase();
43
44private:
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 );
24ca04e7 56 CPPUNIT_TEST( Unescaping );
97ad053b 57 CPPUNIT_TEST( FileScheme );
b60b2ec8
RN
58#if TEST_URL
59 CPPUNIT_TEST( URLCompat );
402ccadc 60#if 0 && wxUSE_PROTOCOL_HTTP
e605541c 61 CPPUNIT_TEST( URLProxy );
26531700 62#endif
b60b2ec8 63#endif
dd65d8c8
RN
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();
24ca04e7 76 void Unescaping();
97ad053b 77 void FileScheme();
dd65d8c8 78
86470d43 79#if TEST_URL
b60b2ec8 80 void URLCompat();
402ccadc 81#if 0 && wxUSE_PROTOCOL_HTTP
e605541c 82 void URLProxy();
402ccadc 83#endif
b60b2ec8
RN
84#endif
85
dd65d8c8
RN
86 DECLARE_NO_COPY_CLASS(URITestCase)
87};
88
89// register in the unnamed registry so that these tests are run by default
90CPPUNIT_TEST_SUITE_REGISTRATION( URITestCase );
91
92// also include in it's own registry so that these tests can be run alone
93CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( URITestCase, "URITestCase" );
94
95URITestCase::URITestCase()
96{
97}
98
5c3d5ef2
VZ
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)
dd65d8c8
RN
102
103void URITestCase::IPv4()
104{
5c3d5ef2
VZ
105 URI_TEST_EQUAL("http://user:password@192.168.1.100:5050/path",
106 wxURI_IPV4ADDRESS, GetHostType());
dd65d8c8 107
5c3d5ef2
VZ
108 URI_TEST_EQUAL("http://user:password@192.255.1.100:5050/path",
109 wxURI_IPV4ADDRESS, GetHostType());
dd65d8c8
RN
110
111 //bogus ipv4
5c3d5ef2
VZ
112 CPPUNIT_ASSERT( wxURI("http://user:password@192.256.1.100:5050/path").
113 GetHostType() != wxURI_IPV4ADDRESS);
dd65d8c8
RN
114}
115
116void URITestCase::IPv6()
117{
dd65d8c8
RN
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
2b9afe40 128
5c3d5ef2
VZ
129 URI_TEST_EQUAL("http://user:password@[aa:aa:aa:aa:aa:aa:192.168.1.100]:5050/path",
130 wxURI_IPV6ADDRESS, GetHostType());
dd65d8c8 131
5c3d5ef2
VZ
132 URI_TEST_EQUAL("http://user:password@[aa:aa:aa:aa:aa:aa:aa:aa]:5050/path",
133 wxURI_IPV6ADDRESS, GetHostType());
dd65d8c8 134
5c3d5ef2
VZ
135 URI_TEST_EQUAL("http://user:password@[aa:aa:aa:aa::192.168.1.100]:5050/path",
136 wxURI_IPV6ADDRESS, GetHostType());
dd65d8c8 137
5c3d5ef2
VZ
138 URI_TEST_EQUAL("http://user:password@[aa:aa:aa:aa::aa:aa]:5050/path",
139 wxURI_IPV6ADDRESS, GetHostType());
dd65d8c8
RN
140}
141
142void URITestCase::Paths()
143{
5c3d5ef2
VZ
144 URI_TEST_EQUAL("http://user:password@192.256.1.100:5050/../path",
145 "/path", GetPath());
dd65d8c8 146
5c3d5ef2
VZ
147 URI_TEST_EQUAL("http://user:password@192.256.1.100:5050/path/../",
148 "/", GetPath());
dd65d8c8 149
5c3d5ef2
VZ
150 URI_TEST_EQUAL("http://user:password@192.256.1.100:5050/path/.",
151 "/path/", GetPath());
dd65d8c8 152
5c3d5ef2
VZ
153 URI_TEST_EQUAL("http://user:password@192.256.1.100:5050/path/./",
154 "/path/", GetPath());
dd65d8c8 155
5c3d5ef2
VZ
156 URI_TEST_EQUAL("path/john/../../../joe",
157 "../joe", BuildURI());
dd65d8c8 158}
dd65d8c8 159
5c3d5ef2 160#define URI_TEST_RESOLVE_IMPL(string, eq, strict) \
dd65d8c8
RN
161 uri = new wxURI(wxT(string));\
162 uri->Resolve(masteruri, strict);\
86470d43 163 CPPUNIT_ASSERT(uri->BuildURI() == wxT(eq));\
dd65d8c8
RN
164 delete uri;
165
5c3d5ef2
VZ
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);
dd65d8c8
RN
171
172
173//examples taken from RFC 2396.bis
174
175void URITestCase::NormalResolving()
176{
177 wxURI masteruri(wxT("http://a/b/c/d;p?q"));
178 wxURI* uri;
2b9afe40 179
5c3d5ef2
VZ
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")
dd65d8c8
RN
204}
205
206void URITestCase::ComplexResolving()
207{
208 wxURI masteruri(wxT("http://a/b/c/d;p?q"));
209 wxURI* uri;
210
211 //odd path examples
5c3d5ef2
VZ
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")
dd65d8c8
RN
218}
219 //Should Fail
220 //"../../../g" = "http://a/g"
221 //"../../../../g" = "http://a/g"
222
223void URITestCase::ReallyComplexResolving()
224{
225 wxURI masteruri(wxT("http://a/b/c/d;p?q"));
226 wxURI* uri;
227
228 //even more odder path examples
5c3d5ef2
VZ
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")
dd65d8c8
RN
235}
236
237void URITestCase::QueryFragmentResolving()
238{
239 wxURI masteruri(wxT("http://a/b/c/d;p?q"));
240 wxURI* uri;
241
2b9afe40 242 //query/fragment ambigiousness
5c3d5ef2
VZ
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")
dd65d8c8
RN
247}
248
249void URITestCase::BackwardsResolving()
250{
251 wxURI masteruri(wxT("http://a/b/c/d;p?q"));
252 wxURI* uri;
253
2b9afe40 254 //"NEW"
5c3d5ef2 255 URI_TEST_RESOLVE("http:g" , "http:g") //strict
2b9afe40 256 //bw compat
5c3d5ef2 257 URI_TEST_RESOLVE_LAX("http:g", "http://a/b/c/g");
dd65d8c8
RN
258}
259
260void URITestCase::Assignment()
261{
262 wxURI uri1(wxT("http://mysite.com")),
263 uri2(wxT("http://mysite2.com"));
264
265 uri2 = uri1;
266
86470d43 267 CPPUNIT_ASSERT(uri1.BuildURI() == uri2.BuildURI());
dd65d8c8
RN
268}
269
270void URITestCase::Comparison()
271{
272 CPPUNIT_ASSERT(wxURI(wxT("http://mysite.com")) == wxURI(wxT("http://mysite.com")));
273}
b60b2ec8 274
24ca04e7
VZ
275void 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}
97ad053b
VZ
294
295void URITestCase::FileScheme()
296{
297 //file:// variety (NOT CONFORMANT TO THE RFC)
5c3d5ef2 298 CPPUNIT_ASSERT(wxURI(wxString(wxT("file://e:/wxcode/script1.xml"))).GetPath()
97ad053b
VZ
299 == wxT("e:/wxcode/script1.xml") );
300
301 //file:/// variety
5c3d5ef2 302 CPPUNIT_ASSERT(wxURI(wxString(wxT("file:///e:/wxcode/script1.xml"))).GetPath()
97ad053b
VZ
303 == wxT("/e:/wxcode/script1.xml") );
304
305 //file:/ variety
5c3d5ef2 306 CPPUNIT_ASSERT(wxURI(wxString(wxT("file:/e:/wxcode/script1.xml"))).GetPath()
97ad053b
VZ
307 == wxT("/e:/wxcode/script1.xml") );
308
309 //file: variety
5c3d5ef2 310 CPPUNIT_ASSERT(wxURI(wxString(wxT("file:e:/wxcode/script1.xml"))).GetPath()
97ad053b
VZ
311 == wxT("e:/wxcode/script1.xml") );
312}
313
b60b2ec8
RN
314#if TEST_URL
315
24fec908
VZ
316const wxChar* pszProblemUrls[] = { wxT("http://www.csdn.net"),
317 wxT("http://www.163.com"),
318 wxT("http://www.sina.com.cn") };
319
b60b2ec8 320#include "wx/url.h"
24ca04e7 321#include "wx/file.h"
b60b2ec8
RN
322
323void URITestCase::URLCompat()
324{
325 wxURL url(wxT("http://user:password@wxwidgets.org"));
326
327 CPPUNIT_ASSERT(url.GetError() == wxURL_NOERR);
2b9afe40 328
9fc7a1d2 329#if TEST_NETWORK
b60b2ec8
RN
330 wxInputStream* pInput = url.GetInputStream();
331
332 CPPUNIT_ASSERT( pInput != NULL );
9fc7a1d2 333#endif
b60b2ec8
RN
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 );
113e181a
RN
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") );
24fec908 358
6c1a7bc4
MW
359 // these could be put under a named registry since they take some
360 // time to complete
361#if 0
24fec908
VZ
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 }
6c1a7bc4 387#endif
b60b2ec8
RN
388}
389
402ccadc
VZ
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
e605541c
RN
393void URITestCase::URLProxy()
394{
6ca3a322
WS
395 wxURL url(wxT("http://www.asite.com/index.html"));
396 url.SetProxy(wxT("pserv:3122"));
fd725bce
WS
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);
e605541c 402}
26531700 403#endif // wxUSE_PROTOCOL_HTTP
24ca04e7
VZ
404
405#endif // TEST_URL