Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: test serde stability in types #295

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Cargo.lock

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

12 changes: 12 additions & 0 deletions REUSE.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,15 @@ path = "app/.fvmrc"
precedence = "aggregate"
SPDX-FileCopyrightText = "2024 Phoenix R&D GmbH <[email protected]>"
SPDX-License-Identifier = "AGPL-3.0-or-later"

[[annotations]]
path = "**/snapshots/*.snap"
precedence = "aggregate"
SPDX-FileCopyrightText = "2025 Phoenix R&D GmbH <[email protected]>"
SPDX-License-Identifier = "AGPL-3.0-or-later"

[[annotations]]
path = "**/snapshots/*.snap.cbor"
precedence = "aggregate"
SPDX-FileCopyrightText = "2025 Phoenix R&D GmbH <[email protected]>"
SPDX-License-Identifier = "AGPL-3.0-or-later"
1 change: 1 addition & 0 deletions types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ privacypass = { workspace = true }
sqlx = { workspace = true, optional = true }

[dev-dependencies]
insta = { version = "1.42.0", features = ["json"] }
serde_json = "1.0"

[features]
Expand Down
71 changes: 71 additions & 0 deletions types/src/credentials/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,3 +695,74 @@ pub mod persistence {
}
}
}

#[cfg(test)]
mod tests {
use std::sync::LazyLock;

use uuid::Uuid;

use crate::codec::PhnxCodec;
use crate::crypto::signatures::private_keys::VerifyingKey;

use super::*;

static CLIENT_CREDENTIAL_CSR: LazyLock<ClientCredentialCsr> = LazyLock::new(|| {
let client_id =
AsClientId::with_uuid("alice@localhost".parse().unwrap(), Uuid::from_u128(42));
ClientCredentialCsr {
version: MlsInfraVersion::Alpha,
client_id,
signature_scheme: SignatureScheme::ED25519,
verifying_key: ClientVerifyingKey(VerifyingKey::for_testing(vec![1, 2, 3])),
}
});

static CLIENT_CREDENTIAL_PAYLOAD: LazyLock<ClientCredentialPayload> =
LazyLock::new(|| ClientCredentialPayload {
csr: CLIENT_CREDENTIAL_CSR.clone(),
expiration_data: ExpirationData::new_from(
"1985-11-16T00:00:00Z".parse().unwrap(),
DEFAULT_CLIENT_CREDENTIAL_LIFETIME,
),
signer_fingerprint: CredentialFingerprint(vec![1, 2, 3]),
});

static CLIENT_CREDENTIAL: LazyLock<ClientCredential> = LazyLock::new(|| ClientCredential {
payload: CLIENT_CREDENTIAL_PAYLOAD.clone(),
signature: Signature::for_testing(vec![1, 2, 3]),
});

#[test]
fn client_credential_csr_serde_stability_json() {
insta::assert_json_snapshot!(&*CLIENT_CREDENTIAL_CSR);
}

#[test]
fn client_credential_csr_serde_stability_cbor() {
let bytes = PhnxCodec::to_vec(&*CLIENT_CREDENTIAL_CSR).unwrap();
insta::assert_binary_snapshot!(".cbor", bytes);
}

#[test]
fn client_credential_payload_serde_stability_json() {
insta::assert_json_snapshot!(&*CLIENT_CREDENTIAL_PAYLOAD);
}

#[test]
fn client_credential_payload_cbor_stability_cbor() {
let bytes = PhnxCodec::to_vec(&*CLIENT_CREDENTIAL_PAYLOAD).unwrap();
insta::assert_binary_snapshot!(".cbor", bytes);
}

#[test]
fn client_credential_serde_stability_json() {
insta::assert_json_snapshot!(&*CLIENT_CREDENTIAL);
}

#[test]
fn client_credential_serde_stability_cbor() {
let bytes = PhnxCodec::to_vec(&*CLIENT_CREDENTIAL).unwrap();
insta::assert_binary_snapshot!(".cbor", bytes);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: types/src/credentials/mod.rs
expression: bytes
extension: cbor
snapshot_kind: binary
---
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
source: types/src/credentials/mod.rs
expression: "&*CLIENT_CREDENTIAL_CSR"
snapshot_kind: text
---
{
"version": "Alpha",
"client_id": {
"user_name": {
"user_name": "alice",
"domain": {
"domain": {
"Domain": "localhost"
}
}
},
"client_id": "00000000-0000-0000-0000-00000000002a"
},
"signature_scheme": "ED25519",
"verifying_key": [
1,
2,
3
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: types/src/credentials/mod.rs
expression: bytes
extension: cbor
snapshot_kind: binary
---
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
source: types/src/credentials/mod.rs
expression: "&*CLIENT_CREDENTIAL_PAYLOAD"
snapshot_kind: text
---
{
"csr": {
"version": "Alpha",
"client_id": {
"user_name": {
"user_name": "alice",
"domain": {
"domain": {
"Domain": "localhost"
}
}
},
"client_id": "00000000-0000-0000-0000-00000000002a"
},
"signature_scheme": "ED25519",
"verifying_key": [
1,
2,
3
]
},
"expiration_data": {
"not_before": "1985-11-15T23:45:00Z",
"not_after": "1986-02-13T23:45:00Z"
},
"signer_fingerprint": [
1,
2,
3
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: types/src/credentials/mod.rs
expression: bytes
extension: cbor
snapshot_kind: binary
---
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
source: types/src/credentials/mod.rs
expression: "&*CLIENT_CREDENTIAL"
snapshot_kind: text
---
{
"payload": {
"csr": {
"version": "Alpha",
"client_id": {
"user_name": {
"user_name": "alice",
"domain": {
"domain": {
"Domain": "localhost"
}
}
},
"client_id": "00000000-0000-0000-0000-00000000002a"
},
"signature_scheme": "ED25519",
"verifying_key": [
1,
2,
3
]
},
"expiration_data": {
"not_before": "1985-11-15T23:45:00Z",
"not_after": "1986-02-13T23:45:00Z"
},
"signer_fingerprint": [
1,
2,
3
]
},
"signature": [
1,
2,
3
]
}
6 changes: 6 additions & 0 deletions types/src/crypto/kdf/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//! trait.

use mls_assist::openmls::prelude::GroupEpoch;
use rand::{CryptoRng, RngCore};
use serde::{Deserialize, Serialize};
use tls_codec::{TlsDeserializeBytes, TlsSerialize, TlsSize};

Expand Down Expand Up @@ -121,6 +122,11 @@ impl RatchetSecret {
let key = Secret::random()?;
Ok(Self { key })
}

pub fn random_with_rng(rng: &mut (impl RngCore + CryptoRng)) -> Result<Self, RandomnessError> {
let key = Secret::random_with_rng(rng)?;
Ok(Self { key })
}
}

impl AsRef<Secret<KDF_KEY_SIZE>> for RatchetSecret {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: types/src/crypto/ratchet/tests.rs
expression: bytes
extension: cbor
snapshot_kind: binary
---
Binary file not shown.
Loading
Loading