Skip to content

Commit

Permalink
chore: add test coverage for xcall-ibc-connection
Browse files Browse the repository at this point in the history
  • Loading branch information
bishalbikram committed Apr 2, 2024
1 parent 545da5c commit 3ff85ba
Show file tree
Hide file tree
Showing 8 changed files with 900 additions and 347 deletions.
101 changes: 101 additions & 0 deletions contracts/cosmwasm-vm/cw-xcall-ibc-connection/src/assertion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,104 @@ impl<'a> CwIbcConnection<'a> {
Ok(())
}
}

#[cfg(test)]
mod test {
use crate::state::CwIbcConnection;
use cosmwasm_std::{
testing::{mock_dependencies, mock_info},
Addr,
};

#[test]
fn test_ensure_length() {
let contract = CwIbcConnection::new();

let res = contract.ensure_data_length(20 as usize);
assert!(res.is_ok())
}

#[test]
#[should_panic(expected = "MaxDataSizeExceeded")]
fn test_ensure_length_fail() {
let contract = CwIbcConnection::new();

contract.ensure_data_length(u64::MAX as usize).unwrap();
}

#[test]
fn test_ensure_rollback_length() {
let contract = CwIbcConnection::new();

let rollback_size: Vec<u8> = Vec::new();
let res = contract.ensure_rollback_length(&rollback_size);
assert!(res.is_ok())
}

#[test]
#[should_panic(expected = "MaxRollbackSizeExceeded")]
fn test_ensure_rollback_length_fail() {
let contract = CwIbcConnection::new();

let rollback_size: Vec<u8> = vec![0; 2048];
contract.ensure_rollback_length(&rollback_size).unwrap()
}

#[test]
fn test_ensure_owner() {
let mut deps = mock_dependencies();
let contract = CwIbcConnection::new();
let info = mock_info("owner", &[]);

contract
.add_owner(deps.as_mut().storage, Addr::unchecked("owner"))
.unwrap();

let res = contract.ensure_owner(deps.as_ref().storage, &info).unwrap();
assert_eq!(res, ())
}

#[test]
#[should_panic(expected = "Unauthorized")]
fn test_ensure_owner_fail() {
let mut deps = mock_dependencies();
let contract = CwIbcConnection::new();
let info = mock_info("owner", &[]);

contract
.add_owner(deps.as_mut().storage, Addr::unchecked("test_owner"))
.unwrap();

contract.ensure_owner(deps.as_ref().storage, &info).unwrap();
}

#[test]
#[should_panic(expected = "OnlyIbcHandler")]
fn test_ensure_xcall_handler_fail() {
let mut deps = mock_dependencies();
let contract = CwIbcConnection::new();

contract
.set_xcall_host(deps.as_mut().storage, Addr::unchecked("xcall_host"))
.unwrap();

contract
.ensure_xcall_handler(deps.as_ref().storage, Addr::unchecked("ibc_host"))
.unwrap()
}

#[test]
#[should_panic(expected = "OnlyIbcHandler")]
fn test_ensure_ibc_handler_fail() {
let mut deps = mock_dependencies();
let contract = CwIbcConnection::new();

contract
.set_ibc_host(deps.as_mut().storage, Addr::unchecked("ibc_host"))
.unwrap();

contract
.ensure_ibc_handler(deps.as_ref().storage, Addr::unchecked("xcall_host"))
.unwrap()
}
}
59 changes: 59 additions & 0 deletions contracts/cosmwasm-vm/cw-xcall-ibc-connection/src/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,62 @@ impl<'a> CwIbcConnection<'a> {
self.get_unclaimed_packet_fee(store, &nid, &address)
}
}

#[cfg(test)]
mod test {
use cosmwasm_std::testing::{mock_dependencies, mock_info};
use cosmwasm_std::Response;
use cw_xcall_lib::network_address::NetId;

use super::CwIbcConnection;

#[test]
#[should_panic(expected = "NoFeesAccrued")]
fn test_claim_fees_on_zero_unclaimed_fee() {
let mut deps = mock_dependencies();
let contract = CwIbcConnection::new();
let info = mock_info("relayer", &[]);

let nid = NetId::from("default".to_string());

contract
.claim_fees(deps.as_mut(), info, nid, "relayer".to_string())
.unwrap();
}

#[test]
fn test_set_fee() {
let mut deps = mock_dependencies();
let contract = CwIbcConnection::new();

let nid = NetId::from("default".to_string());

let res = contract
.set_fee(deps.as_mut().storage, nid, 1_000, 1_000)
.unwrap();

assert_eq!(res, Response::new())
}

#[test]
fn test_reset_unclaimed_ack_fees() {
let mut deps = mock_dependencies();
let contract = CwIbcConnection::new();

let nid = NetId::from("default".to_string());

contract
.add_unclaimed_ack_fees(deps.as_mut().storage, &nid, 1, 10)
.unwrap();

let ack_fee = contract.get_unclaimed_ack_fee(deps.as_ref().storage, nid.as_str(), 1);
assert_eq!(ack_fee, 10);

contract
.reset_unclaimed_ack_fees(deps.as_mut().storage, nid.as_str(), 1)
.unwrap();

let ack_fee = contract.get_unclaimed_ack_fee(deps.as_ref().storage, nid.as_str(), 1);
assert_eq!(ack_fee, 0)
}
}
Loading

0 comments on commit 3ff85ba

Please sign in to comment.