2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-08-24 12:21:56 -04:00
|
|
|
import {
|
|
|
|
assertEquals,
|
|
|
|
assertThrows,
|
|
|
|
assertThrowsAsync,
|
2020-09-27 06:22:32 -04:00
|
|
|
unitTest,
|
2020-08-24 12:21:56 -04:00
|
|
|
} 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-07-06 07:15:13 -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-12-21 10:30:59 -05:00
|
|
|
cmd: ["id", "-u"],
|
2020-07-06 07:15:13 -04:00
|
|
|
});
|
|
|
|
const gidProc = Deno.run({
|
|
|
|
stdout: "piped",
|
2020-12-21 10:30:59 -05:00
|
|
|
cmd: ["id", "-g"],
|
2020-07-06 07:15:13 -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()));
|
|
|
|
uidProc.close();
|
|
|
|
const gid = parseInt(new TextDecoder("utf-8").decode(await gidProc.output()));
|
|
|
|
gidProc.close();
|
|
|
|
|
|
|
|
return { uid, gid };
|
|
|
|
}
|
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ ignore: Deno.build.os == "windows" },
|
|
|
|
async function chownNoWritePermission(): Promise<void> {
|
2019-05-07 21:58:58 -04:00
|
|
|
const filePath = "chown_test_file.txt";
|
2020-08-24 12:21:56 -04:00
|
|
|
await assertThrowsAsync(async () => {
|
2019-05-07 21:58:58 -04:00
|
|
|
await Deno.chown(filePath, 1000, 1000);
|
2020-08-24 12:21:56 -04:00
|
|
|
}, Deno.errors.PermissionDenied);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-07-06 07:15:13 -04:00
|
|
|
);
|
2019-05-07 21:58:58 -04:00
|
|
|
|
2020-07-06 07:15:13 -04:00
|
|
|
unitTest(
|
|
|
|
{ perms: { run: true, write: true }, ignore: Deno.build.os == "windows" },
|
|
|
|
async function chownSyncFileNotExist(): Promise<void> {
|
|
|
|
const { uid, gid } = await getUidAndGid();
|
|
|
|
const filePath = Deno.makeTempDirSync() + "/chown_test_file.txt";
|
2019-05-07 21:58:58 -04:00
|
|
|
|
2020-08-24 12:21:56 -04:00
|
|
|
assertThrows(() => {
|
2020-07-06 07:15:13 -04:00
|
|
|
Deno.chownSync(filePath, uid, gid);
|
2020-08-24 12:21:56 -04:00
|
|
|
}, Deno.errors.NotFound);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-07-06 07:15:13 -04:00
|
|
|
);
|
2019-05-07 21:58:58 -04:00
|
|
|
|
2020-07-06 07:15:13 -04:00
|
|
|
unitTest(
|
|
|
|
{ perms: { run: true, write: true }, ignore: Deno.build.os == "windows" },
|
|
|
|
async function chownFileNotExist(): Promise<void> {
|
|
|
|
const { uid, gid } = await getUidAndGid();
|
|
|
|
const filePath = (await Deno.makeTempDir()) + "/chown_test_file.txt";
|
2020-06-11 12:36:20 -04:00
|
|
|
|
2020-08-24 12:21:56 -04:00
|
|
|
await assertThrowsAsync(async () => {
|
2020-07-06 07:15:13 -04:00
|
|
|
await Deno.chown(filePath, uid, gid);
|
2020-08-24 12:21:56 -04:00
|
|
|
}, Deno.errors.NotFound);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-07-06 07:15:13 -04:00
|
|
|
);
|
2020-06-11 12:36:20 -04:00
|
|
|
|
2020-07-06 07:15:13 -04:00
|
|
|
unitTest(
|
|
|
|
{ perms: { write: true }, ignore: Deno.build.os == "windows" },
|
|
|
|
function chownSyncPermissionDenied(): void {
|
|
|
|
const dirPath = Deno.makeTempDirSync();
|
|
|
|
const filePath = dirPath + "/chown_test_file.txt";
|
|
|
|
Deno.writeTextFileSync(filePath, "Hello");
|
2020-06-11 12:36:20 -04:00
|
|
|
|
2020-08-24 12:21:56 -04:00
|
|
|
assertThrows(() => {
|
2020-07-06 07:15:13 -04:00
|
|
|
// try changing the file's owner to root
|
|
|
|
Deno.chownSync(filePath, 0, 0);
|
2020-08-24 12:21:56 -04:00
|
|
|
}, Deno.errors.PermissionDenied);
|
2020-07-06 07:15:13 -04:00
|
|
|
Deno.removeSync(dirPath, { recursive: true });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-07-06 07:15:13 -04:00
|
|
|
);
|
2019-05-07 21:58:58 -04:00
|
|
|
|
2020-07-06 07:15:13 -04:00
|
|
|
unitTest(
|
|
|
|
{ perms: { write: true }, ignore: Deno.build.os == "windows" },
|
|
|
|
async function chownPermissionDenied(): Promise<void> {
|
|
|
|
const dirPath = await Deno.makeTempDir();
|
|
|
|
const filePath = dirPath + "/chown_test_file.txt";
|
|
|
|
await Deno.writeTextFile(filePath, "Hello");
|
|
|
|
|
2020-08-24 12:21:56 -04:00
|
|
|
await assertThrowsAsync(async () => {
|
2020-07-06 07:15:13 -04:00
|
|
|
// try changing the file's owner to root
|
|
|
|
await Deno.chown(filePath, 0, 0);
|
2020-08-24 12:21:56 -04:00
|
|
|
}, Deno.errors.PermissionDenied);
|
2020-07-06 07:15:13 -04:00
|
|
|
await Deno.remove(dirPath, { recursive: true });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-07-06 07:15:13 -04:00
|
|
|
);
|
2020-06-11 12:36:20 -04:00
|
|
|
|
2020-07-06 07:15:13 -04:00
|
|
|
unitTest(
|
|
|
|
{ perms: { run: true, write: true }, ignore: Deno.build.os == "windows" },
|
|
|
|
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 privilege, so need to find a better way to test this case
|
|
|
|
const { uid, gid } = await getUidAndGid();
|
2020-06-11 12:36:20 -04:00
|
|
|
|
2020-07-06 07:15:13 -04:00
|
|
|
const dirPath = Deno.makeTempDirSync();
|
|
|
|
const filePath = dirPath + "/chown_test_file.txt";
|
|
|
|
Deno.writeTextFileSync(filePath, "Hello");
|
2020-06-11 12:36:20 -04:00
|
|
|
|
2020-07-06 07:15:13 -04: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
|
|
|
|
Deno.chownSync(filePath, uid, gid);
|
2020-06-11 12:36:20 -04:00
|
|
|
|
2020-07-06 07:15:13 -04:00
|
|
|
Deno.removeSync(dirPath, { recursive: true });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-07-06 07:15:13 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ perms: { run: true, write: true }, ignore: Deno.build.os == "windows" },
|
|
|
|
async function chownSyncWithUrl(): Promise<void> {
|
|
|
|
const { uid, gid } = await getUidAndGid();
|
|
|
|
const dirPath = Deno.makeTempDirSync();
|
|
|
|
const fileUrl = new URL(`file://${dirPath}/chown_test_file.txt`);
|
|
|
|
Deno.writeTextFileSync(fileUrl, "Hello");
|
|
|
|
Deno.chownSync(fileUrl, uid, gid);
|
|
|
|
Deno.removeSync(dirPath, { recursive: true });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-07-06 07:15:13 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ perms: { run: true, write: true }, ignore: Deno.build.os == "windows" },
|
|
|
|
async function chownSucceed(): Promise<void> {
|
|
|
|
const { uid, gid } = await getUidAndGid();
|
|
|
|
const dirPath = await Deno.makeTempDir();
|
|
|
|
const filePath = dirPath + "/chown_test_file.txt";
|
|
|
|
await Deno.writeTextFile(filePath, "Hello");
|
|
|
|
await Deno.chown(filePath, uid, gid);
|
|
|
|
Deno.removeSync(dirPath, { recursive: true });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-07-06 07:15:13 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ perms: { run: true, write: true }, ignore: Deno.build.os == "windows" },
|
|
|
|
async function chownUidOnly(): Promise<void> {
|
|
|
|
const { uid } = await getUidAndGid();
|
|
|
|
const dirPath = await Deno.makeTempDir();
|
|
|
|
const filePath = dirPath + "/chown_test_file.txt";
|
|
|
|
await Deno.writeTextFile(filePath, "Foo");
|
|
|
|
await Deno.chown(filePath, uid, null);
|
|
|
|
Deno.removeSync(dirPath, { recursive: true });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-07-06 07:15:13 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
unitTest(
|
|
|
|
{ perms: { run: true, write: true }, ignore: Deno.build.os == "windows" },
|
|
|
|
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 });
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-07-06 07:15:13 -04:00
|
|
|
);
|