2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2020-11-14 07:10:23 -05:00
|
|
|
import {
|
2021-02-24 22:33:09 -05:00
|
|
|
assert,
|
2020-11-14 07:10:23 -05:00
|
|
|
assertEquals,
|
2021-09-22 09:21:11 -04:00
|
|
|
assertRejects,
|
2020-11-14 07:10:23 -05:00
|
|
|
assertThrows,
|
|
|
|
} from "./test_util.ts";
|
2019-10-27 11:22:53 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(async function permissionInvalidName() {
|
2021-09-22 09:21:11 -04:00
|
|
|
await assertRejects(async () => {
|
2020-11-03 10:19:29 -05:00
|
|
|
// deno-lint-ignore no-explicit-any
|
2020-07-13 12:23:24 -04:00
|
|
|
await Deno.permissions.query({ name: "foo" as any });
|
2021-02-24 22:33:09 -05:00
|
|
|
}, TypeError);
|
2019-10-27 11:22:53 -04:00
|
|
|
});
|
|
|
|
|
2023-01-24 18:42:44 -05:00
|
|
|
Deno.test(function permissionInvalidNameSync() {
|
|
|
|
assertThrows(() => {
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
Deno.permissions.querySync({ name: "foo" as any });
|
|
|
|
}, TypeError);
|
|
|
|
});
|
|
|
|
|
2024-10-04 15:55:41 -04:00
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: [] } },
|
|
|
|
async function permissionNetInvalidHost() {
|
|
|
|
await assertRejects(async () => {
|
|
|
|
await Deno.permissions.query({ name: "net", host: ":" });
|
|
|
|
}, URIError);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { net: [] } },
|
|
|
|
function permissionNetInvalidHostSync() {
|
|
|
|
assertThrows(() => {
|
|
|
|
Deno.permissions.querySync({ name: "net", host: ":" });
|
|
|
|
}, URIError);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { sys: [] } },
|
|
|
|
async function permissionSysValidKind() {
|
|
|
|
await Deno.permissions.query({ name: "sys", kind: "loadavg" });
|
|
|
|
await Deno.permissions.query({ name: "sys", kind: "osRelease" });
|
|
|
|
await Deno.permissions.query({ name: "sys", kind: "osUptime" });
|
|
|
|
await Deno.permissions.query({ name: "sys", kind: "networkInterfaces" });
|
|
|
|
await Deno.permissions.query({ name: "sys", kind: "systemMemoryInfo" });
|
|
|
|
await Deno.permissions.query({ name: "sys", kind: "hostname" });
|
|
|
|
await Deno.permissions.query({ name: "sys", kind: "uid" });
|
|
|
|
await Deno.permissions.query({ name: "sys", kind: "gid" });
|
|
|
|
await Deno.permissions.query({ name: "sys", kind: "cpus" });
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { sys: [] } },
|
|
|
|
function permissionSysValidKindSync() {
|
|
|
|
Deno.permissions.querySync({ name: "sys", kind: "loadavg" });
|
|
|
|
Deno.permissions.querySync({ name: "sys", kind: "osRelease" });
|
|
|
|
Deno.permissions.querySync({ name: "sys", kind: "networkInterfaces" });
|
|
|
|
Deno.permissions.querySync({ name: "sys", kind: "systemMemoryInfo" });
|
|
|
|
Deno.permissions.querySync({ name: "sys", kind: "hostname" });
|
|
|
|
Deno.permissions.querySync({ name: "sys", kind: "uid" });
|
|
|
|
Deno.permissions.querySync({ name: "sys", kind: "gid" });
|
|
|
|
Deno.permissions.querySync({ name: "sys", kind: "cpus" });
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { sys: [] } },
|
|
|
|
async function permissionSysInvalidKind() {
|
|
|
|
await assertRejects(async () => {
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
await Deno.permissions.query({ name: "sys", kind: "abc" as any });
|
|
|
|
}, TypeError);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { sys: [] } },
|
|
|
|
function permissionSysInvalidKindSync() {
|
|
|
|
assertThrows(() => {
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
Deno.permissions.querySync({ name: "sys", kind: "abc" as any });
|
|
|
|
}, TypeError);
|
|
|
|
},
|
|
|
|
);
|
2023-01-24 18:42:44 -05:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(async function permissionQueryReturnsEventTarget() {
|
2024-09-03 05:24:25 -04:00
|
|
|
const status = await Deno.permissions.query({ name: "read", path: "." });
|
2021-02-24 22:33:09 -05:00
|
|
|
assert(["granted", "denied", "prompt"].includes(status.state));
|
|
|
|
let called = false;
|
|
|
|
status.addEventListener("change", () => {
|
|
|
|
called = true;
|
|
|
|
});
|
|
|
|
status.dispatchEvent(new Event("change"));
|
|
|
|
assert(called);
|
2024-09-03 05:24:25 -04:00
|
|
|
assert(
|
|
|
|
status === (await Deno.permissions.query({ name: "read", path: "." })),
|
|
|
|
);
|
2021-02-24 22:33:09 -05:00
|
|
|
});
|
|
|
|
|
2023-01-24 18:42:44 -05:00
|
|
|
Deno.test(function permissionQueryReturnsEventTargetSync() {
|
2024-09-03 05:24:25 -04:00
|
|
|
const status = Deno.permissions.querySync({ name: "read", path: "." });
|
2023-01-24 18:42:44 -05:00
|
|
|
assert(["granted", "denied", "prompt"].includes(status.state));
|
|
|
|
let called = false;
|
|
|
|
status.addEventListener("change", () => {
|
|
|
|
called = true;
|
|
|
|
});
|
|
|
|
status.dispatchEvent(new Event("change"));
|
|
|
|
assert(called);
|
2024-09-03 05:24:25 -04:00
|
|
|
assert(status === Deno.permissions.querySync({ name: "read", path: "." }));
|
2023-01-24 18:42:44 -05:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(async function permissionQueryForReadReturnsSameStatus() {
|
2021-02-24 22:33:09 -05:00
|
|
|
const status1 = await Deno.permissions.query({
|
|
|
|
name: "read",
|
|
|
|
path: ".",
|
|
|
|
});
|
|
|
|
const status2 = await Deno.permissions.query({
|
|
|
|
name: "read",
|
|
|
|
path: ".",
|
|
|
|
});
|
|
|
|
assert(status1 === status2);
|
|
|
|
});
|
|
|
|
|
2023-01-24 18:42:44 -05:00
|
|
|
Deno.test(function permissionQueryForReadReturnsSameStatusSync() {
|
|
|
|
const status1 = Deno.permissions.querySync({
|
|
|
|
name: "read",
|
|
|
|
path: ".",
|
|
|
|
});
|
|
|
|
const status2 = Deno.permissions.querySync({
|
|
|
|
name: "read",
|
|
|
|
path: ".",
|
|
|
|
});
|
|
|
|
assert(status1 === status2);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function permissionsIllegalConstructor() {
|
2024-09-13 05:38:45 -04:00
|
|
|
assertThrows(() => new Deno.Permissions(), TypeError, "Illegal constructor");
|
2021-02-24 22:33:09 -05:00
|
|
|
assertEquals(Deno.Permissions.length, 0);
|
2020-09-19 17:30:59 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function permissionStatusIllegalConstructor() {
|
2020-09-19 17:30:59 -04:00
|
|
|
assertThrows(
|
|
|
|
() => new Deno.PermissionStatus(),
|
|
|
|
TypeError,
|
2024-09-13 05:38:45 -04:00
|
|
|
"Illegal constructor",
|
2020-09-19 17:30:59 -04:00
|
|
|
);
|
2020-11-14 07:10:23 -05:00
|
|
|
assertEquals(Deno.PermissionStatus.length, 0);
|
2020-09-19 17:30:59 -04:00
|
|
|
});
|
2021-08-06 09:04:00 -04:00
|
|
|
|
2023-01-03 15:50:14 -05:00
|
|
|
// Regression test for https://github.com/denoland/deno/issues/17020
|
2024-10-04 15:55:41 -04:00
|
|
|
Deno.test(
|
|
|
|
{ permissions: { read: [], write: [], ffi: [], run: [] } },
|
|
|
|
async function permissionURL() {
|
|
|
|
const path = new URL(".", import.meta.url);
|
|
|
|
|
|
|
|
await Deno.permissions.query({ name: "read", path });
|
|
|
|
await Deno.permissions.query({ name: "write", path });
|
|
|
|
await Deno.permissions.query({ name: "ffi", path });
|
|
|
|
await Deno.permissions.query({ name: "run", command: path });
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
{ permissions: { read: [], write: [], ffi: [], run: [] } },
|
|
|
|
function permissionURLSync() {
|
|
|
|
Deno.permissions.querySync({
|
|
|
|
name: "read",
|
|
|
|
path: new URL(".", import.meta.url),
|
|
|
|
});
|
|
|
|
Deno.permissions.querySync({
|
|
|
|
name: "write",
|
|
|
|
path: new URL(".", import.meta.url),
|
|
|
|
});
|
|
|
|
Deno.permissions.querySync({
|
|
|
|
name: "run",
|
|
|
|
command: new URL(".", import.meta.url),
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
2023-01-24 18:42:44 -05:00
|
|
|
|
2022-05-19 17:45:09 -04:00
|
|
|
Deno.test(async function permissionDescriptorValidation() {
|
|
|
|
for (const value of [undefined, null, {}]) {
|
|
|
|
for (const method of ["query", "request", "revoke"]) {
|
|
|
|
await assertRejects(
|
|
|
|
async () => {
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
await (Deno.permissions as any)[method](value as any);
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
'"undefined" is not a valid permission name',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2022-09-16 07:46:38 -04:00
|
|
|
|
2023-01-24 18:42:44 -05:00
|
|
|
Deno.test(function permissionDescriptorValidationSync() {
|
|
|
|
for (const value of [undefined, null, {}]) {
|
|
|
|
for (const method of ["querySync", "revokeSync", "requestSync"]) {
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
(Deno.permissions as any)[method](value as any);
|
|
|
|
},
|
|
|
|
TypeError,
|
|
|
|
'"undefined" is not a valid permission name',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-09-16 07:46:38 -04:00
|
|
|
// Regression test for https://github.com/denoland/deno/issues/15894.
|
|
|
|
Deno.test(async function permissionStatusObjectsNotEqual() {
|
|
|
|
assert(
|
|
|
|
await Deno.permissions.query({ name: "env", variable: "A" }) !=
|
|
|
|
await Deno.permissions.query({ name: "env", variable: "B" }),
|
|
|
|
);
|
|
|
|
});
|
2023-01-24 18:42:44 -05:00
|
|
|
|
|
|
|
Deno.test(function permissionStatusObjectsNotEqualSync() {
|
|
|
|
assert(
|
|
|
|
Deno.permissions.querySync({ name: "env", variable: "A" }) !=
|
|
|
|
Deno.permissions.querySync({ name: "env", variable: "B" }),
|
|
|
|
);
|
|
|
|
});
|