Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: check that modules backed by read-only files can load (win32) #20116

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions test/parallel/test-module-readonly.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const fs = require('fs');
const path = require('path');
const cp = require('child_process');

const tmpdir = require('../common/tmpdir');
tmpdir.refresh();

if (process.platform == 'win32') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

common.isWindows

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is currently a Windows only test, you can do:

if (!common.isWindows) {
  common.skip('test only runs on Windows');
}

This will skip the test on other platforms and exit the process.

// Create readOnlyMod.js and set to read only
const readOnlyMod = path.join(tmpdir.path, 'readOnlyMod');
const readOnlyModRelative = path.relative(__dirname, readOnlyMod);
const readOnlyModFullPath = readOnlyMod + ".js";
fs.writeFileSync(readOnlyModFullPath, "module.exports = 42;");

// Removed any inherited ACEs, and any explicitly granted ACEs for the current user
cp.execSync(`icacls.exe "${readOnlyModFullPath}" /inheritance:r /remove "%USERNAME%"`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Long line here, whitespace/indent errors elsewhere. Can you run make lint?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this is a Windows test the equivalent is vcbuild lint (or vcbuild lint-js to just lint the JavaScript files).

// Grant the current user read & execute only
cp.execSync(`icacls.exe "${readOnlyModFullPath}" /grant "%USERNAME%":RX`);

let except = null;
try {
// Attempt to load the module. Will fail if write access is required
const mymod = require(readOnlyModRelative);
} catch(err) {
except = err;
}

// Remove the expliclty granted rights, and reenable inheritance
cp.execSync(`icacls.exe "${readOnlyModFullPath}" /remove "%USERNAME%" /inheritance:e`);

// Delete the file
fs.unlinkSync(readOnlyModFullPath);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(If we use the tmpdir module instead of the fixtures module, then there is no need to unlink.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just pushed a change to use tmpdir. I figured cleaning up is safer than having another run try and overwrite the same file (permissions shouldn't conflict as it should be a different tmpdir if under a different account, but just to be safe...).

Copy link
Member

@Trott Trott Apr 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(If you use common/tmpdir rather than os.tmpdir, there is no need to clean up because tests are expected to refresh tmpdir before they do anything in it.)


if (except) throw except;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of an if, we'd ideally want an assertion, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
// TODO: Similar checks on *nix-like systems (e.g. using chmod or the like)