netwerk/test/urltest.cpp

Wed, 31 Dec 2014 06:55:46 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:46 +0100
changeset 1
ca08bd8f51b2
permissions
-rw-r--r--

Added tag TORBROWSER_REPLICA for changeset 6474c204b198

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /*
michael@0 7 A test file to check default URL parsing.
michael@0 8 -Gagan Saksena 03/25/99
michael@0 9 */
michael@0 10
michael@0 11 #include <stdio.h>
michael@0 12
michael@0 13 #include "TestCommon.h"
michael@0 14 #include "plstr.h"
michael@0 15 #include "nsIServiceManager.h"
michael@0 16 #include "nsIIOService.h"
michael@0 17 #include "nsIURL.h"
michael@0 18 #include "nsCOMPtr.h"
michael@0 19 #include "nsStringAPI.h"
michael@0 20 #include "nsNetCID.h"
michael@0 21 #include "nsIComponentRegistrar.h"
michael@0 22 #include "nsComponentManagerUtils.h"
michael@0 23 #include "nsServiceManagerUtils.h"
michael@0 24 #include "nsXPCOM.h"
michael@0 25 #include "prprf.h"
michael@0 26
michael@0 27 // Define CIDs...
michael@0 28 static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
michael@0 29 static NS_DEFINE_CID(kStdURLCID, NS_STANDARDURL_CID);
michael@0 30
michael@0 31 char* gFileIO = 0;
michael@0 32
michael@0 33 enum {
michael@0 34 URL_FACTORY_DEFAULT,
michael@0 35 URL_FACTORY_STDURL
michael@0 36 };
michael@0 37
michael@0 38 nsresult writeoutto(const char* i_pURL, char** o_Result, int32_t urlFactory = URL_FACTORY_DEFAULT)
michael@0 39 {
michael@0 40 if (!o_Result || !i_pURL)
michael@0 41 return NS_ERROR_FAILURE;
michael@0 42 *o_Result = 0;
michael@0 43 nsCOMPtr<nsIURI> pURL;
michael@0 44 nsresult result = NS_OK;
michael@0 45
michael@0 46 switch (urlFactory) {
michael@0 47 case URL_FACTORY_STDURL: {
michael@0 48 nsIURI* url;
michael@0 49 result = CallCreateInstance(kStdURLCID, &url);
michael@0 50 if (NS_FAILED(result))
michael@0 51 {
michael@0 52 printf("CreateInstance failed\n");
michael@0 53 return NS_ERROR_FAILURE;
michael@0 54 }
michael@0 55 pURL = url;
michael@0 56 pURL->SetSpec(nsDependentCString(i_pURL));
michael@0 57 break;
michael@0 58 }
michael@0 59 case URL_FACTORY_DEFAULT: {
michael@0 60 nsCOMPtr<nsIIOService> pService =
michael@0 61 do_GetService(kIOServiceCID, &result);
michael@0 62 if (NS_FAILED(result))
michael@0 63 {
michael@0 64 printf("Service failed!\n");
michael@0 65 return NS_ERROR_FAILURE;
michael@0 66 }
michael@0 67 result = pService->NewURI(nsDependentCString(i_pURL), nullptr, nullptr, getter_AddRefs(pURL));
michael@0 68 }
michael@0 69 }
michael@0 70
michael@0 71 nsCString output;
michael@0 72 if (NS_SUCCEEDED(result))
michael@0 73 {
michael@0 74 nsCOMPtr<nsIURL> tURL = do_QueryInterface(pURL);
michael@0 75 nsAutoCString temp;
michael@0 76 int32_t port;
michael@0 77 nsresult rv;
michael@0 78
michael@0 79 #define RESULT() NS_SUCCEEDED(rv) ? temp.get() : ""
michael@0 80
michael@0 81 rv = tURL->GetScheme(temp);
michael@0 82 output += RESULT();
michael@0 83 output += ',';
michael@0 84 rv = tURL->GetUsername(temp);
michael@0 85 output += RESULT();
michael@0 86 output += ',';
michael@0 87 rv = tURL->GetPassword(temp);
michael@0 88 output += RESULT();
michael@0 89 output += ',';
michael@0 90 rv = tURL->GetHost(temp);
michael@0 91 output += RESULT();
michael@0 92 output += ',';
michael@0 93 rv = tURL->GetPort(&port);
michael@0 94 char portbuffer[40];
michael@0 95 PR_snprintf(portbuffer, sizeof(portbuffer), "%d", port);
michael@0 96 output.Append(portbuffer);
michael@0 97 output += ',';
michael@0 98 rv = tURL->GetDirectory(temp);
michael@0 99 output += RESULT();
michael@0 100 output += ',';
michael@0 101 rv = tURL->GetFileBaseName(temp);
michael@0 102 output += RESULT();
michael@0 103 output += ',';
michael@0 104 rv = tURL->GetFileExtension(temp);
michael@0 105 output += RESULT();
michael@0 106 output += ',';
michael@0 107 // removed with https://bugzilla.mozilla.org/show_bug.cgi?id=665706
michael@0 108 // rv = tURL->GetParam(temp);
michael@0 109 // output += RESULT();
michael@0 110 output += ',';
michael@0 111 rv = tURL->GetQuery(temp);
michael@0 112 output += RESULT();
michael@0 113 output += ',';
michael@0 114 rv = tURL->GetRef(temp);
michael@0 115 output += RESULT();
michael@0 116 output += ',';
michael@0 117 rv = tURL->GetSpec(temp);
michael@0 118 output += RESULT();
michael@0 119 *o_Result = ToNewCString(output);
michael@0 120 } else {
michael@0 121 output = "Can not create URL";
michael@0 122 *o_Result = ToNewCString(output);
michael@0 123 }
michael@0 124 return NS_OK;
michael@0 125 }
michael@0 126
michael@0 127 nsresult writeout(const char* i_pURL, int32_t urlFactory = URL_FACTORY_DEFAULT)
michael@0 128 {
michael@0 129 if (!i_pURL) return NS_ERROR_FAILURE;
michael@0 130 nsCString temp;
michael@0 131 nsresult rv = writeoutto(i_pURL, getter_Copies(temp), urlFactory);
michael@0 132 printf("%s\n%s\n", i_pURL, temp.get());
michael@0 133 return rv;
michael@0 134 }
michael@0 135
michael@0 136 /* construct a url and print out its elements separated by commas and
michael@0 137 the whole spec */
michael@0 138 nsresult testURL(const char* i_pURL, int32_t urlFactory = URL_FACTORY_DEFAULT)
michael@0 139 {
michael@0 140
michael@0 141 if (i_pURL)
michael@0 142 return writeout(i_pURL, urlFactory);
michael@0 143
michael@0 144 if (!gFileIO)
michael@0 145 return NS_ERROR_FAILURE;
michael@0 146
michael@0 147 FILE *testfile = fopen(gFileIO, "rt");
michael@0 148 if (!testfile)
michael@0 149 {
michael@0 150 fprintf(stderr, "Cannot open testfile: %s\n", gFileIO);
michael@0 151 return NS_ERROR_FAILURE;
michael@0 152 }
michael@0 153
michael@0 154 char temp[512];
michael@0 155 int count=0;
michael@0 156 int failed=0;
michael@0 157 nsCString prevResult;
michael@0 158 nsCString tempurl;
michael@0 159
michael@0 160 while (fgets(temp,512,testfile))
michael@0 161 {
michael@0 162 if (*temp == '#' || !*temp)
michael@0 163 continue;
michael@0 164
michael@0 165 if (0 == count%3)
michael@0 166 {
michael@0 167 printf("Testing: %s\n", temp);
michael@0 168 writeoutto(temp, getter_Copies(prevResult), urlFactory);
michael@0 169 }
michael@0 170 else if (1 == count%3) {
michael@0 171 tempurl.Assign(temp);
michael@0 172 } else {
michael@0 173 if (prevResult.IsEmpty())
michael@0 174 printf("no results to compare to!\n");
michael@0 175 else
michael@0 176 {
michael@0 177 int32_t res;
michael@0 178 printf("Result: %s\n", prevResult.get());
michael@0 179 if (urlFactory != URL_FACTORY_DEFAULT) {
michael@0 180 printf("Expected: %s\n", tempurl.get());
michael@0 181 res = PL_strcmp(tempurl.get(), prevResult.get());
michael@0 182 } else {
michael@0 183 printf("Expected: %s\n", temp);
michael@0 184 res = PL_strcmp(temp, prevResult.get());
michael@0 185 }
michael@0 186
michael@0 187 if (res == 0)
michael@0 188 printf("\tPASSED\n\n");
michael@0 189 else
michael@0 190 {
michael@0 191 printf("\tFAILED\n\n");
michael@0 192 failed++;
michael@0 193 }
michael@0 194 }
michael@0 195 }
michael@0 196 count++;
michael@0 197 }
michael@0 198 if (failed>0) {
michael@0 199 printf("%d tests FAILED out of %d\n", failed, count/3);
michael@0 200 return NS_ERROR_FAILURE;
michael@0 201 } else {
michael@0 202 printf("All %d tests PASSED.\n", count/3);
michael@0 203 return NS_OK;
michael@0 204 }
michael@0 205 }
michael@0 206
michael@0 207 nsresult makeAbsTest(const char* i_BaseURI, const char* relativePortion,
michael@0 208 const char* expectedResult)
michael@0 209 {
michael@0 210 if (!i_BaseURI)
michael@0 211 return NS_ERROR_FAILURE;
michael@0 212
michael@0 213 // build up the base URL
michael@0 214 nsresult status;
michael@0 215 nsCOMPtr<nsIURI> baseURL = do_CreateInstance(kStdURLCID, &status);
michael@0 216 if (NS_FAILED(status))
michael@0 217 {
michael@0 218 printf("CreateInstance failed\n");
michael@0 219 return status;
michael@0 220 }
michael@0 221 status = baseURL->SetSpec(nsDependentCString(i_BaseURI));
michael@0 222 if (NS_FAILED(status)) return status;
michael@0 223
michael@0 224
michael@0 225 // get the new spec
michael@0 226 nsAutoCString newURL;
michael@0 227 status = baseURL->Resolve(nsDependentCString(relativePortion), newURL);
michael@0 228 if (NS_FAILED(status)) return status;
michael@0 229
michael@0 230 nsAutoCString temp;
michael@0 231 baseURL->GetSpec(temp);
michael@0 232
michael@0 233 printf("Analyzing %s\n", temp.get());
michael@0 234 printf("With %s\n", relativePortion);
michael@0 235
michael@0 236 printf("Got %s\n", newURL.get());
michael@0 237 if (expectedResult) {
michael@0 238 printf("Expect %s\n", expectedResult);
michael@0 239 int res = PL_strcmp(newURL.get(), expectedResult);
michael@0 240 if (res == 0) {
michael@0 241 printf("\tPASSED\n\n");
michael@0 242 return NS_OK;
michael@0 243 } else {
michael@0 244 printf("\tFAILED\n\n");
michael@0 245 return NS_ERROR_FAILURE;
michael@0 246 }
michael@0 247 }
michael@0 248 return NS_OK;
michael@0 249 }
michael@0 250
michael@0 251 nsresult doMakeAbsTest(const char* i_URL = 0, const char* i_relativePortion=0)
michael@0 252 {
michael@0 253 if (i_URL && i_relativePortion)
michael@0 254 {
michael@0 255 return makeAbsTest(i_URL, i_relativePortion, nullptr);
michael@0 256 }
michael@0 257
michael@0 258 // Run standard tests. These tests are based on the ones described in
michael@0 259 // rfc2396 with the exception of the handling of ?y which is wrong as
michael@0 260 // notified by on of the RFC authors.
michael@0 261
michael@0 262 /* Section C.1. Normal Examples
michael@0 263
michael@0 264 g:h = <URL:g:h>
michael@0 265 g = <URL:http://a/b/c/g>
michael@0 266 ./g = <URL:http://a/b/c/g>
michael@0 267 g/ = <URL:http://a/b/c/g/>
michael@0 268 /g = <URL:http://a/g>
michael@0 269 //g = <URL:http://g>
michael@0 270 ?y = <URL:http://a/b/c/d;p?y>
michael@0 271 g?y = <URL:http://a/b/c/g?y>
michael@0 272 g?y/./x = <URL:http://a/b/c/g?y/./x>
michael@0 273 #s = <URL:http://a/b/c/d;p?q#s>
michael@0 274 g#s = <URL:http://a/b/c/g#s>
michael@0 275 g#s/./x = <URL:http://a/b/c/g#s/./x>
michael@0 276 g?y#s = <URL:http://a/b/c/g?y#s>
michael@0 277 ;x = <URL:http://a/b/c/;x>
michael@0 278 g;x = <URL:http://a/b/c/g;x>
michael@0 279 g;x?y#s = <URL:http://a/b/c/g;x?y#s>
michael@0 280 . = <URL:http://a/b/c/>
michael@0 281 ./ = <URL:http://a/b/c/>
michael@0 282 .. = <URL:http://a/b/>
michael@0 283 ../ = <URL:http://a/b/>
michael@0 284 ../g = <URL:http://a/b/g>
michael@0 285 ../.. = <URL:http://a/>
michael@0 286 ../../ = <URL:http://a/>
michael@0 287 ../../g = <URL:http://a/g>
michael@0 288 */
michael@0 289
michael@0 290 struct test {
michael@0 291 const char* baseURL;
michael@0 292 const char* relativeURL;
michael@0 293 const char* expectedResult;
michael@0 294 };
michael@0 295
michael@0 296 test tests[] = {
michael@0 297 // Tests from rfc2396, section C.1 with the exception of the
michael@0 298 // handling of ?y
michael@0 299 { "http://a/b/c/d;p?q#f", "g:h", "g:h" },
michael@0 300 { "http://a/b/c/d;p?q#f", "g", "http://a/b/c/g" },
michael@0 301 { "http://a/b/c/d;p?q#f", "./g", "http://a/b/c/g" },
michael@0 302 { "http://a/b/c/d;p?q#f", "g/", "http://a/b/c/g/" },
michael@0 303 { "http://a/b/c/d;p?q#f", "/g", "http://a/g" },
michael@0 304 { "http://a/b/c/d;p?q#f", "//g", "http://g" },
michael@0 305 { "http://a/b/c/d;p?q#f", "?y", "http://a/b/c/d;p?y" },
michael@0 306 { "http://a/b/c/d;p?q#f", "g?y", "http://a/b/c/g?y" },
michael@0 307 { "http://a/b/c/d;p?q#f", "g?y/./x", "http://a/b/c/g?y/./x" },
michael@0 308 { "http://a/b/c/d;p?q#f", "#s", "http://a/b/c/d;p?q#s" },
michael@0 309 { "http://a/b/c/d;p?q#f", "g#s", "http://a/b/c/g#s" },
michael@0 310 { "http://a/b/c/d;p?q#f", "g#s/./x", "http://a/b/c/g#s/./x" },
michael@0 311 { "http://a/b/c/d;p?q#f", "g?y#s", "http://a/b/c/g?y#s" },
michael@0 312 { "http://a/b/c/d;p?q#f", ";x", "http://a/b/c/;x" },
michael@0 313 { "http://a/b/c/d;p?q#f", "g;x", "http://a/b/c/g;x" },
michael@0 314 { "http://a/b/c/d;p?q#f", "g;x?y#s", "http://a/b/c/g;x?y#s" },
michael@0 315 { "http://a/b/c/d;p?q#f", ".", "http://a/b/c/" },
michael@0 316 { "http://a/b/c/d;p?q#f", "./", "http://a/b/c/" },
michael@0 317 { "http://a/b/c/d;p?q#f", "..", "http://a/b/" },
michael@0 318 { "http://a/b/c/d;p?q#f", "../", "http://a/b/" },
michael@0 319 { "http://a/b/c/d;p?q#f", "../g", "http://a/b/g" },
michael@0 320 { "http://a/b/c/d;p?q#f", "../..", "http://a/" },
michael@0 321 { "http://a/b/c/d;p?q#f", "../../", "http://a/" },
michael@0 322 { "http://a/b/c/d;p?q#f", "../../g", "http://a/g" },
michael@0 323
michael@0 324 // Our additional tests...
michael@0 325 { "http://a/b/c/d;p?q#f", "#my::anchor", "http://a/b/c/d;p?q#my::anchor" },
michael@0 326 { "http://a/b/c/d;p?q#f", "get?baseRef=viewcert.jpg", "http://a/b/c/get?baseRef=viewcert.jpg" },
michael@0 327
michael@0 328 // Make sure relative query's work right even if the query
michael@0 329 // string contains absolute urls or other junk.
michael@0 330 { "http://a/b/c/d;p?q#f", "?http://foo", "http://a/b/c/d;p?http://foo" },
michael@0 331 { "http://a/b/c/d;p?q#f", "g?http://foo", "http://a/b/c/g?http://foo" },
michael@0 332 {"http://a/b/c/d;p?q#f", "g/h?http://foo", "http://a/b/c/g/h?http://foo" },
michael@0 333 { "http://a/b/c/d;p?q#f", "g/h/../H?http://foo","http://a/b/c/g/H?http://foo" },
michael@0 334 { "http://a/b/c/d;p?q#f", "g/h/../H?http://foo?baz", "http://a/b/c/g/H?http://foo?baz" },
michael@0 335 { "http://a/b/c/d;p?q#f", "g/h/../H?http://foo;baz", "http://a/b/c/g/H?http://foo;baz" },
michael@0 336 { "http://a/b/c/d;p?q#f", "g/h/../H?http://foo#bar", "http://a/b/c/g/H?http://foo#bar" },
michael@0 337 { "http://a/b/c/d;p?q#f", "g/h/../H;baz?http://foo", "http://a/b/c/g/H;baz?http://foo" },
michael@0 338 { "http://a/b/c/d;p?q#f", "g/h/../H;baz?http://foo#bar", "http://a/b/c/g/H;baz?http://foo#bar" },
michael@0 339 { "http://a/b/c/d;p?q#f", "g/h/../H;baz?C:\\temp", "http://a/b/c/g/H;baz?C:\\temp" },
michael@0 340 { "http://a/b/c/d;p?q#f", "", "http://a/b/c/d;p?q" },
michael@0 341 { "http://a/b/c/d;p?q#f", "#", "http://a/b/c/d;p?q#" },
michael@0 342 { "http://a/b/c;p/d;p?q#f", "../g;p" , "http://a/b/g;p" },
michael@0 343
michael@0 344 };
michael@0 345
michael@0 346 const int numTests = sizeof(tests) / sizeof(tests[0]);
michael@0 347 int failed = 0;
michael@0 348 nsresult rv;
michael@0 349 for (int i = 0 ; i<numTests ; ++i)
michael@0 350 {
michael@0 351 rv = makeAbsTest(tests[i].baseURL, tests[i].relativeURL,
michael@0 352 tests[i].expectedResult);
michael@0 353 if (NS_FAILED(rv))
michael@0 354 failed++;
michael@0 355 }
michael@0 356 if (failed>0) {
michael@0 357 printf("%d tests FAILED out of %d\n", failed, numTests);
michael@0 358 return NS_ERROR_FAILURE;
michael@0 359 } else {
michael@0 360 printf("All %d tests PASSED.\n", numTests);
michael@0 361 return NS_OK;
michael@0 362 }
michael@0 363 }
michael@0 364
michael@0 365 void printusage(void)
michael@0 366 {
michael@0 367 printf("urltest [-std] [-file <filename>] <URL> "
michael@0 368 " [-abs <relative>]\n\n"
michael@0 369 "\t-std : Generate results using nsStdURL.\n"
michael@0 370 "\t-file : Read URLs from file.\n"
michael@0 371 "\t-abs : Make an absolute URL from the base (<URL>) and the\n"
michael@0 372 "\t\trelative path specified. If -abs is given without\n"
michael@0 373 "\t\ta base URI standard RFC 2396 relative URL tests\n"
michael@0 374 "\t\tare performed. Implies -std.\n"
michael@0 375 "\t<URL> : The string representing the URL.\n");
michael@0 376 }
michael@0 377
michael@0 378 int main(int argc, char **argv)
michael@0 379 {
michael@0 380 if (test_common_init(&argc, &argv) != 0)
michael@0 381 return -1;
michael@0 382
michael@0 383 if (argc < 2) {
michael@0 384 printusage();
michael@0 385 return 0;
michael@0 386 }
michael@0 387 {
michael@0 388 nsCOMPtr<nsIServiceManager> servMan;
michael@0 389 NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
michael@0 390
michael@0 391 // end of all messages from register components...
michael@0 392 printf("------------------\n\n");
michael@0 393
michael@0 394 int32_t urlFactory = URL_FACTORY_DEFAULT;
michael@0 395 bool bMakeAbs= false;
michael@0 396 char* relativePath = 0;
michael@0 397 char* url = 0;
michael@0 398 for (int i=1; i<argc; i++) {
michael@0 399 if (PL_strcasecmp(argv[i], "-std") == 0)
michael@0 400 {
michael@0 401 urlFactory = URL_FACTORY_STDURL;
michael@0 402 if (i+1 >= argc)
michael@0 403 {
michael@0 404 printusage();
michael@0 405 return 0;
michael@0 406 }
michael@0 407 }
michael@0 408 else if (PL_strcasecmp(argv[i], "-abs") == 0)
michael@0 409 {
michael@0 410 if (!gFileIO)
michael@0 411 {
michael@0 412 relativePath = argv[i+1];
michael@0 413 i++;
michael@0 414 }
michael@0 415 bMakeAbs = true;
michael@0 416 }
michael@0 417 else if (PL_strcasecmp(argv[i], "-file") == 0)
michael@0 418 {
michael@0 419 if (i+1 >= argc)
michael@0 420 {
michael@0 421 printusage();
michael@0 422 return 0;
michael@0 423 }
michael@0 424 gFileIO = argv[i+1];
michael@0 425 i++;
michael@0 426 }
michael@0 427 else
michael@0 428 {
michael@0 429 url = argv[i];
michael@0 430 }
michael@0 431 }
michael@0 432 PRTime startTime = PR_Now();
michael@0 433 if (bMakeAbs)
michael@0 434 {
michael@0 435 if (url && relativePath) {
michael@0 436 doMakeAbsTest(url, relativePath);
michael@0 437 } else {
michael@0 438 doMakeAbsTest();
michael@0 439 }
michael@0 440 }
michael@0 441 else
michael@0 442 {
michael@0 443 if (gFileIO) {
michael@0 444 testURL(0, urlFactory);
michael@0 445 } else {
michael@0 446 testURL(url, urlFactory);
michael@0 447 }
michael@0 448 }
michael@0 449 if (gFileIO)
michael@0 450 {
michael@0 451 PRTime endTime = PR_Now();
michael@0 452 printf("Elapsed time: %d micros.\n", (int32_t)
michael@0 453 (endTime - startTime));
michael@0 454 }
michael@0 455 } // this scopes the nsCOMPtrs
michael@0 456 // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
michael@0 457 return NS_FAILED(NS_ShutdownXPCOM(nullptr)) ? 1 : 0;
michael@0 458 }

mercurial