/
symlink.js
68 lines (58 loc) · 1.65 KB
/
symlink.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
if (!process.getuid || !process.getgid) {
throw new Error("Tests require getuid/getgid support")
}
var skip = process.platform === 'darwin' ? false
: 'linux and windows do not treat links as separate fs objects'
var curUid = +process.getuid()
, curGid = +process.getgid()
, chownr = require("../")
, test = require("tap").test
, mkdirp = require("mkdirp")
, rimraf = require("rimraf")
, fs = require("fs")
// sniff the 'id' command for other groups that i can legally assign to
var exec = require("child_process").exec
, groups
, dirs = []
exec("id", function (code, output) {
if (code) throw new Error("failed to run 'id' command")
groups = output.trim().split("=")[3].split(",").map(function (s) {
return parseInt(s, 10)
}).filter(function (g) {
return g !== curGid
})
// console.error([curUid, groups[0]], "uid, gid")
rimraf("dir", function (er) {
if (er) throw er
fs.mkdirSync("dir")
fs.writeFileSync("dir/sh")
fs.symlinkSync("./sh", "dir/sh-link")
runTest()
})
})
function runTest () {
test("should complete successfully", function (t) {
// console.error("calling chownr", curUid, groups[0], typeof curUid, typeof groups[0])
chownr("dir", curUid, groups[0], function (er) {
t.ifError(er)
t.end()
})
})
test("verify "+"sh-link", function (t) {
fs.stat("dir/sh-link", function (er, st) {
if (er) {
t.ifError(er)
return t.end()
}
t.notEqual(st.uid, curUid, "uid", {skip})
t.notEqual(st.gid, groups[0], "gid", {skip})
t.end()
})
})
test("cleanup", function (t) {
rimraf("dir", function (er) {
t.ifError(er)
t.end()
})
})
}