Files
SHiNE-server/shine-solana/shine/tests/registration_v2_vectors.rs
T

60 lines
3.1 KiB
Rust

//! Общие зафиксированные Rust/Java/JS фикстуры. Запуск: cargo test --test registration_v2_vectors.
use std::str::FromStr;
use solana_program::{hash::hash, pubkey::Pubkey};
use shine_users::{certificate_inner_message, find_namespace_pda, find_user_pda, serialize_namespace, user_pda_seed, NamespaceState};
fn fixture(key: &str) -> String {
include_str!("fixtures/registration-v2.properties").lines()
.find_map(|line| line.split_once('=').filter(|(name,_)| *name==key).map(|(_,value)| value.to_string()))
.expect("ключ тест-вектора")
}
fn decode_hex(text: &str) -> Vec<u8> {
assert_eq!(text.len()%2,0);
(0..text.len()).step_by(2).map(|i| u8::from_str_radix(&text[i..i+2],16).unwrap()).collect()
}
fn encode_hex(bytes: &[u8]) -> String {
bytes.iter().map(|b| format!("{:02x}",b)).collect()
}
#[test]
fn user_pdas_match_portable_vectors() {
let program=Pubkey::from_str(&fixture("program_id")).unwrap();
for (login,key) in [
("alice@shine","alice_shine"), ("bob@free","bob_free"),
("a_b@shine","a_underscore_b_shine"), ("a@shine","a_shine"),
("aaaaaaaaaaaaaaaaaaaaaaaa@shine","aaaaaaaaaaaaaaaaaaaaaaaa_shine"),
("aaaaaaaaaaaaaaaaaaaaaaaaa@shine","aaaaaaaaaaaaaaaaaaaaaaaaa_shine")
] {
assert_eq!(encode_hex(&user_pda_seed(login)),fixture(&format!("user_{key}_seed")));
let (pda,bump)=find_user_pda(&program,login);
assert_eq!(pda.to_string(),fixture(&format!("user_{key}_pda")));
assert_eq!(bump.to_string(),fixture(&format!("user_{key}_bump")));
}
}
#[test]
fn namespace_pdas_and_binaries_match_vectors() {
let program=Pubkey::from_str(&fixture("program_id")).unwrap();
for (name,typ,fee) in [("shine",1,10_000_000), ("free",2,5_000_000)] {
let (pda,bump)=find_namespace_pda(&program,name);
assert_eq!(pda.to_string(),fixture(&format!("namespace_{name}_pda")));
assert_eq!(bump.to_string(),fixture(&format!("namespace_{name}_bump")));
assert_eq!(encode_hex(hash(format!("namespace={name}").as_bytes()).as_ref()),fixture(&format!("namespace_{name}_seed")));
let registrar=if typ==2 {Some(Pubkey::new_from_array(decode_hex(&fixture("registrar_pubkey_hex")).try_into().unwrap()))}else{None};
let state=NamespaceState {namespace_type:typ,min_localpart_len:3,namespace:name.to_string(),registration_fee:fee,registrar_pubkey:registrar,server_login:if typ==2{Some("registrar@shine".to_string())}else{None}};
assert_eq!(encode_hex(&serialize_namespace(&state)),fixture(&format!("namespace_{name}_bytes_hex")));
}
}
#[test]
fn free_certificate_inner_message_matches_vector() {
let msg=decode_hex(&fixture("inner_message_hex"));
assert!(msg.starts_with(b"SHiNE_FREE_REG_V1\0"));
assert_eq!(msg[18],1);
assert_eq!(msg[19],0);
assert_eq!(msg[20],8);
assert_eq!(&msg[21..29],b"bob@free");
let message=certificate_inner_message("bob@free",
&Pubkey::new_from_array(msg[29..61].try_into().unwrap()),
&Pubkey::new_from_array(msg[61..93].try_into().unwrap()),
&Pubkey::new_from_array(msg[93..125].try_into().unwrap())).unwrap();
assert_eq!(message,msg);
}