netwerk/test/PropertiesTest.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

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "TestCommon.h"
michael@0 7 #include "nsXPCOM.h"
michael@0 8 #include "nsStringAPI.h"
michael@0 9 #include "nsIPersistentProperties2.h"
michael@0 10 #include "nsIServiceManager.h"
michael@0 11 #include "nsIComponentRegistrar.h"
michael@0 12 #include "nsIURL.h"
michael@0 13 #include "nsIIOService.h"
michael@0 14 #include "nsNetCID.h"
michael@0 15 #include "nsIChannel.h"
michael@0 16 #include "nsIComponentManager.h"
michael@0 17 #include <stdio.h>
michael@0 18 #include "nsComponentManagerUtils.h"
michael@0 19 #include "nsServiceManagerUtils.h"
michael@0 20 #include "nsISimpleEnumerator.h"
michael@0 21
michael@0 22 #define TEST_URL "resource:/res/test.properties"
michael@0 23 static NS_DEFINE_CID(kPersistentPropertiesCID, NS_IPERSISTENTPROPERTIES_CID);
michael@0 24
michael@0 25 static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
michael@0 26
michael@0 27 /***************************************************************************/
michael@0 28
michael@0 29 int
michael@0 30 main(int argc, char* argv[])
michael@0 31 {
michael@0 32 if (test_common_init(&argc, &argv) != 0)
michael@0 33 return -1;
michael@0 34
michael@0 35 nsresult ret;
michael@0 36
michael@0 37 nsCOMPtr<nsIServiceManager> servMan;
michael@0 38 NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
michael@0 39
michael@0 40 nsIInputStream* in = nullptr;
michael@0 41
michael@0 42 nsCOMPtr<nsIIOService> service(do_GetService(kIOServiceCID, &ret));
michael@0 43 if (NS_FAILED(ret)) return 1;
michael@0 44
michael@0 45 nsIChannel *channel = nullptr;
michael@0 46 ret = service->NewChannel(NS_LITERAL_CSTRING(TEST_URL), nullptr, nullptr, &channel);
michael@0 47 if (NS_FAILED(ret)) return 1;
michael@0 48
michael@0 49 ret = channel->Open(&in);
michael@0 50 if (NS_FAILED(ret)) return 1;
michael@0 51
michael@0 52 nsIPersistentProperties* props;
michael@0 53 ret = CallCreateInstance(kPersistentPropertiesCID, &props);
michael@0 54 if (NS_FAILED(ret) || (!props)) {
michael@0 55 printf("create nsIPersistentProperties failed\n");
michael@0 56 return 1;
michael@0 57 }
michael@0 58 ret = props->Load(in);
michael@0 59 if (NS_FAILED(ret)) {
michael@0 60 printf("cannot load properties\n");
michael@0 61 return 1;
michael@0 62 }
michael@0 63 int i = 1;
michael@0 64 while (1) {
michael@0 65 char name[16];
michael@0 66 name[0] = 0;
michael@0 67 sprintf(name, "%d", i);
michael@0 68 nsAutoString v;
michael@0 69 ret = props->GetStringProperty(nsDependentCString(name), v);
michael@0 70 if (NS_FAILED(ret) || (!v.Length())) {
michael@0 71 break;
michael@0 72 }
michael@0 73 printf("\"%d\"=\"%s\"\n", i, NS_ConvertUTF16toUTF8(v).get());
michael@0 74 i++;
michael@0 75 }
michael@0 76
michael@0 77 nsCOMPtr<nsISimpleEnumerator> propEnum;
michael@0 78 ret = props->Enumerate(getter_AddRefs(propEnum));
michael@0 79
michael@0 80 if (NS_FAILED(ret)) {
michael@0 81 printf("cannot enumerate properties\n");
michael@0 82 return 1;
michael@0 83 }
michael@0 84
michael@0 85
michael@0 86 printf("\nKey\tValue\n");
michael@0 87 printf( "---\t-----\n");
michael@0 88
michael@0 89 bool hasMore;
michael@0 90 while (NS_SUCCEEDED(propEnum->HasMoreElements(&hasMore)) &&
michael@0 91 hasMore) {
michael@0 92 nsCOMPtr<nsISupports> sup;
michael@0 93 ret = propEnum->GetNext(getter_AddRefs(sup));
michael@0 94
michael@0 95 nsCOMPtr<nsIPropertyElement> propElem = do_QueryInterface(sup, &ret);
michael@0 96 if (NS_FAILED(ret)) {
michael@0 97 printf("failed to get current item\n");
michael@0 98 return 1;
michael@0 99 }
michael@0 100
michael@0 101 nsAutoCString key;
michael@0 102 nsAutoString value;
michael@0 103
michael@0 104 ret = propElem->GetKey(key);
michael@0 105 if (NS_FAILED(ret)) {
michael@0 106 printf("failed to get current element's key\n");
michael@0 107 return 1;
michael@0 108 }
michael@0 109 ret = propElem->GetValue(value);
michael@0 110 if (NS_FAILED(ret)) {
michael@0 111 printf("failed to get current element's value\n");
michael@0 112 return 1;
michael@0 113 }
michael@0 114
michael@0 115 printf("%s\t%s\n", key.get(), NS_ConvertUTF16toUTF8(value).get());
michael@0 116 }
michael@0 117 return 0;
michael@0 118 }

mercurial