xpcom/tests/TestRegistrationOrder.cpp

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:1ee467048ae4
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "TestHarness.h"
8 #include "nsIFile.h"
9 #include "nsIDirectoryService.h"
10 #include "nsDirectoryServiceDefs.h"
11 #include "nsCOMArray.h"
12 #include "nsArrayEnumerator.h"
13 #include "nsXULAppAPI.h"
14 #include "nsIComponentRegistrar.h"
15
16 #define SERVICE_A_CONTRACT_ID "@mozilla.org/RegTestServiceA;1"
17 #define SERVICE_B_CONTRACT_ID "@mozilla.org/RegTestServiceB;1"
18
19 // {56ab1cd4-ac44-4f86-8104-171f8b8f2fc7}
20 #define CORE_SERVICE_A_CID \
21 { 0x56ab1cd4, 0xac44, 0x4f86, \
22 { 0x81, 0x04, 0x17, 0x1f, 0x8b, 0x8f, 0x2f, 0xc7} }
23 NS_DEFINE_CID(kCoreServiceA_CID, CORE_SERVICE_A_CID);
24
25 // {fe64efb7-c5ab-41a6-b639-e6c0f483181e}
26 #define EXT_SERVICE_A_CID \
27 { 0xfe64efb7, 0xc5ab, 0x41a6, \
28 { 0xb6, 0x39, 0xe6, 0xc0, 0xf4, 0x83, 0x18, 0x1e} }
29 NS_DEFINE_CID(kExtServiceA_CID, EXT_SERVICE_A_CID);
30
31 // {d04d1298-6dac-459b-a13b-bcab235730a0}
32 #define CORE_SERVICE_B_CID \
33 { 0xd04d1298, 0x6dac, 0x459b, \
34 { 0xa1, 0x3b, 0xbc, 0xab, 0x23, 0x57, 0x30, 0xa0 } }
35 NS_DEFINE_CID(kCoreServiceB_CID, CORE_SERVICE_B_CID);
36
37 // {e93dadeb-a6f6-4667-bbbc-ac8c6d440b20}
38 #define EXT_SERVICE_B_CID \
39 { 0xe93dadeb, 0xa6f6, 0x4667, \
40 { 0xbb, 0xbc, 0xac, 0x8c, 0x6d, 0x44, 0x0b, 0x20 } }
41 NS_DEFINE_CID(kExtServiceB_CID, EXT_SERVICE_B_CID);
42
43 nsresult execRegOrderTest(const char *aTestName, const char *aContractID,
44 const nsCID &aCoreCID, const nsCID &aExtCID)
45 {
46 // Make sure the core service loaded (it won't be found using contract ID).
47 nsresult rv = NS_ERROR_FAILURE;
48 nsCOMPtr<nsISupports> coreService = do_CreateInstance(aCoreCID, &rv);
49 #ifdef DEBUG_brade
50 if (rv) fprintf(stderr, "rv: %d (%x)\n", rv, rv);
51 fprintf(stderr, "coreService: %p\n", coreService.get());
52 #endif
53 if (NS_FAILED(rv))
54 {
55 fail("%s FAILED - cannot create core service\n", aTestName);
56 return rv;
57 }
58
59 // Get the extension service.
60 nsCOMPtr<nsISupports> extService = do_CreateInstance(aExtCID, &rv);
61 #ifdef DEBUG_brade
62 if (rv) fprintf(stderr, "rv: %d (%x)\n", rv, rv);
63 fprintf(stderr, "extService: %p\n", extService.get());
64 #endif
65 if (NS_FAILED(rv))
66 {
67 fail("%s FAILED - cannot create extension service\n", aTestName);
68 return rv;
69 }
70
71 /*
72 * Get the service by contract ID and make sure it is the extension
73 * service (it should be, since the extension directory was registered
74 * after the core one).
75 */
76 nsCOMPtr<nsISupports> service = do_CreateInstance(aContractID, &rv);
77 #ifdef DEBUG_brade
78 if (rv) fprintf(stderr, "rv: %d (%x)\n", rv, rv);
79 fprintf(stderr, "service: %p\n", service.get());
80 #endif
81 if (NS_FAILED(rv))
82 {
83 fail("%s FAILED - cannot create service\n", aTestName);
84 return rv;
85 }
86
87 if (service != extService)
88 {
89 fail("%s FAILED - wrong service registered\n", aTestName);
90 return NS_ERROR_FAILURE;
91 }
92
93 passed(aTestName);
94 return NS_OK;
95 }
96
97 nsresult TestRegular()
98 {
99 return execRegOrderTest("TestRegular", SERVICE_A_CONTRACT_ID,
100 kCoreServiceA_CID, kExtServiceA_CID);
101 }
102
103 nsresult TestJar()
104 {
105 return execRegOrderTest("TestJar", SERVICE_B_CONTRACT_ID,
106 kCoreServiceB_CID, kExtServiceB_CID);
107 }
108
109 bool TestContractFirst()
110 {
111 nsCOMPtr<nsIComponentRegistrar> r;
112 NS_GetComponentRegistrar(getter_AddRefs(r));
113
114 nsCID* cid = nullptr;
115 nsresult rv = r->ContractIDToCID("@mozilla.org/RegTestOrderC;1", &cid);
116 if (NS_FAILED(rv)) {
117 fail("RegTestOrderC: contract not registered");
118 return false;
119 }
120
121 nsCID goodcid;
122 goodcid.Parse("{ada15884-bb89-473c-8b50-dcfbb8447ff4}");
123
124 if (!goodcid.Equals(*cid)) {
125 fail("RegTestOrderC: CID doesn't match");
126 return false;
127 }
128
129 passed("RegTestOrderC");
130 return true;
131 }
132
133 static already_AddRefed<nsIFile>
134 GetRegDirectory(const char* basename, const char* dirname, const char* leafname)
135 {
136 nsCOMPtr<nsIFile> f;
137 nsresult rv = NS_NewNativeLocalFile(nsDependentCString(basename), true,
138 getter_AddRefs(f));
139 if (NS_FAILED(rv))
140 return nullptr;
141
142 f->AppendNative(nsDependentCString(dirname));
143 if (leafname)
144 f->AppendNative(nsDependentCString(leafname));
145 return f.forget();
146 }
147
148
149
150 int main(int argc, char** argv)
151 {
152 if (argc < 2)
153 {
154 fprintf(stderr, "not enough arguments -- need registration dir path\n");
155 return 1;
156 }
157
158 ScopedLogging logging;
159
160 #ifdef XP_WIN
161 // On Windows, convert to backslashes
162 size_t regPathLen = strlen(argv[1]);
163 char* regPath = new char[regPathLen + 1];
164 for (size_t i = 0; i < regPathLen; i++) {
165 char curr = argv[1][i];
166 regPath[i] = (curr == '/') ? '\\' : curr;
167 }
168 regPath[regPathLen] = '\0';
169 #else
170 const char *regPath = argv[1];
171 #endif
172
173 XRE_AddManifestLocation(NS_COMPONENT_LOCATION,
174 nsCOMPtr<nsIFile>(GetRegDirectory(regPath, "core", "component.manifest")));
175 XRE_AddManifestLocation(NS_COMPONENT_LOCATION,
176 nsCOMPtr<nsIFile>(GetRegDirectory(regPath, "extension", "extComponent.manifest")));
177 XRE_AddJarManifestLocation(NS_COMPONENT_LOCATION,
178 nsCOMPtr<nsIFile>(GetRegDirectory(regPath, "extension2.jar", nullptr)));
179 ScopedXPCOM xpcom("RegistrationOrder");
180 if (xpcom.failed())
181 return 1;
182
183 int rv = 0;
184 if (NS_FAILED(TestRegular()))
185 rv = 1;
186
187 if (NS_FAILED(TestJar()))
188 rv = 1;
189
190 if (!TestContractFirst())
191 rv = 1;
192
193 return rv;
194 }

mercurial