diff --git a/crates/dwn-rs-core/src/interfaces/messages/fields.rs b/crates/dwn-rs-core/src/interfaces/messages/fields.rs index ebd8418..75b0ed7 100644 --- a/crates/dwn-rs-core/src/interfaces/messages/fields.rs +++ b/crates/dwn-rs-core/src/interfaces/messages/fields.rs @@ -70,7 +70,7 @@ pub enum DerivationScheme { pub enum KeyEncryptionAlgorithm { #[serde(rename = "ECIES-ES256K")] #[allow(non_camel_case_types)] - ECIES_ES256K, + EciesEs256k, } /// KeyEncryption represents the key encryption used for encrypting keys. @@ -102,3 +102,225 @@ pub struct Encryption { #[serde(rename = "keyEncryption")] pub key_encryption: Vec, } + +#[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(), + }], + }), + 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() + }]), + ..Default::default() + }), + }, + encoded_data: Some("encoded_data".to_string()), + }), + 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" + }}"# + ), + }, + 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()), + })), + }), + }), + 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(), + }, + ]; + + 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!(), + } + } + + #[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!(), + } + } +}