unit tests for invalid signatures

This commit is contained in:
Jędrzej Stuczyński
2023-02-28 15:05:49 +00:00
parent 542e9baacf
commit b803b2f667
3 changed files with 143 additions and 55 deletions
+70 -2
View File
@@ -191,6 +191,7 @@ pub(crate) fn _try_remove_gateway(
#[cfg(test)]
pub mod tests {
use super::*;
use crate::contract::execute;
use crate::gateways::queries;
use crate::gateways::transactions::{
@@ -200,7 +201,7 @@ pub mod tests {
use crate::mixnet_contract_settings::storage::minimum_gateway_pledge;
use crate::support::tests;
use crate::support::tests::fixtures;
use crate::support::tests::fixtures::good_gateway_pledge;
use crate::support::tests::fixtures::{good_gateway_pledge, good_mixnode_pledge};
use crate::support::tests::test_helpers::TestSetup;
use cosmwasm_std::testing::mock_info;
use cosmwasm_std::{Addr, BankMsg, Response, Uint128};
@@ -276,7 +277,74 @@ pub mod tests {
#[test]
fn adding_gateway_with_invalid_signatures() {
todo!()
let mut test = TestSetup::new();
let env = test.env();
let sender = "alice";
let pledge = good_mixnode_pledge();
let info = mock_info(sender, pledge.as_ref());
let (gateway, signature) = test.gateway_with_signature(sender, Some(pledge.clone()));
// using different parameters than what the signature was made on
let mut modified_gateway = gateway.clone();
modified_gateway.mix_port += 1;
let res = try_add_gateway(
test.deps_mut(),
env.clone(),
info,
modified_gateway,
signature.clone(),
);
assert_eq!(res, Err(MixnetContractError::InvalidEd25519Signature));
// even stake amount is protected
let mut different_pledge = pledge.clone();
different_pledge[0].amount += Uint128::new(12345);
let info = mock_info(sender, different_pledge.as_ref());
let res = try_add_gateway(
test.deps_mut(),
env.clone(),
info.clone(),
gateway.clone(),
signature.clone(),
);
assert_eq!(res, Err(MixnetContractError::InvalidEd25519Signature));
let other_sender = mock_info("another-sender", pledge.as_ref());
let res = try_add_gateway(
test.deps_mut(),
env.clone(),
other_sender,
gateway.clone(),
signature.clone(),
);
assert_eq!(res, Err(MixnetContractError::InvalidEd25519Signature));
// trying to reuse the same signature for another bonding fails (because nonce doesn't match!)
let info = mock_info(sender, pledge.as_ref());
let current_nonce =
signing_storage::get_signing_nonce(test.deps().storage, Addr::unchecked(sender))
.unwrap();
assert_eq!(0, current_nonce);
let res = try_add_gateway(
test.deps_mut(),
env.clone(),
info.clone(),
gateway.clone(),
signature.clone(),
);
assert!(res.is_ok());
let updated_nonce =
signing_storage::get_signing_nonce(test.deps().storage, Addr::unchecked(sender))
.unwrap();
assert_eq!(1, updated_nonce);
_try_remove_gateway(test.deps_mut(), Addr::unchecked(sender), None).unwrap();
let res = try_add_gateway(test.deps_mut(), env, info, gateway, signature);
assert_eq!(res, Err(MixnetContractError::InvalidEd25519Signature));
}
#[test]
+73 -1
View File
@@ -504,7 +504,79 @@ pub mod tests {
#[test]
fn adding_mixnode_with_invalid_signatures() {
todo!()
let mut test = TestSetup::new();
let env = test.env();
let sender = "alice";
let pledge = good_mixnode_pledge();
let info = mock_info(sender, pledge.as_ref());
let (mixnode, signature, _) = test.mixnode_with_signature(sender, Some(pledge.clone()));
// the above using cost params fixture
let cost_params = fixtures::mix_node_cost_params_fixture();
// using different parameters than what the signature was made on
let mut modified_mixnode = mixnode.clone();
modified_mixnode.mix_port += 1;
let res = try_add_mixnode(
test.deps_mut(),
env.clone(),
info,
modified_mixnode,
cost_params.clone(),
signature.clone(),
);
assert_eq!(res, Err(MixnetContractError::InvalidEd25519Signature));
// even stake amount is protected
let mut different_pledge = pledge.clone();
different_pledge[0].amount += Uint128::new(12345);
let info = mock_info(sender, different_pledge.as_ref());
let res = try_add_mixnode(
test.deps_mut(),
env.clone(),
info.clone(),
mixnode.clone(),
cost_params.clone(),
signature.clone(),
);
assert_eq!(res, Err(MixnetContractError::InvalidEd25519Signature));
let other_sender = mock_info("another-sender", pledge.as_ref());
let res = try_add_mixnode(
test.deps_mut(),
env.clone(),
other_sender,
mixnode.clone(),
cost_params.clone(),
signature.clone(),
);
assert_eq!(res, Err(MixnetContractError::InvalidEd25519Signature));
// trying to reuse the same signature for another bonding fails (because nonce doesn't match!)
let info = mock_info(sender, pledge.as_ref());
let current_nonce =
signing_storage::get_signing_nonce(test.deps().storage, Addr::unchecked(sender))
.unwrap();
assert_eq!(0, current_nonce);
let res = try_add_mixnode(
test.deps_mut(),
env.clone(),
info.clone(),
mixnode.clone(),
cost_params.clone(),
signature.clone(),
);
assert!(res.is_ok());
let updated_nonce =
signing_storage::get_signing_nonce(test.deps().storage, Addr::unchecked(sender))
.unwrap();
assert_eq!(1, updated_nonce);
test.immediately_unbond_mixnode(1);
let res = try_add_mixnode(test.deps_mut(), env, info, mixnode, cost_params, signature);
assert_eq!(res, Err(MixnetContractError::InvalidEd25519Signature));
}
#[test]
-52
View File
@@ -1144,58 +1144,6 @@ pub mod test_helpers {
SignableGatewayBondingMsg::new(nonce, content)
}
// // note to whoever wants to refactor this function, you dont want to grab rng here directly
// // via `let rng = test_rng()`
// // because it's extremely likely you might end up calling `add_mixnode()` multiple times
// // in the same test and thus you're going to get mixnodes with the same keys and that's
// // not what you want (presumably)
// pub fn add_mixnode(
// rng: impl RngCore + CryptoRng,
// deps: DepsMut<'_>,
// env: Env,
// sender: &str,
// stake: Vec<Coin>,
// ) -> MixId {
// let (mixnode, owner_signature, keypair) =
// mixnode_with_signature(rng, deps.as_ref(), sender, Some(stake));
//
// let info = mock_info(sender, stake.as_ref());
// let current_id_counter = mixnodes_storage::MIXNODE_ID_COUNTER
// .may_load(deps.storage)
// .unwrap()
// .unwrap_or_default();
//
// try_add_mixnode(
// deps,
// env,
// info,
// mixnode,
// tests::fixtures::mix_node_cost_params_fixture(),
// owner_signature,
// )
// .unwrap();
//
// // newly added mixnode gets assigned the current counter + 1
// current_id_counter + 1
// }
//
// // same note as with `add_mixnode`
// pub fn add_gateway(
// rng: impl RngCore + CryptoRng,
// deps: DepsMut<'_>,
// env: Env,
// sender: &str,
// stake: Vec<Coin>,
// ) -> String {
// let (gateway, owner_signature) =
// gateway_with_signature(rng, deps.as_ref(), sender, Some(stake.clone()));
//
// let info = mock_info(sender, &stake);
// let key = gateway.identity_key.clone();
// try_add_gateway(deps, env, info, gateway, owner_signature).unwrap();
// key
// }
fn initial_rewarding_params() -> InitialRewardingParams {
let reward_pool = 250_000_000_000_000u128;
let staking_supply = 100_000_000_000_000u128;