2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2021-11-23 17:45:18 +01:00
|
|
|
import { assertEquals, assertRejects, assertThrows } from "./test_util.ts";
|
2019-05-07 18:58:58 -07: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
|
2022-12-02 14:43:17 +01:00
|
|
|
const uidProc = await new Deno.Command("id", {
|
2022-05-18 22:00:11 +02:00
|
|
|
args: ["-u"],
|
2022-12-02 14:43:17 +01:00
|
|
|
}).output();
|
|
|
|
const gidProc = await new Deno.Command("id", {
|
2022-05-18 22:00:11 +02:00
|
|
|
args: ["-g"],
|
2022-12-02 14:43:17 +01:00
|
|
|
}).output();
|
2020-07-06 07:15:13 -04:00
|
|
|
|
2022-07-18 14:16:12 +01:00
|
|
|
assertEquals(uidProc.code, 0);
|
|
|
|
assertEquals(gidProc.code, 0);
|
2022-05-18 22:00:11 +02:00
|
|
|
const uid = parseInt(new TextDecoder("utf-8").decode(uidProc.stdout));
|
|
|
|
const gid = parseInt(new TextDecoder("utf-8").decode(gidProc.stdout));
|
2020-07-06 07:15:13 -04:00
|
|
|
|
|
|
|
return { uid, gid };
|
|
|
|
}
|
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
Deno.test(
|
|
|
|
{ ignore: Deno.build.os == "windows", permissions: { write: false } },
|
2021-08-05 13:08:58 +02:00
|
|
|
async function chownNoWritePermission() {
|
2019-05-07 18:58:58 -07:00
|
|
|
const filePath = "chown_test_file.txt";
|
2021-09-22 21:21:11 +08:00
|
|
|
await assertRejects(async () => {
|
2019-05-07 18:58:58 -07:00
|
|
|
await Deno.chown(filePath, 1000, 1000);
|
2020-08-25 00:21:56 +08:00
|
|
|
}, Deno.errors.PermissionDenied);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-07-06 07:15:13 -04:00
|
|
|
);
|
2019-05-07 18:58:58 -07:00
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
Deno.test(
|
2021-09-23 07:50:50 +08:00
|
|
|
{
|
|
|
|
permissions: { run: true, write: true },
|
|
|
|
ignore: Deno.build.os == "windows",
|
|
|
|
},
|
2021-08-05 13:08:58 +02:00
|
|
|
async function chownSyncFileNotExist() {
|
2020-07-06 07:15:13 -04:00
|
|
|
const { uid, gid } = await getUidAndGid();
|
|
|
|
const filePath = Deno.makeTempDirSync() + "/chown_test_file.txt";
|
2019-05-07 18:58:58 -07:00
|
|
|
|
2021-10-11 21:21:18 +08:00
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
Deno.chownSync(filePath, uid, gid);
|
|
|
|
},
|
|
|
|
Deno.errors.NotFound,
|
|
|
|
`chown '${filePath}'`,
|
|
|
|
);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-07-06 07:15:13 -04:00
|
|
|
);
|
2019-05-07 18:58:58 -07:00
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
Deno.test(
|
2021-09-23 07:50:50 +08:00
|
|
|
{
|
|
|
|
permissions: { run: true, write: true },
|
|
|
|
ignore: Deno.build.os == "windows",
|
|
|
|
},
|
2021-08-05 13:08:58 +02:00
|
|
|
async function chownFileNotExist() {
|
2020-07-06 07:15:13 -04:00
|
|
|
const { uid, gid } = await getUidAndGid();
|
|
|
|
const filePath = (await Deno.makeTempDir()) + "/chown_test_file.txt";
|
2020-06-12 02:36:20 +10:00
|
|
|
|
2021-10-11 21:21:18 +08:00
|
|
|
await assertRejects(
|
|
|
|
async () => {
|
|
|
|
await Deno.chown(filePath, uid, gid);
|
|
|
|
},
|
|
|
|
Deno.errors.NotFound,
|
|
|
|
`chown '${filePath}'`,
|
|
|
|
);
|
2020-07-14 15:24:17 -04:00
|
|
|
},
|
2020-07-06 07:15:13 -04:00
|
|
|
);
|
2020-06-12 02:36:20 +10:00
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
Deno.test(
|
2021-09-23 07:50:50 +08:00
|
|
|
{ permissions: { write: true }, ignore: Deno.build.os == "windows" },
|
2021-08-05 13:08:58 +02:00
|
|
|
function chownSyncPermissionDenied() {
|
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-12 02:36:20 +10:00
|
|
|
|
2020-08-25 00:21:56 +08: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-25 00:21:56 +08: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 18:58:58 -07:00
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
Deno.test(
|
2021-09-23 07:50:50 +08:00
|
|
|
{ permissions: { write: true }, ignore: Deno.build.os == "windows" },
|
2021-08-05 13:08:58 +02:00
|
|
|
async function chownPermissionDenied() {
|
2020-07-06 07:15:13 -04:00
|
|
|
const dirPath = await Deno.makeTempDir();
|
|
|
|
const filePath = dirPath + "/chown_test_file.txt";
|
|
|
|
await Deno.writeTextFile(filePath, "Hello");
|
|
|
|
|
2021-09-22 21:21:11 +08:00
|
|
|
await assertRejects(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-25 00:21:56 +08: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-12 02:36:20 +10:00
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
Deno.test(
|
2021-09-23 07:50:50 +08:00
|
|
|
{
|
|
|
|
permissions: { run: true, write: true },
|
|
|
|
ignore: Deno.build.os == "windows",
|
|
|
|
},
|
2021-08-05 13:08:58 +02:00
|
|
|
async function chownSyncSucceed() {
|
2021-01-17 00:32:59 +01:00
|
|
|
// TODO(bartlomieju): when a file's owner is actually being changed,
|
2023-06-26 15:10:27 +02:00
|
|
|
// chown only succeeds if run under privileged user (root)
|
2020-07-06 07:15:13 -04:00
|
|
|
// 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-12 02:36:20 +10: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-12 02:36:20 +10:00
|
|
|
|
2020-07-06 07:15:13 -04:00
|
|
|
// the test script creates this file with the same uid and gid,
|
2023-06-26 15:10:27 +02:00
|
|
|
// here chown is a noop so it succeeds under non-privileged user
|
2020-07-06 07:15:13 -04:00
|
|
|
Deno.chownSync(filePath, uid, gid);
|
2020-06-12 02:36:20 +10: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
|
|
|
);
|
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
Deno.test(
|
2021-09-23 07:50:50 +08:00
|
|
|
{
|
|
|
|
permissions: { run: true, write: true },
|
|
|
|
ignore: Deno.build.os == "windows",
|
|
|
|
},
|
2021-08-05 13:08:58 +02:00
|
|
|
async function chownSyncWithUrl() {
|
2020-07-06 07:15:13 -04:00
|
|
|
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
|
|
|
);
|
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
Deno.test(
|
2021-09-23 07:50:50 +08:00
|
|
|
{
|
|
|
|
permissions: { run: true, write: true },
|
|
|
|
ignore: Deno.build.os == "windows",
|
|
|
|
},
|
2021-08-05 13:08:58 +02:00
|
|
|
async function chownSucceed() {
|
2020-07-06 07:15:13 -04:00
|
|
|
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
|
|
|
);
|
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
Deno.test(
|
2021-09-23 07:50:50 +08:00
|
|
|
{
|
|
|
|
permissions: { run: true, write: true },
|
|
|
|
ignore: Deno.build.os == "windows",
|
|
|
|
},
|
2021-08-05 13:08:58 +02:00
|
|
|
async function chownUidOnly() {
|
2020-07-06 07:15:13 -04:00
|
|
|
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
|
|
|
);
|
|
|
|
|
2021-11-23 17:45:18 +01:00
|
|
|
Deno.test(
|
2021-09-23 07:50:50 +08:00
|
|
|
{
|
|
|
|
permissions: { run: true, write: true },
|
|
|
|
ignore: Deno.build.os == "windows",
|
|
|
|
},
|
2021-08-05 13:08:58 +02:00
|
|
|
async function chownWithUrl() {
|
2021-01-17 00:32:59 +01:00
|
|
|
// TODO(bartlomieju): same as chownSyncSucceed
|
2020-07-06 07:15:13 -04:00
|
|
|
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,
|
2023-06-26 15:10:27 +02:00
|
|
|
// here chown is a noop so it succeeds under non-privileged user
|
2020-07-06 07:15:13 -04:00
|
|
|
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
|
|
|
);
|