Skip to content

Commit

Permalink
runtime-sdk: Better support tests setting the chain context
Browse files Browse the repository at this point in the history
  • Loading branch information
kostko committed Oct 4, 2022
1 parent ac4dcbb commit 3695905
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
3 changes: 3 additions & 0 deletions runtime-sdk/modules/evm/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,14 @@ fn test_evm_calls() {

#[test]
fn test_c10l_evm_calls() {
let _guard = crypto::signature::context::test_using_chain_context();
crypto::signature::context::set_chain_context(Default::default(), "test");
do_test_evm_calls::<ConfidentialEVMConfig>();
}

#[test]
fn test_c10l_evm_balance_transfer() {
let _guard = crypto::signature::context::test_using_chain_context();
crypto::signature::context::set_chain_context(Default::default(), "test");
let mut mock = mock::Mock::default();
let mut ctx = mock.create_ctx();
Expand Down Expand Up @@ -771,6 +773,7 @@ fn test_evm_runtime() {

#[test]
fn test_c10l_evm_runtime() {
let _guard = crypto::signature::context::test_using_chain_context();
crypto::signature::context::set_chain_context(Default::default(), "test");
do_test_evm_runtime::<ConfidentialEVMConfig>();
}
Expand Down
34 changes: 22 additions & 12 deletions runtime-sdk/src/crypto/signature/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,32 @@ pub fn set_chain_context(runtime_id: Namespace, consensus_chain_context: &str) {
*guard = Some(ctx.into_bytes());
}

/// Test helper to serialize unit tests using the global chain context. The chain context is reset
/// when this method is called.
///
/// # Example
///
/// ```rust
/// # use oasis_runtime_sdk::crypto::signature::context::test_using_chain_context;
/// let _guard = test_using_chain_context();
/// // ... rest of the test code follows ...
/// ```
#[cfg(any(test, feature = "test"))]
pub fn test_using_chain_context() -> std::sync::MutexGuard<'static, ()> {
static TEST_USING_CHAIN_CONTEXT: Lazy<Mutex<()>> = Lazy::new(Default::default);
let guard = TEST_USING_CHAIN_CONTEXT.lock().unwrap();
*CHAIN_CONTEXT.lock().unwrap() = None;

guard
}

#[cfg(test)]
mod test {
use super::*;

static TEST_GUARD: Lazy<Mutex<()>> = Lazy::new(Default::default);

fn reset_chain_context() {
*CHAIN_CONTEXT.lock().unwrap() = None;
}

#[test]
fn test_chain_context() {
let _guard = TEST_GUARD.lock().unwrap();
reset_chain_context();
let _guard = test_using_chain_context();
set_chain_context(
"8000000000000000000000000000000000000000000000000000000000000000".into(),
"643fb06848be7e970af3b5b2d772eb8cfb30499c8162bc18ac03df2f5e22520e",
Expand All @@ -94,17 +106,15 @@ mod test {

#[test]
fn test_chain_context_not_configured() {
let _guard = TEST_GUARD.lock().unwrap();
reset_chain_context();
let _guard = test_using_chain_context();

let result = std::panic::catch_unwind(|| get_chain_context_for(b"test"));
assert!(result.is_err());
}

#[test]
fn test_chain_context_already_configured() {
let _guard = TEST_GUARD.lock().unwrap();
reset_chain_context();
let _guard = test_using_chain_context();
set_chain_context(
"8000000000000000000000000000000000000000000000000000000000000000".into(),
"643fb06848be7e970af3b5b2d772eb8cfb30499c8162bc18ac03df2f5e22520e",
Expand Down

0 comments on commit 3695905

Please sign in to comment.