Debian Codegraph @

third_party/rust/mls-platform-api/tests/clear_pending_commit.rs

raw
1 // Copyright (c) 2024 Mozilla Corporation and contributors.
2 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
3
4 use mls_platform_api::MessageOrAck;
5 use mls_platform_api::PlatformError;
6
7 //
8 // Scenario
9 //
10 // * Alice, Bob and Charlie create signing identity (generate_signature_keypair)
11 // * Alice, Bob and Charlie create credentials (generate_credential_basic)
12 // * Bob and Charlie create key packages (generate_key_package)
13 // * Alice creates a group (group_create)
14 // * Alice adds Bob to the group (group_add)
15 // - Alice has a pending commit and cannot do another operation
16 // * Alice clears the pending commit
17 // * Alice can add Charlie to the group
18
19 #[test]
20 fn test_clear_pending_commit() -> Result<(), PlatformError> {
21 // Default group configuration
22 let group_config = mls_platform_api::GroupConfig::default();
23
24 // Storage states
25 let mut state_global = mls_platform_api::state_access("global.db", &[0u8; 32])?;
26
27 // Credentials
28 let alice_cred = mls_platform_api::mls_generate_credential_basic("alice".as_bytes())?;
29 let bob_cred = mls_platform_api::mls_generate_credential_basic("bob".as_bytes())?;
30 let charlie_cred = mls_platform_api::mls_generate_credential_basic("charlie".as_bytes())?;
31
32 println!("\nAlice credential: {}", hex::encode(&alice_cred));
33 println!("Bob credential: {}", hex::encode(&bob_cred));
34 println!("Charlie credential: {}", hex::encode(&charlie_cred));
35
36 // Create signature keypairs and store them in the state
37 let alice_id =
38 mls_platform_api::mls_generate_identity(&state_global, group_config.ciphersuite)?;
39
40 let bob_id = mls_platform_api::mls_generate_identity(&state_global, group_config.ciphersuite)?;
41 let charlie_id =
42 mls_platform_api::mls_generate_identity(&state_global, group_config.ciphersuite)?;
43
44 println!("\nAlice identifier: {}", hex::encode(&alice_id));
45 println!("Bob identifier: {}", hex::encode(&bob_id));
46 println!("Charlie identifier: {}", hex::encode(&charlie_id));
47
48 // Create Key Package for Bob
49 let bob_kp = mls_platform_api::mls_generate_key_package(
50 &state_global,
51 &bob_id,
52 &bob_cred,
53 &Default::default(),
54 )?;
55
56 // Create Key Package for Charlie
57 let charlie_kp = mls_platform_api::mls_generate_key_package(
58 &state_global,
59 &charlie_id,
60 &charlie_cred,
61 &Default::default(),
62 )?;
63
64 // Create a group with Alice
65 let gide = mls_platform_api::mls_group_create(
66 &mut state_global,
67 &alice_id,
68 &alice_cred,
69 None,
70 None,
71 &Default::default(),
72 )?;
73
74 println!("\nGroup created by Alice: {}", hex::encode(&gide.group_id));
75
76 // List the members of the group
77 let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &alice_id)?;
78 println!("Members (alice, before adding bob): {members:?}");
79
80 //
81 // Alice adds Bob to a group
82 //
83 println!("\nAlice adds Bob to the Group");
84 let _commit_output = mls_platform_api::mls_group_add(
85 &mut state_global,
86 &gide.group_id,
87 &alice_id,
88 vec![bob_kp],
89 )?;
90
91 // Check if there's a pending commit
92 let pending =
93 mls_platform_api::mls_has_pending_commit(&state_global, &gide.group_id, &alice_id)?;
94 assert!(pending);
95
96 // Try to add Charlie while there's a pending commit - should fail
97 println!("\nAlice tries to add Charlie while there's a pending commit");
98 let result = mls_platform_api::mls_group_add(
99 &mut state_global,
100 &gide.group_id,
101 &alice_id,
102 vec![charlie_kp.clone()],
103 );
104
105 // Verify we get an error
106 assert!(result.is_err());
107 println!("Got expected error when trying to add with pending commit: {result:?}");
108
109 // Discard the pending commit
110 println!("\nAlice discards the pending commit");
111 mls_platform_api::mls_clear_pending_commit(&mut state_global, &gide.group_id, &alice_id)?;
112
113 // Check if there's a pending commit again
114 let pending =
115 mls_platform_api::mls_has_pending_commit(&state_global, &gide.group_id, &alice_id)?;
116 assert!(!pending);
117
118 // Alice can now add Charlie to the group
119 println!("\nAlice adds Charlie to the Group");
120 let commit_output = mls_platform_api::mls_group_add(
121 &mut state_global,
122 &gide.group_id,
123 &alice_id,
124 vec![charlie_kp.clone()],
125 )?;
126
127 // Alice process her own commit
128 println!("\nAlice process her commit to add Bob to the Group");
129 mls_platform_api::mls_receive(
130 &state_global,
131 &alice_id,
132 &MessageOrAck::MlsMessage(commit_output.commit),
133 )?;
134
135 // List the members of the group
136 let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &alice_id)?;
137 println!("Members (alice, after adding charlie): {members:?}");
138
139 Ok(())
140 }