Skip to content

Commit

Permalink
chore(cli): add permission test (#23633)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmastrac committed May 1, 2024
1 parent e0069c8 commit 505130d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tests/specs/permission/special/__test__.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"args": "run --allow-read main.js",
"output": "[WILDCARD]",
"exitCode": 123
}
55 changes: 55 additions & 0 deletions tests/specs/permission/special/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Opening certain files requires --allow-all permission. This file is executed with
// --allow-read only.

const testCases = [
// Allowed, safe
[["darwin", "linux"], null, "/dev/null"],
// Denied, requires `--allow-all`
[["darwin", "linux"], /PermissionDenied/, "/etc/hosts"],
[["darwin", "linux"], /PermissionDenied/, "/dev/ptmx"],
[["linux"], /PermissionDenied/, "/proc/self/environ"],
[["linux"], /PermissionDenied/, "/proc/self/mem"],
[["windows"], /PermissionDenied/, "\\\\.\\PhysicalDrive0"],
];

const os = Deno.build.os;
let failed = false;
let ran = false;

for (const [oses, error, file] of testCases) {
if (oses.indexOf(os) === -1) {
console.log(`Skipping test for ${file} on ${os}`);
continue;
}
ran = true;
try {
console.log(`Opening ${file}...`);
Deno.readTextFileSync(file);
if (error === null) {
console.log("Succeeded, as expected.");
} else {
console.log(`*** Shouldn't have succeeded: ${file}`);
failed = true;
}
} catch (e) {
if (error === null) {
console.log(`*** Shouldn't have failed: ${file}: ${e}`);
failed = true;
} else {
if (String(e).match(error)) {
console.log(`Got an error (expected) for ${file}: ${e}`);
} else {
console.log(`*** Got an unexpected error for ${file}: ${e}`);
}
}
}
}

if (!ran) {
console.log(`Uh-oh: didn't run any tests for ${Deno.build.os}.`);
failed = true;
}
if (failed) {
console.log("One or more tests failed");
}
Deno.exit(failed ? 321 : 123);

0 comments on commit 505130d

Please sign in to comment.