diff --git a/browser/components/extensions/parent/ext-pkcs11.js b/browser/components/extensions/parent/ext-pkcs11.js index 279cb2839641..334adaa2c3ed 100644 --- a/browser/components/extensions/parent/ext-pkcs11.js +++ b/browser/components/extensions/parent/ext-pkcs11.js @@ -7,6 +7,7 @@ XPCOMUtils.defineLazyModuleGetters(this, { ctypes: "resource://gre/modules/ctypes.jsm", NativeManifests: "resource://gre/modules/NativeManifests.jsm", + OS: "resource://gre/modules/osfile.jsm", }); XPCOMUtils.defineLazyServiceGetter( @@ -37,12 +38,12 @@ this.pkcs11 = class extends ExtensionAPI { ); if (hostInfo) { if (AppConstants.platform === "win") { - hostInfo.manifest.path = PathUtils.join( - PathUtils.parent(hostInfo.path), + hostInfo.manifest.path = OS.Path.join( + OS.Path.dirname(hostInfo.path), hostInfo.manifest.path ); } - let manifestLib = PathUtils.filename(hostInfo.manifest.path); + let manifestLib = OS.Path.basename(hostInfo.manifest.path); if (AppConstants.platform !== "linux") { manifestLib = manifestLib.toLowerCase(manifestLib); } diff --git a/browser/components/extensions/test/xpcshell/test_ext_pkcs11_management.js b/browser/components/extensions/test/xpcshell/test_ext_pkcs11_management.js index b94fce7aa6b9..6463c5964870 100644 --- a/browser/components/extensions/test/xpcshell/test_ext_pkcs11_management.js +++ b/browser/components/extensions/test/xpcshell/test_ext_pkcs11_management.js @@ -3,39 +3,28 @@ XPCOMUtils.defineLazyModuleGetters(this, { ctypes: "resource://gre/modules/ctypes.jsm", MockRegistry: "resource://testing-common/MockRegistry.jsm", + OS: "resource://gre/modules/osfile.jsm", }); do_get_profile(); - -let tmpDir; -let baseDir; +let tmpDir = FileUtils.getDir("TmpD", ["PKCS11"]); let slug = AppConstants.platform === "linux" ? "pkcs11-modules" : "PKCS11Modules"; +tmpDir.createUnique(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY); +let baseDir = OS.Path.join(tmpDir.path, slug); +OS.File.makeDir(baseDir); -add_task(async function setup() { - let tmpDir = await IOUtils.createUniqueDirectory( - await PathUtils.getTempDir(), - "PKCS11" - ); - - baseDir = PathUtils.join(tmpDir, slug); - await IOUtils.createDirectory(baseDir); +registerCleanupFunction(() => { + tmpDir.remove(true); }); -registerCleanupFunction(async () => { - await IOUtils.remove(tmpDir, { recursive: true }); -}); +function getPath(filename) { + return OS.Path.join(baseDir, filename); +} -const testmodule = PathUtils.join( - PathUtils.parent(Services.dirsvc.get("CurWorkD", Ci.nsIFile).path, 5), - "security", - "manager", - "ssl", - "tests", - "unit", - "pkcs11testmodule", - ctypes.libraryName("pkcs11testModule") -); +const testmodule = + "../../../../../security/manager/ssl/tests/unit/pkcs11testmodule/" + + ctypes.libraryName("pkcs11testmodule"); // This function was inspired by the native messaging test under // toolkit/components/extensions @@ -50,8 +39,8 @@ async function setupManifests(modules) { allowed_extensions: [module.id], }; - let manifestPath = PathUtils.join(baseDir, `${module.name}.json`); - await IOUtils.writeJSON(manifestPath, manifest); + let manifestPath = getPath(`${module.name}.json`); + await OS.File.writeAtomic(manifestPath, JSON.stringify(manifest)); return manifestPath; } @@ -89,6 +78,10 @@ async function setupManifests(modules) { }); for (let module of modules) { + if (!OS.Path.winIsAbsolute(module.path)) { + let cwd = await OS.File.getCurrentDirectory(); + module.path = OS.Path.join(cwd, module.path); + } let manifestPath = await writeManifest(module); registry.setValue( Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER, @@ -255,16 +248,13 @@ add_task(async function test_pkcs11() { { name: "internalmodule", description: "Builtin Roots Module", - path: PathUtils.join( - Services.dirsvc.get("CurWorkD", Ci.nsIFile).path, - ctypes.libraryName("nssckbi") - ), + path: ctypes.libraryName("nssckbi"), id: "pkcs11@tests.mozilla.org", }, { name: "osclientcerts", description: "OS Client Cert Module", - path: PathUtils.join(libDir.path, ctypes.libraryName("osclientcerts")), + path: OS.Path.join(libDir.path, ctypes.libraryName("osclientcerts")), id: "pkcs11@tests.mozilla.org", }, ]); diff --git a/dom/chrome-webidl/PathUtils.webidl b/dom/chrome-webidl/PathUtils.webidl index 36a8fd706f8b..c3807670d627 100644 --- a/dom/chrome-webidl/PathUtils.webidl +++ b/dom/chrome-webidl/PathUtils.webidl @@ -20,19 +20,17 @@ namespace PathUtils { DOMString filename(DOMString path); /** - * Return an ancestor directory of the given path. + * Return the parent directory name of the given path. * * @param path An absolute path. - * @param depth The number of ancestors to remove, defaulting to 1 (i.e., the - * parent). * - * @return The ancestor directory. + * @return The parent directory. * * If the path provided is a root path (e.g., `C:` on Windows or `/` * on *NIX), then null is returned. */ [Throws] - DOMString? parent(DOMString path, optional long depth = 1); + DOMString? parent(DOMString path); /** * Join the given components into a full path. diff --git a/dom/system/PathUtils.cpp b/dom/system/PathUtils.cpp index cb33ceda5876..9b19ff42dfc6 100644 --- a/dom/system/PathUtils.cpp +++ b/dom/system/PathUtils.cpp @@ -142,8 +142,7 @@ void PathUtils::Filename(const GlobalObject&, const nsAString& aPath, } void PathUtils::Parent(const GlobalObject&, const nsAString& aPath, - const int32_t aDepth, nsString& aResult, - ErrorResult& aErr) { + nsString& aResult, ErrorResult& aErr) { if (aPath.IsEmpty()) { aErr.ThrowNotAllowedError(ERROR_EMPTY_PATH); return; @@ -155,18 +154,10 @@ void PathUtils::Parent(const GlobalObject&, const nsAString& aPath, return; } - if (aDepth <= 0) { - aErr.ThrowNotSupportedError("A depth of at least 1 is required"); - return; - } - nsCOMPtr parent; - for (int32_t i = 0; path && i < aDepth; i++) { - if (nsresult rv = path->GetParent(getter_AddRefs(parent)); NS_FAILED(rv)) { - ThrowError(aErr, rv, ERROR_GET_PARENT); - return; - } - path = parent; + if (nsresult rv = path->GetParent(getter_AddRefs(parent)); NS_FAILED(rv)) { + ThrowError(aErr, rv, ERROR_GET_PARENT); + return; } if (parent) { diff --git a/dom/system/PathUtils.h b/dom/system/PathUtils.h index c0a8dd76bdca..51b57634bb5e 100644 --- a/dom/system/PathUtils.h +++ b/dom/system/PathUtils.h @@ -41,8 +41,7 @@ class PathUtils final { nsString& aResult, ErrorResult& aErr); static void Parent(const GlobalObject&, const nsAString& aPath, - const int32_t aDepth, nsString& aResult, - ErrorResult& aErr); + nsString& aResult, ErrorResult& aErr); static void Join(const GlobalObject&, const Sequence& aComponents, nsString& aResult, ErrorResult& aErr); diff --git a/dom/system/tests/test_pathutils.html b/dom/system/tests/test_pathutils.html index dd13257235fa..0f6375ae6c06 100644 --- a/dom/system/tests/test_pathutils.html +++ b/dom/system/tests/test_pathutils.html @@ -119,37 +119,6 @@ "\\\\server", "PathUtils.parent() with a UNC server path and child component" ); - - Assert.throws( - () => PathUtils.parent("C:", -1), - /^NotSupportedError: PathUtils.parent: A depth of at least 1 is required/, - "PathUtils.parent() with a negative depth throws" - ); - Assert.throws( - () => PathUtils.parent("C:", 0), - /^NotSupportedError: PathUtils.parent: A depth of at least 1 is required/, - "PathUtils.parent() with a zero depth throws" - ); - - { - const path = "C:\\Users\\User\\AppData\\Local\\Mozilla\\Firefox\\Profiles\\foo.default"; - - const expected = [ - "C:\\Users\\User\\AppData\\Local\\Mozilla\\Firefox\\Profiles", - "C:\\Users\\User\\AppData\\Local\\Mozilla\\Firefox", - "C:\\Users\\User\\AppData\\Local\\Mozilla", - "C:\\Users\\User\\AppData\\Local", - "C:\\Users\\User\\AppData", - "C:\\Users\\User", - "C:\\Users", - "C:", - null, - ]; - - for (const [i, parent] of expected.entries()) { - is(PathUtils.parent(path, i + 1), parent, `PathUtils.parent() with depth=${i + 1}`) - } - } } else { is( PathUtils.parent("/"), @@ -166,37 +135,6 @@ "/var", "PathUtils.parent() with a 3 component path" ); - - Assert.throws( - () => PathUtils.parent("/", -1), - /^NotSupportedError: PathUtils.parent: A depth of at least 1 is required/, - "PathUtils.parent() with a negative depth throws" - ); - Assert.throws( - () => PathUtils.parent("/", 0), - /^NotSupportedError: PathUtils.parent: A depth of at least 1 is required/, - "PathUtils.parent() with a zero depth throws" - ); - - { - const path = "/home/user/.mozilla/firefox/foo.default"; - const expected = [ - "/home/user/.mozilla/firefox", - "/home/user/.mozilla", - "/home/user", - "/home", - "/", - null, - ]; - - for (const [i, parent] of expected.entries()) { - is( - PathUtils.parent(path, i + 1), - parent, - `PathUtils.parent() with depth=${i + 1}` - ); - } - } } });