Debian Codegraph @

third_party/rust/mls-platform-api/tests/group_remove.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::ClientIdentifiers;
5 use mls_platform_api::MessageOrAck;
6 use mls_platform_api::PlatformError;
7
8 //
9 // Scenario
10 //
11 // * Alice, Bob, Charlie and Diana create signing identity (generate_signature_keypair)
12 // * Alice, Bob, Charlie and Diana 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 sends an application message (send)
19 // - Alice receives the application message (receive for application message)
20 // * Bob adds Charlie to the group
21 // - Bob receives the add commit
22 // - Alice receives the add commit
23 // - Charlie joins the group
24 // * Charlie removes Alice from the group (group_remove)
25 // - Charlie receives her remove commit
26 // - Alice receives the remove commit
27 // - Bob receives the remove commit
28
29 #[test]
30 fn test_group_remove() -> Result<(), PlatformError> {
31 // Default group configuration
32 let group_config = mls_platform_api::GroupConfig::default();
33
34 // Storage states
35 let mut state_global = mls_platform_api::state_access("global.db", &[0u8; 32])?;
36
37 // Credentials
38 let alice_cred = mls_platform_api::mls_generate_credential_basic("alice".as_bytes())?;
39 let bob_cred = mls_platform_api::mls_generate_credential_basic("bob".as_bytes())?;
40 let charlie_cred = mls_platform_api::mls_generate_credential_basic("charlie".as_bytes())?;
41
42 println!("\nAlice credential: {}", hex::encode(&alice_cred));
43 println!("Bob credential: {}", hex::encode(&bob_cred));
44 println!("Charlie credential: {}", hex::encode(&charlie_cred));
45
46 // Create signature keypairs and store them in the state
47 let alice_id =
48 mls_platform_api::mls_generate_identity(&state_global, group_config.ciphersuite)?;
49
50 let bob_id = mls_platform_api::mls_generate_identity(&state_global, group_config.ciphersuite)?;
51
52 let charlie_id =
53 mls_platform_api::mls_generate_identity(&state_global, group_config.ciphersuite)?;
54
55 println!("\nAlice identifier: {}", hex::encode(&alice_id));
56 println!("Bob identifier: {}", hex::encode(&bob_id));
57 println!("Charlie identifier: {}", hex::encode(&charlie_id));
58
59 // Create Key Package for Bob
60 let bob_kp = mls_platform_api::mls_generate_key_package(
61 &state_global,
62 &bob_id,
63 &bob_cred,
64 &Default::default(),
65 )?;
66
67 // Create Key Package for Charlie
68 let charlie_kp = mls_platform_api::mls_generate_key_package(
69 &state_global,
70 &charlie_id,
71 &charlie_cred,
72 &Default::default(),
73 )?;
74
75 // Create a group with Alice
76 let gide = mls_platform_api::mls_group_create(
77 &mut state_global,
78 &alice_id,
79 &alice_cred,
80 None,
81 None,
82 &Default::default(),
83 )?;
84
85 println!("\nGroup created by Alice: {}", hex::encode(&gide.group_id));
86
87 // List the members of the group
88 let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &alice_id)?;
89 println!("Members (alice, before adding bob): {members:?}");
90
91 //
92 // Alice adds Bob to a group
93 //
94 println!("\nAlice adds Bob to the Group");
95 let commit_output = mls_platform_api::mls_group_add(
96 &mut state_global,
97 &gide.group_id,
98 &alice_id,
99 vec![bob_kp],
100 )?;
101
102 let welcome = commit_output
103 .welcome
104 .first()
105 .expect("No welcome messages found")
106 .clone();
107
108 // Alice process her own commit
109 println!("\nAlice process her commit to add Bob to the Group");
110 mls_platform_api::mls_receive(
111 &state_global,
112 &alice_id,
113 &MessageOrAck::MlsMessage(commit_output.commit.clone()),
114 )?;
115
116 // List the members of the group
117 let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &alice_id)?;
118 println!("Members (alice, after adding bob): {members:?}");
119
120 // Bob joins
121 println!("\nBob joins the group created by Alice");
122 mls_platform_api::mls_group_join(&state_global, &bob_id, &welcome, None)?;
123
124 //
125 // Bob adds Charlie
126 //
127 println!("\nBob adds Charlie to the Group");
128 let commit_2_output = mls_platform_api::mls_group_add(
129 &mut state_global,
130 &gide.group_id,
131 &bob_id,
132 vec![charlie_kp],
133 )?;
134
135 let commit_2 = commit_2_output.commit;
136 let welcome_2 = commit_2_output
137 .welcome
138 .first()
139 .expect("No welcome messages found")
140 .clone();
141
142 // Bobs process its commit
143 // println!("\nBob process their Commit");
144 mls_platform_api::mls_receive(
145 &state_global,
146 &bob_id,
147 &MessageOrAck::MlsMessage(commit_2.clone()),
148 )?;
149
150 // List the members of the group
151 let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &bob_id)?;
152 println!("Members (bob, after adding charlie): {members:?}");
153
154 // Alice receives the commit
155 println!("\nAlice receives the commit from Bob to add Charlie");
156 mls_platform_api::mls_receive(
157 &state_global,
158 &alice_id,
159 &MessageOrAck::MlsMessage(commit_2),
160 )?;
161
162 // Charlie joins
163 println!("\nCharlie joins the group");
164 mls_platform_api::mls_group_join(&state_global, &charlie_id, &welcome_2, None)?;
165
166 // List the members of the group
167 let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &charlie_id)?;
168 println!("Members (charlie, after joining the group): {members:?}");
169
170 //
171 // Charlie removes Alice from the group
172 //
173 println!("\nCharlie removes Alice from the Group");
174 let commit_3_output =
175 mls_platform_api::mls_group_remove(&state_global, &gide.group_id, &charlie_id, &alice_id)?;
176
177 let commit_3 = commit_3_output.commit;
178
179 // Charlie receives the commit
180 mls_platform_api::mls_receive(
181 &state_global,
182 &charlie_id,
183 &MessageOrAck::Ack(gide.group_id.to_vec()),
184 )?;
185
186 let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &charlie_id)?;
187 println!("Members (charlie, after removing alice): {members:?}");
188
189 // Alice receives the commit from Charlie
190 println!("\nAlice receives the remove commit from Charlie");
191 let _ = mls_platform_api::mls_receive(
192 &state_global,
193 &alice_id,
194 &MessageOrAck::MlsMessage(commit_3.clone()),
195 )?;
196 println!("Members (alice, after receiving alice's removal the group): {members:?}");
197 println!("Alice's state for the group has been removed");
198
199 // Bob receives the commit from Charlie
200 println!("\nBob receives the remove commit from Charlie");
201 let _ = mls_platform_api::mls_receive(
202 &state_global,
203 &bob_id,
204 &MessageOrAck::MlsMessage(commit_3.clone()),
205 )?;
206
207 let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &bob_id)?;
208 println!("Members (bob, after receiving alice's removal the group): {members:?}");
209
210 // Check if Alice is still in the members list
211 let alice_present = members
212 .group_members
213 .iter()
214 .any(|ClientIdentifiers { identity, .. }| identity == &alice_id);
215
216 // Test that alice was removed from the group
217 assert!(
218 !alice_present,
219 "Alice should not be in the group members after removal"
220 );
221
222 Ok(())
223 }