Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add test coverage for cosmwasm ibc core #850

Merged
merged 1 commit into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use super::*;

use cosmwasm_std::to_vec;
use cw_ibc_core::conversions::to_ibc_channel_id;

use super::*;
use common::ibc::core::ics04_channel::commitment::AcknowledgementCommitment;

#[test]
fn test_acknowledgement_packet_validate_ordered() {
Expand Down Expand Up @@ -89,3 +92,157 @@ fn test_acknowledgement_packet_validate_fail_missing_channel() {
.acknowledgement_packet_validate(deps.as_mut(), info, env, &msg)
.unwrap();
}

#[test]
#[should_panic(expected = "ChannelClosed")]
fn acknowledgement_packet_validate_fail_on_channel_close() {
let msg = get_dummy_raw_msg_acknowledgement(10);
let mut ctx = TestContext::for_acknowledge_packet(get_mock_env(), &msg);
let mut deps = deps();
let contract = CwIbcCoreContext::new();
let info = create_mock_info("channel-creater", "umlg", 2000);

if let Some(chann_end) = &mut ctx.channel_end {
chann_end.state = State::Closed
}

ctx.init_acknowledge_packet(deps.as_mut().storage, &contract);
mock_lightclient_query(ctx.mock_queries, &mut deps);

contract
.acknowledgement_packet_validate(deps.as_mut(), info, ctx.env, &msg)
.unwrap();
}

#[test]
#[should_panic(expected = "InvalidPacketCounterparty")]
fn acknowledgement_packet_validate_fail_on_invalid_packet_counterparty() {
let mut msg = get_dummy_raw_msg_acknowledgement(10);
let mut ctx = TestContext::for_acknowledge_packet(get_mock_env(), &msg);
let mut deps = deps();
let contract = CwIbcCoreContext::new();
let info = create_mock_info("channel-creater", "umlg", 2000);

ctx.init_acknowledge_packet(deps.as_mut().storage, &contract);
mock_lightclient_query(ctx.mock_queries, &mut deps);

if let Some(packet) = &mut msg.packet {
packet.destination_port = "different_port".to_string();
}

contract
.acknowledgement_packet_validate(deps.as_mut(), info, ctx.env, &msg)
.unwrap();
}

#[test]
#[should_panic(
expected = "IbcPacketError { error: IncorrectPacketCommitment { sequence: Sequence(1) } }"
)]
fn acknowledgement_packet_validate_fail_for_incorrect_packet_commitment() {
let mut msg = get_dummy_raw_msg_acknowledgement(10);
let mut ctx = TestContext::for_acknowledge_packet(get_mock_env(), &msg);
let mut deps = deps();
let contract = CwIbcCoreContext::new();
let info = create_mock_info("channel-creater", "umlg", 2000);

ctx.init_acknowledge_packet(deps.as_mut().storage, &contract);
mock_lightclient_query(ctx.mock_queries, &mut deps);

if let Some(packet) = &mut msg.packet {
packet.timeout_height = Some(RawHeight {
revision_height: 100,
revision_number: 100,
});
}

contract
.acknowledgement_packet_validate(deps.as_mut(), info, ctx.env, &msg)
.unwrap();
}

#[test]
#[should_panic(expected = "InvalidPacketSequence")]
fn acknowledgement_packet_validate_fail_for_invalid_packet_sequence() {
let msg = get_dummy_raw_msg_acknowledgement(10);
let mut ctx = TestContext::for_acknowledge_packet(get_mock_env(), &msg);
let mut deps = deps();
let contract = CwIbcCoreContext::new();
let info = create_mock_info("channel-creater", "umlg", 2000);

if let Some(chann_end) = &mut ctx.channel_end {
chann_end.ordering = Order::Ordered
}

ctx.init_acknowledge_packet(deps.as_mut().storage, &contract);
mock_lightclient_query(ctx.mock_queries, &mut deps);

let packet = msg.clone().packet.unwrap();
let dest_port = to_ibc_port_id(&packet.source_port).unwrap();
let dest_channel = to_ibc_channel_id(&packet.source_channel).unwrap();

contract
.store_next_sequence_ack(
deps.as_mut().storage,
&dest_port,
&dest_channel,
&Sequence::from_str("2").unwrap(),
)
.unwrap();

contract
.acknowledgement_packet_validate(deps.as_mut(), info, ctx.env, &msg)
.unwrap();
}

