Skip to content

Commit

Permalink
fixup and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arajasek committed Aug 30, 2022
1 parent d6b6c3c commit bbdb4e4
Show file tree
Hide file tree
Showing 10 changed files with 341 additions and 132 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions actors/account/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ num-traits = "0.2.14"
num-derive = "0.3.3"
fvm_ipld_blockstore = "0.1.1"
fvm_ipld_encoding = "0.2.2"
anyhow = "1.0.56"

[dev-dependencies]
fil_actors_runtime = { path = "../../runtime", features = ["test_utils", "sector-default"] }
Expand Down
52 changes: 50 additions & 2 deletions actors/account/tests/account_actor_test.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
// Copyright 2019-2022 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use anyhow::anyhow;
use fil_actor_account::types::AuthenticateMessageParams;
use fil_actor_account::{testing::check_state_invariants, Actor as AccountActor, State};
use fil_actors_runtime::builtin::SYSTEM_ACTOR_ADDR;
use fil_actors_runtime::test_utils::*;
use fvm_ipld_encoding::RawBytes;
use fvm_shared::address::Address;
use fvm_shared::crypto::signature::Signature;
use fvm_shared::error::ExitCode;

fn check_state(rt: &MockRuntime) {
Expand All @@ -14,7 +17,7 @@ fn check_state(rt: &MockRuntime) {
acc.assert_empty();
}

macro_rules! account_tests {
macro_rules! account_constructor_tests {
($($name:ident: $value:expr,)*) => {
$(
#[test]
Expand Down Expand Up @@ -56,7 +59,7 @@ macro_rules! account_tests {
}
}

account_tests! {
account_constructor_tests! {
happy_construct_secp256k1_address: (
Address::new_secp256k1(&[2; fvm_shared::address::SECP_PUB_LEN]).unwrap(),
ExitCode::OK
Expand All @@ -74,3 +77,48 @@ account_tests! {
ExitCode::USR_ILLEGAL_ARGUMENT
),
}

#[test]
fn authenticate_message() {
let mut rt = MockRuntime {
receiver: Address::new_id(100),
caller: *SYSTEM_ACTOR_ADDR,
caller_type: *SYSTEM_ACTOR_CODE_ID,
..Default::default()
};

let addr = Address::new_secp256k1(&[2; fvm_shared::address::SECP_PUB_LEN]).unwrap();
rt.expect_validate_caller_addr(vec![*SYSTEM_ACTOR_ADDR]);

rt.call::<AccountActor>(1, &RawBytes::serialize(addr).unwrap()).unwrap();

let state: State = rt.get_state();
assert_eq!(state.address, addr);

let params =
RawBytes::serialize(AuthenticateMessageParams { signature: vec![], message: vec![] })
.unwrap();

rt.expect_validate_caller_any();
rt.expect_verify_signature(ExpectedVerifySig {
sig: Signature::new_secp256k1(vec![]),
signer: addr,
plaintext: vec![],
result: Ok(()),
});
assert_eq!(RawBytes::default(), rt.call::<AccountActor>(3, &params).unwrap());

rt.expect_validate_caller_any();
rt.expect_verify_signature(ExpectedVerifySig {
sig: Signature::new_secp256k1(vec![]),
signer: addr,
plaintext: vec![],
result: Err(anyhow!("bad signature")),
});
assert_eq!(
ExitCode::USR_ILLEGAL_ARGUMENT,
rt.call::<AccountActor>(3, &params).unwrap_err().exit_code()
);

rt.verify();
}
2 changes: 1 addition & 1 deletion actors/market/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1287,7 +1287,7 @@ where
let proposal_bytes = serialize_vec(&proposal.proposal, "deal proposal")?;

rt.send(
proposal.proposal.client,
&proposal.proposal.client,
ext::account::AUTHENTICATE_MESSAGE_METHOD,
RawBytes::serialize(ext::account::AuthenticateMessageParams {
signature: signature_bytes,
Expand Down
27 changes: 19 additions & 8 deletions actors/market/tests/cron_tick_timedout_deals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ use fvm_shared::error::ExitCode;
use fvm_shared::sector::StoragePower;
use fvm_shared::METHOD_SEND;

use fil_actor_market::ext::account::{AuthenticateMessageParams, AUTHENTICATE_MESSAGE_METHOD};
use num_traits::Zero;

mod harness;

use harness::*;

const START_EPOCH: ChainEpoch = 50;
Expand Down Expand Up @@ -79,20 +81,29 @@ fn publishing_timed_out_deal_again_should_work_after_cron_tick_as_it_should_no_l
END_EPOCH,
);
let buf = RawBytes::serialize(deal_proposal2.clone()).expect("failed to marshal deal proposal");
let sig = Signature::new_bls("does not matter".as_bytes().to_vec());
let sig = Signature::new_bls(buf.to_vec());
let client_deal_proposal =
ClientDealProposal { proposal: deal_proposal2.clone(), client_signature: sig.clone() };
ClientDealProposal { proposal: deal_proposal2.clone(), client_signature: sig };
let params = PublishStorageDealsParams { deals: vec![client_deal_proposal] };
rt.expect_validate_caller_type(vec![*ACCOUNT_ACTOR_CODE_ID, *MULTISIG_ACTOR_CODE_ID]);
expect_provider_control_address(&mut rt, PROVIDER_ADDR, OWNER_ADDR, WORKER_ADDR);
expect_query_network_info(&mut rt);
rt.set_caller(*ACCOUNT_ACTOR_CODE_ID, WORKER_ADDR);
rt.expect_verify_signature(ExpectedVerifySig {
sig,
signer: deal_proposal2.client,
plaintext: buf.to_vec(),
result: Ok(()),
});
let auth_param = RawBytes::serialize(AuthenticateMessageParams {
signature: buf.to_vec(),
message: buf.to_vec(),
})
.unwrap();

rt.expect_send(
deal_proposal2.client,
AUTHENTICATE_MESSAGE_METHOD,
auth_param,
TokenAmount::from(0u8),
RawBytes::default(),
ExitCode::OK,
);

expect_abort(
ExitCode::USR_ILLEGAL_ARGUMENT,
rt.call::<MarketActor>(
Expand Down
59 changes: 37 additions & 22 deletions actors/market/tests/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use num_traits::{FromPrimitive, Zero};
use regex::Regex;
use std::{cell::RefCell, collections::HashMap};

use fil_actor_market::ext::account::AuthenticateMessageParams;
use fil_actor_market::ext::account::{AuthenticateMessageParams, AUTHENTICATE_MESSAGE_METHOD};
use fil_actor_market::{
balance_table::BalanceTable, ext, ext::miner::GetControlAddressesReturnParams,
gen_rand_next_epoch, testing::check_state_invariants, ActivateDealsParams, ActivateDealsResult,
Expand Down Expand Up @@ -537,16 +537,24 @@ pub fn publish_deals_expect_abort(

let deal_serialized =
RawBytes::serialize(proposal.clone()).expect("Failed to marshal deal proposal");
let client_signature =
Signature::new_bls(b"Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn".to_vec());
let client_signature = Signature::new_bls(deal_serialized.to_vec());

expect_query_network_info(rt);
rt.expect_verify_signature(ExpectedVerifySig {
sig: client_signature.clone(),
signer: proposal.client,
plaintext: deal_serialized.to_vec(),
result: Ok(()),
});
let auth_param = RawBytes::serialize(AuthenticateMessageParams {
signature: deal_serialized.to_vec(),
message: deal_serialized.to_vec(),
})
.unwrap();

rt.expect_send(
proposal.client,
AUTHENTICATE_MESSAGE_METHOD,
auth_param,
TokenAmount::from(0u8),
RawBytes::default(),
ExitCode::OK,
);

rt.set_caller(*ACCOUNT_ACTOR_CODE_ID, WORKER_ADDR);
let deal_params = PublishStorageDealsParams {
deals: vec![ClientDealProposal { proposal, client_signature }],
Expand Down Expand Up @@ -679,12 +687,8 @@ pub fn assert_deal_deleted(rt: &mut MockRuntime, deal_id: DealID, p: DealProposa
assert!(!pending_deals.contains_key(&BytesKey(p_cid.to_bytes())).unwrap());
}

pub fn assert_deal_failure<F>(
add_funds: bool,
post_setup: F,
exit_code: ExitCode,
sig_result: Result<(), anyhow::Error>,
) where
pub fn assert_deal_failure<F>(add_funds: bool, post_setup: F, exit_code: ExitCode, sig_valid: bool)
where
F: FnOnce(&mut MockRuntime, &mut DealProposal),
{
let current_epoch = ChainEpoch::from(5);
Expand Down Expand Up @@ -712,13 +716,24 @@ pub fn assert_deal_failure<F>(
rt.set_caller(*ACCOUNT_ACTOR_CODE_ID, WORKER_ADDR);

let buf = RawBytes::serialize(deal_proposal.clone()).expect("failed to marshal deal proposal");
let sig = Signature::new_bls("does not matter".as_bytes().to_vec());
rt.expect_verify_signature(ExpectedVerifySig {
sig: sig.clone(),
signer: deal_proposal.client,
plaintext: buf.to_vec(),
result: sig_result,
});
let sig = Signature::new_bls(buf.to_vec());
let auth_param = RawBytes::serialize(AuthenticateMessageParams {
signature: buf.to_vec(),
message: buf.to_vec(),
})
.unwrap();

rt.expect_send(
deal_proposal.client,
AUTHENTICATE_MESSAGE_METHOD,
auth_param,
TokenAmount::from(0u8),
RawBytes::default(),
match sig_valid {
true => ExitCode::OK,
false => ExitCode::USR_ILLEGAL_ARGUMENT,
},
);

let params: PublishStorageDealsParams = PublishStorageDealsParams {
deals: vec![ClientDealProposal { proposal: deal_proposal, client_signature: sig }],
Expand Down
Loading

0 comments on commit bbdb4e4

Please sign in to comment.