signatures: Make some test assertions more helpful
This commit is contained in:
parent
2e4e8d82ce
commit
75cf634413
@ -31,3 +31,6 @@ sha2 = "0.9.5"
|
||||
subslice = { version = "0.2.3", optional = true }
|
||||
thiserror = "1.0.26"
|
||||
tracing = { version = "0.1.25", optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
assert_matches = "1.5.0"
|
||||
|
@ -877,6 +877,7 @@ fn is_third_party_invite(object: &CanonicalJsonObject) -> Result<bool, Error> {
|
||||
mod tests {
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use assert_matches::assert_matches;
|
||||
use ruma_common::{
|
||||
serde::{Base64, CanonicalJsonValue},
|
||||
RoomVersionId, ServerSigningKeyId, SigningKeyAlgorithm,
|
||||
@ -949,11 +950,10 @@ mod tests {
|
||||
).unwrap();
|
||||
|
||||
let public_key_map = BTreeMap::new();
|
||||
let verification_result = verify_event(&public_key_map, &signed_event, &RoomVersionId::V6);
|
||||
let verification =
|
||||
verify_event(&public_key_map, &signed_event, &RoomVersionId::V6).unwrap();
|
||||
|
||||
assert!(verification_result.is_ok());
|
||||
let verification = verification_result.unwrap();
|
||||
assert!(matches!(verification, Verified::Signatures));
|
||||
assert_eq!(verification, Verified::Signatures);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -988,11 +988,10 @@ mod tests {
|
||||
add_key_to_map(&mut public_key_map, "domain-sender", &key_pair_sender);
|
||||
add_key_to_map(&mut public_key_map, "domain-event", &key_pair_event);
|
||||
|
||||
let verification_result = verify_event(&public_key_map, &signed_event, &RoomVersionId::V1);
|
||||
let verification =
|
||||
verify_event(&public_key_map, &signed_event, &RoomVersionId::V1).unwrap();
|
||||
|
||||
assert!(verification_result.is_ok());
|
||||
let verification = verification_result.unwrap();
|
||||
assert!(matches!(verification, Verified::Signatures));
|
||||
assert_eq!(verification, Verified::Signatures);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -1027,11 +1026,10 @@ mod tests {
|
||||
add_key_to_map(&mut public_key_map, "domain-sender", &key_pair_sender);
|
||||
add_key_to_map(&mut public_key_map, "domain-authorized", &key_pair_authorized);
|
||||
|
||||
let verification_result = verify_event(&public_key_map, &signed_event, &RoomVersionId::V9);
|
||||
let verification =
|
||||
verify_event(&public_key_map, &signed_event, &RoomVersionId::V9).unwrap();
|
||||
|
||||
assert!(verification_result.is_ok());
|
||||
let verification = verification_result.unwrap();
|
||||
assert!(matches!(verification, Verified::Signatures));
|
||||
assert_eq!(verification, Verified::Signatures);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -1065,8 +1063,11 @@ mod tests {
|
||||
|
||||
let verification_result = verify_event(&public_key_map, &signed_event, &RoomVersionId::V9);
|
||||
|
||||
// Should be Err(VerificationError::signature_not_found("domain-authorized")));
|
||||
assert!(verification_result.is_err());
|
||||
let server = assert_matches!(
|
||||
verification_result,
|
||||
Err(Error::Verification(VerificationError::SignatureNotFound(server))) => server
|
||||
);
|
||||
assert_eq!(server, "domain-authorized");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -1099,13 +1100,11 @@ mod tests {
|
||||
let public_key_map = BTreeMap::new();
|
||||
let verification_result = verify_event(&public_key_map, &signed_event, &RoomVersionId::V6);
|
||||
|
||||
assert!(verification_result.is_err());
|
||||
let error_msg = verification_result.err().unwrap();
|
||||
if let Error::Verification(VerificationError::PublicKeyNotFound(entity)) = error_msg {
|
||||
assert_eq!(entity, "domain-sender");
|
||||
} else {
|
||||
panic!("Error was not VerificationError::UnknownPublicKeysForEvent: {error_msg:?}");
|
||||
};
|
||||
let entity = assert_matches!(
|
||||
verification_result,
|
||||
Err(Error::Verification(VerificationError::PublicKeyNotFound(entity))) => entity
|
||||
);
|
||||
assert_eq!(entity, "domain-sender");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -1147,15 +1146,13 @@ mod tests {
|
||||
|
||||
let verification_result = verify_event(&public_key_map, &signed_event, &RoomVersionId::V6);
|
||||
|
||||
assert!(verification_result.is_err());
|
||||
let error_msg = verification_result.err().unwrap();
|
||||
if let Error::Verification(VerificationError::Signature(error)) = error_msg {
|
||||
// dalek doesn't expose InternalError :(
|
||||
// https://github.com/dalek-cryptography/ed25519-dalek/issues/174
|
||||
assert!(format!("{error:?}").contains("Some(Verification equation was not satisfied)"));
|
||||
} else {
|
||||
panic!("Error was not VerificationError::Signature: {error_msg:?}");
|
||||
};
|
||||
let error = assert_matches!(
|
||||
verification_result,
|
||||
Err(Error::Verification(VerificationError::Signature(error))) => error
|
||||
);
|
||||
// dalek doesn't expose InternalError :(
|
||||
// https://github.com/dalek-cryptography/ed25519-dalek/issues/174
|
||||
assert!(format!("{error:?}").contains("Some(Verification equation was not satisfied)"));
|
||||
}
|
||||
|
||||
fn generate_key_pair() -> Ed25519KeyPair {
|
||||
|
@ -252,7 +252,7 @@ mod tests {
|
||||
let mut public_key_map = BTreeMap::new();
|
||||
public_key_map.insert("domain".into(), signature_set);
|
||||
|
||||
assert!(verify_json(&public_key_map, &value).is_ok());
|
||||
verify_json(&public_key_map, &value).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -293,13 +293,13 @@ mod tests {
|
||||
let mut public_key_map = BTreeMap::new();
|
||||
public_key_map.insert("domain".into(), signature_set);
|
||||
|
||||
assert!(verify_json(&public_key_map, &value).is_ok());
|
||||
verify_json(&public_key_map, &value).unwrap();
|
||||
|
||||
let reverse_value = from_json_str(
|
||||
r#"{"two":"Two","signatures":{"domain":{"ed25519:1":"t6Ehmh6XTDz7qNWI0QI5tNPSliWLPQP/+Fzz3LpdCS7q1k2G2/5b5Embs2j4uG3ZeivejrzqSVoBcdocRpa+AQ"}},"one":1}"#
|
||||
).unwrap();
|
||||
|
||||
assert!(verify_json(&public_key_map, &reverse_value).is_ok());
|
||||
verify_json(&public_key_map, &reverse_value).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -312,7 +312,7 @@ mod tests {
|
||||
let mut public_key_map = BTreeMap::new();
|
||||
public_key_map.insert("domain".into(), signature_set);
|
||||
|
||||
assert!(verify_json(&public_key_map, &value).is_err());
|
||||
verify_json(&public_key_map, &value).unwrap_err();
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -415,6 +415,6 @@ mod tests {
|
||||
}"#
|
||||
).unwrap();
|
||||
|
||||
assert!(verify_event(&public_key_map, &value, &RoomVersionId::V5).is_ok());
|
||||
verify_event(&public_key_map, &value, &RoomVersionId::V5).unwrap();
|
||||
}
|
||||
}
|
||||
|
@ -85,21 +85,21 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn valid_key_id() {
|
||||
assert!(Signature::new("ed25519:abcdef", &[]).is_ok());
|
||||
Signature::new("ed25519:abcdef", &[]).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_valid_key_id_length() {
|
||||
assert!(Signature::new("ed25519:abcdef:123456", &[]).is_err());
|
||||
Signature::new("ed25519:abcdef:123456", &[]).unwrap_err();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_key_id_version() {
|
||||
assert!(Signature::new("ed25519:abc!def", &[]).is_err());
|
||||
Signature::new("ed25519:abc!def", &[]).unwrap_err();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_key_id_algorithm() {
|
||||
assert!(Signature::new("foobar:abcdef", &[]).is_err());
|
||||
Signature::new("foobar:abcdef", &[]).unwrap_err();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user