mirror of
https://github.com/matrix-org/matrix-rust-sdk.git
synced 2026-05-05 14:35:20 -04:00
test(crypto): Add some basic snapshot testing in crypto crate
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -3230,6 +3230,7 @@ dependencies = [
|
||||
"hmac",
|
||||
"http",
|
||||
"indoc",
|
||||
"insta",
|
||||
"itertools 0.13.0",
|
||||
"js_option",
|
||||
"matrix-sdk-common",
|
||||
|
||||
@@ -85,6 +85,7 @@ assert_matches2 = { workspace = true }
|
||||
futures-executor = { workspace = true }
|
||||
http = { workspace = true }
|
||||
indoc = "2.0.5"
|
||||
insta = { workspace = true }
|
||||
matrix-sdk-test = { workspace = true }
|
||||
proptest = { workspace = true }
|
||||
similar-asserts = { workspace = true }
|
||||
|
||||
@@ -1093,3 +1093,24 @@ pub enum RoomEventDecryptionResult {
|
||||
///
|
||||
/// [1]: https://spec.matrix.org/unstable/client-server-api/#server-behaviour-4
|
||||
pub mod tutorial {}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use insta::assert_json_snapshot;
|
||||
|
||||
use crate::{DecryptionSettings, TrustRequirement};
|
||||
|
||||
#[test]
|
||||
fn snapshot_trust_requirement() {
|
||||
assert_json_snapshot!(TrustRequirement::Untrusted);
|
||||
assert_json_snapshot!(TrustRequirement::CrossSignedOrLegacy);
|
||||
assert_json_snapshot!(TrustRequirement::CrossSigned);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn snapshot_decryption_settings() {
|
||||
assert_json_snapshot!(DecryptionSettings {
|
||||
sender_device_trust_requirement: TrustRequirement::Untrusted,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -283,13 +283,16 @@ mod tests {
|
||||
use std::{cmp::Ordering, collections::BTreeMap};
|
||||
|
||||
use assert_matches2::assert_let;
|
||||
use ruma::{device_id, owned_device_id, owned_user_id, user_id};
|
||||
use vodozemac::Ed25519PublicKey;
|
||||
use insta::assert_json_snapshot;
|
||||
use ruma::{
|
||||
device_id, owned_device_id, owned_user_id, user_id, DeviceKeyAlgorithm, DeviceKeyId,
|
||||
};
|
||||
use vodozemac::{Curve25519PublicKey, Ed25519PublicKey};
|
||||
|
||||
use super::SenderData;
|
||||
use crate::{
|
||||
olm::KnownSenderData,
|
||||
types::{DeviceKeys, Signatures},
|
||||
types::{DeviceKey, DeviceKeys, EventEncryptionAlgorithm, Signatures},
|
||||
};
|
||||
|
||||
#[test]
|
||||
@@ -479,4 +482,52 @@ mod tests {
|
||||
assert_eq!(sender_unverified.compare_trust_level(&sender_verified), Ordering::Less);
|
||||
assert_eq!(sender_verified.compare_trust_level(&sender_unverified), Ordering::Greater);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn snapshot_sender_data() {
|
||||
assert_json_snapshot!(SenderData::UnknownDevice {
|
||||
legacy_session: false,
|
||||
owner_check_failed: true,
|
||||
});
|
||||
|
||||
assert_json_snapshot!(SenderData::UnknownDevice {
|
||||
legacy_session: true,
|
||||
owner_check_failed: false,
|
||||
});
|
||||
|
||||
assert_json_snapshot!(SenderData::DeviceInfo {
|
||||
device_keys: DeviceKeys::new(
|
||||
owned_user_id!("@foo:bar.baz"),
|
||||
owned_device_id!("DEV"),
|
||||
vec![
|
||||
EventEncryptionAlgorithm::MegolmV1AesSha2,
|
||||
EventEncryptionAlgorithm::OlmV1Curve25519AesSha2
|
||||
],
|
||||
BTreeMap::from_iter(vec![(
|
||||
DeviceKeyId::from_parts(DeviceKeyAlgorithm::Ed25519, device_id!("ABCDEFGH")),
|
||||
DeviceKey::Curve25519(Curve25519PublicKey::from_bytes([0u8; 32])),
|
||||
)]),
|
||||
Default::default(),
|
||||
),
|
||||
legacy_session: false,
|
||||
});
|
||||
|
||||
assert_json_snapshot!(SenderData::VerificationViolation(KnownSenderData {
|
||||
user_id: owned_user_id!("@foo:bar.baz"),
|
||||
device_id: Some(owned_device_id!("DEV")),
|
||||
master_key: Box::new(Ed25519PublicKey::from_slice(&[0u8; 32]).unwrap()),
|
||||
}));
|
||||
|
||||
assert_json_snapshot!(SenderData::SenderUnverified(KnownSenderData {
|
||||
user_id: owned_user_id!("@foo:bar.baz"),
|
||||
device_id: None,
|
||||
master_key: Box::new(Ed25519PublicKey::from_slice(&[1u8; 32]).unwrap()),
|
||||
}));
|
||||
|
||||
assert_json_snapshot!(SenderData::SenderVerified(KnownSenderData {
|
||||
user_id: owned_user_id!("@foo:bar.baz"),
|
||||
device_id: None,
|
||||
master_key: Box::new(Ed25519PublicKey::from_slice(&[1u8; 32]).unwrap()),
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
---
|
||||
source: crates/matrix-sdk-crypto/src/olm/group_sessions/sender_data.rs
|
||||
expression: "SenderData::UnknownDevice { legacy_session: true, owner_check_failed: false, }"
|
||||
---
|
||||
{
|
||||
"UnknownDevice": {
|
||||
"legacy_session": true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
---
|
||||
source: crates/matrix-sdk-crypto/src/olm/group_sessions/sender_data.rs
|
||||
expression: "SenderData::DeviceInfo\n{\n device_keys:\n DeviceKeys::new(owned_user_id!(\"@foo:bar.baz\"), owned_device_id!(\"DEV\"),\n vec!(EventEncryptionAlgorithm::MegolmV1AesSha2,\n EventEncryptionAlgorithm::OlmV1Curve25519AesSha2),\n BTreeMap::from_iter(vec![(DeviceKeyId::from_parts(DeviceKeyAlgorithm::Ed25519,\n device_id!(\"ABCDEFGH\")),\n DeviceKey::Curve25519(Curve25519PublicKey::from_bytes([0u8; 32])),)]),\n Default::default(),), legacy_session: false,\n}"
|
||||
---
|
||||
{
|
||||
"DeviceInfo": {
|
||||
"device_keys": {
|
||||
"user_id": "@foo:bar.baz",
|
||||
"device_id": "DEV",
|
||||
"algorithms": [
|
||||
"m.megolm.v1.aes-sha2",
|
||||
"m.olm.v1.curve25519-aes-sha2"
|
||||
],
|
||||
"keys": {
|
||||
"ed25519:ABCDEFGH": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
|
||||
},
|
||||
"signatures": {}
|
||||
},
|
||||
"legacy_session": false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
---
|
||||
source: crates/matrix-sdk-crypto/src/olm/group_sessions/sender_data.rs
|
||||
expression: "SenderData::VerificationViolation(KnownSenderData\n{\n user_id: owned_user_id!(\"@foo:bar.baz\"), device_id:\n Some(owned_device_id!(\"DEV\")), master_key:\n Box::new(Ed25519PublicKey::from_slice(&[0u8; 32]).unwrap()),\n})"
|
||||
---
|
||||
{
|
||||
"VerificationViolation": {
|
||||
"user_id": "@foo:bar.baz",
|
||||
"device_id": "DEV",
|
||||
"master_key": [
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
---
|
||||
source: crates/matrix-sdk-crypto/src/olm/group_sessions/sender_data.rs
|
||||
expression: "SenderData::SenderUnverified(KnownSenderData\n{\n user_id: owned_user_id!(\"@foo:bar.baz\"), device_id: None, master_key:\n Box::new(Ed25519PublicKey::from_slice(&[1u8; 32]).unwrap()),\n})"
|
||||
---
|
||||
{
|
||||
"SenderUnverified": {
|
||||
"user_id": "@foo:bar.baz",
|
||||
"device_id": null,
|
||||
"master_key": [
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
---
|
||||
source: crates/matrix-sdk-crypto/src/olm/group_sessions/sender_data.rs
|
||||
expression: "SenderData::SenderVerified(KnownSenderData\n{\n user_id: owned_user_id!(\"@foo:bar.baz\"), device_id: None, master_key:\n Box::new(Ed25519PublicKey::from_slice(&[1u8; 32]).unwrap()),\n})"
|
||||
---
|
||||
{
|
||||
"SenderVerified": {
|
||||
"user_id": "@foo:bar.baz",
|
||||
"device_id": null,
|
||||
"master_key": [
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
---
|
||||
source: crates/matrix-sdk-crypto/src/olm/group_sessions/sender_data.rs
|
||||
expression: "SenderData::UnknownDevice { legacy_session: false, owner_check_failed: true, }"
|
||||
---
|
||||
{
|
||||
"UnknownDevice": {
|
||||
"legacy_session": false,
|
||||
"owner_check_failed": true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
---
|
||||
source: crates/matrix-sdk-crypto/src/lib.rs
|
||||
expression: "DecryptionSettings\n{ sender_device_trust_requirement: TrustRequirement::Untrusted, }"
|
||||
---
|
||||
{
|
||||
"sender_device_trust_requirement": "Untrusted"
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
---
|
||||
source: crates/matrix-sdk-crypto/src/lib.rs
|
||||
expression: "TrustRequirement::CrossSignedOrLegacy"
|
||||
---
|
||||
"CrossSignedOrLegacy"
|
||||
@@ -0,0 +1,5 @@
|
||||
---
|
||||
source: crates/matrix-sdk-crypto/src/lib.rs
|
||||
expression: "TrustRequirement::CrossSigned"
|
||||
---
|
||||
"CrossSigned"
|
||||
@@ -0,0 +1,5 @@
|
||||
---
|
||||
source: crates/matrix-sdk-crypto/src/lib.rs
|
||||
expression: "TrustRequirement::Untrusted"
|
||||
---
|
||||
"Untrusted"
|
||||
@@ -108,10 +108,16 @@ impl Serialize for RoomKeyBackupInfo {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use assert_matches::assert_matches;
|
||||
use serde_json::json;
|
||||
use insta::{assert_json_snapshot, with_settings};
|
||||
use ruma::{user_id, DeviceKeyAlgorithm, KeyId};
|
||||
use serde_json::{json, Value};
|
||||
use vodozemac::{Curve25519PublicKey, Ed25519Signature};
|
||||
|
||||
use super::RoomKeyBackupInfo;
|
||||
use crate::types::{MegolmV1AuthData, Signature, Signatures};
|
||||
|
||||
#[test]
|
||||
fn serialization() {
|
||||
@@ -146,4 +152,32 @@ mod tests {
|
||||
let serialized = serde_json::to_value(deserialized).unwrap();
|
||||
assert_eq!(json, serialized);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn snapshot_room_key_backup_info() {
|
||||
let info = RoomKeyBackupInfo::MegolmBackupV1Curve25519AesSha2(MegolmV1AuthData {
|
||||
public_key: Curve25519PublicKey::from_bytes([2u8; 32]),
|
||||
signatures: Signatures(BTreeMap::from([(
|
||||
user_id!("@alice:localhost").to_owned(),
|
||||
BTreeMap::from([(
|
||||
KeyId::from_parts(DeviceKeyAlgorithm::Ed25519, "ABCDEFG".into()),
|
||||
Ok(Signature::from(Ed25519Signature::from_slice(&[0u8; 64]).unwrap())),
|
||||
)]),
|
||||
)])),
|
||||
extra: BTreeMap::from([("foo".to_owned(), Value::from("bar"))]),
|
||||
});
|
||||
|
||||
with_settings!({sort_maps =>true}, {
|
||||
assert_json_snapshot!(info)
|
||||
});
|
||||
|
||||
let info = RoomKeyBackupInfo::Other {
|
||||
algorithm: "caesar.cipher".to_owned(),
|
||||
auth_data: BTreeMap::from([("foo".to_owned(), Value::from("bar"))]),
|
||||
};
|
||||
|
||||
with_settings!({sort_maps =>true}, {
|
||||
assert_json_snapshot!(info);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -519,6 +519,8 @@ where
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use insta::{assert_debug_snapshot, assert_json_snapshot, with_settings};
|
||||
use ruma::{device_id, user_id};
|
||||
use serde_json::json;
|
||||
use similar_asserts::assert_eq;
|
||||
|
||||
@@ -547,4 +549,79 @@ mod test {
|
||||
|
||||
assert_eq!(json, serialized, "A serialization cycle should yield the same result");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn snapshot_backup_decryption_key() {
|
||||
let decryption_key = BackupDecryptionKey { inner: Box::new([1u8; 32]) };
|
||||
assert_json_snapshot!(decryption_key);
|
||||
|
||||
// should not log the key !
|
||||
assert_debug_snapshot!(decryption_key);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn snapshot_signatures() {
|
||||
let signatures = Signatures(BTreeMap::from([
|
||||
(
|
||||
user_id!("@alice:localhost").to_owned(),
|
||||
BTreeMap::from([
|
||||
(
|
||||
DeviceKeyId::from_parts(
|
||||
DeviceKeyAlgorithm::Ed25519,
|
||||
device_id!("ABCDEFGH"),
|
||||
),
|
||||
Ok(Signature::from(Ed25519Signature::from_slice(&[0u8; 64]).unwrap())),
|
||||
),
|
||||
(
|
||||
DeviceKeyId::from_parts(
|
||||
DeviceKeyAlgorithm::Curve25519,
|
||||
device_id!("IJKLMNOP"),
|
||||
),
|
||||
Ok(Signature::from(Ed25519Signature::from_slice(&[1u8; 64]).unwrap())),
|
||||
),
|
||||
]),
|
||||
),
|
||||
(
|
||||
user_id!("@bob:localhost").to_owned(),
|
||||
BTreeMap::from([(
|
||||
DeviceKeyId::from_parts(DeviceKeyAlgorithm::Ed25519, device_id!("ABCDEFGH")),
|
||||
Err(InvalidSignature { source: "SOME+B64+SOME+B64+SOME+B64+==".to_owned() }),
|
||||
)]),
|
||||
),
|
||||
]));
|
||||
|
||||
with_settings!({sort_maps =>true}, {
|
||||
assert_json_snapshot!(signatures)
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn snapshot_secret_bundle() {
|
||||
let secret_bundle = SecretsBundle {
|
||||
cross_signing: CrossSigningSecrets {
|
||||
master_key: "MSKMSKMSKMSKMSKMSKMSKMSKMSKMSKMSKMSK".to_owned(),
|
||||
user_signing_key: "USKUSKUSKUSKUSKUSKUSKUSKUSKUSKUSKUSK".to_owned(),
|
||||
self_signing_key: "SSKSSKSSKSSKSSKSSKSSKSSKSSKSSKSSK".to_owned(),
|
||||
},
|
||||
backup: Some(BackupSecrets::MegolmBackupV1Curve25519AesSha2(
|
||||
MegolmBackupV1Curve25519AesSha2Secrets {
|
||||
key: BackupDecryptionKey::from_bytes(&[0u8; 32]),
|
||||
backup_version: "v1.1".to_owned(),
|
||||
},
|
||||
)),
|
||||
};
|
||||
|
||||
assert_json_snapshot!(secret_bundle);
|
||||
|
||||
let secret_bundle = SecretsBundle {
|
||||
cross_signing: CrossSigningSecrets {
|
||||
master_key: "MSKMSKMSKMSKMSKMSKMSKMSKMSKMSKMSKMSK".to_owned(),
|
||||
user_signing_key: "USKUSKUSKUSKUSKUSKUSKUSKUSKUSKUSKUSK".to_owned(),
|
||||
self_signing_key: "SSKSSKSSKSSKSSKSSKSSKSSKSSKSSKSSK".to_owned(),
|
||||
},
|
||||
backup: None,
|
||||
};
|
||||
|
||||
assert_json_snapshot!(secret_bundle);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
---
|
||||
source: crates/matrix-sdk-crypto/src/types/backup.rs
|
||||
expression: info
|
||||
---
|
||||
{
|
||||
"algorithm": "caesar.cipher",
|
||||
"auth_data": {
|
||||
"foo": "bar"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
---
|
||||
source: crates/matrix-sdk-crypto/src/types/backup.rs
|
||||
expression: info
|
||||
---
|
||||
{
|
||||
"algorithm": "m.megolm_backup.v1.curve25519-aes-sha2",
|
||||
"auth_data": {
|
||||
"foo": "bar",
|
||||
"public_key": "AgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgI",
|
||||
"signatures": {
|
||||
"@alice:localhost": {
|
||||
"ed25519:ABCDEFG": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
---
|
||||
source: crates/matrix-sdk-crypto/src/types/mod.rs
|
||||
expression: decryption_key
|
||||
---
|
||||
BackupDecryptionKey(
|
||||
"...",
|
||||
)
|
||||
@@ -0,0 +1,38 @@
|
||||
---
|
||||
source: crates/matrix-sdk-crypto/src/types/mod.rs
|
||||
expression: "BackupDecryptionKey { inner: Box::new([1u8;32]) }"
|
||||
---
|
||||
[
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1
|
||||
]
|
||||
@@ -0,0 +1,12 @@
|
||||
---
|
||||
source: crates/matrix-sdk-crypto/src/types/mod.rs
|
||||
expression: secret_bundle
|
||||
---
|
||||
{
|
||||
"cross_signing": {
|
||||
"master_key": "MSKMSKMSKMSKMSKMSKMSKMSKMSKMSKMSKMSK",
|
||||
"user_signing_key": "USKUSKUSKUSKUSKUSKUSKUSKUSKUSKUSKUSK",
|
||||
"self_signing_key": "SSKSSKSSKSSKSSKSSKSSKSSKSSKSSKSSK"
|
||||
},
|
||||
"backup": null
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
---
|
||||
source: crates/matrix-sdk-crypto/src/types/mod.rs
|
||||
expression: secret_bundle
|
||||
---
|
||||
{
|
||||
"cross_signing": {
|
||||
"master_key": "MSKMSKMSKMSKMSKMSKMSKMSKMSKMSKMSKMSK",
|
||||
"user_signing_key": "USKUSKUSKUSKUSKUSKUSKUSKUSKUSKUSKUSK",
|
||||
"self_signing_key": "SSKSSKSSKSSKSSKSSKSSKSSKSSKSSKSSK"
|
||||
},
|
||||
"backup": {
|
||||
"algorithm": "m.megolm_backup.v1.curve25519-aes-sha2",
|
||||
"key": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
|
||||
"backup_version": "v1.1"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
---
|
||||
source: crates/matrix-sdk-crypto/src/types/mod.rs
|
||||
expression: signatures
|
||||
---
|
||||
{
|
||||
"@alice:localhost": {
|
||||
"curve25519:IJKLMNOP": "AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQ",
|
||||
"ed25519:ABCDEFGH": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
|
||||
},
|
||||
"@bob:localhost": {
|
||||
"ed25519:ABCDEFGH": "SOME+B64+SOME+B64+SOME+B64+=="
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user