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