Skip to content

Commit

Permalink
Add --allow-read (#1689)
Browse files Browse the repository at this point in the history
Co-authored-by: Greg Altman <[email protected]>
  • Loading branch information
2 people authored and ry committed Feb 8, 2019
1 parent 3abaf9e commit 9ab0338
Show file tree
Hide file tree
Showing 25 changed files with 395 additions and 118 deletions.
8 changes: 4 additions & 4 deletions js/chmod_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as deno from "deno";

const isNotWindows = deno.platform.os !== "win";

testPerm({ write: true }, function chmodSyncSuccess() {
testPerm({ read: true, write: true }, function chmodSyncSuccess() {
const enc = new TextEncoder();
const data = enc.encode("Hello");
const tempDir = deno.makeTempDirSync();
Expand All @@ -23,7 +23,7 @@ testPerm({ write: true }, function chmodSyncSuccess() {

// Check symlink when not on windows
if (isNotWindows) {
testPerm({ write: true }, function chmodSyncSymlinkSuccess() {
testPerm({ read: true, write: true }, function chmodSyncSymlinkSuccess() {
const enc = new TextEncoder();
const data = enc.encode("Hello");
const tempDir = deno.makeTempDirSync();
Expand Down Expand Up @@ -69,7 +69,7 @@ testPerm({ write: false }, function chmodSyncPerm() {
assertEqual(err.name, "PermissionDenied");
});

testPerm({ write: true }, async function chmodSuccess() {
testPerm({ read: true, write: true }, async function chmodSuccess() {
const enc = new TextEncoder();
const data = enc.encode("Hello");
const tempDir = deno.makeTempDirSync();
Expand All @@ -88,7 +88,7 @@ testPerm({ write: true }, async function chmodSuccess() {

// Check symlink when not on windows
if (isNotWindows) {
testPerm({ write: true }, async function chmodSymlinkSuccess() {
testPerm({ read: true, write: true }, async function chmodSymlinkSuccess() {
const enc = new TextEncoder();
const data = enc.encode("Hello");
const tempDir = deno.makeTempDirSync();
Expand Down
72 changes: 48 additions & 24 deletions js/copy_file_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function assertSameContent(filename1: string, filename2: string) {
assertEqual(data1, data2);
}

testPerm({ write: true }, function copyFileSyncSuccess() {
testPerm({ read: true, write: true }, function copyFileSyncSuccess() {
const tempDir = deno.makeTempDirSync();
const fromFilename = tempDir + "/from.txt";
const toFilename = tempDir + "/to.txt";
Expand All @@ -32,7 +32,7 @@ testPerm({ write: true }, function copyFileSyncSuccess() {
assertSameContent(fromFilename, toFilename);
});

testPerm({ write: true }, function copyFileSyncFailure() {
testPerm({ write: true, read: true }, function copyFileSyncFailure() {
const tempDir = deno.makeTempDirSync();
const fromFilename = tempDir + "/from.txt";
const toFilename = tempDir + "/to.txt";
Expand All @@ -48,7 +48,31 @@ testPerm({ write: true }, function copyFileSyncFailure() {
assertEqual(err.name, "NotFound");
});

testPerm({ write: true }, function copyFileSyncOverwrite() {
testPerm({ write: true, read: false }, function copyFileSyncPerm1() {
let caughtError = false;
try {
deno.copyFileSync("/from.txt", "/to.txt");
} catch (e) {
caughtError = true;
assertEqual(e.kind, deno.ErrorKind.PermissionDenied);
assertEqual(e.name, "PermissionDenied");
}
assert(caughtError);
});

testPerm({ write: false, read: true }, function copyFileSyncPerm2() {
let caughtError = false;
try {
deno.copyFileSync("/from.txt", "/to.txt");
} catch (e) {
caughtError = true;
assertEqual(e.kind, deno.ErrorKind.PermissionDenied);
assertEqual(e.name, "PermissionDenied");
}
assert(caughtError);
});

testPerm({ read: true, write: true }, function copyFileSyncOverwrite() {
const tempDir = deno.makeTempDirSync();
const fromFilename = tempDir + "/from.txt";
const toFilename = tempDir + "/to.txt";
Expand All @@ -62,19 +86,7 @@ testPerm({ write: true }, function copyFileSyncOverwrite() {
assertSameContent(fromFilename, toFilename);
});

testPerm({ write: false }, function copyFileSyncPerm() {
let err;
try {
deno.copyFileSync("/from.txt", "/to.txt");
} catch (e) {
err = e;
}
assert(!!err);
assertEqual(err.kind, deno.ErrorKind.PermissionDenied);
assertEqual(err.name, "PermissionDenied");
});

testPerm({ write: true }, async function copyFileSuccess() {
testPerm({ read: true, write: true }, async function copyFileSuccess() {
const tempDir = deno.makeTempDirSync();
const fromFilename = tempDir + "/from.txt";
const toFilename = tempDir + "/to.txt";
Expand All @@ -86,7 +98,7 @@ testPerm({ write: true }, async function copyFileSuccess() {
assertSameContent(fromFilename, toFilename);
});

testPerm({ write: true }, async function copyFileFailure() {
testPerm({ read: true, write: true }, async function copyFileFailure() {
const tempDir = deno.makeTempDirSync();
const fromFilename = tempDir + "/from.txt";
const toFilename = tempDir + "/to.txt";
Expand All @@ -102,7 +114,7 @@ testPerm({ write: true }, async function copyFileFailure() {
assertEqual(err.name, "NotFound");
});

testPerm({ write: true }, async function copyFileOverwrite() {
testPerm({ read: true, write: true }, async function copyFileOverwrite() {
const tempDir = deno.makeTempDirSync();
const fromFilename = tempDir + "/from.txt";
const toFilename = tempDir + "/to.txt";
Expand All @@ -116,14 +128,26 @@ testPerm({ write: true }, async function copyFileOverwrite() {
assertSameContent(fromFilename, toFilename);
});

testPerm({ write: false }, async function copyFilePerm() {
let err;
testPerm({ read: false, write: true }, async function copyFilePerm1() {
let caughtError = false;
try {
await deno.copyFile("/from.txt", "/to.txt");
} catch (e) {
err = e;
caughtError = true;
assertEqual(e.kind, deno.ErrorKind.PermissionDenied);
assertEqual(e.name, "PermissionDenied");
}
assert(!!err);
assertEqual(err.kind, deno.ErrorKind.PermissionDenied);
assertEqual(err.name, "PermissionDenied");
assert(caughtError);
});

testPerm({ read: true, write: false }, async function copyFilePerm2() {
let caughtError = false;
try {
await deno.copyFile("/from.txt", "/to.txt");
} catch (e) {
caughtError = true;
assertEqual(e.kind, deno.ErrorKind.PermissionDenied);
assertEqual(e.name, "PermissionDenied");
}
assert(caughtError);
});
40 changes: 34 additions & 6 deletions js/files_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ test(function filesStdioFileDescriptors() {
assertEqual(deno.stderr.rid, 2);
});

test(async function filesCopyToStdout() {
testPerm({ read: true }, async function filesCopyToStdout() {
const filename = "package.json";
const file = await deno.open(filename);
assert(file.rid > 2);
Expand All @@ -18,7 +18,7 @@ test(async function filesCopyToStdout() {
console.log("bytes written", bytesWritten);
});

test(async function filesToAsyncIterator() {
testPerm({ read: true }, async function filesToAsyncIterator() {
const filename = "tests/hello.txt";
const file = await deno.open(filename);

Expand All @@ -32,7 +32,7 @@ test(async function filesToAsyncIterator() {

testPerm({ write: false }, async function writePermFailure() {
const filename = "tests/hello.txt";
const writeModes: deno.OpenMode[] = ["r+", "w", "w+", "a", "a+", "x", "x+"];
const writeModes: deno.OpenMode[] = ["w", "a", "x"];
for (const mode of writeModes) {
let err;
try {
Expand All @@ -46,7 +46,35 @@ testPerm({ write: false }, async function writePermFailure() {
}
});

testPerm({ write: true }, async function createFile() {
testPerm({ read: false }, async function readPermFailure() {
let caughtError = false;
try {
await deno.open("package.json", "r");
} catch (e) {
caughtError = true;
assertEqual(e.kind, deno.ErrorKind.PermissionDenied);
assertEqual(e.name, "PermissionDenied");
}
assert(caughtError);
});

testPerm({ write: false, read: false }, async function readWritePermFailure() {
const filename = "tests/hello.txt";
const writeModes: deno.OpenMode[] = ["r+", "w+", "a+", "x+"];
for (const mode of writeModes) {
let err;
try {
await deno.open(filename, mode);
} catch (e) {
err = e;
}
assert(!!err);
assertEqual(err.kind, deno.ErrorKind.PermissionDenied);
assertEqual(err.name, "PermissionDenied");
}
});

testPerm({ read: true, write: true }, async function createFile() {
const tempDir = await deno.makeTempDir();
const filename = tempDir + "/test.txt";
const f = await deno.open(filename, "w");
Expand All @@ -64,7 +92,7 @@ testPerm({ write: true }, async function createFile() {
await deno.remove(tempDir, { recursive: true });
});

testPerm({ write: true }, async function openModeWrite() {
testPerm({ read: true, write: true }, async function openModeWrite() {
const tempDir = deno.makeTempDirSync();
const encoder = new TextEncoder();
const filename = tempDir + "hello.txt";
Expand Down Expand Up @@ -98,7 +126,7 @@ testPerm({ write: true }, async function openModeWrite() {
await deno.remove(tempDir, { recursive: true });
});

testPerm({ write: true }, async function openModeWriteRead() {
testPerm({ read: true, write: true }, async function openModeWriteRead() {
const tempDir = deno.makeTempDirSync();
const encoder = new TextEncoder();
const filename = tempDir + "hello.txt";
Expand Down
10 changes: 5 additions & 5 deletions js/mkdir_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
import { testPerm, assert, assertEqual } from "./test_util.ts";
import * as deno from "deno";

testPerm({ write: true }, function mkdirSyncSuccess() {
testPerm({ read: true, write: true }, function mkdirSyncSuccess() {
const path = deno.makeTempDirSync() + "/dir";
deno.mkdirSync(path);
const pathInfo = deno.statSync(path);
assert(pathInfo.isDirectory());
});

testPerm({ write: true }, function mkdirSyncMode() {
testPerm({ read: true, write: true }, function mkdirSyncMode() {
const path = deno.makeTempDirSync() + "/dir";
deno.mkdirSync(path, false, 0o755); // no perm for x
const pathInfo = deno.statSync(path);
Expand All @@ -30,7 +30,7 @@ testPerm({ write: false }, function mkdirSyncPerm() {
assertEqual(err.name, "PermissionDenied");
});

testPerm({ write: true }, async function mkdirSuccess() {
testPerm({ read: true, write: true }, async function mkdirSuccess() {
const path = deno.makeTempDirSync() + "/dir";
await deno.mkdir(path);
const pathInfo = deno.statSync(path);
Expand All @@ -48,14 +48,14 @@ testPerm({ write: true }, function mkdirErrIfExists() {
assertEqual(err.name, "AlreadyExists");
});

testPerm({ write: true }, function mkdirSyncRecursive() {
testPerm({ read: true, write: true }, function mkdirSyncRecursive() {
const path = deno.makeTempDirSync() + "/nested/directory";
deno.mkdirSync(path, true);
const pathInfo = deno.statSync(path);
assert(pathInfo.isDirectory());
});

testPerm({ write: true }, async function mkdirRecursive() {
testPerm({ read: true, write: true }, async function mkdirRecursive() {
const path = deno.makeTempDirSync() + "/nested/directory";
await deno.mkdir(path, true);
const pathInfo = deno.statSync(path);
Expand Down
34 changes: 29 additions & 5 deletions js/read_dir_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { test, testPerm, assert, assertEqual } from "./test_util.ts";
import { testPerm, assert, assertEqual } from "./test_util.ts";
import * as deno from "deno";
import { FileInfo } from "deno";

Expand All @@ -22,12 +22,24 @@ function assertSameContent(files: FileInfo[]) {
assertEqual(counter, 2);
}

test(function readDirSyncSuccess() {
testPerm({ read: true }, function readDirSyncSuccess() {
const files = deno.readDirSync("tests/");
assertSameContent(files);
});

test(function readDirSyncNotDir() {
testPerm({ read: false }, function readDirSyncPerm() {
let caughtError = false;
try {
const files = deno.readDirSync("tests/");
} catch (e) {
caughtError = true;
assertEqual(e.kind, deno.ErrorKind.PermissionDenied);
assertEqual(e.name, "PermissionDenied");
}
assert(caughtError);
});

testPerm({ read: true }, function readDirSyncNotDir() {
let caughtError = false;
let src;

Expand All @@ -41,7 +53,7 @@ test(function readDirSyncNotDir() {
assertEqual(src, undefined);
});

test(function readDirSyncNotFound() {
testPerm({ read: true }, function readDirSyncNotFound() {
let caughtError = false;
let src;

Expand All @@ -55,7 +67,19 @@ test(function readDirSyncNotFound() {
assertEqual(src, undefined);
});

test(async function readDirSuccess() {
testPerm({ read: true }, async function readDirSuccess() {
const files = await deno.readDir("tests/");
assertSameContent(files);
});

testPerm({ read: false }, async function readDirPerm() {
let caughtError = false;
try {
const files = await deno.readDir("tests/");
} catch (e) {
caughtError = true;
assertEqual(e.kind, deno.ErrorKind.PermissionDenied);
assertEqual(e.name, "PermissionDenied");
}
assert(caughtError);
});
Loading

0 comments on commit 9ab0338

Please sign in to comment.