2019-01-21 14:03:30 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2018-08-23 19:47:43 -04:00
|
|
|
//
|
|
|
|
// We want to test many ops in deno which have different behavior depending on
|
|
|
|
// the permissions set. These tests can specify which permissions they expect,
|
|
|
|
// which appends a special string like "permW1N0" to the end of the test name.
|
|
|
|
// Here we run several copies of deno with different permissions, filtering the
|
|
|
|
// tests by the special string. permW0N0 means allow-write but not allow-net.
|
|
|
|
// See tools/unit_tests.py for more details.
|
|
|
|
|
|
|
|
import * as deno from "deno";
|
2019-01-03 14:55:22 -05:00
|
|
|
import * as testing from "./deps/https/deno.land/x/std/testing/mod.ts";
|
|
|
|
export {
|
|
|
|
assert,
|
|
|
|
assertEqual
|
|
|
|
} from "./deps/https/deno.land/x/std/testing/mod.ts";
|
2018-08-23 19:47:43 -04:00
|
|
|
|
|
|
|
// testing.setFilter must be run before any tests are defined.
|
2018-09-22 08:47:44 -04:00
|
|
|
testing.setFilter(deno.args[1]);
|
2018-08-23 19:47:43 -04:00
|
|
|
|
|
|
|
interface DenoPermissions {
|
|
|
|
write?: boolean;
|
|
|
|
net?: boolean;
|
2018-08-31 07:51:12 -04:00
|
|
|
env?: boolean;
|
2018-11-15 23:07:40 -05:00
|
|
|
run?: boolean;
|
2018-08-23 19:47:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function permToString(perms: DenoPermissions): string {
|
|
|
|
const w = perms.write ? 1 : 0;
|
|
|
|
const n = perms.net ? 1 : 0;
|
2018-08-31 07:51:12 -04:00
|
|
|
const e = perms.env ? 1 : 0;
|
2018-11-15 23:07:40 -05:00
|
|
|
const r = perms.run ? 1 : 0;
|
|
|
|
return `permW${w}N${n}E${e}R${r}`;
|
2018-08-23 19:47:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function permFromString(s: string): DenoPermissions {
|
2018-11-15 23:07:40 -05:00
|
|
|
const re = /^permW([01])N([01])E([01])R([01])$/;
|
2018-08-23 19:47:43 -04:00
|
|
|
const found = s.match(re);
|
|
|
|
if (!found) {
|
|
|
|
throw Error("Not a permission string");
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
write: Boolean(Number(found[1])),
|
2018-08-31 07:51:12 -04:00
|
|
|
net: Boolean(Number(found[2])),
|
2018-11-15 23:07:40 -05:00
|
|
|
env: Boolean(Number(found[3])),
|
|
|
|
run: Boolean(Number(found[4]))
|
2018-08-23 19:47:43 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function testPerm(perms: DenoPermissions, fn: testing.TestFunction) {
|
|
|
|
const name = `${fn.name}_${permToString(perms)}`;
|
|
|
|
testing.test({ fn, name });
|
|
|
|
}
|
|
|
|
|
|
|
|
export function test(fn: testing.TestFunction) {
|
2018-08-31 07:51:12 -04:00
|
|
|
testPerm({ write: false, net: false, env: false }, fn);
|
2018-08-23 19:47:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
test(function permSerialization() {
|
2018-08-31 07:51:12 -04:00
|
|
|
for (const write of [true, false]) {
|
|
|
|
for (const net of [true, false]) {
|
|
|
|
for (const env of [true, false]) {
|
2018-11-15 23:07:40 -05:00
|
|
|
for (const run of [true, false]) {
|
|
|
|
const perms: DenoPermissions = { write, net, env, run };
|
|
|
|
testing.assertEqual(perms, permFromString(permToString(perms)));
|
|
|
|
}
|
2018-08-31 07:51:12 -04:00
|
|
|
}
|
2018-08-23 19:47:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// To better catch internal errors, permFromString should throw if it gets an
|
|
|
|
// invalid permission string.
|
|
|
|
test(function permFromStringThrows() {
|
|
|
|
let threw = false;
|
|
|
|
try {
|
|
|
|
permFromString("bad");
|
|
|
|
} catch (e) {
|
|
|
|
threw = true;
|
|
|
|
}
|
|
|
|
testing.assert(threw);
|
|
|
|
});
|