Skip to content

Commit

Permalink
test(crypto): Add some basic snapshot testing in crypto crate
Browse files Browse the repository at this point in the history
  • Loading branch information
BillCarsonFr committed Dec 20, 2024
1 parent 667a8e6 commit 4e6c060
Show file tree
Hide file tree
Showing 23 changed files with 495 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/matrix-sdk-crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
21 changes: 21 additions & 0 deletions crates/matrix-sdk-crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}
}
57 changes: 54 additions & 3 deletions crates/matrix-sdk-crypto/src/olm/group_sessions/sender_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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()),
}));
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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
]
}
}
Original file line number Diff line number Diff line change
@@ -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
]
}
}
Original file line number Diff line number Diff line change
@@ -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
]
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/matrix-sdk-crypto/src/lib.rs
expression: "TrustRequirement::CrossSignedOrLegacy"
---
"CrossSignedOrLegacy"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/matrix-sdk-crypto/src/lib.rs
expression: "TrustRequirement::CrossSigned"
---
"CrossSigned"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/matrix-sdk-crypto/src/lib.rs
expression: "TrustRequirement::Untrusted"
---
"Untrusted"
36 changes: 35 additions & 1 deletion crates/matrix-sdk-crypto/src/types/backup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
})
}
}
Loading

0 comments on commit 4e6c060

Please sign in to comment.