Skip to content

Commit

Permalink
test: enable validate tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zsluedem committed Apr 3, 2023
1 parent 2e6de86 commit 25004a3
Showing 1 changed file with 93 additions and 51 deletions.
144 changes: 93 additions & 51 deletions tests/validate_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ fn existing_storage_account_user_op(
async fn validate(
context: &TestContext<ClientType>,
user_op: UserOperation,
) -> Result<SimulateValidationResult, SimulateValidationError<ClientType>> {
) -> Result<SimulateValidationResult, SimulateValidationError> {
context
.uopool
.simulate_user_operation(&user_op, &context.entry_point.address)
Expand All @@ -240,7 +240,7 @@ async fn test_user_op(
init_code: Bytes,
init_func: Bytes,
factory_address: Address,
) -> Result<SimulateValidationResult, SimulateValidationError<ClientType>> {
) -> Result<SimulateValidationResult, SimulateValidationError> {
let user_op = create_test_user_op(
&context,
validate_rule,
Expand All @@ -257,7 +257,7 @@ async fn test_user_op(
async fn test_existing_user_op(
validate_rule: String,
pm_rule: String,
) -> Result<SimulateValidationResult, SimulateValidationError<ClientType>> {
) -> Result<SimulateValidationResult, SimulateValidationError> {
let context = setup().await.expect("Setup context failed");

let user_op = existing_storage_account_user_op(&context, validate_rule, pm_rule);
Expand Down Expand Up @@ -289,16 +289,19 @@ async fn reject_unkown_rule() -> anyhow::Result<()> {
let (init_code, init_func) = create_opcode_factory_init_code("".to_string())
.await
.unwrap();
test_user_op(
let res = test_user_op(
&context,
"<unknown-rule>".to_string(),
None,
init_code,
init_func,
context.opcodes_factory.address,
)
.await
.expect_err("unknown rule");
.await;
assert!(matches!(
res,
Err(SimulateValidationError::UserOperationRejected { message }) if message.contains("unknown-rule")
));
Ok(())
}

Expand All @@ -308,16 +311,19 @@ async fn fail_with_bad_opcode_in_ctr() -> anyhow::Result<()> {
let (init_code, init_func) = create_opcode_factory_init_code("coinbase".to_string())
.await
.unwrap();
test_user_op(
let res = test_user_op(
&context,
"".to_string(),
None,
init_code,
init_func,
context.opcodes_factory.address,
)
.await
.expect_err("factory uses banned opcode: COINBASE");
.await;
assert!(matches!(
res,
Err(SimulateValidationError::OpcodeValidation { entity, opcode }) if entity=="factory" && opcode == "COINBASE"
));
Ok(())
}

Expand All @@ -327,16 +333,20 @@ async fn fail_with_bad_opcode_in_paymaster() -> anyhow::Result<()> {
let (init_code, init_func) = create_opcode_factory_init_code("".to_string())
.await
.unwrap();
test_user_op(
let res = test_user_op(
&context,
"".to_string(),
Some("coinbase".to_string()),
init_code,
init_func,
context.opcodes_factory.address,
)
.await
.expect_err("paymaster uses banned opcode: COINBASE");
.await;
println!("{res:?}");
assert!(matches!(
res,
Err(SimulateValidationError::OpcodeValidation { entity, opcode }) if entity=="paymaster" && opcode == "COINBASE"
));
Ok(())
}

Expand All @@ -346,16 +356,20 @@ async fn fail_with_bad_opcode_in_validation() -> anyhow::Result<()> {
let (init_code, init_func) = create_opcode_factory_init_code("".to_string())
.await
.unwrap();
test_user_op(
let res = test_user_op(
&context,
"blockhash".to_string(),
None,
init_code,
init_func,
context.opcodes_factory.address,
)
.await
.expect_err("account uses banned opcode: BLOCKHASH");
.await;
println!("{res:?}");
assert!(matches!(
res,
Err(SimulateValidationError::OpcodeValidation { entity, opcode }) if entity=="account" && opcode == "BLOCKHASH"
));
Ok(())
}

Expand All @@ -365,16 +379,20 @@ async fn fail_if_create_too_many() -> anyhow::Result<()> {
let (init_code, init_func) = create_opcode_factory_init_code("".to_string())
.await
.unwrap();
test_user_op(
let res = test_user_op(
&context,
"create2".to_string(),
None,
init_code,
init_func,
context.opcodes_factory.address,
)
.await
.expect("account uses banned opcode: CREATE2");
.await;
println!("{res:?}");
assert!(matches!(
res,
Err(SimulateValidationError::OpcodeValidation { entity, opcode }) if entity=="account" && opcode == "CREATE2"
));
Ok(())
}

Expand All @@ -384,52 +402,62 @@ async fn fail_referencing_self_token() -> anyhow::Result<()> {
let (init_code, init_func) = create_storage_factory_init_code(0, "".to_string())
.await
.unwrap();
test_user_op(
let res = test_user_op(
&context,
"balance-self".to_string(),
None,
init_code,
init_func,
context.storage_factory.address,
)
.await
.expect_err("unstaked account accessed");
.await;
println!("{res:?}");
assert!(matches!(
res,
Err(SimulateValidationError::StorageAccessValidation { .. })
));
Ok(())
}

#[tokio::test]
async fn account_succeeds_referecing_its_own_balance() {
test_existing_user_op("balance-self".to_string(), "".to_string())
.await
.expect("succeed");
let res = test_existing_user_op("balance-self".to_string(), "".to_string()).await;
println!("{res:?}");
assert!(matches!(res, Ok(..)));
}

#[tokio::test]
async fn account_fail_to_read_allowance_of_address() {
test_existing_user_op("allowance-self-1".to_string(), "".to_string())
.await
.expect_err("account has forbidden read");
let res = test_existing_user_op("allowance-self-1".to_string(), "".to_string()).await;
println!("{res:?}");
assert!(matches!(
res,
Err(SimulateValidationError::StorageAccessValidation { .. })
));
}

#[tokio::test]
async fn account_can_reference_its_own_allowance_on_other_contract_balance() {
test_existing_user_op("allowance-1-self".to_string(), "".to_string())
.await
.expect("succeed");
let res = test_existing_user_op("allowance-1-self".to_string(), "".to_string()).await;
println!("{res:?}");
assert!(matches!(res, Ok(..)));
}

#[tokio::test]
async fn access_self_struct_data() {
test_existing_user_op("struct-self".to_string(), "".to_string())
.await
.expect("succeed");
let res = test_existing_user_op("struct-self".to_string(), "".to_string()).await;
println!("{res:?}");
assert!(matches!(res, Ok(..)));
}

#[tokio::test]
async fn fail_to_access_other_address_struct_data() {
test_existing_user_op("struct-1".to_string(), "".to_string())
.await
.expect_err("account has forbidden read");
let res = test_existing_user_op("struct-1".to_string(), "".to_string()).await;
println!("{res:?}");
assert!(matches!(
res,
Err(SimulateValidationError::StorageAccessValidation { .. })
));
}

#[tokio::test]
Expand All @@ -438,24 +466,28 @@ async fn fail_if_referencing_other_token_balance() -> anyhow::Result<()> {
let (init_code, init_func) = create_storage_factory_init_code(0, "".to_string())
.await
.unwrap();
test_user_op(
let res = test_user_op(
&context,
"balance-1".to_string(),
None,
init_code,
init_func,
context.storage_factory.address,
)
.await
.expect_err("account has forbidden read");
.await;
println!("{res:?}");
assert!(matches!(
res,
Err(SimulateValidationError::StorageAccessValidation { .. })
));
Ok(())
}

#[tokio::test]
async fn fail_if_referencing_self_token_balance_after_wallet_creation() {
test_existing_user_op("balance-self".to_string(), "".to_string())
.await
.expect("succeed");
let res = test_existing_user_op("balance-self".to_string(), "".to_string()).await;
println!("{res:?}");
assert!(matches!(res, Ok(..)));
}

#[tokio::test]
Expand Down Expand Up @@ -485,9 +517,12 @@ async fn fail_with_unstaked_paymaster_returning_context() -> anyhow::Result<()>
paymaster_and_data: Bytes::from(paymaster_and_data),
signature: Bytes::default(),
};
validate(&context, user_op)
.await
.expect_err("unstaked paymaster must not return context");
let res = validate(&context, user_op).await;
println!("{res:?}");
assert!(matches!(
res,
Err(SimulateValidationError::StorageAccessValidation { .. })
));
Ok(())
}

Expand All @@ -510,9 +545,12 @@ async fn fail_with_validation_recursively_calls_handle_ops() -> anyhow::Result<(
paymaster_and_data: Bytes::default(),
signature: Bytes::from("handleOps".as_bytes().to_vec()),
};
validate(&context, user_op)
.await
.expect_err("illegal call into EntryPoint");
let res = validate(&context, user_op).await;
println!("{res:?}");
assert!(matches!(
res,
Err(SimulateValidationError::StorageAccessValidation { .. })
));
Ok(())
}

Expand Down Expand Up @@ -541,15 +579,19 @@ async fn fail_with_inner_oog_revert() -> anyhow::Result<()> {
let (init_code, init_func) = create_storage_factory_init_code(0, "".to_string())
.await
.unwrap();
test_user_op(
let res = test_user_op(
&context,
"oog".to_string(),
None,
init_code,
init_func,
context.storage_factory.address,
)
.await
.expect_err("oog");
.await;
println!("{res:?}");
assert!(matches!(
res,
Err(SimulateValidationError::StorageAccessValidation { .. })
));
Ok(())
}

0 comments on commit 25004a3

Please sign in to comment.