1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
23 * Pierre Phaneuf <pp@ludusdesign.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
40 A test file to check default URL parsing.
41 -Gagan Saksena 03/25/99
46 #include "TestCommon.h"
48 #include "nsIServiceManager.h"
49 #include "nsIIOService.h"
52 #include "nsStringAPI.h"
54 #include "nsIComponentRegistrar.h"
55 #include "nsComponentManagerUtils.h"
56 #include "nsServiceManagerUtils.h"
61 static NS_DEFINE_CID(kIOServiceCID
, NS_IOSERVICE_CID
);
62 static NS_DEFINE_CID(kStdURLCID
, NS_STANDARDURL_CID
);
71 nsresult
writeoutto(const char* i_pURL
, char** o_Result
, PRInt32 urlFactory
= URL_FACTORY_DEFAULT
)
73 if (!o_Result
|| !i_pURL
)
74 return NS_ERROR_FAILURE
;
76 nsCOMPtr
<nsIURI
> pURL
;
77 nsresult result
= NS_OK
;
80 case URL_FACTORY_STDURL
: {
82 result
= CallCreateInstance(kStdURLCID
, &url
);
83 if (NS_FAILED(result
))
85 printf("CreateInstance failed\n");
86 return NS_ERROR_FAILURE
;
89 pURL
->SetSpec(nsDependentCString(i_pURL
));
92 case URL_FACTORY_DEFAULT
: {
93 nsCOMPtr
<nsIIOService
> pService
=
94 do_GetService(kIOServiceCID
, &result
);
95 if (NS_FAILED(result
))
97 printf("Service failed!\n");
98 return NS_ERROR_FAILURE
;
100 result
= pService
->NewURI(nsDependentCString(i_pURL
), nsnull
, nsnull
, getter_AddRefs(pURL
));
105 if (NS_SUCCEEDED(result
))
107 nsCOMPtr
<nsIURL
> tURL
= do_QueryInterface(pURL
);
112 #define RESULT() NS_SUCCEEDED(rv) ? temp.get() : ""
114 rv
= tURL
->GetScheme(temp
);
117 rv
= tURL
->GetUsername(temp
);
120 rv
= tURL
->GetPassword(temp
);
123 rv
= tURL
->GetHost(temp
);
126 rv
= tURL
->GetPort(&port
);
128 PR_snprintf(portbuffer
, sizeof(portbuffer
), "%d", port
);
129 output
.Append(portbuffer
);
131 rv
= tURL
->GetDirectory(temp
);
134 rv
= tURL
->GetFileBaseName(temp
);
137 rv
= tURL
->GetFileExtension(temp
);
140 rv
= tURL
->GetParam(temp
);
143 rv
= tURL
->GetQuery(temp
);
146 rv
= tURL
->GetRef(temp
);
149 rv
= tURL
->GetSpec(temp
);
151 *o_Result
= ToNewCString(output
);
153 output
= "Can not create URL";
154 *o_Result
= ToNewCString(output
);
159 nsresult
writeout(const char* i_pURL
, PRInt32 urlFactory
= URL_FACTORY_DEFAULT
)
162 if (!i_pURL
) return NS_ERROR_FAILURE
;
163 int rv
= writeoutto(i_pURL
, &temp
, urlFactory
);
164 printf("%s\n%s\n", i_pURL
, temp
);
169 /* construct a url and print out its elements separated by commas and
171 nsresult
testURL(const char* i_pURL
, PRInt32 urlFactory
= URL_FACTORY_DEFAULT
)
175 return writeout(i_pURL
, urlFactory
);
178 return NS_ERROR_FAILURE
;
180 FILE *testfile
= fopen(gFileIO
, "rt");
183 fprintf(stderr
, "Cannot open testfile: %s\n", gFileIO
);
184 return NS_ERROR_FAILURE
;
190 char* prevResult
= nsnull
;
191 char* tempurl
= nsnull
;
193 while (fgets(temp
,512,testfile
))
195 if (*temp
== '#' || !*temp
)
200 if (prevResult
) delete[] prevResult
;
201 printf("Testing: %s\n", temp
);
202 writeoutto(temp
, &prevResult
, urlFactory
);
204 else if (1 == count
%3) {
205 if (tempurl
) delete[] tempurl
;
206 tempurl
= strdup(temp
);
209 printf("no results to compare to!\n");
213 printf("Result: %s\n", prevResult
);
214 if (urlFactory
!= URL_FACTORY_DEFAULT
) {
215 printf("Expected: %s\n", tempurl
);
216 res
= PL_strcmp(tempurl
, prevResult
);
218 printf("Expected: %s\n", temp
);
219 res
= PL_strcmp(temp
, prevResult
);
223 printf("\tPASSED\n\n");
226 printf("\tFAILED\n\n");
234 printf("%d tests FAILED out of %d\n", failed
, count
/3);
235 return NS_ERROR_FAILURE
;
237 printf("All %d tests PASSED.\n", count
/3);
242 nsresult
makeAbsTest(const char* i_BaseURI
, const char* relativePortion
,
243 const char* expectedResult
)
246 return NS_ERROR_FAILURE
;
248 // build up the base URL
250 nsCOMPtr
<nsIURI
> baseURL
= do_CreateInstance(kStdURLCID
, &status
);
251 if (NS_FAILED(status
))
253 printf("CreateInstance failed\n");
256 status
= baseURL
->SetSpec(nsDependentCString(i_BaseURI
));
257 if (NS_FAILED(status
)) return status
;
261 nsCAutoString newURL
;
262 status
= baseURL
->Resolve(nsDependentCString(relativePortion
), newURL
);
263 if (NS_FAILED(status
)) return status
;
266 baseURL
->GetSpec(temp
);
268 printf("Analyzing %s\n", temp
.get());
269 printf("With %s\n", relativePortion
);
271 printf("Got %s\n", newURL
.get());
272 if (expectedResult
) {
273 printf("Expect %s\n", expectedResult
);
274 int res
= PL_strcmp(newURL
.get(), expectedResult
);
276 printf("\tPASSED\n\n");
279 printf("\tFAILED\n\n");
280 return NS_ERROR_FAILURE
;
286 int doMakeAbsTest(const char* i_URL
= 0, const char* i_relativePortion
=0)
288 if (i_URL
&& i_relativePortion
)
290 return makeAbsTest(i_URL
, i_relativePortion
, nsnull
);
293 // Run standard tests. These tests are based on the ones described in
294 // rfc2396 with the exception of the handling of ?y which is wrong as
295 // notified by on of the RFC authors.
297 /* Section C.1. Normal Examples
300 g = <URL:http://a/b/c/g>
301 ./g = <URL:http://a/b/c/g>
302 g/ = <URL:http://a/b/c/g/>
303 /g = <URL:http://a/g>
305 ?y = <URL:http://a/b/c/d;p?y>
306 g?y = <URL:http://a/b/c/g?y>
307 g?y/./x = <URL:http://a/b/c/g?y/./x>
308 #s = <URL:http://a/b/c/d;p?q#s>
309 g#s = <URL:http://a/b/c/g#s>
310 g#s/./x = <URL:http://a/b/c/g#s/./x>
311 g?y#s = <URL:http://a/b/c/g?y#s>
312 ;x = <URL:http://a/b/c/;x>
313 g;x = <URL:http://a/b/c/g;x>
314 g;x?y#s = <URL:http://a/b/c/g;x?y#s>
315 . = <URL:http://a/b/c/>
316 ./ = <URL:http://a/b/c/>
317 .. = <URL:http://a/b/>
318 ../ = <URL:http://a/b/>
319 ../g = <URL:http://a/b/g>
320 ../.. = <URL:http://a/>
321 ../../ = <URL:http://a/>
322 ../../g = <URL:http://a/g>
327 const char* relativeURL
;
328 const char* expectedResult
;
332 // Tests from rfc2396, section C.1 with the exception of the
334 { "http://a/b/c/d;p?q#f", "g:h", "g:h" },
335 { "http://a/b/c/d;p?q#f", "g", "http://a/b/c/g" },
336 { "http://a/b/c/d;p?q#f", "./g", "http://a/b/c/g" },
337 { "http://a/b/c/d;p?q#f", "g/", "http://a/b/c/g/" },
338 { "http://a/b/c/d;p?q#f", "/g", "http://a/g" },
339 { "http://a/b/c/d;p?q#f", "//g", "http://g" },
340 { "http://a/b/c/d;p?q#f", "?y", "http://a/b/c/d;p?y" },
341 { "http://a/b/c/d;p?q#f", "g?y", "http://a/b/c/g?y" },
342 { "http://a/b/c/d;p?q#f", "g?y/./x", "http://a/b/c/g?y/./x" },
343 { "http://a/b/c/d;p?q#f", "#s", "http://a/b/c/d;p?q#s" },
344 { "http://a/b/c/d;p?q#f", "g#s", "http://a/b/c/g#s" },
345 { "http://a/b/c/d;p?q#f", "g#s/./x", "http://a/b/c/g#s/./x" },
346 { "http://a/b/c/d;p?q#f", "g?y#s", "http://a/b/c/g?y#s" },
347 { "http://a/b/c/d;p?q#f", ";x", "http://a/b/c/;x" },
348 { "http://a/b/c/d;p?q#f", "g;x", "http://a/b/c/g;x" },
349 { "http://a/b/c/d;p?q#f", "g;x?y#s", "http://a/b/c/g;x?y#s" },
350 { "http://a/b/c/d;p?q#f", ".", "http://a/b/c/" },
351 { "http://a/b/c/d;p?q#f", "./", "http://a/b/c/" },
352 { "http://a/b/c/d;p?q#f", "..", "http://a/b/" },
353 { "http://a/b/c/d;p?q#f", "../", "http://a/b/" },
354 { "http://a/b/c/d;p?q#f", "../g", "http://a/b/g" },
355 { "http://a/b/c/d;p?q#f", "../..", "http://a/" },
356 { "http://a/b/c/d;p?q#f", "../../", "http://a/" },
357 { "http://a/b/c/d;p?q#f", "../../g", "http://a/g" },
359 // Our additional tests...
360 { "http://a/b/c/d;p?q#f", "#my::anchor", "http://a/b/c/d;p?q#my::anchor" },
361 { "http://a/b/c/d;p?q#f", "get?baseRef=viewcert.jpg", "http://a/b/c/get?baseRef=viewcert.jpg" },
363 // Make sure relative query's work right even if the query
364 // string contains absolute urls or other junk.
365 { "http://a/b/c/d;p?q#f", "?http://foo", "http://a/b/c/d;p?http://foo" },
366 { "http://a/b/c/d;p?q#f", "g?http://foo", "http://a/b/c/g?http://foo" },
367 {"http://a/b/c/d;p?q#f", "g/h?http://foo", "http://a/b/c/g/h?http://foo" },
368 { "http://a/b/c/d;p?q#f", "g/h/../H?http://foo","http://a/b/c/g/H?http://foo" },
369 { "http://a/b/c/d;p?q#f", "g/h/../H?http://foo?baz", "http://a/b/c/g/H?http://foo?baz" },
370 { "http://a/b/c/d;p?q#f", "g/h/../H?http://foo;baz", "http://a/b/c/g/H?http://foo;baz" },
371 { "http://a/b/c/d;p?q#f", "g/h/../H?http://foo#bar", "http://a/b/c/g/H?http://foo#bar" },
372 { "http://a/b/c/d;p?q#f", "g/h/../H;baz?http://foo", "http://a/b/c/g/H;baz?http://foo" },
373 { "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" },
374 { "http://a/b/c/d;p?q#f", "g/h/../H;baz?C:\\temp", "http://a/b/c/g/H;baz?C:\\temp" },
375 { "http://a/b/c/d;p?q#f", "", "http://a/b/c/d;p?q" },
376 { "http://a/b/c/d;p?q#f", "#", "http://a/b/c/d;p?q#" },
377 { "http://a/b/c;p/d;p?q#f", "../g;p" , "http://a/b/g;p" },
381 const int numTests
= sizeof(tests
) / sizeof(tests
[0]);
384 for (int i
= 0 ; i
<numTests
; ++i
)
386 rv
= makeAbsTest(tests
[i
].baseURL
, tests
[i
].relativeURL
,
387 tests
[i
].expectedResult
);
392 printf("%d tests FAILED out of %d\n", failed
, numTests
);
393 return NS_ERROR_FAILURE
;
395 printf("All %d tests PASSED.\n", numTests
);
400 void printusage(void)
402 printf("urltest [-std] [-file <filename>] <URL> "
403 " [-abs <relative>]\n\n"
404 "\t-std : Generate results using nsStdURL.\n"
405 "\t-file : Read URLs from file.\n"
406 "\t-abs : Make an absolute URL from the base (<URL>) and the\n"
407 "\t\trelative path specified. If -abs is given without\n"
408 "\t\ta base URI standard RFC 2396 relative URL tests\n"
409 "\t\tare performed. Implies -std.\n"
410 "\t<URL> : The string representing the URL.\n");
413 int main(int argc
, char **argv
)
415 if (test_common_init(&argc
, &argv
) != 0)
425 nsCOMPtr
<nsIServiceManager
> servMan
;
426 NS_InitXPCOM2(getter_AddRefs(servMan
), nsnull
, nsnull
);
427 nsCOMPtr
<nsIComponentRegistrar
> registrar
= do_QueryInterface(servMan
);
428 NS_ASSERTION(registrar
, "Null nsIComponentRegistrar");
430 registrar
->AutoRegister(nsnull
);
432 // end of all messages from register components...
433 printf("------------------\n\n");
435 PRInt32 urlFactory
= URL_FACTORY_DEFAULT
;
436 PRBool bMakeAbs
= PR_FALSE
;
437 char* relativePath
= 0;
439 for (int i
=1; i
<argc
; i
++) {
440 if (PL_strcasecmp(argv
[i
], "-std") == 0)
442 urlFactory
= URL_FACTORY_STDURL
;
449 else if (PL_strcasecmp(argv
[i
], "-abs") == 0)
453 relativePath
= argv
[i
+1];
458 else if (PL_strcasecmp(argv
[i
], "-file") == 0)
473 PRTime startTime
= PR_Now();
476 rv
= (url
&& relativePath
)
477 ? doMakeAbsTest(url
, relativePath
)
482 rv
= gFileIO
? testURL(0, urlFactory
) : testURL(url
, urlFactory
);
486 PRTime endTime
= PR_Now();
487 printf("Elapsed time: %d micros.\n", (PRInt32
)
488 (endTime
- startTime
));
490 } // this scopes the nsCOMPtrs
491 // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
492 rv
= NS_ShutdownXPCOM(nsnull
);