Browse Source

test, win: fix up symlink tests

On Windows, creating a symlink requires admin privileges.
There were two tests which created symlinks which were failing when run
as non-admin.

test-fs-symlink.js already had a check for privileges on Windows
but it had a couple issues:
1. It assumed that whoami was the one that came with windows.
   However, whoami also ships with Win32 Unix utility ports
   like the distribution with git, which can cause this to get check
   tripped up.
2. On failure, the check would just return from the callback instead of
   exiting
3. whoami was executed asynchronously so the test would run regardless
   of privilege state.

test-fs-options-immutable had no check.

As part of this change, I refactored the privilege checking to
a function in common, and changed both above tests to use the
refactored function.

Also documented this function in test\README.md

PR-URL: https://github.com/nodejs/node/pull/10477
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: João Reis <reis@janeasystems.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
v7.x
Hitesh Kanwathirtha 8 years ago
committed by Italo A. Casas
parent
commit
1555ced404
No known key found for this signature in database GPG Key ID: 23EFEFE93C4CFFFE
  1. 7
      test/README.md
  2. 29
      test/common.js
  3. 2
      test/parallel/test-fs-options-immutable.js
  4. 13
      test/parallel/test-fs-symlink.js

7
test/README.md

@ -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 [&lt;Object>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)

29
test/common.js

@ -8,6 +8,7 @@ const child_process = require('child_process');
const stream = require('stream');
const util = require('util');
const Timer = process.binding('timer_wrap').Timer;
const execSync = require('child_process').execSync;
const testRoot = process.env.NODE_TEST_DIR ?
fs.realpathSync(process.env.NODE_TEST_DIR) : __dirname;
@ -438,6 +439,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);
}

2
test/parallel/test-fs-options-immutable.js

@ -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');

13
test/parallel/test-fs-symlink.js

@ -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();

Loading…
Cancel
Save