#[test]
#[should_panic(expected = "FrozenClient")]
fn acknowledgement_packet_validate_fail_for_frozen_client() {
let msg = get_dummy_raw_msg_acknowledgement(10);
let mut ctx = TestContext::for_acknowledge_packet(get_mock_env(), &msg);
let mut deps = deps();
let contract = CwIbcCoreContext::new();
let info = create_mock_info("channel-creater", "umlg", 2000);

if let Some(client_state) = &mut ctx.client_state {
client_state.frozen_height = 10
}

ctx.init_acknowledge_packet(deps.as_mut().storage, &contract);
mock_lightclient_query(ctx.mock_queries, &mut deps);

contract
.acknowledgement_packet_validate(deps.as_mut(), info, ctx.env, &msg)
.unwrap();
}

#[test]
fn test_get_packet_acknowledgement() {
let ctx = TestContext::default(get_mock_env());
let mut deps = deps();
let contract = CwIbcCoreContext::new();

let sequence = Sequence::from_str("0").unwrap();
let ack_commitment = AcknowledgementCommitment::from(to_vec("ack").unwrap());

contract
.store_packet_acknowledgement(
deps.as_mut().storage,
&ctx.port_id,
&ctx.channel_id,
sequence,
ack_commitment.clone(),
)
.unwrap();

let res = contract
.get_packet_acknowledgement(
deps.as_ref().storage,
&ctx.port_id,
&ctx.channel_id,
sequence,
)
.unwrap();

assert_eq!(ack_commitment, res);
}
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,24 @@ fn test_on_chan_close_init_submessage() {
channel_close_init_validate.channel().order
);
}

#[test]
#[should_panic(expected = "ClientFrozen")]
fn fail_test_validate_chanel_close_init_for_frozen_client() {
let msg = get_dummy_raw_msg_chan_close_init();
let mut ctx = TestContext::for_channel_close_init(get_mock_env(), &msg);
let mut deps = deps();
let contract = CwIbcCoreContext::default();
let info = create_mock_info("channel-creater", "umlg", 2000);

if let Some(client_state) = &mut ctx.client_state {
client_state.frozen_height = 1
}

ctx.init_channel_close_init(deps.as_mut().storage, &contract);
mock_lightclient_query(ctx.mock_queries, &mut deps);

contract
.validate_channel_close_init(deps.as_mut(), info, &msg)
.unwrap();
}
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,24 @@ pub fn test_channel_close_confirm_validate_fail_connection_hops() {
};
channel_close_confirm_validate(&channel_id, &channel_end).unwrap();
}

#[test]
#[should_panic(expected = "FrozenClient")]
fn fail_test_validate_channel_close_confirm_for_frozen_client() {
let msg = get_dummy_raw_msg_chan_close_confirm(10);
let mut ctx = TestContext::for_channel_close_confirm(get_mock_env(), &msg);
let mut deps = deps();
let contract = CwIbcCoreContext::default();
let info = create_mock_info("channel-creater", "umlg", 2000);

if let Some(client_state) = &mut ctx.client_state {
client_state.frozen_height = 1
}

ctx.init_channel_close_confirm(deps.as_mut().storage, &contract);
mock_lightclient_query(ctx.mock_queries, &mut deps);

contract
.validate_channel_close_confirm(deps.as_mut(), info, &msg)
.unwrap();
}
114 changes: 114 additions & 0 deletions contracts/cosmwasm-vm/cw-ibc-core/tests/channel/test_handler.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
use super::*;

use common::ibc::core::ics04_channel::channel::Order;
use common::ibc::core::ics24_host::identifier::PortId;

use cw_ibc_core::ics03_connection::State as ConnectionState;
use cw_ibc_core::ics04_channel::{open_init, open_try};
use cw_ibc_core::{context::CwIbcCoreContext, ConnectionEnd};

