-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from CosmWasm/test-mock-api
Add tests for `MockApi` in `App`
- Loading branch information
Showing
5 changed files
with
201 additions
and
27 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
use bech32::{decode, encode, FromBase32, ToBase32, Variant}; | ||
use cosmwasm_std::{ | ||
Addr, Api, CanonicalAddr, HexBinary, RecoverPubkeyError, StdError, StdResult, VerificationError, | ||
}; | ||
use cw_multi_test::AppBuilder; | ||
use sha2::{Digest, Sha256}; | ||
|
||
struct MyApi { | ||
prefix: &'static str, | ||
} | ||
|
||
impl MyApi { | ||
fn new(prefix: &'static str) -> Self { | ||
Self { prefix } | ||
} | ||
} | ||
|
||
impl Api for MyApi { | ||
fn addr_validate(&self, input: &str) -> StdResult<Addr> { | ||
let canonical = self.addr_canonicalize(input)?; | ||
let normalized = self.addr_humanize(&canonical)?; | ||
if input != normalized { | ||
Err(StdError::generic_err( | ||
"Invalid input: address not normalized", | ||
)) | ||
} else { | ||
Ok(Addr::unchecked(input)) | ||
} | ||
} | ||
|
||
fn addr_canonicalize(&self, input: &str) -> StdResult<CanonicalAddr> { | ||
if let Ok((prefix, decoded, Variant::Bech32)) = decode(input) { | ||
if prefix == self.prefix { | ||
if let Ok(bytes) = Vec::<u8>::from_base32(&decoded) { | ||
return Ok(bytes.into()); | ||
} | ||
} | ||
} | ||
Err(StdError::generic_err("Invalid input")) | ||
} | ||
|
||
fn addr_humanize(&self, canonical: &CanonicalAddr) -> StdResult<Addr> { | ||
if let Ok(encoded) = encode( | ||
self.prefix, | ||
canonical.as_slice().to_base32(), | ||
Variant::Bech32, | ||
) { | ||
Ok(Addr::unchecked(encoded)) | ||
} else { | ||
Err(StdError::generic_err("Invalid canonical address")) | ||
} | ||
} | ||
|
||
fn secp256k1_verify( | ||
&self, | ||
_message_hash: &[u8], | ||
_signature: &[u8], | ||
_public_key: &[u8], | ||
) -> Result<bool, VerificationError> { | ||
unimplemented!() | ||
} | ||
|
||
fn secp256k1_recover_pubkey( | ||
&self, | ||
_message_hash: &[u8], | ||
_signature: &[u8], | ||
_recovery_param: u8, | ||
) -> Result<Vec<u8>, RecoverPubkeyError> { | ||
unimplemented!() | ||
} | ||
|
||
fn ed25519_verify( | ||
&self, | ||
_message: &[u8], | ||
_signature: &[u8], | ||
_public_key: &[u8], | ||
) -> Result<bool, VerificationError> { | ||
unimplemented!() | ||
} | ||
|
||
fn ed25519_batch_verify( | ||
&self, | ||
_messages: &[&[u8]], | ||
_signatures: &[&[u8]], | ||
_public_keys: &[&[u8]], | ||
) -> Result<bool, VerificationError> { | ||
unimplemented!() | ||
} | ||
|
||
fn debug(&self, _message: &str) { | ||
unimplemented!() | ||
} | ||
} | ||
|
||
impl MyApi { | ||
fn addr_make(&self, input: &str) -> Addr { | ||
let digest = Sha256::digest(input).to_vec(); | ||
match encode(self.prefix, digest.to_base32(), Variant::Bech32) { | ||
Ok(address) => Addr::unchecked(address), | ||
Err(reason) => panic!("Generating address failed with reason: {reason}"), | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn building_app_with_custom_api_should_work() { | ||
// prepare test data | ||
let human = "juno1h34lmpywh4upnjdg90cjf4j70aee6z8qqfspugamjp42e4q28kqsksmtyp"; | ||
let hex = "bc6bfd848ebd7819c9a82bf124d65e7f739d08e002601e23bb906aacd40a3d81"; | ||
|
||
// create application with custom api that implements | ||
// Bech32 address encoding with 'juno' prefix | ||
let app = AppBuilder::default() | ||
.with_api(MyApi::new("juno")) | ||
.build(|_, _, _| {}); | ||
|
||
// check address validation function | ||
assert_eq!( | ||
app.api().addr_validate(human).unwrap(), | ||
Addr::unchecked(human) | ||
); | ||
|
||
// check if address can be canonicalized | ||
assert_eq!( | ||
app.api().addr_canonicalize(human).unwrap(), | ||
CanonicalAddr::from(HexBinary::from_hex(hex).unwrap()) | ||
); | ||
|
||
// check if address can be humanized | ||
assert_eq!( | ||
app.api() | ||
.addr_humanize(&app.api().addr_canonicalize(human).unwrap()) | ||
.unwrap(), | ||
Addr::unchecked(human) | ||
); | ||
|
||
// check extension function for creating Bech32 encoded addresses | ||
assert_eq!(app.api().addr_make("creator"), Addr::unchecked(human)); | ||
} |