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