Fix process tests

This commit is contained in:
Sosthene 2024-12-06 15:27:53 +01:00
parent 1789f95761
commit 0f47588e8d

View File

@ -475,7 +475,6 @@ mod tests {
};
use crate::pcd::{Member, ValidationRule};
use crate::signature::{AnkValidationNoHash, AnkValidationYesHash};
use super::*;
@ -573,62 +572,47 @@ mod tests {
ValidationRule::new(1.0, vec!["field1".to_owned(), "roles".to_owned()], 0.5).unwrap();
let validation_rule2 = ValidationRule::new(1.0, vec!["field2".to_owned()], 0.5).unwrap();
let encrypted_pcd = json!({
let role_def1 = RoleDefinition {
members: vec![alice_bob],
validation_rules: vec![validation_rule1],
storages: vec![]
};
let role_def2 = RoleDefinition {
members: vec![carol],
validation_rules: vec![validation_rule2],
storages: vec![]
};
let clear_pcd = json!({
"field1": "value1",
"field2": "value2",
"roles": {
"role1": {
"members": [alice_bob],
"validation_rules": [validation_rule1]
},
"role2": {
"members": [carol],
"validation_rules": [validation_rule2]
}
"role1": role_def1,
"role2": role_def2
}
});
let outpoint = OutPoint::null();
let pcd_commitment = encrypted_pcd.hash_fields(outpoint).unwrap();
let pcd_commitment = clear_pcd.hash_all_fields(outpoint).unwrap();
let mut fields2keys = Map::new();
let mut fields2cipher = Map::new();
// let field_to_encrypt: Vec<String> = encrypted_pcd.as_object().unwrap().keys().map(|k| k.clone()).collect();
let field_to_encrypt = vec!["field1".to_string(), "field2".to_string()];
let field_to_encrypt = vec!["field1".to_string(), "field2".to_string(), "roles".to_string()];
encrypted_pcd
clear_pcd
.encrypt_fields(&field_to_encrypt, &mut fields2keys, &mut fields2cipher)
.unwrap();
ProcessState {
commited_in: outpoint,
pcd_commitment: Value::Object(pcd_commitment),
encrypted_pcd,
keys: Map::new(),
encrypted_pcd: Value::Object(fields2cipher),
keys: fields2keys,
validation_tokens: vec![],
}
}
fn add_validation_token(state: &mut ProcessState, signing_key: SecretKey, accept: bool) {
let pcd_hash = AnkPcdHash::from_value(&state.encrypted_pcd);
if accept {
let validation_hash = AnkValidationYesHash::from_commitment(pcd_hash);
let proof = Proof::new(
crate::signature::AnkHash::ValidationYes(validation_hash),
signing_key,
);
state.validation_tokens.push(proof);
} else {
let validation_hash = AnkValidationNoHash::from_commitment(pcd_hash);
let proof = Proof::new(
crate::signature::AnkHash::ValidationNo(validation_hash),
signing_key,
);
state.validation_tokens.push(proof);
}
}
#[test]
fn test_error_no_proofs() {
let state = dummy_process_state();
@ -647,7 +631,8 @@ mod tests {
let signing_key =
SecretKey::from_str("39b2a765dc93e02da04a0e9300224b4f99fa7b83cfae49036dff58613fd3277c")
.unwrap();
add_validation_token(&mut state, signing_key, true);
let message_hash = state.get_message_hash(true).unwrap();
state.validation_tokens.push(Proof::new(message_hash, signing_key));
let result = state.is_valid(Some(&state));
assert!(result.is_err());
assert_eq!(
@ -664,7 +649,8 @@ mod tests {
let signing_key =
SecretKey::from_str("39b2a765dc93e02da04a0e9300224b4f99fa7b83cfae49036dff58613fd3277c")
.unwrap();
add_validation_token(&mut state, signing_key, true);
let message_hash = state.get_message_hash(true).unwrap();
state.validation_tokens.push(Proof::new(message_hash, signing_key));
let result = state.is_valid(None);
assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), "Not enough valid proofs");
@ -680,7 +666,8 @@ mod tests {
.get_spend_key()
.try_into()
.unwrap();
add_validation_token(&mut state, carol_key, true);
let message_hash = state.get_message_hash(true).unwrap();
state.validation_tokens.push(Proof::new(message_hash, carol_key));
let result = state.is_valid(None);
assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), "Not enough valid proofs");
@ -701,8 +688,9 @@ mod tests {
.get_spend_key()
.try_into()
.unwrap();
add_validation_token(&mut state, alice_key, true);
add_validation_token(&mut state, carol_key, true);
let message_hash = state.get_message_hash(true).unwrap();
state.validation_tokens.push(Proof::new(message_hash, alice_key));
state.validation_tokens.push(Proof::new(message_hash, carol_key));
let result = state.is_valid(None);
assert!(result.is_ok());
}
@ -726,9 +714,10 @@ mod tests {
.get_spend_key()
.try_into()
.unwrap();
add_validation_token(&mut state, alice_key, true);
add_validation_token(&mut state, bob_key, true);
add_validation_token(&mut state, carol_key, true);
let message_hash = state.get_message_hash(true).unwrap();
state.validation_tokens.push(Proof::new(message_hash, alice_key));
state.validation_tokens.push(Proof::new(message_hash, bob_key));
state.validation_tokens.push(Proof::new(message_hash, carol_key));
let result = state.is_valid(None);
assert!(result.is_ok());
}
@ -752,9 +741,11 @@ mod tests {
.get_spend_key()
.try_into()
.unwrap();
add_validation_token(&mut state, alice_key, true);
add_validation_token(&mut state, bob_key, true);
add_validation_token(&mut state, carol_key, false);
let message_hash_yes = state.get_message_hash(true).unwrap();
let message_hash_no = state.get_message_hash(false).unwrap();
state.validation_tokens.push(Proof::new(message_hash_yes, alice_key));
state.validation_tokens.push(Proof::new(message_hash_yes, bob_key));
state.validation_tokens.push(Proof::new(message_hash_no, carol_key));
let result = state.is_valid(None);
assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), "Not enough valid proofs");
@ -779,9 +770,11 @@ mod tests {
.get_spend_key()
.try_into()
.unwrap();
add_validation_token(&mut state, alice_key, true);
add_validation_token(&mut state, bob_key, false);
add_validation_token(&mut state, carol_key, true);
let message_hash_yes = state.get_message_hash(true).unwrap();
let message_hash_no = state.get_message_hash(false).unwrap();
state.validation_tokens.push(Proof::new(message_hash_yes, alice_key));
state.validation_tokens.push(Proof::new(message_hash_no, bob_key));
state.validation_tokens.push(Proof::new(message_hash_yes, carol_key));
let result = state.is_valid(None);
assert!(result.is_ok());
}
@ -791,13 +784,8 @@ mod tests {
fn test_valid_everyone_signs_with_prev_state() {
let state = dummy_process_state();
let mut new_state = state.clone();
if let Value::Object(ref mut map) = new_state.encrypted_pcd {
// Modify the field
map.insert("field1".to_string(), Value::String("new_value1".to_owned()));
} else {
// Handle the case where encrypted_pcd is not an object
panic!("encrypted_pcd is not a JSON object.");
}
let key_to_modify = state.encrypted_pcd.as_object().unwrap().keys().next().unwrap();
new_state.update_value(key_to_modify.as_str(), Value::String("new_value1".to_string())).unwrap();
let alice_key: SecretKey = create_alice_wallet()
.get_client()
.get_spend_key()
@ -813,9 +801,10 @@ mod tests {
.get_spend_key()
.try_into()
.unwrap();
add_validation_token(&mut new_state, alice_key, true);
add_validation_token(&mut new_state, bob_key, true);
add_validation_token(&mut new_state, carol_key, true);
let message_hash = new_state.get_message_hash(true).unwrap();
new_state.validation_tokens.push(Proof::new(message_hash, alice_key));
new_state.validation_tokens.push(Proof::new(message_hash, bob_key));
new_state.validation_tokens.push(Proof::new(message_hash, carol_key));
let result = new_state.is_valid(Some(&state));
assert!(result.is_ok());
}
@ -825,21 +814,16 @@ mod tests {
fn test_error_not_right_signatures_with_prev_state() {
let state = dummy_process_state();
let mut new_state = state.clone();
if let Value::Object(ref mut map) = new_state.encrypted_pcd {
// Modify the field
map.insert("field1".to_string(), Value::String("new_value1".to_owned()));
} else {
// Handle the case where encrypted_pcd is not an object
panic!("encrypted_pcd is not a JSON object.");
}
let key_to_modify = state.encrypted_pcd.as_object().unwrap().keys().next().unwrap();
new_state.update_value(key_to_modify.as_str(), Value::String("new_value1".to_string())).unwrap();
let carol_key: SecretKey = create_carol_wallet()
.get_client()
.get_spend_key()
.try_into()
.unwrap();
add_validation_token(&mut new_state, carol_key, true);
let message_hash = new_state.get_message_hash(true).unwrap();
new_state.validation_tokens.push(Proof::new(message_hash, carol_key));
let result = new_state.is_valid(Some(&state));
assert!(result.is_err());
assert_eq!(result.unwrap_err().to_string(), "Not enough valid proofs");
}
}