Skip to content

Commit

Permalink
Test for deserialization between versions (#1389)
Browse files Browse the repository at this point in the history
  • Loading branch information
codabrink authored Dec 6, 2024
1 parent 2d0640c commit e1178cc
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions xmtp_mls/src/preferences.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8> },
ConsentUpdate(StoredConsentRecord) = 1,
HmacKeyUpdate { key: Vec<u8> } = 2,
}

impl TryFrom<UserPreferenceUpdateProto> for UserPreferenceUpdate {
Expand All @@ -28,3 +29,34 @@ impl TryInto<UserPreferenceUpdateProto> 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);
}
}

0 comments on commit e1178cc

Please sign in to comment.