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: add basic serialization tests for message fields #106

Merged
merged 1 commit into from
Sep 29, 2024
Merged
Changes from all 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
224 changes: 223 additions & 1 deletion crates/dwn-rs-core/src/interfaces/messages/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
pub enum KeyEncryptionAlgorithm {
#[serde(rename = "ECIES-ES256K")]
#[allow(non_camel_case_types)]
ECIES_ES256K,
EciesEs256k,
}

/// KeyEncryption represents the key encryption used for encrypting keys.
Expand Down Expand Up @@ -102,3 +102,225 @@
#[serde(rename = "keyEncryption")]
pub key_encryption: Vec<KeyEncryption>,
}

#[cfg(test)]
mod tests {
use crate::{auth::jws::SignatureEntry, descriptors::Records, Descriptor, Message};

use super::*;
use serde_json;
use ssi_jwk::JWK;

#[test]
fn test_fields_serialization() {
use serde_json::json;

let jwk = JWK::generate_ed25519().unwrap();

// Define your test cases as structs or tuples.
struct TestCase {
fields: Fields,
expected_json: String,
}

// Populate the vector with the test cases.
let tests = vec![
TestCase {
fields: Fields::EncodedWrite(EncodedWriteField {
write_fields: WriteFields {
record_id: Some("record_id".to_string()),
context_id: Some("context_id".to_string()),
authorization: None,
encryption: Some(Encryption {
algorithm: EncryptionAlgorithm::A256CTR,
initialization_vector: "initialization_vector".to_string(),
key_encryption: vec![KeyEncryption {
algorithm: KeyEncryptionAlgorithm::EciesEs256k,
root_key_id: "root_key_id".to_string(),
derivation_scheme: DerivationScheme::DataFormats,
derived_public_key: None,
encrypted_key: "encrypted_key".to_string(),
initialization_vector: "initialization_vector".to_string(),
ephemeral_public_key: jwk.clone(),
message_authentication_code: "message_authentication_code"
.to_string(),
}],
}),

Check warning on line 148 in crates/dwn-rs-core/src/interfaces/messages/fields.rs

View check run for this annotation

Codecov / codecov/patch

crates/dwn-rs-core/src/interfaces/messages/fields.rs#L147-L148

Added lines #L147 - L148 were not covered by tests
attestation: Some(JWS {
payload: Some("payload".to_string()),
signatures: Some(vec![SignatureEntry {
payload: Some("payload".to_string()),
protected: Some("protected".to_string()),
signature: Some("signature".to_string()),
..Default::default()
}]),

Check warning on line 156 in crates/dwn-rs-core/src/interfaces/messages/fields.rs

View check run for this annotation

Codecov / codecov/patch

crates/dwn-rs-core/src/interfaces/messages/fields.rs#L156

Added line #L156 was not covered by tests
..Default::default()
}),
},

Check warning on line 159 in crates/dwn-rs-core/src/interfaces/messages/fields.rs

View check run for this annotation

Codecov / codecov/patch

crates/dwn-rs-core/src/interfaces/messages/fields.rs#L158-L159

Added lines #L158 - L159 were not covered by tests
encoded_data: Some("encoded_data".to_string()),
}),

Check warning on line 161 in crates/dwn-rs-core/src/interfaces/messages/fields.rs

View check run for this annotation

Codecov / codecov/patch

crates/dwn-rs-core/src/interfaces/messages/fields.rs#L161

Added line #L161 was not covered by tests
expected_json: format!(
r#"{{
"recordId": "record_id",
"contextId": "context_id",
"encryption": {{
"algorithm": "A256CTR",
"initializationVector": "initialization_vector",
"keyEncryption": [
{{
"algorithm": "ECIES-ES256K",
"rootKeyId": "root_key_id",
"derivationScheme": "dataFormats",
"derivedPublicKey": null,
"encryptedKey": "encrypted_key",
"initializationVector": "initialization_vector",
"ephemeralPublicKey": {jwk},
"messageAuthenticationCode": "message_authentication_code"
}}
]
}},
"attestation": {{
"payload": "payload",
"signatures": [
{{
"payload": "payload",
"protected": "protected",
"signature": "signature"
}}
]
}},
"encodedData": "encoded_data"
}}"#
),
},

Check warning on line 195 in crates/dwn-rs-core/src/interfaces/messages/fields.rs

View check run for this annotation

Codecov / codecov/patch

crates/dwn-rs-core/src/interfaces/messages/fields.rs#L195

Added line #L195 was not covered by tests
TestCase {
fields: Fields::AuthorizationDelegatedGrant(AuthorizationDelegatedGrantFields {
authorization: Some(AuthorizationDelegatedGrant {
// fill in all the fields with fake details
signature: JWS::default(),
author_delegated_grant: Some(Box::new(Message {
descriptor: Descriptor::Records(Records::Write(
crate::descriptors::RecordsWriteDescriptor::default(),
)),
fields: Fields::Write(WriteFields::default()),
})),
}),

Check warning on line 207 in crates/dwn-rs-core/src/interfaces/messages/fields.rs

View check run for this annotation

Codecov / codecov/patch

crates/dwn-rs-core/src/interfaces/messages/fields.rs#L206-L207

Added lines #L206 - L207 were not covered by tests
}),
expected_json: json!({
"authorization": {
"signature": JWS::default(),
"authorDelegatedGrant": {
"descriptor": Descriptor::Records(Records::Write(
crate::descriptors::RecordsWriteDescriptor::default()
)),
// there are no Fields on this request for test serialization
}
},
}
)
.to_string(),
},
];

