ic-query 0.101.0

Internet Computer query library for CloudEngine, NNS, SNS, ICRC, system canisters, and public network metadata
Documentation
use super::*;
use ic_platform::Principal;
use serde_json::json;

#[test]
fn canonical_accounts_validate_wire_lengths_and_preserve_explicit_subaccounts() {
    ic_platform::candid_record! {

        struct UncheckedAccount {
            947_296_307 => owner: Principal,
            1_349_681_965 => subaccount: Option<Vec<u8>>,
        }
    }
    for length in [0, 31, 32, 33] {
        let bytes = crate::candid_codec::encode_value(&UncheckedAccount {
            owner: Principal::anonymous(),
            subaccount: Some(vec![0; length]),
        })
        .unwrap();
        assert_eq!(
            crate::candid_codec::decode_reply::<LedgerAccount>(&bytes).is_ok(),
            length == 32
        );
    }
    for (subaccount, expected) in [
        (None, json!(null)),
        (Some([0; 32]), json!("00".repeat(32))),
        (Some([0xff; 32]), json!("ff".repeat(32))),
    ] {
        let account = LedgerAccount {
            owner: Principal::anonymous(),
            subaccount,
        };
        assert_eq!(
            serde_json::to_value(Account(&account)).unwrap(),
            json!({
                "owner": "2vxsx-fae", "subaccount_hex": expected,
            })
        );
    }
}