1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Netscape Public License
6
* Version 1.1 (the "License"); you may not use this file except in
7
* compliance with the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/NPL/
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 NPL, 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 NPL, the GPL or the LGPL.
37
* ***** END LICENSE BLOCK ***** */
40
A test file to check default URL parsing.
41
-Gagan Saksena 03/25/99
47
#include "nsIServiceManager.h"
48
#include "nsIIOService.h"
51
#include "nsXPIDLString.h"
53
#include "nsReadableUtils.h"
56
#include "nsIComponentRegistrar.h"
59
static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
60
static NS_DEFINE_CID(kStdURLCID, NS_STANDARDURL_CID);
69
nsresult writeoutto(const char* i_pURL, char** o_Result, PRInt32 urlFactory = URL_FACTORY_DEFAULT)
71
if (!o_Result || !i_pURL)
72
return NS_ERROR_FAILURE;
74
nsCOMPtr<nsIURI> pURL;
75
nsresult result = NS_OK;
78
case URL_FACTORY_STDURL: {
80
result = nsComponentManager::CreateInstance(kStdURLCID, nsnull,
81
NS_GET_IID(nsIURI), (void**)&url);
82
if (NS_FAILED(result))
84
printf("CreateInstance failed\n");
85
return NS_ERROR_FAILURE;
88
pURL->SetSpec(nsDependentCString(i_pURL));
91
case URL_FACTORY_DEFAULT: {
92
nsCOMPtr<nsIIOService> pService =
93
do_GetService(kIOServiceCID, &result);
94
if (NS_FAILED(result))
96
printf("Service failed!\n");
97
return NS_ERROR_FAILURE;
99
result = pService->NewURI(nsDependentCString(i_pURL), nsnull, nsnull, getter_AddRefs(pURL));
104
if (NS_SUCCEEDED(result))
106
nsCOMPtr<nsIURL> tURL = do_QueryInterface(pURL);
111
#define RESULT() NS_SUCCEEDED(rv) ? temp.get() : ""
113
rv = tURL->GetScheme(temp);
116
rv = tURL->GetUsername(temp);
119
rv = tURL->GetPassword(temp);
122
rv = tURL->GetHost(temp);
125
rv = tURL->GetPort(&port);
126
output.AppendInt(port);
128
rv = tURL->GetDirectory(temp);
131
rv = tURL->GetFileBaseName(temp);
134
rv = tURL->GetFileExtension(temp);
137
rv = tURL->GetParam(temp);
140
rv = tURL->GetQuery(temp);
143
rv = tURL->GetRef(temp);
146
rv = tURL->GetSpec(temp);
148
*o_Result = ToNewCString(output);
150
output = "Can not create URL";
151
*o_Result = ToNewCString(output);
156
nsresult writeout(const char* i_pURL, PRInt32 urlFactory = URL_FACTORY_DEFAULT)
159
if (!i_pURL) return NS_ERROR_FAILURE;
160
int rv = writeoutto(i_pURL, &temp, urlFactory);
161
printf("%s\n%s\n", i_pURL, temp);
166
/* construct a url and print out its elements separated by commas and
168
nsresult testURL(const char* i_pURL, PRInt32 urlFactory = URL_FACTORY_DEFAULT)
172
return writeout(i_pURL, urlFactory);
175
return NS_ERROR_FAILURE;
177
FILE *testfile = fopen(gFileIO, "rt");
180
fprintf(stderr, "Cannot open testfile: %s\n", gFileIO);
181
return NS_ERROR_FAILURE;
187
char* prevResult = nsnull;
188
char* tempurl = nsnull;
190
while (fgets(temp,512,testfile))
192
if (*temp == '#' || !*temp)
197
if (prevResult) delete[] prevResult;
198
printf("Testing: %s\n", temp);
199
writeoutto(temp, &prevResult, urlFactory);
201
else if (1 == count%3) {
202
if (tempurl) delete[] tempurl;
203
tempurl = nsCRT::strdup(temp);
206
printf("no results to compare to!\n");
210
printf("Result: %s\n", prevResult);
211
if (urlFactory != URL_FACTORY_DEFAULT) {
212
printf("Expected: %s\n", tempurl);
213
res = PL_strcmp(tempurl, prevResult);
215
printf("Expected: %s\n", temp);
216
res = PL_strcmp(temp, prevResult);
220
printf("\tPASSED\n\n");
223
printf("\tFAILED\n\n");
231
printf("%d tests FAILED out of %d\n", failed, count/3);
232
return NS_ERROR_FAILURE;
234
printf("All %d tests PASSED.\n", count/3);
239
nsresult makeAbsTest(const char* i_BaseURI, const char* relativePortion,
240
const char* expectedResult)
243
return NS_ERROR_FAILURE;
245
// build up the base URL
246
nsCOMPtr<nsIURI> baseURL;
247
nsresult status = nsComponentManager::CreateInstance(kStdURLCID, nsnull,
248
NS_GET_IID(nsIURI), getter_AddRefs(baseURL));
249
if (NS_FAILED(status))
251
printf("CreateInstance failed\n");
254
status = baseURL->SetSpec(nsDependentCString(i_BaseURI));
255
if (NS_FAILED(status)) return status;
259
nsCAutoString newURL;
260
status = baseURL->Resolve(nsDependentCString(relativePortion), newURL);
261
if (NS_FAILED(status)) return status;
264
baseURL->GetSpec(temp);
266
printf("Analyzing %s\n", temp.get());
267
printf("With %s\n", relativePortion);
269
printf("Got %s\n", newURL.get());
270
if (expectedResult) {
271
printf("Expect %s\n", expectedResult);
272
int res = PL_strcmp(newURL.get(), expectedResult);
274
printf("\tPASSED\n\n");
277
printf("\tFAILED\n\n");
278
return NS_ERROR_FAILURE;
284
int doMakeAbsTest(const char* i_URL = 0, const char* i_relativePortion=0)
286
if (i_URL && i_relativePortion)
288
return makeAbsTest(i_URL, i_relativePortion, nsnull);
291
// Run standard tests. These tests are based on the ones described in
292
// rfc2396 with the exception of the handling of ?y which is wrong as
293
// notified by on of the RFC authors.
295
/* Section C.1. Normal Examples
298
g = <URL:http://a/b/c/g>
299
./g = <URL:http://a/b/c/g>
300
g/ = <URL:http://a/b/c/g/>
301
/g = <URL:http://a/g>
303
?y = <URL:http://a/b/c/d;p?y>
304
g?y = <URL:http://a/b/c/g?y>
305
g?y/./x = <URL:http://a/b/c/g?y/./x>
306
#s = <URL:http://a/b/c/d;p?q#s>
307
g#s = <URL:http://a/b/c/g#s>
308
g#s/./x = <URL:http://a/b/c/g#s/./x>
309
g?y#s = <URL:http://a/b/c/g?y#s>
310
;x = <URL:http://a/b/c/;x>
311
g;x = <URL:http://a/b/c/g;x>
312
g;x?y#s = <URL:http://a/b/c/g;x?y#s>
313
. = <URL:http://a/b/c/>
314
./ = <URL:http://a/b/c/>
315
.. = <URL:http://a/b/>
316
../ = <URL:http://a/b/>
317
../g = <URL:http://a/b/g>
318
../.. = <URL:http://a/>
319
../../ = <URL:http://a/>
320
../../g = <URL:http://a/g>
325
const char* relativeURL;
326
const char* expectedResult;
330
// Tests from rfc2396, section C.1 with the exception of the
332
{ "http://a/b/c/d;p?q#f", "g:h", "g:h" },
333
{ "http://a/b/c/d;p?q#f", "g", "http://a/b/c/g" },
334
{ "http://a/b/c/d;p?q#f", "./g", "http://a/b/c/g" },
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/g" },
337
{ "http://a/b/c/d;p?q#f", "//g", "http://g" },
338
{ "http://a/b/c/d;p?q#f", "?y", "http://a/b/c/d;p?y" },
339
{ "http://a/b/c/d;p?q#f", "g?y", "http://a/b/c/g?y" },
340
{ "http://a/b/c/d;p?q#f", "g?y/./x", "http://a/b/c/g?y/./x" },
341
{ "http://a/b/c/d;p?q#f", "#s", "http://a/b/c/d;p?q#s" },
342
{ "http://a/b/c/d;p?q#f", "g#s", "http://a/b/c/g#s" },
343
{ "http://a/b/c/d;p?q#f", "g#s/./x", "http://a/b/c/g#s/./x" },
344
{ "http://a/b/c/d;p?q#f", "g?y#s", "http://a/b/c/g?y#s" },
345
{ "http://a/b/c/d;p?q#f", ";x", "http://a/b/c/;x" },
346
{ "http://a/b/c/d;p?q#f", "g;x", "http://a/b/c/g;x" },
347
{ "http://a/b/c/d;p?q#f", "g;x?y#s", "http://a/b/c/g;x?y#s" },
348
{ "http://a/b/c/d;p?q#f", ".", "http://a/b/c/" },
349
{ "http://a/b/c/d;p?q#f", "./", "http://a/b/c/" },
350
{ "http://a/b/c/d;p?q#f", "..", "http://a/b/" },
351
{ "http://a/b/c/d;p?q#f", "../", "http://a/b/" },
352
{ "http://a/b/c/d;p?q#f", "../g", "http://a/b/g" },
353
{ "http://a/b/c/d;p?q#f", "../..", "http://a/" },
354
{ "http://a/b/c/d;p?q#f", "../../", "http://a/" },
355
{ "http://a/b/c/d;p?q#f", "../../g", "http://a/g" },
357
// Our additional tests...
358
{ "http://a/b/c/d;p?q#f", "#my::anchor", "http://a/b/c/d;p?q#my::anchor" },
359
{ "http://a/b/c/d;p?q#f", "get?baseRef=viewcert.jpg", "http://a/b/c/get?baseRef=viewcert.jpg" },
361
// Make sure relative query's work right even if the query
362
// string contains absolute urls or other junk.
363
{ "http://a/b/c/d;p?q#f", "?http://foo", "http://a/b/c/d;p?http://foo" },
364
{ "http://a/b/c/d;p?q#f", "g?http://foo", "http://a/b/c/g?http://foo" },
365
{"http://a/b/c/d;p?q#f", "g/h?http://foo", "http://a/b/c/g/h?http://foo" },
366
{ "http://a/b/c/d;p?q#f", "g/h/../H?http://foo","http://a/b/c/g/H?http://foo" },
367
{ "http://a/b/c/d;p?q#f", "g/h/../H?http://foo?baz", "http://a/b/c/g/H?http://foo?baz" },
368
{ "http://a/b/c/d;p?q#f", "g/h/../H?http://foo;baz", "http://a/b/c/g/H?http://foo;baz" },
369
{ "http://a/b/c/d;p?q#f", "g/h/../H?http://foo#bar", "http://a/b/c/g/H?http://foo#bar" },
370
{ "http://a/b/c/d;p?q#f", "g/h/../H;baz?http://foo", "http://a/b/c/g/H;baz?http://foo" },
371
{ "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" },
372
{ "http://a/b/c/d;p?q#f", "g/h/../H;baz?C:\\temp", "http://a/b/c/g/H;baz?C:\\temp" },
373
{ "http://a/b/c/d;p?q#f", "", "http://a/b/c/d;p?q" },
374
{ "http://a/b/c/d;p?q#f", "#", "http://a/b/c/d;p?q#" },
375
{ "http://a/b/c;p/d;p?q#f", "../g;p" , "http://a/b/g;p" },
379
const int numTests = sizeof(tests) / sizeof(tests[0]);
382
for (int i = 0 ; i<numTests ; ++i)
384
rv = makeAbsTest(tests[i].baseURL, tests[i].relativeURL,
385
tests[i].expectedResult);
390
printf("%d tests FAILED out of %d\n", failed, numTests);
391
return NS_ERROR_FAILURE;
393
printf("All %d tests PASSED.\n", numTests);
398
void printusage(void)
400
printf("urltest [-std] [-file <filename>] <URL> "
401
" [-abs <relative>]\n\n"
402
"\t-std : Generate results using nsStdURL.\n"
403
"\t-file : Read URLs from file.\n"
404
"\t-abs : Make an absolute URL from the base (<URL>) and the\n"
405
"\t\trelative path specified. If -abs is given without\n"
406
"\t\ta base URI standard RFC 2396 relative URL tests\n"
407
"\t\tare performed. Implies -std.\n"
408
"\t<URL> : The string representing the URL.\n");
411
int main(int argc, char **argv)
420
nsCOMPtr<nsIServiceManager> servMan;
421
NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
422
nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
423
NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
425
registrar->AutoRegister(nsnull);
427
// end of all messages from register components...
428
printf("------------------\n\n");
430
PRInt32 urlFactory = URL_FACTORY_DEFAULT;
431
PRBool bMakeAbs= PR_FALSE;
432
char* relativePath = 0;
434
for (int i=1; i<argc; i++) {
435
if (PL_strcasecmp(argv[i], "-std") == 0)
437
urlFactory = URL_FACTORY_STDURL;
444
else if (PL_strcasecmp(argv[i], "-abs") == 0)
448
relativePath = argv[i+1];
453
else if (PL_strcasecmp(argv[i], "-file") == 0)
468
PRTime startTime = PR_Now();
471
rv = (url && relativePath)
472
? doMakeAbsTest(url, relativePath)
477
rv = gFileIO ? testURL(0, urlFactory) : testURL(url, urlFactory);
481
PRTime endTime = PR_Now();
482
printf("Elapsed time: %d micros.\n", (PRInt32)
483
(endTime - startTime));
485
} // this scopes the nsCOMPtrs
486
// no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
487
rv = NS_ShutdownXPCOM(nsnull);