2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-04-15 08:24:41 -04:00
|
|
|
|
2024-02-27 22:30:17 -05:00
|
|
|
use crate::tools::test::TestContainer;
|
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;
|
2023-09-25 22:21:18 -04:00
|
|
|
use crate::tools::test::TestFailure;
|
2022-07-15 13:09:22 -04:00
|
|
|
use crate::tools::test::TestLocation;
|
|
|
|
use crate::tools::test::TestStepDescription;
|
2023-09-25 22:21:18 -04:00
|
|
|
use crate::tools::test::TestStepResult;
|
2022-04-26 19:00:04 -04:00
|
|
|
|
2021-04-25 17:38:59 -04:00
|
|
|
use deno_core::error::generic_error;
|
2023-09-25 22:21:18 -04:00
|
|
|
use deno_core::error::type_error;
|
2021-04-25 17:38:59 -04:00
|
|
|
use deno_core::error::AnyError;
|
2023-09-12 07:14:45 -04:00
|
|
|
use deno_core::op2;
|
2023-04-13 13:43:23 -04:00
|
|
|
use deno_core::v8;
|
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;
|
2023-01-07 11:25:34 -05:00
|
|
|
use deno_runtime::permissions::PermissionsContainer;
|
2022-07-15 13:09:22 -04:00
|
|
|
use serde::Serialize;
|
|
|
|
use std::sync::atomic::AtomicUsize;
|
|
|
|
use std::sync::atomic::Ordering;
|
2021-04-25 17:38:59 -04:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
2023-03-17 14:22:15 -04:00
|
|
|
deno_core::extension!(deno_test,
|
|
|
|
ops = [
|
|
|
|
op_pledge_test_permissions,
|
|
|
|
op_restore_test_permissions,
|
|
|
|
op_register_test,
|
|
|
|
op_register_test_step,
|
2024-02-27 22:30:17 -05:00
|
|
|
op_test_get_origin,
|
2023-09-25 22:21:18 -04:00
|
|
|
op_test_event_step_wait,
|
|
|
|
op_test_event_step_result_ok,
|
|
|
|
op_test_event_step_result_ignored,
|
|
|
|
op_test_event_step_result_failed,
|
2023-03-17 14:22:15 -04:00
|
|
|
],
|
2023-03-17 18:15:27 -04:00
|
|
|
options = {
|
2023-03-17 14:22:15 -04:00
|
|
|
sender: TestEventSender,
|
|
|
|
},
|
2023-03-17 18:15:27 -04:00
|
|
|
state = |state, options| {
|
|
|
|
state.put(options.sender);
|
2023-04-13 13:43:23 -04:00
|
|
|
state.put(TestContainer::default());
|
2023-03-17 14:22:15 -04:00
|
|
|
},
|
|
|
|
);
|
2021-04-25 17:38:59 -04:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
2023-01-07 11:25:34 -05:00
|
|
|
struct PermissionsHolder(Uuid, PermissionsContainer);
|
2021-04-25 17:38:59 -04:00
|
|
|
|
2023-09-12 07:14:45 -04:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
2021-04-25 17:38:59 -04:00
|
|
|
pub fn op_pledge_test_permissions(
|
|
|
|
state: &mut OpState,
|
2023-09-12 07:14:45 -04:00
|
|
|
#[serde] args: ChildPermissionsArg,
|
2021-04-25 17:38:59 -04:00
|
|
|
) -> Result<Uuid, AnyError> {
|
|
|
|
let token = Uuid::new_v4();
|
2023-01-07 11:25:34 -05:00
|
|
|
let parent_permissions = state.borrow_mut::<PermissionsContainer>();
|
|
|
|
let worker_permissions = {
|
|
|
|
let mut parent_permissions = parent_permissions.0.lock();
|
|
|
|
let perms = create_child_permissions(&mut parent_permissions, args)?;
|
|
|
|
PermissionsContainer::new(perms)
|
|
|
|
};
|
2021-10-13 13:04:44 -04:00
|
|
|
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
|
2023-01-07 11:25:34 -05:00
|
|
|
state.put::<PermissionsContainer>(worker_permissions);
|
2021-04-25 17:38:59 -04:00
|
|
|
|
|
|
|
Ok(token)
|
|
|
|
}
|
|
|
|
|
2023-09-12 07:14:45 -04:00
|
|
|
#[op2]
|
2021-04-25 17:38:59 -04:00
|
|
|
pub fn op_restore_test_permissions(
|
|
|
|
state: &mut OpState,
|
2023-09-12 07:14:45 -04:00
|
|
|
#[serde] token: Uuid,
|
2021-04-25 17:38:59 -04:00
|
|
|
) -> 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;
|
2023-01-07 11:25:34 -05:00
|
|
|
state.put::<PermissionsContainer>(permissions);
|
2021-04-25 17:38:59 -04:00
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(generic_error("no permissions to restore"))
|
|
|
|
}
|
|
|
|
}
|
2021-04-28 14:17:04 -04:00
|
|
|
|
2022-07-15 13:09:22 -04:00
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct TestRegisterResult {
|
|
|
|
id: usize,
|
2023-04-13 13:43:23 -04:00
|
|
|
origin: String,
|
2022-07-15 13:09:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static NEXT_ID: AtomicUsize = AtomicUsize::new(0);
|
|
|
|
|
2023-10-07 11:34:03 -04:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2023-09-12 07:14:45 -04:00
|
|
|
#[op2]
|
2023-09-25 22:21:18 -04:00
|
|
|
fn op_register_test(
|
2022-07-15 13:09:22 -04:00
|
|
|
state: &mut OpState,
|
2023-09-25 22:21:18 -04:00
|
|
|
#[global] function: v8::Global<v8::Function>,
|
|
|
|
#[string] name: String,
|
|
|
|
ignore: bool,
|
|
|
|
only: bool,
|
2024-02-05 14:21:29 -05:00
|
|
|
sanitize_ops: bool,
|
|
|
|
sanitize_resources: bool,
|
2023-09-25 22:21:18 -04:00
|
|
|
#[string] file_name: String,
|
|
|
|
#[smi] line_number: u32,
|
|
|
|
#[smi] column_number: u32,
|
|
|
|
#[buffer] ret_buf: &mut [u8],
|
2024-02-27 22:30:17 -05:00
|
|
|
) -> Result<(), AnyError> {
|
2023-09-25 22:21:18 -04:00
|
|
|
if ret_buf.len() != 4 {
|
|
|
|
return Err(type_error(format!(
|
|
|
|
"Invalid ret_buf length: {}",
|
|
|
|
ret_buf.len()
|
|
|
|
)));
|
|
|
|
}
|
2022-07-15 13:09:22 -04:00
|
|
|
let id = NEXT_ID.fetch_add(1, Ordering::SeqCst);
|
2023-04-13 13:43:23 -04:00
|
|
|
let origin = state.borrow::<ModuleSpecifier>().to_string();
|
2022-07-15 13:09:22 -04:00
|
|
|
let description = TestDescription {
|
|
|
|
id,
|
2023-09-25 22:21:18 -04:00
|
|
|
name,
|
|
|
|
ignore,
|
|
|
|
only,
|
2024-02-05 14:21:29 -05:00
|
|
|
sanitize_ops,
|
|
|
|
sanitize_resources,
|
2023-04-13 13:43:23 -04:00
|
|
|
origin: origin.clone(),
|
2023-09-25 22:21:18 -04:00
|
|
|
location: TestLocation {
|
|
|
|
file_name,
|
|
|
|
line_number,
|
|
|
|
column_number,
|
|
|
|
},
|
2022-07-15 13:09:22 -04:00
|
|
|
};
|
2024-02-27 22:30:17 -05:00
|
|
|
let container = state.borrow_mut::<TestContainer>();
|
|
|
|
container.register(description, function);
|
2023-09-25 22:21:18 -04:00
|
|
|
ret_buf.copy_from_slice(&(id as u32).to_le_bytes());
|
2024-02-27 22:30:17 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[op2]
|
|
|
|
#[string]
|
|
|
|
fn op_test_get_origin(state: &mut OpState) -> String {
|
|
|
|
state.borrow::<ModuleSpecifier>().to_string()
|
2022-07-15 13:09:22 -04:00
|
|
|
}
|
|
|
|
|
2023-09-25 22:21:18 -04:00
|
|
|
#[op2(fast)]
|
|
|
|
#[smi]
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
2022-07-15 13:09:22 -04:00
|
|
|
fn op_register_test_step(
|
|
|
|
state: &mut OpState,
|
2023-09-25 22:21:18 -04:00
|
|
|
#[string] name: String,
|
|
|
|
#[string] file_name: String,
|
|
|
|
#[smi] line_number: u32,
|
|
|
|
#[smi] column_number: u32,
|
|
|
|
#[smi] level: usize,
|
|
|
|
#[smi] parent_id: usize,
|
|
|
|
#[smi] root_id: usize,
|
|
|
|
#[string] root_name: String,
|
|
|
|
) -> Result<usize, AnyError> {
|
2022-07-15 13:09:22 -04:00
|
|
|
let id = NEXT_ID.fetch_add(1, Ordering::SeqCst);
|
2023-04-13 13:43:23 -04:00
|
|
|
let origin = state.borrow::<ModuleSpecifier>().to_string();
|
2022-07-15 13:09:22 -04:00
|
|
|
let description = TestStepDescription {
|
|
|
|
id,
|
2023-09-25 22:21:18 -04:00
|
|
|
name,
|
2023-04-13 13:43:23 -04:00
|
|
|
origin: origin.clone(),
|
2023-09-25 22:21:18 -04:00
|
|
|
location: TestLocation {
|
|
|
|
file_name,
|
|
|
|
line_number,
|
|
|
|
column_number,
|
|
|
|
},
|
|
|
|
level,
|
|
|
|
parent_id,
|
|
|
|
root_id,
|
|
|
|
root_name,
|
2022-07-15 13:09:22 -04:00
|
|
|
};
|
2023-09-25 22:21:18 -04:00
|
|
|
let sender = state.borrow_mut::<TestEventSender>();
|
2022-07-15 13:09:22 -04:00
|
|
|
sender.send(TestEvent::StepRegister(description)).ok();
|
2023-09-25 22:21:18 -04:00
|
|
|
Ok(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[op2(fast)]
|
|
|
|
fn op_test_event_step_wait(state: &mut OpState, #[smi] id: usize) {
|
|
|
|
let sender = state.borrow_mut::<TestEventSender>();
|
|
|
|
sender.send(TestEvent::StepWait(id)).ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[op2(fast)]
|
|
|
|
fn op_test_event_step_result_ok(
|
|
|
|
state: &mut OpState,
|
|
|
|
#[smi] id: usize,
|
|
|
|
#[smi] duration: u64,
|
|
|
|
) {
|
|
|
|
let sender = state.borrow_mut::<TestEventSender>();
|
|
|
|
sender
|
|
|
|
.send(TestEvent::StepResult(id, TestStepResult::Ok, duration))
|
|
|
|
.ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[op2(fast)]
|
|
|
|
fn op_test_event_step_result_ignored(
|
|
|
|
state: &mut OpState,
|
|
|
|
#[smi] id: usize,
|
|
|
|
#[smi] duration: u64,
|
|
|
|
) {
|
|
|
|
let sender = state.borrow_mut::<TestEventSender>();
|
|
|
|
sender
|
|
|
|
.send(TestEvent::StepResult(id, TestStepResult::Ignored, duration))
|
|
|
|
.ok();
|
2022-07-15 13:09:22 -04:00
|
|
|
}
|
|
|
|
|
2023-09-12 07:14:45 -04:00
|
|
|
#[op2]
|
2023-09-25 22:21:18 -04:00
|
|
|
fn op_test_event_step_result_failed(
|
2021-04-28 14:17:04 -04:00
|
|
|
state: &mut OpState,
|
2023-09-25 22:21:18 -04:00
|
|
|
#[smi] id: usize,
|
|
|
|
#[serde] failure: TestFailure,
|
|
|
|
#[smi] duration: u64,
|
|
|
|
) {
|
|
|
|
let sender = state.borrow_mut::<TestEventSender>();
|
|
|
|
sender
|
|
|
|
.send(TestEvent::StepResult(
|
|
|
|
id,
|
|
|
|
TestStepResult::Failed(failure),
|
|
|
|
duration,
|
|
|
|
))
|
|
|
|
.ok();
|
2022-04-15 08:24:41 -04:00
|
|
|
}
|