netwerk/test/PropertiesTest.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 "TestCommon.h"
     7 #include "nsXPCOM.h"
     8 #include "nsStringAPI.h"
     9 #include "nsIPersistentProperties2.h"
    10 #include "nsIServiceManager.h"
    11 #include "nsIComponentRegistrar.h"
    12 #include "nsIURL.h"
    13 #include "nsIIOService.h"
    14 #include "nsNetCID.h"
    15 #include "nsIChannel.h"
    16 #include "nsIComponentManager.h"
    17 #include <stdio.h>
    18 #include "nsComponentManagerUtils.h"
    19 #include "nsServiceManagerUtils.h"
    20 #include "nsISimpleEnumerator.h"
    22 #define TEST_URL "resource:/res/test.properties"
    23 static NS_DEFINE_CID(kPersistentPropertiesCID, NS_IPERSISTENTPROPERTIES_CID);
    25 static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
    27 /***************************************************************************/
    29 int
    30 main(int argc, char* argv[])
    31 {
    32   if (test_common_init(&argc, &argv) != 0)
    33     return -1;
    35   nsresult ret;
    37   nsCOMPtr<nsIServiceManager> servMan;
    38   NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
    40   nsIInputStream* in = nullptr;
    42   nsCOMPtr<nsIIOService> service(do_GetService(kIOServiceCID, &ret));
    43   if (NS_FAILED(ret)) return 1;
    45   nsIChannel *channel = nullptr;
    46   ret = service->NewChannel(NS_LITERAL_CSTRING(TEST_URL), nullptr, nullptr, &channel);
    47   if (NS_FAILED(ret)) return 1;
    49   ret = channel->Open(&in);
    50   if (NS_FAILED(ret)) return 1;
    52   nsIPersistentProperties* props;
    53   ret = CallCreateInstance(kPersistentPropertiesCID, &props);
    54   if (NS_FAILED(ret) || (!props)) {
    55     printf("create nsIPersistentProperties failed\n");
    56     return 1;
    57   }
    58   ret = props->Load(in);
    59   if (NS_FAILED(ret)) {
    60     printf("cannot load properties\n");
    61     return 1;
    62   }
    63   int i = 1;
    64   while (1) {
    65     char name[16];
    66     name[0] = 0;
    67     sprintf(name, "%d", i);
    68     nsAutoString v;
    69     ret = props->GetStringProperty(nsDependentCString(name), v);
    70     if (NS_FAILED(ret) || (!v.Length())) {
    71       break;
    72     }
    73     printf("\"%d\"=\"%s\"\n", i, NS_ConvertUTF16toUTF8(v).get());
    74     i++;
    75   }
    77   nsCOMPtr<nsISimpleEnumerator> propEnum;
    78   ret = props->Enumerate(getter_AddRefs(propEnum));
    80   if (NS_FAILED(ret)) {
    81     printf("cannot enumerate properties\n");
    82     return 1;
    83   }
    86   printf("\nKey\tValue\n");
    87   printf(  "---\t-----\n");
    89   bool hasMore;
    90   while (NS_SUCCEEDED(propEnum->HasMoreElements(&hasMore)) &&
    91          hasMore) {
    92     nsCOMPtr<nsISupports> sup;
    93     ret = propEnum->GetNext(getter_AddRefs(sup));
    95     nsCOMPtr<nsIPropertyElement> propElem = do_QueryInterface(sup, &ret);
    96 	  if (NS_FAILED(ret)) {
    97       printf("failed to get current item\n");
    98       return 1;
    99 	  }
   101     nsAutoCString key;
   102     nsAutoString value;
   104     ret = propElem->GetKey(key);
   105 	  if (NS_FAILED(ret)) {
   106 		  printf("failed to get current element's key\n");
   107 		  return 1;
   108 	  }
   109     ret = propElem->GetValue(value);
   110 	  if (NS_FAILED(ret)) {
   111 		  printf("failed to get current element's value\n");
   112 		  return 1;
   113 	  }
   115     printf("%s\t%s\n", key.get(), NS_ConvertUTF16toUTF8(value).get());
   116   }
   117   return 0;
   118 }

mercurial