mirror of
https://github.com/denoland/deno.git
synced 2024-12-23 07:44:48 -05:00
c3a205bae8
Corrections made: * cli/js/tests/README.md:44:7: corrected "discoveres" to "discovers" * cli/js/tests/chown_test.ts:111:37: corrected "priviledge" to "privilege" * cli/worker.rs:231:56: corrected "decendants" to "descendants" * deno_typescript/lib.rs:136:50: corrected "emmited" to "emitted" * core/es_isolate.rs:492:67: corrected "registerd" to "registered" * core/isolate.rs:103:28: corrected "initalize" to "initialize" * docs/runtime.md:29:14: corrected "ect" to "etc" * docs/tools/debugger.md:122:16: corrected "implementes" to "implements" * std/encoding/_yaml/dumper/dumper_state.ts:57:63: corrected "everwhere" to "everywhere" * std/encoding/csv.ts:37:43: corrected "referal" to "referral" * std/fmt/sprintf.ts:209:20: corrected "unusuable" to "unusable" * std/fmt/README.md:21:40: corrected "Alternativly" to "Alternatively" * std/fmt/README.md:35:68: corrected "seperated" to "separated" * std/fmt/README.md:179:59: corrected "provded" to "provided" * std/mime/multipart.ts:581:46: corrected "writen" to "written" * std/path/_globrex.ts:19:52: corrected "equivelant" to "equivalent" * std/node/events_test.ts:447:9: corrected "asyncronous" to "asynchronous" * std/node/events_test.ts:475:9: corrected "asyncronous" to "asynchronous" * std/node/events_test.ts:500:29: corrected "asyncronous" to "asynchronous" * std/node/events_test.ts:530:40: corrected "asyncronous" to "asynchronous" * std/node/events_test.ts:555:9: corrected "asyncronous" to "asynchronous" * tools/deno_tcp_proxy.ts:1:42: corrected "perfromance" to "performance" * std/node/module.ts:1003:18: corrected "existend" to "existed"
147 lines
4.6 KiB
TypeScript
147 lines
4.6 KiB
TypeScript
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
import { unitTest, assertEquals, assert } from "./test_util.ts";
|
|
|
|
// chown on Windows is noop for now, so ignore its testing on Windows
|
|
if (Deno.build.os !== "windows") {
|
|
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",
|
|
cmd: ["python", "-c", "import os; print(os.getuid())"],
|
|
});
|
|
const gidProc = Deno.run({
|
|
stdout: "piped",
|
|
cmd: ["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 };
|
|
}
|
|
|
|
unitTest(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);
|
|
}
|
|
});
|
|
|
|
unitTest(
|
|
{ perms: { 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);
|
|
}
|
|
}
|
|
);
|
|
|
|
unitTest(
|
|
{ perms: { 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);
|
|
}
|
|
}
|
|
);
|
|
|
|
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);
|
|
|
|
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 });
|
|
}
|
|
);
|
|
|
|
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);
|
|
|
|
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 });
|
|
}
|
|
);
|
|
|
|
unitTest(
|
|
{ perms: { 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 privilege, 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 });
|
|
}
|
|
);
|
|
|
|
unitTest(
|
|
{ perms: { 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 });
|
|
}
|
|
);
|
|
}
|