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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { testPerm, assertEquals, assert } from "./test_util.ts";
// chown on Windows is noop for now, so ignore its testing on Windows
if (Deno.build.os !== "win") {
async function getUidAndGid(): Promise<{ uid: number; gid: number }> {
// get the user ID and group ID of the current process
const uidProc = Deno.run({
stdout: "piped",
args: ["python", "-c", "import os; print(os.getuid())"]
});
const gidProc = Deno.run({
stdout: "piped",
args: ["python", "-c", "import os; print(os.getgid())"]
});
assertEquals((await uidProc.status()).code, 0);
assertEquals((await gidProc.status()).code, 0);
const uid = parseInt(
new TextDecoder("utf-8").decode(await uidProc.output())
);
uidProc.close();
const gid = parseInt(
new TextDecoder("utf-8").decode(await gidProc.output())
);
gidProc.close();
return { uid, gid };
}
testPerm({}, async function chownNoWritePermission(): Promise<void> {
const filePath = "chown_test_file.txt";
try {
await Deno.chown(filePath, 1000, 1000);
} catch (e) {
assert(e instanceof Deno.errors.PermissionDenied);
}
});
testPerm(
{ run: true, write: true },
async function chownSyncFileNotExist(): Promise<void> {
const { uid, gid } = await getUidAndGid();
const filePath = Deno.makeTempDirSync() + "/chown_test_file.txt";
try {
Deno.chownSync(filePath, uid, gid);
} catch (e) {
assert(e instanceof Deno.errors.NotFound);
}
}
);
testPerm(
{ run: true, write: true },
async function chownFileNotExist(): Promise<void> {
const { uid, gid } = await getUidAndGid();
const filePath = (await Deno.makeTempDir()) + "/chown_test_file.txt";
try {
await Deno.chown(filePath, uid, gid);
} catch (e) {
assert(e instanceof Deno.errors.NotFound);
}
}
);
testPerm({ write: true }, function chownSyncPermissionDenied(): void {
const enc = new TextEncoder();
const dirPath = Deno.makeTempDirSync();
const filePath = dirPath + "/chown_test_file.txt";
const fileData = enc.encode("Hello");
Deno.writeFileSync(filePath, fileData);
try {
// try changing the file's owner to root
Deno.chownSync(filePath, 0, 0);
} catch (e) {
assert(e instanceof Deno.errors.PermissionDenied);
}
Deno.removeSync(dirPath, { recursive: true });
});
testPerm({ write: true }, async function chownPermissionDenied(): Promise<
void
> {
const enc = new TextEncoder();
const dirPath = await Deno.makeTempDir();
const filePath = dirPath + "/chown_test_file.txt";
const fileData = enc.encode("Hello");
await Deno.writeFile(filePath, fileData);
try {
// try changing the file's owner to root
await Deno.chown(filePath, 0, 0);
} catch (e) {
assert(e instanceof Deno.errors.PermissionDenied);
}
await Deno.remove(dirPath, { recursive: true });
});
testPerm(
{ run: true, write: true },
async function chownSyncSucceed(): Promise<void> {
// TODO: when a file's owner is actually being changed,
// chown only succeeds if run under priviledged user (root)
// The test script has no such priviledge, so need to find a better way to test this case
const { uid, gid } = await getUidAndGid();
const enc = new TextEncoder();
const dirPath = Deno.makeTempDirSync();
const filePath = dirPath + "/chown_test_file.txt";
const fileData = enc.encode("Hello");
Deno.writeFileSync(filePath, fileData);
// the test script creates this file with the same uid and gid,
// here chown is a noop so it succeeds under non-priviledged user
Deno.chownSync(filePath, uid, gid);
Deno.removeSync(dirPath, { recursive: true });
}
);
testPerm({ run: true, write: true }, async function chownSucceed(): Promise<
void
> {
// TODO: same as chownSyncSucceed
const { uid, gid } = await getUidAndGid();
const enc = new TextEncoder();
const dirPath = await Deno.makeTempDir();
const filePath = dirPath + "/chown_test_file.txt";
const fileData = enc.encode("Hello");
await Deno.writeFile(filePath, fileData);
// the test script creates this file with the same uid and gid,
// here chown is a noop so it succeeds under non-priviledged user
await Deno.chown(filePath, uid, gid);
Deno.removeSync(dirPath, { recursive: true });
});
}
|