Skip to content

Commit

Permalink
chore: add unit test coverage (#289)
Browse files Browse the repository at this point in the history
* chore: add unit test for cosmwasm xcall lib

* chore: update test coverage for cosmwasm xcall

* chore: add test cases for solidity CallService

* chore: add test cases for previous xcall version
  • Loading branch information
bishalbikram authored May 1, 2024
1 parent 6f055c9 commit 5e61bf9
Show file tree
Hide file tree
Showing 12 changed files with 1,870 additions and 72 deletions.
22 changes: 22 additions & 0 deletions contracts/cosmwasm-vm/cw-xcall-lib/src/message/call_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,25 @@ impl IMessage for CallMessage {
Ok(rlp::encode(self).to_vec())
}
}

#[cfg(test)]
mod tests {
use super::*;

use rlp::Rlp;

#[test]
fn test_call_message() {
let msg = CallMessage {
data: vec![1, 2, 3],
};

let encoded = msg.rlp_bytes().to_vec();
let decoded = CallMessage::decode(&Rlp::new(&encoded)).unwrap();

assert_eq!(msg, decoded);
assert_eq!(msg.rollback(), None);
assert_eq!(msg.data(), msg.data);
assert_eq!(msg.to_bytes().unwrap(), encoded)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,25 @@ impl IMessage for CallMessagePersisted {
Ok(rlp::encode(self).to_vec())
}
}

#[cfg(test)]
mod tests {
use common::rlp::Rlp;

use super::*;

#[test]
fn test_call_message_persisted() {
let msg = CallMessagePersisted {
data: vec![1, 2, 3],
};

let encoded = msg.rlp_bytes().to_vec();
let decoded = CallMessagePersisted::decode(&Rlp::new(&encoded)).unwrap();

assert_eq!(msg, decoded);
assert_eq!(msg.rollback(), None);
assert_eq!(msg.data(), msg.data);
assert_eq!(msg.to_bytes().unwrap(), encoded)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,26 @@ impl IMessage for CallMessageWithRollback {
Ok(rlp::encode(self).to_vec())
}
}

#[cfg(test)]
mod tests {
use common::rlp::Rlp;

use super::*;

#[test]
fn test_call_message_with_rollback() {
let msg = CallMessageWithRollback {
data: vec![1, 2, 3],
rollback: vec![1, 2, 3],
};

let encoded = msg.rlp_bytes().to_vec();
let decoded = CallMessageWithRollback::decode(&Rlp::new(&encoded)).unwrap();

assert_eq!(msg, decoded);
assert_eq!(msg.rollback().unwrap(), msg.rollback);
assert_eq!(msg.data(), msg.data);
assert_eq!(msg.to_bytes().unwrap(), encoded)
}
}
71 changes: 46 additions & 25 deletions contracts/cosmwasm-vm/cw-xcall-lib/src/message/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,43 +81,64 @@ mod tests {
use super::*;

#[test]
fn test_encoding_decoding_envelope_call_message() {
// Create a sample Envelope
let message = AnyMessage::CallMessage(CallMessage {
fn test_envelope_call_message() {
let msg = AnyMessage::CallMessage(CallMessage {
data: vec![1, 2, 3],
});
let sources = vec!["source1".to_string(), "source2".to_string()];
let destinations = vec!["dest1".to_string(), "dest2".to_string()];
let envelope = Envelope::new(message, sources, destinations);
let encoded_data = rlp::encode(&envelope).to_vec();
let sources = vec!["src".to_string()];
let destinations = vec!["dst".to_string()];
let envelope = Envelope::new(msg.clone(), sources, destinations);

assert_eq!(
"e50085c483010203d087736f757263653187736f7572636532cc856465737431856465737432",
hex::encode(&encoded_data)
);
let decoded: Envelope = rlp::decode(&encoded_data).unwrap();
let encoded = envelope.rlp_bytes().to_vec();
let decoded = Envelope::decode(&rlp::Rlp::new(&encoded)).unwrap();
assert_eq!(envelope, decoded);

let encoded = msg.to_bytes().unwrap();
let decoded = decode_message(MessageType::CallMessage, encoded.clone()).unwrap();
assert_eq!(decoded.rollback(), None);
assert_eq!(decoded.data(), vec![1, 2, 3]);
assert_eq!(decoded.to_bytes().unwrap(), encoded)
}

#[test]
fn test_envelope_call_message_persisted() {
let msg = AnyMessage::CallMessagePersisted(CallMessagePersisted {
data: vec![1, 2, 3],
});
let sources = vec!["src".to_string()];
let destinations = vec!["dst".to_string()];
let envelope = Envelope::new(msg.clone(), sources, destinations);

let encoded = envelope.rlp_bytes().to_vec();
let decoded = Envelope::decode(&rlp::Rlp::new(&encoded)).unwrap();
assert_eq!(envelope, decoded);

let encoded = msg.to_bytes().unwrap();
let decoded = decode_message(MessageType::CallMessagePersisted, encoded.clone()).unwrap();
assert_eq!(decoded.rollback(), None);
assert_eq!(decoded.data(), vec![1, 2, 3]);
assert_eq!(decoded.to_bytes().unwrap(), encoded)
}

#[test]
fn test_encoding_decoding_envelope_call_message_rollback() {
// Create a sample Envelope
let message = AnyMessage::CallMessageWithRollback(CallMessageWithRollback {
fn test_envelope_call_message_with_rollback() {
let msg = AnyMessage::CallMessageWithRollback(CallMessageWithRollback {
data: vec![1, 2, 3],
rollback: vec![1, 2, 3],
});
let sources = vec!["source1".to_string(), "source2".to_string()];
let destinations = vec!["dest1".to_string(), "dest2".to_string()];
let envelope = Envelope::new(message, sources, destinations);
let encoded_data = rlp::encode(&envelope).to_vec();

assert_eq!(
"e90189c88301020383010203d087736f757263653187736f7572636532cc856465737431856465737432",
hex::encode(&encoded_data)
);
let decoded: Envelope = rlp::decode(&encoded_data).unwrap();
let sources = vec!["src".to_string()];
let destinations = vec!["dst".to_string()];
let envelope = Envelope::new(msg.clone(), sources, destinations);

let encoded = envelope.rlp_bytes().to_vec();
let decoded = Envelope::decode(&rlp::Rlp::new(&encoded)).unwrap();
assert_eq!(envelope, decoded);

let encoded = msg.to_bytes().unwrap();
let decoded =
decode_message(MessageType::CallMessageWithRollback, encoded.clone()).unwrap();
assert_eq!(decoded.rollback(), Some(vec![1, 2, 3]));
assert_eq!(decoded.data(), vec![1, 2, 3]);
assert_eq!(decoded.to_bytes().unwrap(), encoded)
}
}
34 changes: 34 additions & 0 deletions contracts/cosmwasm-vm/cw-xcall-lib/src/message/msg_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,37 @@ impl MessageType {
MessageType::from(val)
}
}

#[cfg(test)]
mod tests {
use crate::message::msg_type::MessageType;

#[test]
fn test_message_type_for_u8() {
assert_eq!(MessageType::from(MessageType::CallMessage), 0.into());
assert_eq!(
MessageType::from(MessageType::CallMessagePersisted),
2.into()
);
assert_eq!(
MessageType::from(MessageType::CallMessageWithRollback),
1.into()
)
}

#[test]
fn test_message_type_from_int() {
assert_eq!(MessageType::from_int(0), MessageType::CallMessage);
assert_eq!(
MessageType::from_int(1),
MessageType::CallMessageWithRollback
);
assert_eq!(MessageType::from_int(2), MessageType::CallMessagePersisted)
}

#[test]
#[should_panic(expected = "unsupported message type")]
fn test_message_type_from_int_fail() {
MessageType::from_int(4);
}
}
29 changes: 0 additions & 29 deletions contracts/cosmwasm-vm/cw-xcall/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,32 +244,3 @@ impl<'a> CwCallService<'a> {
Ok(na)
}
}

#[cfg(test)]
mod tests {
use cosmwasm_std::testing::{mock_dependencies, mock_env};
use cw2::{get_contract_version, ContractVersion};

use crate::{
contract::{CONTRACT_NAME, CONTRACT_VERSION},
state::CwCallService,
MigrateMsg,
};

#[test]
fn test_migrate() {
let mut mock_deps = mock_dependencies();
let env = mock_env();

let contract = CwCallService::default();
let result = contract.migrate(mock_deps.as_mut(), env, MigrateMsg {});
assert!(result.is_ok());
let expected = ContractVersion {
contract: CONTRACT_NAME.to_string(),
version: CONTRACT_VERSION.to_string(),
};
let version = get_contract_version(&mock_deps.storage).unwrap();
println!("{version:?}");
assert_eq!(expected, version);
}
}
Loading

0 comments on commit 5e61bf9

Please sign in to comment.