xpcom/tests/TestRegistrationOrder.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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

mercurial