From 86d25fe6810e4eade30e07aaceb82da2b9b4b92d Mon Sep 17 00:00:00 2001 From: beltram Date: Mon, 27 Nov 2023 12:12:45 +0100 Subject: [PATCH] wip --- memory_keystore/Cargo.toml | 2 +- openmls/Cargo.toml | 2 +- openmls/tests/book_code.rs | 251 +++++++++---------------------------- 3 files changed, 64 insertions(+), 191 deletions(-) diff --git a/memory_keystore/Cargo.toml b/memory_keystore/Cargo.toml index 9a62cbdbab..f1a701669e 100644 --- a/memory_keystore/Cargo.toml +++ b/memory_keystore/Cargo.toml @@ -14,4 +14,4 @@ openmls_traits = { version = "0.2.0", path = "../traits" } thiserror = "1.0" serde_json = "1.0" async-trait = { workspace = true } -async-lock = "2.7" +async-lock = "3.1" diff --git a/openmls/Cargo.toml b/openmls/Cargo.toml index 6e0d6a0c7b..9e4deb5537 100644 --- a/openmls/Cargo.toml +++ b/openmls/Cargo.toml @@ -32,7 +32,7 @@ getrandom = { version = "0.2", optional = true, features = ["js"] } serde_json = { version = "1.0", optional = true } # Crypto backends required for KAT and testing - "test-utils" feature openmls_rust_crypto = { version = "0.2.0", path = "../openmls_rust_crypto", optional = true } -async-lock = { version = "2.7", optional = true } +async-lock = { version = "3.1", optional = true } rstest = { version = "0.18.2", optional = true } rstest_reuse = { version = "0.6.0", optional = true } tokio = { version = "1.24", optional = true, features = ["macros", "rt", "rt-multi-thread"] } diff --git a/openmls/tests/book_code.rs b/openmls/tests/book_code.rs index fc06843e3a..9a74de77e2 100644 --- a/openmls/tests/book_code.rs +++ b/openmls/tests/book_code.rs @@ -30,11 +30,8 @@ async fn generate_credential( let credential = Credential::new_basic(identity); // ANCHOR_END: create_basic_credential // ANCHOR: create_credential_keys - let signature_keys = SignatureKeyPair::new( - signature_algorithm, - &mut *backend.rand().borrow_rand().unwrap(), - ) - .unwrap(); + let signature_keys = + SignatureKeyPair::new(signature_algorithm, &mut *backend.rand().borrow_rand().unwrap()).unwrap(); signature_keys.store(backend.key_store()).await.unwrap(); // ANCHOR_END: create_credential_keys @@ -115,12 +112,8 @@ async fn book_operations() { // Define the MlsGroup configuration // delivery service credentials - let (ds_credential_with_key, ds_signature_keys) = generate_credential( - "delivery-service".into(), - ciphersuite.signature_algorithm(), - backend, - ) - .await; + let (ds_credential_with_key, ds_signature_keys) = + generate_credential("delivery-service".into(), ciphersuite.signature_algorithm(), backend).await; // ANCHOR: mls_group_config_example let mls_group_config = MlsGroupConfig::builder() @@ -185,10 +178,7 @@ async fn book_operations() { // Check that we received the correct proposals if let Some(staged_commit) = alice_group.pending_commit() { - let add = staged_commit - .add_proposals() - .next() - .expect("Expected a proposal."); + let add = staged_commit.add_proposals().next().expect("Expected a proposal."); // Check that Bob was added assert_eq!( add.add_proposal().key_package().leaf_node().credential(), @@ -272,13 +262,10 @@ async fn book_operations() { // Message serialization - let bytes = mls_message_out - .to_bytes() - .expect("Could not serialize message."); + let bytes = mls_message_out.to_bytes().expect("Could not serialize message."); // ANCHOR: mls_message_in_from_bytes - let mls_message = - MlsMessageIn::tls_deserialize_exact(bytes).expect("Could not deserialize message."); + let mls_message = MlsMessageIn::tls_deserialize_exact(bytes).expect("Could not deserialize message."); // ANCHOR_END: mls_message_in_from_bytes // ANCHOR: process_message @@ -291,9 +278,7 @@ async fn book_operations() { // Check that we received the correct message // ANCHOR: inspect_application_message - if let ProcessedMessageContent::ApplicationMessage(application_message) = - processed_message.into_content() - { + if let ProcessedMessageContent::ApplicationMessage(application_message) = processed_message.into_content() { // Check the message assert_eq!(application_message.into_bytes(), b"Hi, I'm Alice!"); } @@ -321,9 +306,7 @@ async fn book_operations() { .expect("Could not process message."); // Check that we received the correct message - if let ProcessedMessageContent::StagedCommitMessage(staged_commit) = - alice_processed_message.into_content() - { + if let ProcessedMessageContent::StagedCommitMessage(staged_commit) = alice_processed_message.into_content() { // Merge staged Commit alice_group .merge_staged_commit(backend, *staged_commit) @@ -348,10 +331,7 @@ async fn book_operations() { ); // Make sure that both groups have the same public tree - assert_eq!( - alice_group.export_ratchet_tree(), - bob_group.export_ratchet_tree() - ); + assert_eq!(alice_group.export_ratchet_tree(), bob_group.export_ratchet_tree()); // === Alice updates and commits === // ANCHOR: propose_self_update @@ -372,15 +352,10 @@ async fn book_operations() { .expect("Could not process message."); // Check that we received the correct proposals - if let ProcessedMessageContent::ProposalMessage(staged_proposal) = - bob_processed_message.into_content() - { + if let ProcessedMessageContent::ProposalMessage(staged_proposal) = bob_processed_message.into_content() { if let Proposal::Update(ref update_proposal) = staged_proposal.proposal() { // Check that Alice updated - assert_eq!( - update_proposal.leaf_node().credential(), - &alice_credential.credential - ); + assert_eq!(update_proposal.leaf_node().credential(), &alice_credential.credential); // Store proposal alice_group.store_pending_proposal(*staged_proposal.clone()); } else { @@ -418,9 +393,7 @@ async fn book_operations() { .expect("Could not process message."); // Check that we received the correct message - if let ProcessedMessageContent::StagedCommitMessage(staged_commit) = - bob_processed_message.into_content() - { + if let ProcessedMessageContent::StagedCommitMessage(staged_commit) = bob_processed_message.into_content() { bob_group .merge_staged_commit(backend, *staged_commit) .await @@ -441,10 +414,7 @@ async fn book_operations() { ); // Make sure that both groups have the same public tree - assert_eq!( - alice_group.export_ratchet_tree(), - bob_group.export_ratchet_tree() - ); + assert_eq!(alice_group.export_ratchet_tree(), bob_group.export_ratchet_tree()); // === Bob adds Charlie === let charlie_key_package = generate_key_package( @@ -457,20 +427,14 @@ async fn book_operations() { .await; let (queued_message, welcome, _group_info) = bob_group - .add_members( - backend, - &bob_signature_keys, - vec![charlie_key_package.into()], - ) + .add_members(backend, &bob_signature_keys, vec![charlie_key_package.into()]) .await .unwrap(); let alice_processed_message = alice_group .process_message( backend, - queued_message - .into_protocol_message() - .expect("Unexpected message type"), + queued_message.into_protocol_message().expect("Unexpected message type"), ) .await .expect("Could not process message."); @@ -480,9 +444,7 @@ async fn book_operations() { .expect("error merging pending commit"); // Merge Commit - if let ProcessedMessageContent::StagedCommitMessage(staged_commit) = - alice_processed_message.into_content() - { + if let ProcessedMessageContent::StagedCommitMessage(staged_commit) = alice_processed_message.into_content() { alice_group .merge_staged_commit(backend, *staged_commit) .await @@ -501,14 +463,8 @@ async fn book_operations() { .expect("Error creating group from Welcome"); // Make sure that all groups have the same public tree - assert_eq!( - alice_group.export_ratchet_tree(), - bob_group.export_ratchet_tree(), - ); - assert_eq!( - alice_group.export_ratchet_tree(), - charlie_group.export_ratchet_tree() - ); + assert_eq!(alice_group.export_ratchet_tree(), bob_group.export_ratchet_tree(),); + assert_eq!(alice_group.export_ratchet_tree(), charlie_group.export_ratchet_tree()); // Check that Alice, Bob & Charlie are the members of the group let members = alice_group.members().collect::>(); @@ -536,9 +492,7 @@ async fn book_operations() { let _bob_processed_message = bob_group .process_message( backend, - queued_message - .into_protocol_message() - .expect("Unexpected message type"), + queued_message.into_protocol_message().expect("Unexpected message type"), ) .await .expect("Could not process message."); @@ -562,9 +516,7 @@ async fn book_operations() { let bob_processed_message = bob_group .process_message( backend, - queued_message - .into_protocol_message() - .expect("Unexpected message type"), + queued_message.into_protocol_message().expect("Unexpected message type"), ) .await .expect("Could not process message."); @@ -574,9 +526,7 @@ async fn book_operations() { .expect("error merging pending commit"); // Merge Commit - if let ProcessedMessageContent::StagedCommitMessage(staged_commit) = - alice_processed_message.into_content() - { + if let ProcessedMessageContent::StagedCommitMessage(staged_commit) = alice_processed_message.into_content() { alice_group .merge_staged_commit(backend, *staged_commit) .await @@ -586,9 +536,7 @@ async fn book_operations() { } // Merge Commit - if let ProcessedMessageContent::StagedCommitMessage(staged_commit) = - bob_processed_message.into_content() - { + if let ProcessedMessageContent::StagedCommitMessage(staged_commit) = bob_processed_message.into_content() { bob_group .merge_staged_commit(backend, *staged_commit) .await @@ -611,14 +559,8 @@ async fn book_operations() { ); // Make sure that all groups have the same public tree - assert_eq!( - alice_group.export_ratchet_tree(), - bob_group.export_ratchet_tree(), - ); - assert_eq!( - alice_group.export_ratchet_tree(), - charlie_group.export_ratchet_tree() - ); + assert_eq!(alice_group.export_ratchet_tree(), bob_group.export_ratchet_tree(),); + assert_eq!(alice_group.export_ratchet_tree(), charlie_group.export_ratchet_tree()); // ANCHOR: retrieve_members let charlie_members = charlie_group.members().collect::>(); @@ -628,9 +570,7 @@ async fn book_operations() { .iter() .find( |Member { - index: _, - credential, - .. + index: _, credential, .. }| credential.identity() == b"Bob", ) .expect("Couldn't find Bob in the list of group members."); @@ -638,9 +578,7 @@ async fn book_operations() { // Make sure that this is Bob's actual KP reference. assert_eq!( bob_member.credential.identity(), - bob_group - .own_identity() - .expect("An unexpected error occurred.") + bob_group.own_identity().expect("An unexpected error occurred.") ); // === Charlie removes Bob === @@ -696,19 +634,11 @@ async fn book_operations() { // Check that we receive the correct proposal for Alice // ANCHOR: inspect_staged_commit - if let ProcessedMessageContent::StagedCommitMessage(staged_commit) = - alice_processed_message.into_content() - { + if let ProcessedMessageContent::StagedCommitMessage(staged_commit) = alice_processed_message.into_content() { // We expect a remove proposal - let remove = staged_commit - .remove_proposals() - .next() - .expect("Expected a proposal."); + let remove = staged_commit.remove_proposals().next().expect("Expected a proposal."); // Check that Bob was removed - assert_eq!( - remove.remove_proposal().removed(), - bob_group.own_leaf_index() - ); + assert_eq!(remove.remove_proposal().removed(), bob_group.own_leaf_index()); // Check that Charlie removed Bob assert!(matches!( remove.sender(), @@ -728,17 +658,15 @@ async fn book_operations() { // Check that we receive the correct proposal for Bob // ANCHOR: remove_operation // ANCHOR: getting_removed - if let ProcessedMessageContent::StagedCommitMessage(staged_commit) = - bob_processed_message.into_content() - { + if let ProcessedMessageContent::StagedCommitMessage(staged_commit) = bob_processed_message.into_content() { let remove_proposal = staged_commit .remove_proposals() .next() .expect("An unexpected error occurred."); // We construct a RemoveOperation enum to help us interpret the remove operation - let remove_operation = RemoveOperation::new(remove_proposal, &bob_group) - .expect("An unexpected Error occurred."); + let remove_operation = + RemoveOperation::new(remove_proposal, &bob_group).expect("An unexpected Error occurred."); match remove_operation { RemoveOperation::WeLeft => unreachable!(), @@ -773,10 +701,7 @@ async fn book_operations() { // ANCHOR_END: getting_removed // Make sure that all groups have the same public tree - assert_eq!( - alice_group.export_ratchet_tree(), - charlie_group.export_ratchet_tree() - ); + assert_eq!(alice_group.export_ratchet_tree(), charlie_group.export_ratchet_tree()); // Make sure the group only contains two members assert_eq!(alice_group.members().count(), 2); @@ -806,11 +731,7 @@ async fn book_operations() { // Create RemoveProposal and process it // ANCHOR: propose_remove let (mls_message_out, _proposal_ref) = alice_group - .propose_remove_member( - backend, - &alice_signature_keys, - charlie_group.own_leaf_index(), - ) + .propose_remove_member(backend, &alice_signature_keys, charlie_group.own_leaf_index()) .expect("Could not create proposal to remove Charlie."); // ANCHOR_END: propose_remove @@ -825,9 +746,7 @@ async fn book_operations() { .expect("Could not process message."); // Check that we received the correct proposals - if let ProcessedMessageContent::ProposalMessage(staged_proposal) = - charlie_processed_message.into_content() - { + if let ProcessedMessageContent::ProposalMessage(staged_proposal) = charlie_processed_message.into_content() { if let Proposal::Remove(ref remove_proposal) = staged_proposal.proposal() { // Check that Charlie was removed assert_eq!(remove_proposal.removed(), charlie_group.own_leaf_index()); @@ -849,11 +768,7 @@ async fn book_operations() { // Create AddProposal and remove it // ANCHOR: rollback_proposal_by_ref let (_mls_message_out, proposal_ref) = alice_group - .propose_add_member( - backend, - &alice_signature_keys, - bob_key_package.clone().into(), - ) + .propose_add_member(backend, &alice_signature_keys, bob_key_package.clone().into()) .expect("Could not create proposal to add Bob"); alice_group .remove_pending_proposal(backend.key_store(), &proposal_ref) @@ -864,11 +779,7 @@ async fn book_operations() { // Create AddProposal and process it // ANCHOR: propose_add let (mls_message_out, _proposal_ref) = alice_group - .propose_add_member( - backend, - &alice_signature_keys, - bob_key_package.clone().into(), - ) + .propose_add_member(backend, &alice_signature_keys, bob_key_package.clone().into()) .expect("Could not create proposal to add Bob"); // ANCHOR_END: propose_add @@ -884,9 +795,7 @@ async fn book_operations() { // Check that we received the correct proposals // ANCHOR: inspect_add_proposal - if let ProcessedMessageContent::ProposalMessage(staged_proposal) = - charlie_processed_message.into_content() - { + if let ProcessedMessageContent::ProposalMessage(staged_proposal) = charlie_processed_message.into_content() { // In the case we received an Add Proposal if let Proposal::Add(add_proposal) = staged_proposal.proposal() { // Check that Bob was added @@ -920,9 +829,7 @@ async fn book_operations() { let charlie_processed_message = charlie_group .process_message( backend, - queued_message - .into_protocol_message() - .expect("Unexpected message type"), + queued_message.into_protocol_message().expect("Unexpected message type"), ) .await .expect("Could not process message."); @@ -934,9 +841,7 @@ async fn book_operations() { .expect("error merging pending commit"); // Merge Commit - if let ProcessedMessageContent::StagedCommitMessage(staged_commit) = - charlie_processed_message.into_content() - { + if let ProcessedMessageContent::StagedCommitMessage(staged_commit) = charlie_processed_message.into_content() { charlie_group .merge_staged_commit(backend, *staged_commit) .await @@ -991,9 +896,7 @@ async fn book_operations() { let bob_processed_message = bob_group .process_message( backend, - queued_message - .into_protocol_message() - .expect("Unexpected message type"), + queued_message.into_protocol_message().expect("Unexpected message type"), ) .await .expect("Could not process message."); @@ -1004,21 +907,18 @@ async fn book_operations() { // As provided by looking up the sender manually via the `member()` function // ANCHOR: member_lookup - let sender_cred_from_group = - if let Sender::Member(sender_index) = bob_processed_message.sender() { - bob_group - .member(*sender_index) - .expect("Could not find sender in group.") - .clone() - } else { - unreachable!("Expected sender type to be `Member`.") - }; + let sender_cred_from_group = if let Sender::Member(sender_index) = bob_processed_message.sender() { + bob_group + .member(*sender_index) + .expect("Could not find sender in group.") + .clone() + } else { + unreachable!("Expected sender type to be `Member`.") + }; // ANCHOR_END: member_lookup // Check that we received the correct message - if let ProcessedMessageContent::ApplicationMessage(application_message) = - bob_processed_message.into_content() - { + if let ProcessedMessageContent::ApplicationMessage(application_message) = bob_processed_message.into_content() { // Check the message assert_eq!(application_message.into_bytes(), message_alice); // Check that Alice sent the message @@ -1042,17 +942,13 @@ async fn book_operations() { let alice_processed_message = alice_group .process_message( backend, - queued_message - .into_protocol_message() - .expect("Unexpected message type"), + queued_message.into_protocol_message().expect("Unexpected message type"), ) .await .expect("Could not process message."); // Store proposal - if let ProcessedMessageContent::ProposalMessage(staged_proposal) = - alice_processed_message.into_content() - { + if let ProcessedMessageContent::ProposalMessage(staged_proposal) = alice_processed_message.into_content() { // Store proposal alice_group.store_pending_proposal(*staged_proposal); } else { @@ -1079,15 +975,9 @@ async fn book_operations() { // Check that we received the correct proposals if let Some(staged_commit) = alice_group.pending_commit() { - let remove = staged_commit - .remove_proposals() - .next() - .expect("Expected a proposal."); + let remove = staged_commit.remove_proposals().next().expect("Expected a proposal."); // Check that Bob was removed - assert_eq!( - remove.remove_proposal().removed(), - bob_group.own_leaf_index() - ); + assert_eq!(remove.remove_proposal().removed(), bob_group.own_leaf_index()); // Check that Bob removed himself assert!(matches!( remove.sender(), @@ -1106,26 +996,16 @@ async fn book_operations() { let bob_processed_message = bob_group .process_message( backend, - queued_message - .into_protocol_message() - .expect("Unexpected message type"), + queued_message.into_protocol_message().expect("Unexpected message type"), ) .await .expect("Could not process message."); // Check that we received the correct proposals - if let ProcessedMessageContent::StagedCommitMessage(staged_commit) = - bob_processed_message.into_content() - { - let remove = staged_commit - .remove_proposals() - .next() - .expect("Expected a proposal."); + if let ProcessedMessageContent::StagedCommitMessage(staged_commit) = bob_processed_message.into_content() { + let remove = staged_commit.remove_proposals().next().expect("Expected a proposal."); // Check that Bob was removed - assert_eq!( - remove.remove_proposal().removed(), - bob_group.own_leaf_index() - ); + assert_eq!(remove.remove_proposal().removed(), bob_group.own_leaf_index()); // Check that Bob removed himself assert!(matches!( remove.sender(), @@ -1177,9 +1057,7 @@ async fn book_operations() { let alice_processed_message = alice_group .process_message( backend, - proposal - .into_protocol_message() - .expect("Unexpected message type."), + proposal.into_protocol_message().expect("Unexpected message type."), ) .await .expect("Could not process message."); @@ -1200,10 +1078,7 @@ async fn book_operations() { let bob_group = MlsGroup::new_from_welcome( backend, &mls_group_config, - welcome - .unwrap() - .into_welcome() - .expect("Unexpected message type."), + welcome.unwrap().into_welcome().expect("Unexpected message type."), None, ) .await @@ -1241,9 +1116,7 @@ async fn book_operations() { let alice_processed_message = alice_group .process_message( backend, - proposal - .into_protocol_message() - .expect("Unexpected message type."), + proposal.into_protocol_message().expect("Unexpected message type."), ) .await .expect("Could not process message.");