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

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

mercurial