From e1178cc3f49caa4af14cc24d67ab63d92bb5f695 Mon Sep 17 00:00:00 2001 From: Dakota Brink <779390+codabrink@users.noreply.github.com> Date: Fri, 6 Dec 2024 10:09:41 -0500 Subject: [PATCH] Test for deserialization between versions (#1389) --- xmtp_mls/src/preferences.rs | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/xmtp_mls/src/preferences.rs b/xmtp_mls/src/preferences.rs index 5f4bb90a7..fa9c48628 100644 --- a/xmtp_mls/src/preferences.rs +++ b/xmtp_mls/src/preferences.rs @@ -5,9 +5,10 @@ use xmtp_proto::xmtp::mls::message_contents::UserPreferenceUpdate as UserPrefere use crate::storage::consent_record::StoredConsentRecord; #[derive(Serialize, Deserialize, Clone)] +#[repr(i32)] pub enum UserPreferenceUpdate { - ConsentUpdate(StoredConsentRecord), - HmacKeyUpdate { key: Vec }, + ConsentUpdate(StoredConsentRecord) = 1, + HmacKeyUpdate { key: Vec } = 2, } impl TryFrom for UserPreferenceUpdate { @@ -28,3 +29,34 @@ impl TryInto for UserPreferenceUpdate { Ok(UserPreferenceUpdateProto { content }) } } + +#[cfg(test)] +mod tests { + use crate::storage::consent_record::{ConsentState, ConsentType}; + + use super::*; + + #[derive(Serialize, Deserialize, Clone)] + #[repr(i32)] + enum OldUserPreferenceUpdate { + ConsentUpdate(StoredConsentRecord) = 1, + } + + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] + #[cfg_attr(not(target_arch = "wasm32"), tokio::test)] + async fn test_can_deserialize_between_versions() { + let consent_record = StoredConsentRecord { + entity: "hello there".to_string(), + entity_type: ConsentType::Address, + state: ConsentState::Allowed, + }; + let update = UserPreferenceUpdate::ConsentUpdate(consent_record); + + let bytes = bincode::serialize(&update).unwrap(); + + let old_update: OldUserPreferenceUpdate = bincode::deserialize(&bytes).unwrap(); + + let OldUserPreferenceUpdate::ConsentUpdate(update) = old_update; + assert_eq!(update.state, ConsentState::Allowed); + } +}