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