#[test]
#[should_panic(expected = "UndefinedConnectionCounterparty")]
fn test_validate_open_try_channel_fail_missing_counterparty() {
Expand All @@ -20,3 +27,110 @@ fn test_validate_open_try_channel_fail_missing_counterparty() {
.validate_channel_open_try(deps.as_mut(), info, &raw)
.unwrap();
}

#[test]
#[should_panic(expected = "InvalidConnectionHopsLength")]
fn fail_test_validate_open_init_channel_on_invalid_connection_hops_length() {
let mut deps = deps();
let contract = CwIbcCoreContext::default();
let info = create_mock_info("channel-creater", "umlg", 2000);

let mut raw_channel = get_dummy_raw_channel_end(Some(0));
raw_channel.connection_hops = Vec::new();

let msg = RawMsgChannelOpenInit {
port_id: PortId::default().to_string(),
channel: Some(raw_channel),
signer: get_dummy_bech32_account(),
};
contract
.validate_channel_open_init(deps.as_mut(), info, &msg)
.unwrap();
}

#[test]
#[should_panic(expected = "InvalidVersionLengthConnection")]
fn fail_channel_open_init_msg_validate_on_invalid_version_length_of_connection() {
let ctx = TestContext::default(get_mock_env());

let conn_end = ConnectionEnd::new(
ctx.connection_end().state.clone(),
ctx.client_id.clone(),
ctx.connection_end().counterparty().clone(),
Vec::new(),
ctx.connection_end().delay_period().clone(),
);
open_init::channel_open_init_msg_validate(&ctx.channel_end(), conn_end).unwrap();
}

#[test]
#[should_panic(expected = "ChannelFeatureNotSupportedByConnection")]
fn fail_channel_open_init_msg_validate_on_channel_feature_not_supported() {
let mut ctx = TestContext::default(get_mock_env());
if let Some(chann_end) = &mut ctx.channel_end {
chann_end.ordering = Order::None
}

open_init::channel_open_init_msg_validate(&ctx.channel_end(), ctx.connection_end()).unwrap();
}

#[test]
#[should_panic(expected = "ConnectionNotOpen")]
fn fail_channel_open_try_msg_validate_on_invalid_connection_state() {
let mut ctx = TestContext::default(get_mock_env());
if let Some(conn_end) = &mut ctx.connection_end {
conn_end.state = ConnectionState::Init;
}

open_try::channel_open_try_msg_validate(&ctx.channel_end(), &ctx.connection_end()).unwrap();
}

#[test]
#[should_panic(expected = "ChannelFeatureNotSupportedByConnection")]
fn fail_channel_open_try_msg_validate_on_feature_not_supported() {
let mut ctx = TestContext::default(get_mock_env());
if let Some(chann_end) = &mut ctx.channel_end {
chann_end.ordering = Order::None
}

open_try::channel_open_try_msg_validate(&ctx.channel_end(), &ctx.connection_end()).unwrap();
}

#[test]
#[should_panic(expected = "ClientFrozen")]
fn fail_test_validate_open_init_channel_for_frozen_client() {
let mut ctx = TestContext::default(get_mock_env());
let mut deps = deps();
let contract = CwIbcCoreContext::default();
let info = create_mock_info("channel-creater", "umlg", 2000);

if let Some(client_state) = &mut ctx.client_state {
client_state.frozen_height = 1
}

ctx.init_channel_open_init(deps.as_mut().storage, &contract);
mock_lightclient_query(ctx.mock_queries, &mut deps);

let msg = get_dummy_raw_msg_chan_open_init(Some(0));
contract
.validate_channel_open_init(deps.as_mut(), info, &msg)
.unwrap();
}

#[test]
#[should_panic(expected = "InvalidConnectionHopsLength")]
fn fail_test_validate_open_try_channel_on_invalid_connection_hops_length() {
let mut deps = deps();
let contract = CwIbcCoreContext::default();
let info = create_mock_info("channel-creater", "umlg", 2000);

let mut raw_channel = get_dummy_raw_channel_end(Some(0));
raw_channel.connection_hops = Vec::new();

let mut msg = get_dummy_raw_msg_chan_open_try(0);
msg.channel = Some(raw_channel);

contract
.validate_channel_open_try(deps.as_mut(), info, &msg)
.unwrap();
}
Loading
Loading