Format test

This commit is contained in:
cygnet 2023-07-23 22:36:23 +02:00
parent 6c83881619
commit 74d6050151
1 changed files with 75 additions and 94 deletions

View File

@ -8,109 +8,90 @@ mod utils;
mod tests { mod tests {
use std::{collections::HashSet, str::FromStr}; use std::{collections::HashSet, str::FromStr};
use secp256k1::XOnlyPublicKey; use secp256k1::XOnlyPublicKey;
use crate::{input::{self, ComparableHashMap}, sending::create_outputs, receiving::{derive_silent_payment_key_pair, get_receiving_addresses, get_A_sum_public_keys, scanning, verify_and_calculate_signatures}, utils::hash_outpoints}; use crate::{input::{self, ComparableHashMap, TestData}, sending::create_outputs, receiving::{derive_silent_payment_key_pair, get_receiving_addresses, get_A_sum_public_keys, scanning, verify_and_calculate_signatures}, utils::hash_outpoints};
#[test] #[test]
fn test_with_test_vectors() { fn test_with_test_vectors() {
let testdata = input::read_file(); let testdata = input::read_file();
for test in testdata { for test in testdata {
let mut sending_outputs: HashSet<String> = HashSet::new(); process_test_case(test);
eprintln!("test.comment = {:?}", test.comment);
for sendingtest in test.sending {
let given = sendingtest.given;
let expected = sendingtest.expected;
let expected_comparable: HashSet<ComparableHashMap> =
expected.outputs.into_iter().map(|x| x.into()).collect();
let outputs = create_outputs(&given);
for map in &outputs {
for key in map.keys() {
sending_outputs.insert(key.clone());
}
}
let outputs_comparable: HashSet<ComparableHashMap> =
outputs.into_iter().map(|x| x.into()).collect();
assert_eq!(outputs_comparable, expected_comparable);
// if outputs_comparable == expected_comparable {
// println!("sending succeeded");
// } else {
// eprintln!("sending expected = {:#?}", expected_comparable);
// eprintln!("sending outputs = {:#?}", outputs_comparable);
// std::process::exit(0);
// }
}
for receivingtest in test.receiving {
let given = &receivingtest.given;
let expected = &receivingtest.expected;
let receiving_outputs: HashSet<String> = given.outputs.iter().cloned().collect();
// assert that the sending outputs generated are equal
// to the expected receiving outputs
assert!(sending_outputs.is_subset(&receiving_outputs));
// todo fix seed?
let bip32_seed_str = &given.bip32_seed;
let (b_scan, b_spend, B_scan, B_spend) = derive_silent_payment_key_pair(bip32_seed_str);
let receiving_addresses = get_receiving_addresses(B_scan, B_spend, &given.labels);
let set1: HashSet<_> = receiving_addresses.iter().collect();
let set2: HashSet<_> = expected.addresses.iter().collect();
assert_eq!(set1, set2);
// if !set1.eq(&set2) {
// println!("receiving addressess failed");
// eprintln!("receiving_addresses = {:#?}", receiving_addresses);
// eprintln!("expected.addresses = {:#?}", expected.addresses);
// std::process::exit(0);
// }
// can be even or odd !
let outputs_to_check: Vec<XOnlyPublicKey> = given
.outputs
.iter()
.map(|x| XOnlyPublicKey::from_str(x).unwrap())
.collect();
let outpoints_hash = hash_outpoints(&given.outpoints);
let A_sum = get_A_sum_public_keys(&given.input_pub_keys);
let labels = match &given.labels.len() {
0 => None,
_ => Some(&given.labels),
};
let mut add_to_wallet = scanning(
b_scan,
B_spend,
A_sum,
outpoints_hash,
outputs_to_check,
labels,
);
let res = verify_and_calculate_signatures(&mut add_to_wallet, b_spend).unwrap();
assert_eq!(res, expected.outputs);
// if res.eq(&expected.outputs) {
// println!("receiving succeeded");
// } else {
// eprintln!("res = {:#?}", res);
// eprintln!("expected.outputs = {:#?}", expected.outputs);
// println!("receiving failed");
// std::process::exit(0);
// }
}
} }
} }
fn process_test_case(test_case: TestData) {
let mut sending_outputs: HashSet<String> = HashSet::new();
eprintln!("test.comment = {:?}", test_case.comment);
for sendingtest in test_case.sending {
let given = sendingtest.given;
let expected = sendingtest.expected;
let expected_comparable: HashSet<ComparableHashMap> =
expected.outputs.into_iter().map(|x| x.into()).collect();
let outputs = create_outputs(&given);
for map in &outputs {
for key in map.keys() {
sending_outputs.insert(key.clone());
}
}
let outputs_comparable: HashSet<ComparableHashMap> =
outputs.into_iter().map(|x| x.into()).collect();
assert_eq!(outputs_comparable, expected_comparable);
}
for receivingtest in &test_case.receiving {
let given = &receivingtest.given;
let expected = &receivingtest.expected;
let receiving_outputs: HashSet<String> = given.outputs.iter().cloned().collect();
// assert that the sending outputs generated are equal
// to the expected receiving outputs
assert!(sending_outputs.is_subset(&receiving_outputs));
// todo fix seed?
let bip32_seed_str = &given.bip32_seed;
let (b_scan, b_spend, B_scan, B_spend) = derive_silent_payment_key_pair(bip32_seed_str);
let receiving_addresses = get_receiving_addresses(B_scan, B_spend, &given.labels);
let set1: HashSet<_> = receiving_addresses.iter().collect();
let set2: HashSet<_> = expected.addresses.iter().collect();
assert_eq!(set1, set2);
// can be even or odd !
let outputs_to_check: Vec<XOnlyPublicKey> = given
.outputs
.iter()
.map(|x| XOnlyPublicKey::from_str(x).unwrap())
.collect();
let outpoints_hash = hash_outpoints(&given.outpoints);
let A_sum = get_A_sum_public_keys(&given.input_pub_keys);
let labels = match &given.labels.len() {
0 => None,
_ => Some(&given.labels),
};
let mut add_to_wallet = scanning(
b_scan,
B_spend,
A_sum,
outpoints_hash,
outputs_to_check,
labels,
);
let res = verify_and_calculate_signatures(&mut add_to_wallet, b_spend).unwrap();
assert_eq!(res, expected.outputs);
}
}
} }