netwerk/test/TestMakeAbs.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     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 #include "nspr.h"
     7 #include "nscore.h"
     8 #include "nsCOMPtr.h"
     9 #include "nsIIOService.h"
    10 #include "nsIServiceManager.h"
    11 #include "nsIComponentRegistrar.h"
    12 #include "nsIURI.h"
    14 static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
    16 nsresult ServiceMakeAbsolute(nsIURI *baseURI, char *relativeInfo, char **_retval) {
    17     nsresult rv;
    18     nsCOMPtr<nsIIOService> serv(do_GetService(kIOServiceCID, &rv));
    19     if (NS_FAILED(rv)) return rv;
    21     return serv->MakeAbsolute(relativeInfo, baseURI, _retval);
    22 }
    24 nsresult URLMakeAbsolute(nsIURI *baseURI, char *relativeInfo, char **_retval) {
    25     return baseURI->MakeAbsolute(relativeInfo, _retval);
    26 }
    28 int
    29 main(int argc, char* argv[])
    30 {
    31     nsresult rv = NS_OK;
    32     if (argc < 4) {
    33         printf("usage: %s int (loop count) baseURL relativeSpec\n", argv[0]);
    34         return -1;
    35     }
    37     uint32_t cycles = atoi(argv[1]);
    38     char *base = argv[2];
    39     char *rel  = argv[3];
    40     {
    41         nsCOMPtr<nsIServiceManager> servMan;
    42         NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
    43         nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
    44         NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
    45         if (registrar)
    46             registrar->AutoRegister(nullptr);
    48         nsCOMPtr<nsIIOService> serv(do_GetService(kIOServiceCID, &rv));
    49         if (NS_FAILED(rv)) return rv;
    51         nsCOMPtr<nsIURI> uri;
    52         rv = serv->NewURI(base, nullptr, getter_AddRefs(uri));
    53         if (NS_FAILED(rv)) return rv;
    55         char *absURLString;
    56         uint32_t i = 0;
    57         while (i++ < cycles) {
    58             rv = ServiceMakeAbsolute(uri, rel, &absURLString);
    59             if (NS_FAILED(rv)) return rv;
    60             nsMemory::Free(absURLString);
    62             rv = URLMakeAbsolute(uri, rel, &absURLString);
    63             nsMemory::Free(absURLString);
    64         }
    65     } // this scopes the nsCOMPtrs
    66     // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
    67     NS_ShutdownXPCOM(nullptr);
    68     return rv;
    69 }

mercurial