Debian Codegraph @

third_party/rust/mls-platform-api/tests/group_close.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 use mls_platform_api::Received;
7
8 //
9 // Scenario
10 //
11 // * Alice, Bob, Charlie create signing identity (generate_signature_keypair)
12 // * Alice, Bob, Charlie create credentials (generate_credential_basic)
13 // * Bob and Charlie create key packages (generate_key_package)
14 // * Alice creates a group (group_create)
15 // * Alice adds Bob to the group (group_add)
16 // - Alice receives her add commit (receive for commit)
17 // - Bob joins the group (group_join)
18 // * Bob adds Charlie to the group
19 // - Bob receives the add commit
20 // - Alice receives the add commit
21 // - Charlie joins the group
22 // * Charlie decides that it's enough and closes the group (group_close)
23 // - Alice processes the close commit
24 // - Bob processes the close commit
25 // - Charlie processes the close commit
26 // * Charlie removes her state for the group (state_delete_group)
27
28 #[test]
29 fn test_group_close() -> Result<(), PlatformError> {
30 // Default group configuration
31 let group_config = mls_platform_api::GroupConfig::default();
32
33 // Storage states
34 let mut state_global = mls_platform_api::state_access("global.db", &[0u8; 32])?;
35
36 // Credentials
37 let alice_cred = mls_platform_api::mls_generate_credential_basic("alice".as_bytes())?;
38 let bob_cred = mls_platform_api::mls_generate_credential_basic("bob".as_bytes())?;
39 let charlie_cred = mls_platform_api::mls_generate_credential_basic("charlie".as_bytes())?;
40
41 println!("\nAlice credential: {}", hex::encode(&alice_cred));
42 println!("Bob credential: {}", hex::encode(&bob_cred));
43 println!("Charlie credential: {}", hex::encode(&charlie_cred));
44
45 // Create signature keypairs and store them in the state
46 let alice_id =
47 mls_platform_api::mls_generate_identity(&state_global, group_config.ciphersuite)?;
48
49 let bob_id = mls_platform_api::mls_generate_identity(&state_global, group_config.ciphersuite)?;
50
51 let charlie_id =
52 mls_platform_api::mls_generate_identity(&state_global, group_config.ciphersuite)?;
53
54 println!("\nAlice identifier: {}", hex::encode(&alice_id));
55 println!("Bob identifier: {}", hex::encode(&bob_id));
56 println!("Charlie identifier: {}", hex::encode(&charlie_id));
57
58 // Create Key Package for Bob
59 let bob_kp = mls_platform_api::mls_generate_key_package(
60 &state_global,
61 &bob_id,
62 &bob_cred,
63 &Default::default(),
64 )?;
65
66 // Create Key Package for Charlie
67 let charlie_kp = mls_platform_api::mls_generate_key_package(
68 &state_global,
69 &charlie_id,
70 &charlie_cred,
71 &Default::default(),
72 )?;
73
74 // Create a group with Alice
75 let gide = mls_platform_api::mls_group_create(
76 &mut state_global,
77 &alice_id,
78 &alice_cred,
79 None,
80 None,
81 &Default::default(),
82 )?;
83
84 println!("\nGroup created by Alice: {}", hex::encode(&gide.group_id));
85
86 // List the members of the group
87 let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &alice_id)?;
88 println!("Members (alice, before adding bob): {members:?}");
89
90 //
91 // Alice adds Bob to a group
92 //
93 println!("\nAlice adds Bob to the Group");
94 let commit_output = mls_platform_api::mls_group_add(
95 &mut state_global,
96 &gide.group_id,
97 &alice_id,
98 vec![bob_kp],
99 )?;
100
101 let welcome = commit_output
102 .welcome
103 .first()
104 .expect("No welcome messages found")
105 .clone();
106
107 // Alice process her own commit
108 println!("\nAlice process her commit to add Bob to the Group");
109 mls_platform_api::mls_receive(
110 &state_global,
111 &alice_id,
112 &MessageOrAck::MlsMessage(commit_output.commit),
113 )?;
114
115 // List the members of the group
116 let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &alice_id)?;
117 println!("Members (alice, after adding bob): {members:?}");
118
119 // Bob joins
120 println!("\nBob joins the group created by Alice");
121 mls_platform_api::mls_group_join(&state_global, &bob_id, &welcome, None)?;
122
123 // List the members of the group
124 let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &bob_id)?;
125 println!("Members (bob, after joining the group): {members:?}");
126
127 //
128 // Bob sends message to alice
129 //
130 println!("\nBob sends a message to Alice");
131 let ciphertext = mls_platform_api::mls_send(&state_global, &gide.group_id, &bob_id, b"hello")?;
132
133 // Alice receives the message
134 let message = mls_platform_api::mls_receive(
135 &state_global,
136 &alice_id,
137 &MessageOrAck::MlsMessage(ciphertext),
138 )?;
139
140 let (_, Received::ApplicationMessage(app_msg)) = message else {
141 panic!("Expected a different type.");
142 };
143
144 println!(
145 "\nAlice receives the message from Bob {:?}",
146 String::from_utf8(app_msg).unwrap()
147 );
148
149 //
150 // Bob adds Charlie
151 //
152 println!("\nBob adds Charlie to the Group");
153 let commit_2_output = mls_platform_api::mls_group_add(
154 &mut state_global,
155 &gide.group_id,
156 &bob_id,
157 vec![charlie_kp],
158 )?;
159
160 let commit_2 = commit_2_output.commit;
161 let welcome_2 = commit_2_output
162 .welcome
163 .first()
164 .expect("No welcome messages found")
165 .clone();
166
167 // Bobs process its commit
168 println!("\nBob process their Commit");
169 mls_platform_api::mls_receive(
170 &state_global,
171 &bob_id,
172 &MessageOrAck::MlsMessage(commit_2.clone()),
173 )?;
174
175 // List the members of the group
176 let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &bob_id)?;
177 println!("Members (bob, after adding charlie): {members:?}");
178
179 // Alice receives the commit
180 println!("\nAlice receives the commit from Bob to add Charlie");
181 mls_platform_api::mls_receive(
182 &state_global,
183 &alice_id,
184 &MessageOrAck::MlsMessage(commit_2),
185 )?;
186
187 // Charlie joins
188 println!("\nCharlie joins the group");
189 mls_platform_api::mls_group_join(&state_global, &charlie_id, &welcome_2, None)?;
190
191 // List the members of the group
192 let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &charlie_id)?;
193 println!("Members (charlie, after joining the group): {members:?}");
194
195 //
196 // Charlie decides to close the group
197 //
198 println!("\nCharlie decides that it's enough and closes the group");
199 let commit_6_output =
200 mls_platform_api::mls_group_close(&state_global, &gide.group_id, &charlie_id)?;
201
202 let commit_6_msg = MessageOrAck::MlsMessage(commit_6_output.commit);
203
204 // Alice processes the close commit
205 println!("\nAlice processes the close commit");
206 let out_alice = mls_platform_api::mls_receive(&state_global, &alice_id, &commit_6_msg)?;
207
208 println!("Alice, out_alice_str {out_alice:?}");
209 println!("Alice's state for the group has been removed");
210 // Note: Alice cannot look at its own group state because it was already removed
211
212 // Bob processes the close commit
213 println!("\nBob processes the close commit");
214 let out_bob = mls_platform_api::mls_receive(&state_global, &bob_id, &commit_6_msg)?;
215
216 println!("Bob, out_bob_str {out_bob:?}");
217 println!("Bob's state for the group has been removed");
218 // Note: Bob cannot look at its own group state because it was already removed
219
220 let (_, Received::GroupIdEpoch(groupidepoch_bob)) = out_bob.clone() else {
221 panic!("Expected a different type.");
222 };
223
224 // Charlie processes the close commit
225 println!("\nCharlie processes the close commit");
226 mls_platform_api::mls_receive(&state_global, &charlie_id, &commit_6_msg)?;
227
228 let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &charlie_id)?;
229 println!("Members (charlie, after processing their group_close commit): {members:?}");
230
231 // Charlie deletes her state for the group
232 println!("\nCharlie deletes her state");
233 let out_charlie =
234 mls_platform_api::state_delete_group(&state_global, &gide.group_id, &charlie_id)?;
235
236 println!("Charlie, group deletion confirmation {out_charlie:?}");
237
238 // Test that Alice, Bob and Charlie have closed their group
239 // (checks the group id and epoch are equals)
240 assert!(out_alice == out_bob);
241 assert!(groupidepoch_bob == out_charlie);
242
243 Ok(())
244 }