2021-04-30 11:56:47 -04:00
|
|
|
use crate::tools::test_runner::TestEvent;
|
2021-04-28 14:17:04 -04:00
|
|
|
use crate::tools::test_runner::TestMessage;
|
2021-04-25 17:38:59 -04:00
|
|
|
use deno_core::error::generic_error;
|
|
|
|
use deno_core::error::AnyError;
|
2021-04-28 14:17:04 -04:00
|
|
|
use deno_core::JsRuntime;
|
2021-04-30 11:56:47 -04:00
|
|
|
use deno_core::ModuleSpecifier;
|
2021-04-25 17:38:59 -04:00
|
|
|
use deno_core::OpState;
|
|
|
|
use deno_runtime::ops::worker_host::create_worker_permissions;
|
|
|
|
use deno_runtime::ops::worker_host::PermissionsArg;
|
|
|
|
use deno_runtime::permissions::Permissions;
|
2021-04-28 14:17:04 -04:00
|
|
|
use serde::Deserialize;
|
|
|
|
use std::sync::mpsc::Sender;
|
2021-04-25 17:38:59 -04:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
2021-04-28 14:17:04 -04:00
|
|
|
pub fn init(rt: &mut JsRuntime) {
|
2021-04-25 17:38:59 -04:00
|
|
|
super::reg_sync(rt, "op_pledge_test_permissions", op_pledge_test_permissions);
|
|
|
|
super::reg_sync(
|
|
|
|
rt,
|
|
|
|
"op_restore_test_permissions",
|
|
|
|
op_restore_test_permissions,
|
|
|
|
);
|
2021-04-28 14:17:04 -04:00
|
|
|
super::reg_sync(rt, "op_post_test_message", op_post_test_message);
|
2021-04-25 17:38:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct PermissionsHolder(Uuid, Permissions);
|
|
|
|
|
|
|
|
pub fn op_pledge_test_permissions(
|
|
|
|
state: &mut OpState,
|
2021-05-14 08:25:50 -04:00
|
|
|
args: PermissionsArg,
|
2021-05-08 08:37:42 -04:00
|
|
|
_: (),
|
2021-04-25 17:38:59 -04:00
|
|
|
) -> Result<Uuid, AnyError> {
|
|
|
|
deno_runtime::ops::check_unstable(state, "Deno.test.permissions");
|
|
|
|
|
|
|
|
let token = Uuid::new_v4();
|
|
|
|
let parent_permissions = state.borrow::<Permissions>().clone();
|
2021-05-14 08:25:50 -04:00
|
|
|
let worker_permissions =
|
|
|
|
create_worker_permissions(parent_permissions.clone(), args)?;
|
2021-04-25 17:38:59 -04:00
|
|
|
|
|
|
|
state.put::<PermissionsHolder>(PermissionsHolder(token, parent_permissions));
|
|
|
|
|
|
|
|
// NOTE: This call overrides current permission set for the worker
|
|
|
|
state.put::<Permissions>(worker_permissions);
|
|
|
|
|
|
|
|
Ok(token)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn op_restore_test_permissions(
|
|
|
|
state: &mut OpState,
|
|
|
|
token: Uuid,
|
2021-05-08 08:37:42 -04:00
|
|
|
_: (),
|
2021-04-25 17:38:59 -04:00
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
deno_runtime::ops::check_unstable(state, "Deno.test.permissions");
|
|
|
|
|
|
|
|
if let Some(permissions_holder) = state.try_take::<PermissionsHolder>() {
|
|
|
|
if token != permissions_holder.0 {
|
|
|
|
panic!("restore test permissions token does not match the stored token");
|
|
|
|
}
|
|
|
|
|
|
|
|
let permissions = permissions_holder.1;
|
|
|
|
state.put::<Permissions>(permissions);
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(generic_error("no permissions to restore"))
|
|
|
|
}
|
|
|
|
}
|
2021-04-28 14:17:04 -04:00
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct PostTestMessageArgs {
|
|
|
|
message: TestMessage,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn op_post_test_message(
|
|
|
|
state: &mut OpState,
|
2021-05-14 08:25:50 -04:00
|
|
|
args: PostTestMessageArgs,
|
2021-05-08 08:37:42 -04:00
|
|
|
_: (),
|
2021-05-14 08:25:50 -04:00
|
|
|
) -> Result<bool, AnyError> {
|
2021-04-30 11:56:47 -04:00
|
|
|
let origin = state.borrow::<ModuleSpecifier>().to_string();
|
|
|
|
let message = args.message;
|
2021-04-28 14:17:04 -04:00
|
|
|
|
2021-04-30 11:56:47 -04:00
|
|
|
let event = TestEvent { origin, message };
|
|
|
|
|
|
|
|
let sender = state.borrow::<Sender<TestEvent>>().clone();
|
|
|
|
|
|
|
|
if sender.send(event).is_err() {
|
2021-05-14 08:25:50 -04:00
|
|
|
Ok(false)
|
2021-04-28 14:17:04 -04:00
|
|
|
} else {
|
2021-05-14 08:25:50 -04:00
|
|
|
Ok(true)
|
2021-04-28 14:17:04 -04:00
|
|
|
}
|
|
|
|
}
|