michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "TestHarness.h" michael@0: #include "nsIFile.h" michael@0: #include "nsIDirectoryService.h" michael@0: #include "nsDirectoryServiceDefs.h" michael@0: #include "nsCOMArray.h" michael@0: #include "nsArrayEnumerator.h" michael@0: #include "nsXULAppAPI.h" michael@0: #include "nsIComponentRegistrar.h" michael@0: michael@0: #define SERVICE_A_CONTRACT_ID "@mozilla.org/RegTestServiceA;1" michael@0: #define SERVICE_B_CONTRACT_ID "@mozilla.org/RegTestServiceB;1" michael@0: michael@0: // {56ab1cd4-ac44-4f86-8104-171f8b8f2fc7} michael@0: #define CORE_SERVICE_A_CID \ michael@0: { 0x56ab1cd4, 0xac44, 0x4f86, \ michael@0: { 0x81, 0x04, 0x17, 0x1f, 0x8b, 0x8f, 0x2f, 0xc7} } michael@0: NS_DEFINE_CID(kCoreServiceA_CID, CORE_SERVICE_A_CID); michael@0: michael@0: // {fe64efb7-c5ab-41a6-b639-e6c0f483181e} michael@0: #define EXT_SERVICE_A_CID \ michael@0: { 0xfe64efb7, 0xc5ab, 0x41a6, \ michael@0: { 0xb6, 0x39, 0xe6, 0xc0, 0xf4, 0x83, 0x18, 0x1e} } michael@0: NS_DEFINE_CID(kExtServiceA_CID, EXT_SERVICE_A_CID); michael@0: michael@0: // {d04d1298-6dac-459b-a13b-bcab235730a0} michael@0: #define CORE_SERVICE_B_CID \ michael@0: { 0xd04d1298, 0x6dac, 0x459b, \ michael@0: { 0xa1, 0x3b, 0xbc, 0xab, 0x23, 0x57, 0x30, 0xa0 } } michael@0: NS_DEFINE_CID(kCoreServiceB_CID, CORE_SERVICE_B_CID); michael@0: michael@0: // {e93dadeb-a6f6-4667-bbbc-ac8c6d440b20} michael@0: #define EXT_SERVICE_B_CID \ michael@0: { 0xe93dadeb, 0xa6f6, 0x4667, \ michael@0: { 0xbb, 0xbc, 0xac, 0x8c, 0x6d, 0x44, 0x0b, 0x20 } } michael@0: NS_DEFINE_CID(kExtServiceB_CID, EXT_SERVICE_B_CID); michael@0: michael@0: nsresult execRegOrderTest(const char *aTestName, const char *aContractID, michael@0: const nsCID &aCoreCID, const nsCID &aExtCID) michael@0: { michael@0: // Make sure the core service loaded (it won't be found using contract ID). michael@0: nsresult rv = NS_ERROR_FAILURE; michael@0: nsCOMPtr coreService = do_CreateInstance(aCoreCID, &rv); michael@0: #ifdef DEBUG_brade michael@0: if (rv) fprintf(stderr, "rv: %d (%x)\n", rv, rv); michael@0: fprintf(stderr, "coreService: %p\n", coreService.get()); michael@0: #endif michael@0: if (NS_FAILED(rv)) michael@0: { michael@0: fail("%s FAILED - cannot create core service\n", aTestName); michael@0: return rv; michael@0: } michael@0: michael@0: // Get the extension service. michael@0: nsCOMPtr extService = do_CreateInstance(aExtCID, &rv); michael@0: #ifdef DEBUG_brade michael@0: if (rv) fprintf(stderr, "rv: %d (%x)\n", rv, rv); michael@0: fprintf(stderr, "extService: %p\n", extService.get()); michael@0: #endif michael@0: if (NS_FAILED(rv)) michael@0: { michael@0: fail("%s FAILED - cannot create extension service\n", aTestName); michael@0: return rv; michael@0: } michael@0: michael@0: /* michael@0: * Get the service by contract ID and make sure it is the extension michael@0: * service (it should be, since the extension directory was registered michael@0: * after the core one). michael@0: */ michael@0: nsCOMPtr service = do_CreateInstance(aContractID, &rv); michael@0: #ifdef DEBUG_brade michael@0: if (rv) fprintf(stderr, "rv: %d (%x)\n", rv, rv); michael@0: fprintf(stderr, "service: %p\n", service.get()); michael@0: #endif michael@0: if (NS_FAILED(rv)) michael@0: { michael@0: fail("%s FAILED - cannot create service\n", aTestName); michael@0: return rv; michael@0: } michael@0: michael@0: if (service != extService) michael@0: { michael@0: fail("%s FAILED - wrong service registered\n", aTestName); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: passed(aTestName); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult TestRegular() michael@0: { michael@0: return execRegOrderTest("TestRegular", SERVICE_A_CONTRACT_ID, michael@0: kCoreServiceA_CID, kExtServiceA_CID); michael@0: } michael@0: michael@0: nsresult TestJar() michael@0: { michael@0: return execRegOrderTest("TestJar", SERVICE_B_CONTRACT_ID, michael@0: kCoreServiceB_CID, kExtServiceB_CID); michael@0: } michael@0: michael@0: bool TestContractFirst() michael@0: { michael@0: nsCOMPtr r; michael@0: NS_GetComponentRegistrar(getter_AddRefs(r)); michael@0: michael@0: nsCID* cid = nullptr; michael@0: nsresult rv = r->ContractIDToCID("@mozilla.org/RegTestOrderC;1", &cid); michael@0: if (NS_FAILED(rv)) { michael@0: fail("RegTestOrderC: contract not registered"); michael@0: return false; michael@0: } michael@0: michael@0: nsCID goodcid; michael@0: goodcid.Parse("{ada15884-bb89-473c-8b50-dcfbb8447ff4}"); michael@0: michael@0: if (!goodcid.Equals(*cid)) { michael@0: fail("RegTestOrderC: CID doesn't match"); michael@0: return false; michael@0: } michael@0: michael@0: passed("RegTestOrderC"); michael@0: return true; michael@0: } michael@0: michael@0: static already_AddRefed michael@0: GetRegDirectory(const char* basename, const char* dirname, const char* leafname) michael@0: { michael@0: nsCOMPtr f; michael@0: nsresult rv = NS_NewNativeLocalFile(nsDependentCString(basename), true, michael@0: getter_AddRefs(f)); michael@0: if (NS_FAILED(rv)) michael@0: return nullptr; michael@0: michael@0: f->AppendNative(nsDependentCString(dirname)); michael@0: if (leafname) michael@0: f->AppendNative(nsDependentCString(leafname)); michael@0: return f.forget(); michael@0: } michael@0: michael@0: michael@0: michael@0: int main(int argc, char** argv) michael@0: { michael@0: if (argc < 2) michael@0: { michael@0: fprintf(stderr, "not enough arguments -- need registration dir path\n"); michael@0: return 1; michael@0: } michael@0: michael@0: ScopedLogging logging; michael@0: michael@0: #ifdef XP_WIN michael@0: // On Windows, convert to backslashes michael@0: size_t regPathLen = strlen(argv[1]); michael@0: char* regPath = new char[regPathLen + 1]; michael@0: for (size_t i = 0; i < regPathLen; i++) { michael@0: char curr = argv[1][i]; michael@0: regPath[i] = (curr == '/') ? '\\' : curr; michael@0: } michael@0: regPath[regPathLen] = '\0'; michael@0: #else michael@0: const char *regPath = argv[1]; michael@0: #endif michael@0: michael@0: XRE_AddManifestLocation(NS_COMPONENT_LOCATION, michael@0: nsCOMPtr(GetRegDirectory(regPath, "core", "component.manifest"))); michael@0: XRE_AddManifestLocation(NS_COMPONENT_LOCATION, michael@0: nsCOMPtr(GetRegDirectory(regPath, "extension", "extComponent.manifest"))); michael@0: XRE_AddJarManifestLocation(NS_COMPONENT_LOCATION, michael@0: nsCOMPtr(GetRegDirectory(regPath, "extension2.jar", nullptr))); michael@0: ScopedXPCOM xpcom("RegistrationOrder"); michael@0: if (xpcom.failed()) michael@0: return 1; michael@0: michael@0: int rv = 0; michael@0: if (NS_FAILED(TestRegular())) michael@0: rv = 1; michael@0: michael@0: if (NS_FAILED(TestJar())) michael@0: rv = 1; michael@0: michael@0: if (!TestContractFirst()) michael@0: rv = 1; michael@0: michael@0: return rv; michael@0: }