~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/netwerk/test/urltest.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
4
 *
 
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/
 
9
 *
 
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
 
13
 * License.
 
14
 *
 
15
 * The Original Code is mozilla.org code.
 
16
 *
 
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.
 
21
 *
 
22
 * Contributor(s):
 
23
 *   Pierre Phaneuf <pp@ludusdesign.com>
 
24
 *
 
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.
 
36
 *
 
37
 * ***** END LICENSE BLOCK ***** */
 
38
 
 
39
/*
 
40
    A test file to check default URL parsing.
 
41
    -Gagan Saksena 03/25/99
 
42
*/
 
43
 
 
44
#include <stdio.h>
 
45
 
 
46
#include "plstr.h"
 
47
#include "nsIServiceManager.h"
 
48
#include "nsIIOService.h"
 
49
#include "nsIURL.h"
 
50
#include "nsCOMPtr.h"
 
51
#include "nsXPIDLString.h"
 
52
#include "nsString.h"
 
53
#include "nsReadableUtils.h"
 
54
#include "nsCRT.h"
 
55
#include "nsNetCID.h"
 
56
#include "nsIComponentRegistrar.h"
 
57
 
 
58
// Define CIDs...
 
59
static NS_DEFINE_CID(kIOServiceCID,              NS_IOSERVICE_CID);
 
60
static NS_DEFINE_CID(kStdURLCID,                 NS_STANDARDURL_CID);
 
61
 
 
62
char* gFileIO = 0;
 
63
 
 
64
enum {
 
65
    URL_FACTORY_DEFAULT,
 
66
    URL_FACTORY_STDURL
 
67
};
 
68
 
 
69
nsresult writeoutto(const char* i_pURL, char** o_Result, PRInt32 urlFactory = URL_FACTORY_DEFAULT)
 
70
{
 
71
    if (!o_Result || !i_pURL)
 
72
        return NS_ERROR_FAILURE;
 
73
    *o_Result = 0;
 
74
    nsCOMPtr<nsIURI> pURL;
 
75
    nsresult result = NS_OK;
 
76
 
 
77
    switch (urlFactory) {
 
78
        case URL_FACTORY_STDURL: {
 
79
            nsIURI* url;
 
80
            result = nsComponentManager::CreateInstance(kStdURLCID, nsnull, 
 
81
                    NS_GET_IID(nsIURI), (void**)&url);
 
82
            if (NS_FAILED(result))
 
83
            {
 
84
                printf("CreateInstance failed\n");
 
85
                return NS_ERROR_FAILURE;
 
86
            }
 
87
            pURL = url;
 
88
            pURL->SetSpec(nsDependentCString(i_pURL));
 
89
            break;
 
90
        }
 
91
        case URL_FACTORY_DEFAULT: {
 
92
            nsCOMPtr<nsIIOService> pService = 
 
93
                     do_GetService(kIOServiceCID, &result);
 
94
            if (NS_FAILED(result)) 
 
95
            {
 
96
                printf("Service failed!\n");
 
97
                return NS_ERROR_FAILURE;
 
98
            }   
 
99
            result = pService->NewURI(nsDependentCString(i_pURL), nsnull, nsnull, getter_AddRefs(pURL));
 
100
        }
 
101
    }
 
102
 
 
103
    nsCString output;
 
104
    if (NS_SUCCEEDED(result))
 
105
    {
 
106
        nsCOMPtr<nsIURL> tURL = do_QueryInterface(pURL);
 
107
        nsCAutoString temp;
 
108
        PRInt32 port;
 
109
        nsresult rv;
 
110
 
 
111
#define RESULT() NS_SUCCEEDED(rv) ? temp.get() : ""
 
112
 
 
113
        rv = tURL->GetScheme(temp);
 
114
        output += RESULT();
 
115
        output += ',';
 
116
        rv = tURL->GetUsername(temp);
 
117
        output += RESULT();
 
118
        output += ',';
 
119
        rv = tURL->GetPassword(temp);
 
120
        output += RESULT();
 
121
        output += ',';
 
122
        rv = tURL->GetHost(temp);
 
123
        output += RESULT();
 
124
        output += ',';
 
125
        rv = tURL->GetPort(&port);
 
126
        output.AppendInt(port);
 
127
        output += ',';
 
128
        rv = tURL->GetDirectory(temp);
 
129
        output += RESULT();
 
130
        output += ',';
 
131
        rv = tURL->GetFileBaseName(temp);
 
132
        output += RESULT();
 
133
        output += ',';
 
134
        rv = tURL->GetFileExtension(temp);
 
135
        output += RESULT();
 
136
        output += ',';
 
137
        rv = tURL->GetParam(temp);
 
138
        output += RESULT();
 
139
        output += ',';
 
140
        rv = tURL->GetQuery(temp);
 
141
        output += RESULT();
 
142
        output += ',';
 
143
        rv = tURL->GetRef(temp);
 
144
        output += RESULT();
 
145
        output += ',';
 
146
        rv = tURL->GetSpec(temp);
 
147
        output += RESULT();
 
148
        *o_Result = ToNewCString(output);
 
149
    } else {
 
150
        output = "Can not create URL";
 
151
        *o_Result = ToNewCString(output);
 
152
    }
 
153
    return NS_OK;
 
154
}
 
