2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-03-04 11:31:14 -05:00
|
|
|
import { unitTest, assertEquals, assert } from "./test_util.ts";
|
2019-05-07 21:58:58 -04:00
|
|
|
|
|
|
|
// chown on Windows is noop for now, so ignore its testing on Windows
|
2020-04-28 12:35:23 -04:00
|
|
|
if (Deno.build.os !== "windows") {
|
2019-05-07 21:58:58 -04:00
|
|
|
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",
|
2020-03-28 13:03:49 -04:00
|
|
|
cmd: ["python", "-c", "import os; print(os.getuid())"],
|
2019-05-07 21:58:58 -04:00
|
|
|
});
|
|
|
|
const gidProc = Deno.run({
|
|
|
|
stdout: "piped",
|
2020-03-28 13:03:49 -04:00
|
|
|
cmd: ["python", "-c", "import os; print(os.getgid())"],
|
2019-05-07 21:58:58 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
assertEquals((await uidProc.status()).code, 0);
|
|
|
|
assertEquals((await gidProc.status()).code, 0);
|
|
|
|
const uid = parseInt(
|
|
|
|
new TextDecoder("utf-8").decode(await uidProc.output())
|
|
|
|
);
|
2020-02-29 12:45:47 -05:00
|
|
|
uidProc.close();
|
2019-05-07 21:58:58 -04:00
|
|
|
const gid = parseInt(
|
|
|
|
new TextDecoder("utf-8").decode(await gidProc.output())
|
|
|
|
);
|
2020-02-29 12:45:47 -05:00
|
|
|
gidProc.close();
|
2019-05-07 21:58:58 -04:00
|
|
|
|
|
|
|
return { uid, gid };
|
|
|
|
}
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(async function chownNoWritePermission(): Promise<void> {
|
2019-05-07 21:58:58 -04:00
|
|
|
const filePath = "chown_test_file.txt";
|
|
|
|
try {
|
|
|
|
await Deno.chown(filePath, 1000, 1000);
|
|
|
|
} catch (e) {
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(e instanceof Deno.errors.PermissionDenied);
|
2019-05-07 21:58:58 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { run: true, write: true } },
|
2019-05-07 21:58:58 -04:00
|
|
|
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) {
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(e instanceof Deno.errors.NotFound);
|
2019-05-07 21:58:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { run: true, write: true } },
|
2019-05-07 21:58:58 -04:00
|
|
|
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) {
|
2020-02-24 15:48:35 -05:00
|
|
|
assert(e instanceof Deno.errors.NotFound);
|
2019-05-07 21:58:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { 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);
|
2019-05-07 21:58:58 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
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 });
|
2019-05-07 21:58:58 -04:00
|
|
|
}
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-05-07 21:58:58 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { 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);
|
2019-05-07 21:58:58 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
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 });
|
2019-05-07 21:58:58 -04:00
|
|
|
}
|
2020-03-04 11:31:14 -05:00
|
|
|
);
|
2019-05-07 21:58:58 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { run: true, write: true } },
|
2019-05-07 21:58:58 -04:00
|
|
|
async function chownSyncSucceed(): Promise<void> {
|
|
|
|
// TODO: when a file's owner is actually being changed,
|
|
|
|
// chown only succeeds if run under priviledged user (root)
|
2020-05-14 00:38:42 -04:00
|
|
|
// The test script has no such privilege, so need to find a better way to test this case
|
2019-05-07 21:58:58 -04:00
|
|
|
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 });
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-06-11 12:36:20 -04:00
|
|
|
unitTest(
|
|
|
|
{ perms: { run: true, write: true } },
|
|
|
|
async function chownSyncWithUrl(): Promise<void> {
|
|
|
|
// TODO: same as chownSyncSucceed
|
|
|
|
const { uid, gid } = await getUidAndGid();
|
|
|
|
|
|
|
|
const enc = new TextEncoder();
|
|
|
|
const dirPath = Deno.makeTempDirSync();
|
|
|
|
const fileUrl = new URL(`file://${dirPath}/chown_test_file.txt`);
|
|
|
|
const fileData = enc.encode("Hello");
|
|
|
|
Deno.writeFileSync(fileUrl, 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(fileUrl, uid, gid);
|
|
|
|
|
|
|
|
Deno.removeSync(dirPath, { recursive: true });
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
unitTest(
|
|
|
|
{ perms: { run: true, write: true } },
|
|
|
|
async function chownSucceed(): Promise<void> {
|
|
|
|
// TODO: same as chownSyncSucceed
|
|
|
|
const { uid, gid } = await getUidAndGid();
|
2019-05-07 21:58:58 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
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);
|
2019-05-07 21:58:58 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
// 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);
|
2019-05-07 21:58:58 -04:00
|
|
|
|
2020-03-04 11:31:14 -05:00
|
|
|
Deno.removeSync(dirPath, { recursive: true });
|
|
|
|
}
|
|
|
|
);
|
2020-06-11 12:36:20 -04:00
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ perms: { run: true, write: true } },
|
|
|
|
async function chownWithUrl(): Promise<void> {
|
|
|
|
// TODO: same as chownSyncSucceed
|
|
|
|
const { uid, gid } = await getUidAndGid();
|
|
|
|
|
|
|
|
const enc = new TextEncoder();
|
|
|
|
const dirPath = await Deno.makeTempDir();
|
|
|
|
const fileUrl = new URL(`file://${dirPath}/chown_test_file.txt`);
|
|
|
|
const fileData = enc.encode("Hello");
|
|
|
|
await Deno.writeFile(fileUrl, 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(fileUrl, uid, gid);
|
|
|
|
|
|
|
|
Deno.removeSync(dirPath, { recursive: true });
|
|
|
|
}
|
|
|
|
);
|
2019-05-07 21:58:58 -04:00
|
|
|
}
|