Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1555ced

Browse filesBrowse files
digitalinfinityItalo A. Casas
authored andcommitted
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: #10477 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: João Reis <reis@janeasystems.com> Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
1 parent 60f27f9 commit 1555ced
Copy full SHA for 1555ced

File tree

Expand file treeCollapse file tree

4 files changed

+40
-11
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+40
-11
lines changed
Open diff view settings
Collapse file

‎test/README.md‎

Copy file name to clipboardExpand all lines: test/README.md
+7Lines changed: 7 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,13 @@ A stream to push an array into a REPL
163163

164164
Blocks for `time` amount of time.
165165

166+
### canCreateSymLink
167+
API to indicate whether the current running process can create
168+
symlinks. On Windows, this returns false if the process running
169+
doesn't have privileges to create symlinks (specifically
170+
[SeCreateSymbolicLinkPrivilege](https://msdn.microsoft.com/en-us/library/windows/desktop/bb530716(v=vs.85).aspx)).
171+
On non-Windows platforms, this currently returns true.
172+
166173
### ddCommand(filename, kilobytes)
167174
* return [&lt;Object>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)
168175

Collapse file

‎test/common.js‎

Copy file name to clipboardExpand all lines: test/common.js
+29Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const child_process = require('child_process');
88
const stream = require('stream');
99
const util = require('util');
1010
const Timer = process.binding('timer_wrap').Timer;
11+
const execSync = require('child_process').execSync;
1112

1213
const testRoot = process.env.NODE_TEST_DIR ?
1314
fs.realpathSync(process.env.NODE_TEST_DIR) : __dirname;
@@ -438,6 +439,34 @@ exports.fileExists = function(pathname) {
438439
}
439440
};
440441

442+
exports.canCreateSymLink = function() {
443+
// On Windows, creating symlinks requires admin privileges.
444+
// We'll only try to run symlink test if we have enough privileges.
445+
// On other platforms, creating symlinks shouldn't need admin privileges
446+
if (exports.isWindows) {
447+
// whoami.exe needs to be the one from System32
448+
// If unix tools are in the path, they can shadow the one we want,
449+
// so use the full path while executing whoami
450+
const whoamiPath = path.join(process.env['SystemRoot'],
451+
'System32', 'whoami.exe');
452+
453+
let err = false;
454+
let output = '';
455+
456+
try {
457+
output = execSync(whoamiPath + ' /priv', { timout: 1000 });
458+
} catch (e) {
459+
err = true;
460+
} finally {
461+
if (err || !output.includes('SeCreateSymbolicLinkPrivilege')) {
462+
return false;
463+
}
464+
}
465+
}
466+
467+
return true;
468+
};
469+
441470
function fail(msg) {
442471
assert.fail(null, null, msg);
443472
}
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-fs-options-immutable.js
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ common.refreshTmpDir();
2929
assert.doesNotThrow(() => fs.readdirSync(__dirname, options));
3030
}
3131

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

Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-fs-symlink.js
+3-10Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,13 @@ const common = require('../common');
33
const assert = require('assert');
44
const path = require('path');
55
const fs = require('fs');
6-
const exec = require('child_process').exec;
76

87
var linkTime;
98
var fileTime;
109

11-
if (common.isWindows) {
12-
// On Windows, creating symlinks requires admin privileges.
13-
// We'll only try to run symlink test if we have enough privileges.
14-
exec('whoami /priv', function(err, o) {
15-
if (err || !o.includes('SeCreateSymbolicLinkPrivilege')) {
16-
common.skip('insufficient privileges');
17-
return;
18-
}
19-
});
10+
if (!common.canCreateSymLink()) {
11+
common.skip('insufficient privileges');
12+
return;
2013
}
2114

2215
common.refreshTmpDir();

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.