155
 
 
156
nsresult writeout(const char* i_pURL, PRInt32 urlFactory = URL_FACTORY_DEFAULT)
 
157
{
 
158
    char* temp = 0;
 
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);
 
162
    delete[] temp;
 
163
    return rv;
 
164
}
 
165
 
 
166
/* construct a url and print out its elements separated by commas and
 
167
   the whole spec */
 
168
nsresult testURL(const char* i_pURL, PRInt32 urlFactory = URL_FACTORY_DEFAULT)
 
169
{
 
170
 
 
171
    if (i_pURL)
 
172
        return writeout(i_pURL, urlFactory);
 
173
 
 
174
    if (!gFileIO)
 
175
        return NS_ERROR_FAILURE;
 
176
 
 
177
    FILE *testfile = fopen(gFileIO, "rt");
 
178
    if (!testfile) 
 
179
    {
 
180
        fprintf(stderr, "Cannot open testfile: %s\n", gFileIO);
 
181
        return NS_ERROR_FAILURE;
 
182
    }
 
183
 
 
184
    char temp[512];
 
185
    int count=0;
 
186
    int failed=0;
 
187
    char* prevResult = nsnull;
 
188
    char* tempurl = nsnull;
 
189
 
 
190
    while (fgets(temp,512,testfile))
 
191
    {
 
192
        if (*temp == '#' || !*temp)
 
193
            continue;
 
194
 
 
195
        if (0 == count%3)
 
196
        {
 
197
            if (prevResult) delete[] prevResult;
 
198
            printf("Testing:  %s\n", temp);
 
199
            writeoutto(temp, &prevResult, urlFactory);
 
200
        }
 
201
        else if (1 == count%3) {
 
202
            if (tempurl) delete[] tempurl;
 
203
            tempurl = nsCRT::strdup(temp);
 
204
        } else { 
 
205
            if (!prevResult)
 
206
                printf("no results to compare to!\n");
 
207
            else 
 
208
            {
 
209
                PRInt32 res;
 
210
                printf("Result:   %s\n", prevResult);
 
211
                if (urlFactory != URL_FACTORY_DEFAULT) {
 
212
                    printf("Expected: %s\n", tempurl);
 
213
                    res = PL_strcmp(tempurl, prevResult);
 
214
                } else {
 
215
                    printf("Expected: %s\n", temp);
 
216
                    res = PL_strcmp(temp, prevResult);
 
217
                }
 
218
 
 
219
                if (res == 0)
 
220
                    printf("\tPASSED\n\n");
 
221
                else 
 
222
                {
 
223
                    printf("\tFAILED\n\n");
 
224
                    failed++;
 
225
                }
 
226
            }
 
227
        }
 
228
        count++;
 
229
    }
 
230
    if (failed>0) {
 
231
        printf("%d tests FAILED out of %d\n", failed, count/3);
 
232
        return NS_ERROR_FAILURE;
 
233
    } else {
 
234
        printf("All %d tests PASSED.\n", count/3);
 
235
        return NS_OK;
 
236
    }
 
237
}
 
238
 
 
239
nsresult makeAbsTest(const char* i_BaseURI, const char* relativePortion,
 
240
                     const char* expectedResult)
 
241
{
 
242
    if (!i_BaseURI)
 
243
        return NS_ERROR_FAILURE;
 
244
 
 
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))
 
250
    {
 
251
        printf("CreateInstance failed\n");
 
252
        return status;
 
253
    }
 
254
    status = baseURL->SetSpec(nsDependentCString(i_BaseURI));
 
255
    if (NS_FAILED(status)) return status;
 
256
 
 
257
 
 
258
    // get the new spec
 
259
    nsCAutoString newURL;
 
260
    status = baseURL->Resolve(nsDependentCString(relativePortion), newURL);
 
261
    if (NS_FAILED(status)) return status;
 
262
 
 
263
    nsCAutoString temp;
 
264
    baseURL->GetSpec(temp);
 
265
 
 
266
    printf("Analyzing %s\n", temp.get());
 
267
    printf("With      %s\n", relativePortion);
 
268
 
 
269
    printf("Got       %s\n", newURL.get());
 