Check warning on line 223 in crates/dwn-rs-core/src/interfaces/messages/fields.rs

View check run for this annotation

Codecov / codecov/patch

crates/dwn-rs-core/src/interfaces/messages/fields.rs#L222-L223

Added lines #L222 - L223 were not covered by tests

for test in tests.iter() {
let json = serde_json::to_value(&test.fields).unwrap();
let expected_json: serde_json::Value =
serde_json::from_str(&test.expected_json).unwrap();
assert_eq!(json, expected_json);
}
}

#[test]
fn test_fields_deserialization() {
let jwk = JWK::generate_ed25519().unwrap();
let json = &format!(
r#"
{{
"recordId": "record_id",
"contextId": "context_id",
"encryption": {{
"algorithm": "A256CTR",
"initializationVector": "initialization_vector",
"keyEncryption": [
{{
"algorithm": "ECIES-ES256K",
"rootKeyId": "root_key_id",
"derivationScheme": "dataFormats",
"derivedPublicKey": null,
"encryptedKey": "encrypted_key",
"initializationVector": "initialization_vector",
"ephemeralPublicKey": {jwk},
"messageAuthenticationCode": "message_authentication_code"
}}
]
}},
"attestation": {{
"payload": "payload",
"signatures": [
{{
"payload": "payload",
"protected": "protected",
"signature": "signature"
}}
]
}},
"encodedData": "encoded_data"
}}
"#
);

let fields: Fields = serde_json::from_str(json).unwrap();
match fields {
Fields::EncodedWrite(EncodedWriteField {
write_fields,
encoded_data,
}) => {
assert_eq!(write_fields.record_id, Some("record_id".to_string()));
assert_eq!(write_fields.context_id, Some("context_id".to_string()));
assert!(write_fields.authorization.is_none());
assert!(write_fields.encryption.is_some());
assert!(write_fields.attestation.is_some());
assert_eq!(encoded_data, Some("encoded_data".to_string()));
}
_ => unreachable!(),

Check warning on line 285 in crates/dwn-rs-core/src/interfaces/messages/fields.rs

View check run for this annotation

Codecov / codecov/patch

crates/dwn-rs-core/src/interfaces/messages/fields.rs#L285

Added line #L285 was not covered by tests
}
}

#[test]
fn test_fields_serialization_with_null_fields() {
let fields = Fields::EncodedWrite(EncodedWriteField {
write_fields: WriteFields {
record_id: None,
context_id: None,
authorization: None,
encryption: None,
attestation: None,
},
encoded_data: None,
});

let json = serde_json::to_string(&fields).unwrap();
assert_eq!("{}", json);
}

#[test]
fn test_fields_deserialization_with_null_fields() {
let json = "{}";

let fields: Fields = serde_json::from_str(json).unwrap();
match fields {
Fields::EncodedWrite(EncodedWriteField {
write_fields,
encoded_data,
}) => {
assert!(write_fields.record_id.is_none());
assert!(write_fields.context_id.is_none());
assert!(write_fields.authorization.is_none());
assert!(write_fields.encryption.is_none());
assert!(write_fields.attestation.is_none());
assert!(encoded_data.is_none());
}
_ => unreachable!(),

Check warning on line 323 in crates/dwn-rs-core/src/interfaces/messages/fields.rs

View check run for this annotation

Codecov / codecov/patch

crates/dwn-rs-core/src/interfaces/messages/fields.rs#L323

Added line #L323 was not covered by tests
}
}
}
Loading