Skip to content

Commit

Permalink
Bug 1745352 - Remove osfile.jsm from browser/components/extensions r=…
Browse files Browse the repository at this point in the history
…mixedpuppy

Differential Revision: https://phabricator.services.mozilla.com/D133843
  • Loading branch information
brennie committed Jan 25, 2022
1 parent 46c935e commit 9f641d5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 31 deletions.
20 changes: 14 additions & 6 deletions browser/components/extensions/parent/ext-pkcs11.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
XPCOMUtils.defineLazyModuleGetters(this, {
ctypes: "resource://gre/modules/ctypes.jsm",
NativeManifests: "resource://gre/modules/NativeManifests.jsm",
OS: "resource://gre/modules/osfile.jsm",
});

XPCOMUtils.defineLazyServiceGetter(
Expand All @@ -17,6 +16,9 @@ XPCOMUtils.defineLazyServiceGetter(
"nsIPKCS11ModuleDB"
);

// eslint-disable-next-line mozilla/reject-importGlobalProperties
Cu.importGlobalProperties(["PathUtils"]);

var { DefaultMap } = ExtensionUtils;

const findModuleByPath = function(path) {
Expand All @@ -38,12 +40,18 @@ this.pkcs11 = class extends ExtensionAPI {
);
if (hostInfo) {
if (AppConstants.platform === "win") {
hostInfo.manifest.path = OS.Path.join(
OS.Path.dirname(hostInfo.path),
hostInfo.manifest.path
);
// If the path specified in the manifest is not an abslute path,
// translate it relative to manifest's directory.
if (!PathUtils.isAbsolute(hostInfo.manifest.path)) {
hostInfo.manifest.path = PathUtils.normalize(
PathUtils.joinRelative(
PathUtils.parent(hostInfo.path),
hostInfo.manifest.path
)
);
}
}
let manifestLib = OS.Path.basename(hostInfo.manifest.path);
let manifestLib = PathUtils.filename(hostInfo.manifest.path);
if (AppConstants.platform !== "linux") {
manifestLib = manifestLib.toLowerCase(manifestLib);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,39 @@
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 = FileUtils.getDir("TmpD", ["PKCS11"]);

let tmpDir;
let baseDir;
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);

registerCleanupFunction(() => {
tmpDir.remove(true);
add_task(async function setupTest() {
tmpDir = await IOUtils.createUniqueDirectory(
Services.dirsvc.get("TmpD", Ci.nsIFile).path,
"PKCS11"
);

baseDir = PathUtils.join(tmpDir, slug);
await IOUtils.makeDirectory(baseDir);
});

function getPath(filename) {
return OS.Path.join(baseDir, filename);
}
registerCleanupFunction(async () => {
await IOUtils.remove(tmpDir, { recursive: true });
});

const testmodule =
"../../../../../security/manager/ssl/tests/unit/pkcs11testmodule/" +
ctypes.libraryName("pkcs11testmodule");
const testmodule = PathUtils.join(
PathUtils.parent(Services.dirsvc.get("CurWorkD", Ci.nsIFile).path, 5),
"security",
"manager",
"ssl",
"tests",
"unit",
"pkcs11testmodule",
ctypes.libraryName("pkcs11testmodule")
);

// This function was inspired by the native messaging test under
// toolkit/components/extensions
Expand All @@ -39,8 +50,8 @@ async function setupManifests(modules) {
allowed_extensions: [module.id],
};

let manifestPath = getPath(`${module.name}.json`);
await OS.File.writeAtomic(manifestPath, JSON.stringify(manifest));
let manifestPath = PathUtils.join(baseDir, `${module.name}.json`);
await IOUtils.writeJSON(manifestPath, manifest);

return manifestPath;
}
Expand All @@ -50,10 +61,11 @@ async function setupManifests(modules) {
case "linux":
let dirProvider = {
getFile(property) {
if (property == "XREUserNativeManifests") {
return tmpDir.clone();
} else if (property == "XRESysNativeManifests") {
return tmpDir.clone();
if (
property == "XREUserNativeManifests" ||
property == "XRESysNativeManifests"
) {
return new FileUtils.File(tmpDir);
}
return null;
},
Expand All @@ -78,10 +90,6 @@ 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,
Expand Down Expand Up @@ -248,13 +256,16 @@ add_task(async function test_pkcs11() {
{
name: "internalmodule",
description: "Builtin Roots Module",
path: ctypes.libraryName("nssckbi"),
path: PathUtils.join(
Services.dirsvc.get("CurWorkD", Ci.nsIFile).path,
ctypes.libraryName("nssckbi")
),
id: "pkcs11@tests.mozilla.org",
},
{
name: "osclientcerts",
description: "OS Client Cert Module",
path: OS.Path.join(libDir.path, ctypes.libraryName("osclientcerts")),
path: PathUtils.join(libDir.path, ctypes.libraryName("osclientcerts")),
id: "pkcs11@tests.mozilla.org",
},
]);
Expand Down

0 comments on commit 9f641d5

Please sign in to comment.