270
    if (expectedResult) {
 
271
        printf("Expect    %s\n", expectedResult);
 
272
        int res = PL_strcmp(newURL.get(), expectedResult);
 
273
        if (res == 0) {
 
274
            printf("\tPASSED\n\n");
 
275
            return NS_OK;
 
276
        } else {
 
277
            printf("\tFAILED\n\n");
 
278
            return NS_ERROR_FAILURE;
 
279
        }
 
280
    }
 
281
    return NS_OK;
 
282
}
 
283
 
 
284
int doMakeAbsTest(const char* i_URL = 0, const char* i_relativePortion=0)
 
285
{
 
286
    if (i_URL && i_relativePortion)
 
287
    {
 
288
        return makeAbsTest(i_URL, i_relativePortion, nsnull);
 
289
    }
 
290
 
 
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.
 
294
 
 
295
    /* Section C.1.  Normal Examples
 
296
 
 
297
      g:h        = <URL:g:h>
 
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>
 
302
      //g        = <URL:http://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>
 
321
    */
 
322
 
 
323
    struct test {
 
324
        const char* baseURL;
 
325
        const char* relativeURL;
 
326
        const char* expectedResult;
 
327
    };
 
328
 
 
329
    test tests[] = {
 
330
        // Tests from rfc2396, section C.1 with the exception of the
 
331
        // handling of ?y
 
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" },
 
356
 
 
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" },
 
360
 
 
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" },
 
376
 
 
377
    };
 
378
 
 
379
    const int numTests = sizeof(tests) / sizeof(tests[0]);
 
380
    int failed = 0;
 
381
    nsresult rv;
 
382
    for (int i = 0 ; i<numTests ; ++i)
 
383
    {
 
384
        rv = makeAbsTest(tests[i].baseURL, tests[i].relativeURL,
 
385
                         tests[i].expectedResult);
 
386
        if (NS_FAILED(rv))
 
387
            failed++;
 
388
    }
 
389
    if (failed>0) {
 
390
        printf("%d tests FAILED out of %d\n", failed, numTests);
 
391
        return NS_ERROR_FAILURE;
 
392
    } else {
 
393
        printf("All %d tests PASSED.\n", numTests);
 
394
        return NS_OK;
 
395
    }
 
396
}
 
397
 
 
398
void printusage(void)
 
399
{
 
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");
 
409
}
 
410
 
 
411
int main(int argc, char **argv)
 
412
{
 
413
    int rv = -1;
 
414
 
 
415
    if (argc < 2) {
 
416
        printusage();
 
417
        return NS_OK;
 
418
    }
 
419
    {
 
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");
 
424
        if (registrar)
 
425
            registrar->AutoRegister(nsnull);
 
426
 
 
427
        // end of all messages from register components...
 
428
        printf("------------------\n\n");
 
429
 
 
430
        PRInt32 urlFactory = URL_FACTORY_DEFAULT;
 
431
        PRBool bMakeAbs= PR_FALSE;
 
432
        char* relativePath = 0;
 
433
        char* url = 0;
 
434
        for (int i=1; i<argc; i++) {
 
435
            if (PL_strcasecmp(argv[i], "-std") == 0)
 
436
            {
 
437
                urlFactory = URL_FACTORY_STDURL;
 
438
                if (i+1 >= argc)
 
439
                {
 
440
                    printusage();
 
441
                    return NS_OK;
 
442
                }
 
443
            }
 
444
            else if (PL_strcasecmp(argv[i], "-abs") == 0)
 
445
            {
 
446
                if (!gFileIO)
 
447
                {
 
448
                    relativePath = argv[i+1];
 
449
                    i++;
 
450
                }
 
451
                bMakeAbs = PR_TRUE;
 
452
            }
 
453
            else if (PL_strcasecmp(argv[i], "-file") == 0)
 
454
            {
 
455
                if (i+1 >= argc)
 
456
                {
 
457
                    printusage();
 
458
                    return NS_OK;
 
459
                }
 
460
                gFileIO = argv[i+1];
 
461
                i++;
 
462
            }
 
463
            else
 
464
            {
 
465
                url = argv[i];
 
466
            }
 
467
        }
 
468
        PRTime startTime = PR_Now();
 
469
        if (bMakeAbs)
 
470
        {
 
471
            rv = (url && relativePath)
 
472
               ? doMakeAbsTest(url, relativePath)
 
473
               : doMakeAbsTest();
 
474
        }
 
475
        else
 
476
        {
 
477
            rv = gFileIO ? testURL(0, urlFactory) : testURL(url, urlFactory);
 
478
        }
 
479
        if (gFileIO)
 
480
        {
 
481
            PRTime endTime = PR_Now();
 
482
            printf("Elapsed time: %d micros.\n", (PRInt32)
 
483
                (endTime - startTime));
 
484
        }
 
485
    } // this scopes the nsCOMPtrs
 
486
    // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
 
487
    rv = NS_ShutdownXPCOM(nsnull);
 
488
    return rv;
 
489
}