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, win32: fix up symlink tests #10477

Closed
Closed
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ A stream to push an array into a REPL

Blocks for `time` amount of time.

### canCreateSymLink
API to indicate whether the current running process can create
symlinks. On Windows, this returns false if the process running
doesn't have privileges to create symlinks (specifically
[SeCreateSymbolicLinkPrivilege](https://msdn.microsoft.com/en-us/library/windows/desktop/bb530716(v=vs.85).aspx)).
On non-Windows platforms, this currently returns true.

### ddCommand(filename, kilobytes)
* return [<Object>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)

Expand Down
29 changes: 29 additions & 0 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const stream = require('stream');
const buffer = require('buffer');
const util = require('util');
const Timer = process.binding('timer_wrap').Timer;
const execSync = require('child_process').execSync;

const testRoot = process.env.NODE_TEST_DIR ?
path.resolve(process.env.NODE_TEST_DIR) : __dirname;
Expand Down Expand Up @@ -460,6 +461,34 @@ exports.fileExists = function(pathname) {
}
};

exports.canCreateSymLink = function() {
// On Windows, creating symlinks requires admin privileges.
// We'll only try to run symlink test if we have enough privileges.
// On other platforms, creating symlinks shouldn't need admin privileges
if (exports.isWindows) {
// whoami.exe needs to be the one from System32
// If unix tools are in the path, they can shadow the one we want,
// so use the full path while executing whoami
const whoamiPath = path.join(process.env['SystemRoot'],
'System32', 'whoami.exe');

let err = false;
let output = '';

try {
output = execSync(whoamiPath + ' /priv', { timout: 1000 });
} catch (e) {
err = true;
} finally {
if (err || !output.includes('SeCreateSymbolicLinkPrivilege')) {
return false;
}
}
}

return true;
};

function fail(msg) {
assert.fail(null, null, msg);
}
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-fs-options-immutable.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ common.refreshTmpDir();
assert.doesNotThrow(() => fs.readdirSync(__dirname, options));
}

{
if (common.canCreateSymLink()) {
const sourceFile = path.resolve(common.tmpDir, 'test-readlink');
const linkFile = path.resolve(common.tmpDir, 'test-readlink-link');

Expand Down
13 changes: 3 additions & 10 deletions test/parallel/test-fs-symlink.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,13 @@ const common = require('../common');
const assert = require('assert');
const path = require('path');
const fs = require('fs');
const exec = require('child_process').exec;

var linkTime;
var fileTime;

if (common.isWindows) {
// On Windows, creating symlinks requires admin privileges.
// We'll only try to run symlink test if we have enough privileges.
exec('whoami /priv', function(err, o) {
if (err || !o.includes('SeCreateSymbolicLinkPrivilege')) {
common.skip('insufficient privileges');
return;
}
});
if (!common.canCreateSymLink()) {
common.skip('insufficient privileges');
return;
}

common.refreshTmpDir();
Expand Down