2022-04-26 19:00:04 -04:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2022-04-15 08:24:41 -04:00
|
|
|
|
2022-07-15 13:09:22 -04:00
|
|
|
use crate::tools::test::TestDescription;
|
2021-08-26 15:21:58 -04:00
|
|
|
use crate::tools::test::TestEvent;
|
2022-05-01 14:44:55 -04:00
|
|
|
use crate::tools::test::TestEventSender;
|
2022-07-15 13:09:22 -04:00
|
|
|
use crate::tools::test::TestFilter;
|
|
|
|
use crate::tools::test::TestLocation;
|
|
|
|
use crate::tools::test::TestStepDescription;
|
2022-04-26 19:00:04 -04:00
|
|
|
|
2021-04-25 17:38:59 -04:00
|
|
|
use deno_core::error::generic_error;
|
|
|
|
use deno_core::error::AnyError;
|
2022-03-14 13:44:15 -04:00
|
|
|
use deno_core::op;
|
2021-12-29 08:30:08 -05:00
|
|
|
use deno_core::Extension;
|
2021-04-30 11:56:47 -04:00
|
|
|
use deno_core::ModuleSpecifier;
|
2021-04-25 17:38:59 -04:00
|
|
|
use deno_core::OpState;
|
2021-10-13 13:04:44 -04:00
|
|
|
use deno_runtime::permissions::create_child_permissions;
|
|
|
|
use deno_runtime::permissions::ChildPermissionsArg;
|
2021-04-25 17:38:59 -04:00
|
|
|
use deno_runtime::permissions::Permissions;
|
2022-07-15 13:09:22 -04:00
|
|
|
use serde::Deserialize;
|
|
|
|
use serde::Deserializer;
|
|
|
|
use serde::Serialize;
|
|
|
|
use std::sync::atomic::AtomicUsize;
|
|
|
|
use std::sync::atomic::Ordering;
|
2021-04-25 17:38:59 -04:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
2022-07-15 13:09:22 -04:00
|
|
|
pub fn init(sender: TestEventSender, filter: TestFilter) -> Extension {
|
2021-12-29 08:30:08 -05:00
|
|
|
Extension::builder()
|
|
|
|
.ops(vec![
|
2022-03-14 13:44:15 -04:00
|
|
|
op_pledge_test_permissions::decl(),
|
|
|
|
op_restore_test_permissions::decl(),
|
|
|
|
op_get_test_origin::decl(),
|
2022-07-15 13:09:22 -04:00
|
|
|
op_register_test::decl(),
|
|
|
|
op_register_test_step::decl(),
|
2022-03-14 13:44:15 -04:00
|
|
|
op_dispatch_test_event::decl(),
|
2021-12-29 08:30:08 -05:00
|
|
|
])
|
|
|
|
.state(move |state| {
|
|
|
|
state.put(sender.clone());
|
2022-07-15 13:09:22 -04:00
|
|
|
state.put(filter.clone());
|
2021-12-29 08:30:08 -05:00
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.build()
|
2021-04-25 17:38:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct PermissionsHolder(Uuid, Permissions);
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-04-25 17:38:59 -04:00
|
|
|
pub fn op_pledge_test_permissions(
|
|
|
|
state: &mut OpState,
|
2021-10-13 13:04:44 -04:00
|
|
|
args: ChildPermissionsArg,
|
2021-04-25 17:38:59 -04:00
|
|
|
) -> Result<Uuid, AnyError> {
|
|
|
|
let token = Uuid::new_v4();
|
2021-10-13 13:04:44 -04:00
|
|
|
let parent_permissions = state.borrow_mut::<Permissions>();
|
|
|
|
let worker_permissions = create_child_permissions(parent_permissions, args)?;
|
|
|
|
let parent_permissions = parent_permissions.clone();
|
2021-04-25 17:38:59 -04:00
|
|
|
|
2022-04-17 11:47:24 -04:00
|
|
|
if state.try_take::<PermissionsHolder>().is_some() {
|
|
|
|
panic!("pledge test permissions called before restoring previous pledge");
|
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-04-25 17:38:59 -04:00
|
|
|
pub fn op_restore_test_permissions(
|
|
|
|
state: &mut OpState,
|
|
|
|
token: Uuid,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
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
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2022-03-14 18:38:53 -04:00
|
|
|
fn op_get_test_origin(state: &mut OpState) -> Result<String, AnyError> {
|
2021-07-14 15:05:16 -04:00
|
|
|
Ok(state.borrow::<ModuleSpecifier>().to_string())
|
2021-04-28 14:17:04 -04:00
|
|
|
}
|
|
|
|
|
2022-07-15 13:09:22 -04:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct TestInfo {
|
|
|
|
name: String,
|
|
|
|
origin: String,
|
|
|
|
location: TestLocation,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct TestRegisterResult {
|
|
|
|
id: usize,
|
|
|
|
filtered_out: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
static NEXT_ID: AtomicUsize = AtomicUsize::new(0);
|
|
|
|
|
|
|
|
#[op]
|
|
|
|
fn op_register_test(
|
|
|
|
state: &mut OpState,
|
|
|
|
info: TestInfo,
|
|
|
|
) -> Result<TestRegisterResult, AnyError> {
|
|
|
|
let id = NEXT_ID.fetch_add(1, Ordering::SeqCst);
|
|
|
|
let filter = state.borrow::<TestFilter>().clone();
|
|
|
|
let filtered_out = !filter.includes(&info.name);
|
|
|
|
let description = TestDescription {
|
|
|
|
id,
|
|
|
|
name: info.name,
|
|
|
|
origin: info.origin,
|
|
|
|
location: info.location,
|
|
|
|
};
|
|
|
|
let mut sender = state.borrow::<TestEventSender>().clone();
|
|
|
|
sender.send(TestEvent::Register(description)).ok();
|
|
|
|
Ok(TestRegisterResult { id, filtered_out })
|
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_parent<'de, D>(deserializer: D) -> Result<usize, D::Error>
|
|
|
|
where
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
{
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct Parent {
|
|
|
|
id: usize,
|
|
|
|
}
|
|
|
|
Ok(Parent::deserialize(deserializer)?.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct TestStepInfo {
|
|
|
|
name: String,
|
|
|
|
origin: String,
|
|
|
|
location: TestLocation,
|
|
|
|
level: usize,
|
|
|
|
#[serde(rename = "parent")]
|
|
|
|
#[serde(deserialize_with = "deserialize_parent")]
|
|
|
|
parent_id: usize,
|
|
|
|
root_id: usize,
|
|
|
|
root_name: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[op]
|
|
|
|
fn op_register_test_step(
|
|
|
|
state: &mut OpState,
|
|
|
|
info: TestStepInfo,
|
|
|
|
) -> Result<TestRegisterResult, AnyError> {
|
|
|
|
let id = NEXT_ID.fetch_add(1, Ordering::SeqCst);
|
|
|
|
let description = TestStepDescription {
|
|
|
|
id,
|
|
|
|
name: info.name,
|
|
|
|
origin: info.origin,
|
|
|
|
location: info.location,
|
|
|
|
level: info.level,
|
|
|
|
parent_id: info.parent_id,
|
|
|
|
root_id: info.root_id,
|
|
|
|
root_name: info.root_name,
|
|
|
|
};
|
|
|
|
let mut sender = state.borrow::<TestEventSender>().clone();
|
|
|
|
sender.send(TestEvent::StepRegister(description)).ok();
|
|
|
|
Ok(TestRegisterResult {
|
|
|
|
id,
|
|
|
|
filtered_out: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
#[op]
|
2021-07-14 15:05:16 -04:00
|
|
|
fn op_dispatch_test_event(
|
2021-04-28 14:17:04 -04:00
|
|
|
state: &mut OpState,
|
2021-07-14 15:05:16 -04:00
|
|
|
event: TestEvent,
|
|
|
|
) -> Result<(), AnyError> {
|
2022-05-01 14:44:55 -04:00
|
|
|
let mut sender = state.borrow::<TestEventSender>().clone();
|
2021-07-14 15:05:16 -04:00
|
|
|
sender.send(event).ok();
|
2022-04-15 08:24:41 -04:00
|
|
|
Ok(())
|
|
|
|
}
|