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/. */
5 
6 #include "TestCommon.h"
7 #include "mozilla/Sprintf.h"
8 #include "nsXPCOM.h"
9 #include "nsStringAPI.h"
10 #include "nsIPersistentProperties2.h"
11 #include "nsIServiceManager.h"
12 #include "nsIComponentRegistrar.h"
13 #include "nsIURL.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"
21 #include "nsIScriptSecurityManager.h"
22 #include "nsILoadInfo.h"
23 #include "nsNetUtil.h"
24 
25 #define TEST_URL "resource:/res/test.properties"
26 static NS_DEFINE_CID(kPersistentPropertiesCID, NS_IPERSISTENTPROPERTIES_CID);
27 
28 /***************************************************************************/
29 
30 int
main(int argc,char * argv[])31 main(int argc, char* argv[])
32 {
33   if (test_common_init(&argc, &argv) != 0)
34     return -1;
35 
36   nsresult ret;
37 
38   nsCOMPtr<nsIServiceManager> servMan;
39   NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
40 
41   nsIInputStream* in = nullptr;
42 
43   nsCOMPtr<nsIScriptSecurityManager> secman =
44     do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &ret);
45   if (NS_FAILED(ret)) return 1;
46   nsCOMPtr<nsIPrincipal> systemPrincipal;
47     ret = secman->GetSystemPrincipal(getter_AddRefs(systemPrincipal));
48    if (NS_FAILED(ret)) return 1;
49 
50   nsCOMPtr<nsIURI> uri;
51   ret = NS_NewURI(getter_AddRefs(uri), NS_LITERAL_CSTRING(TEST_URL));
52   if (NS_FAILED(ret)) return 1;
53 
54   nsIChannel *channel = nullptr;
55   ret = NS_NewChannel(&channel,
56                       uri,
57                       systemPrincipal,
58                       nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
59                       nsIContentPolicy::TYPE_OTHER);
60   if (NS_FAILED(ret)) return 1;
61 
62   ret = channel->Open2(&in);
63   if (NS_FAILED(ret)) return 1;
64 
65   nsIPersistentProperties* props;
66   ret = CallCreateInstance(kPersistentPropertiesCID, &props);
67   if (NS_FAILED(ret) || (!props)) {
68     printf("create nsIPersistentProperties failed\n");
69     return 1;
70   }
71   ret = props->Load(in);
72   if (NS_FAILED(ret)) {
73     printf("cannot load properties\n");
74     return 1;
75   }
76   int i = 1;
77   while (true) {
78     char name[16];
79     name[0] = 0;
80     SprintfLiteral(name, "%d", i);
81     nsAutoString v;
82     ret = props->GetStringProperty(nsDependentCString(name), v);
83     if (NS_FAILED(ret) || (!v.Length())) {
84       break;
85     }
86     printf("\"%d\"=\"%s\"\n", i, NS_ConvertUTF16toUTF8(v).get());
87     i++;
88   }
89 
90   nsCOMPtr<nsISimpleEnumerator> propEnum;
91   ret = props->Enumerate(getter_AddRefs(propEnum));
92 
93   if (NS_FAILED(ret)) {
94     printf("cannot enumerate properties\n");
95     return 1;
96   }
97 
98 
99   printf("\nKey\tValue\n");
100   printf(  "---\t-----\n");
101 
102   bool hasMore;
103   while (NS_SUCCEEDED(propEnum->HasMoreElements(&hasMore)) &&
104          hasMore) {
105     nsCOMPtr<nsISupports> sup;
106     ret = propEnum->GetNext(getter_AddRefs(sup));
107 
108     nsCOMPtr<nsIPropertyElement> propElem = do_QueryInterface(sup, &ret);
109 	  if (NS_FAILED(ret)) {
110       printf("failed to get current item\n");
111       return 1;
112 	  }
113 
114     nsAutoCString key;
115     nsAutoString value;
116 
117     ret = propElem->GetKey(key);
118 	  if (NS_FAILED(ret)) {
119 		  printf("failed to get current element's key\n");
120 		  return 1;
121 	  }
122     ret = propElem->GetValue(value);
123 	  if (NS_FAILED(ret)) {
124 		  printf("failed to get current element's value\n");
125 		  return 1;
126 	  }
127 
128     printf("%s\t%s\n", key.get(), NS_ConvertUTF16toUTF8(value).get());
129   }
130   return 0;
131 }
132