Skip to content

Commit 8693044

Browse files
committed
fix(cardano-blockchain-types): add validation test
Signed-off-by: bkioshn <[email protected]>
1 parent 26f7b4d commit 8693044

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

rust/cardano-blockchain-types/src/cip36/validation.rs

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,133 @@ pub(crate) fn validate_purpose(cip36: &Cip36, validation_report: &mut Vec<String
8282
}
8383
true
8484
}
85+
86+
#[cfg(test)]
87+
mod tests {
88+
89+
use ed25519_dalek::VerifyingKey;
90+
use pallas::ledger::addresses::Address;
91+
92+
use super::validate_purpose;
93+
use crate::{
94+
cip36::{
95+
key_registration::VotingPubKey, validate_payment_address_network, validate_voting_keys,
96+
},
97+
Cip36, Cip36KeyRegistration, Cip36RegistrationWitness, Network,
98+
};
99+
100+
fn create_empty_cip36(strict: bool) -> Cip36 {
101+
Cip36 {
102+
key_registration: Cip36KeyRegistration::default(),
103+
registration_witness: Cip36RegistrationWitness::default(),
104+
is_catalyst_strict: strict,
105+
}
106+
}
107+
108+
#[test]
109+
fn test_validate_payment_address_network() {
110+
let mut cip36 = create_empty_cip36(true);
111+
// cSpell:disable
112+
let addr = Address::from_bech32("addr_test1qprhw4s70k0vzyhvxp6h97hvrtlkrlcvlmtgmaxdtjz87xrjkctk27ypuv9dzlzxusqse89naweygpjn5dxnygvus05sdq9h07").expect("Failed to create address");
113+
// cSpell:enable
114+
let Address::Shelley(shelley_addr) = addr else {
115+
panic!("Invalid address type")
116+
};
117+
cip36.key_registration.payment_addr = Some(shelley_addr);
118+
let mut report = Vec::new();
119+
120+
let valid = validate_payment_address_network(&cip36, Network::Preprod, &mut report);
121+
122+
assert_eq!(report.len(), 0);
123+
assert_eq!(valid, Some(true));
124+
}
125+
126+
#[test]
127+
fn test_validate_invalid_payment_address_network() {
128+
let mut cip36 = create_empty_cip36(true);
129+
// cSpell:disable
130+
let addr = Address::from_bech32("addr_test1qprhw4s70k0vzyhvxp6h97hvrtlkrlcvlmtgmaxdtjz87xrjkctk27ypuv9dzlzxusqse89naweygpjn5dxnygvus05sdq9h07").expect("Failed to create address");
131+
// cSpell:enable
132+
let Address::Shelley(shelley_addr) = addr else {
133+
panic!("Invalid address type")
134+
};
135+
cip36.key_registration.payment_addr = Some(shelley_addr);
136+
let mut report = Vec::new();
137+
138+
let valid = validate_payment_address_network(&cip36, Network::Mainnet, &mut report);
139+
140+
assert_eq!(report.len(), 1);
141+
assert!(report
142+
.first()
143+
.expect("Failed to get the first index")
144+
.contains("does not match the network used"));
145+
assert_eq!(valid, Some(false));
146+
}
147+
148+
#[test]
149+
fn test_validate_voting_keys() {
150+
let mut cip36 = create_empty_cip36(true);
151+
cip36.key_registration.voting_pks.push(VotingPubKey {
152+
voting_pk: VerifyingKey::default(),
153+
weight: 1,
154+
});
155+
let mut report = Vec::new();
156+
157+
let valid = validate_voting_keys(&cip36, &mut report);
158+
159+
assert_eq!(report.len(), 0);
160+
assert!(valid);
161+
}
162+
163+
#[test]
164+
fn test_validate_invalid_voting_keys() {
165+
let mut cip36 = create_empty_cip36(true);
166+
cip36.key_registration.voting_pks.push(VotingPubKey {
167+
voting_pk: VerifyingKey::default(),
168+
weight: 1,
169+
});
170+
cip36.key_registration.voting_pks.push(VotingPubKey {
171+
voting_pk: VerifyingKey::default(),
172+
weight: 1,
173+
});
174+
let mut report = Vec::new();
175+
176+
let valid = validate_voting_keys(&cip36, &mut report);
177+
178+
assert_eq!(report.len(), 1);
179+
assert!(report
180+
.first()
181+
.expect("Failed to get the first index")
182+
.contains("Catalyst supports only a single voting key"));
183+
assert!(!valid);
184+
}
185+
186+
#[test]
187+
fn test_validate_purpose() {
188+
let cip36 = create_empty_cip36(true);
189+
let mut report = Vec::new();
190+
191+
let valid = validate_purpose(&cip36, &mut report);
192+
193+
assert_eq!(report.len(), 0);
194+
assert_eq!(cip36.purpose(), 0);
195+
assert!(valid);
196+
}
197+
198+
#[test]
199+
fn test_validate_invalid_purpose() {
200+
let mut cip36 = create_empty_cip36(true);
201+
cip36.key_registration.purpose = 1;
202+
let mut report = Vec::new();
203+
204+
let valid = validate_purpose(&cip36, &mut report);
205+
206+
assert_eq!(report.len(), 1);
207+
assert!(report
208+
.first()
209+
.expect("Failed to get the first index")
210+
.contains("unknown purpose"));
211+
assert_eq!(cip36.purpose(), 1);
212+
assert!(!valid);
213+
}
214+
}

0 commit comments

Comments
 (0)