urltest.cpp 15 KB

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