Debian Codegraph @

third_party/rust/mls-platform-api/tests/group_update.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::ClientConfig;
5 use mls_platform_api::ClientIdentifiers;
6 use mls_platform_api::MessageOrAck;
7 use mls_platform_api::PlatformError;
8
9 //
10 // Scenario
11 //
12 // * Alice, Bob, Charlie and Diana create signing identity (generate_signature_keypair)
13 // * Alice, Bob, Charlie and Diana create credentials (generate_credential_basic)
14 // * Bob and Charlie create key packages (generate_key_package)
15 // * Alice creates a group (group_create)
16 // * Alice adds Bob to the group (group_add)
17 // - Alice receives her add commit (receive for commit)
18 // - Bob joins the group (group_join)
19 // * Bob sends an application message (send)
20 // - Alice receives the application message (receive for application message)
21 // * Bob produces group update with group info (group_update with external join)
22 // - Bob receives his update commit
23 // - Alice receives his update commit
24 // * Diana sends a commit to do an external join (group_update for external join)
25 // - Alice receives the commit
26 // - Bob receives the commit
27
28 #[test]
29 fn test_group_external_join() -> 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 diana_cred = mls_platform_api::mls_generate_credential_basic("diana".as_bytes())?;
40
41 println!("\nAlice credential: {}", hex::encode(&alice_cred));
42 println!("Bob credential: {}", hex::encode(&bob_cred));
43 println!("Diana credential: {}", hex::encode(&diana_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 diana_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!("Diana identifier: {}", hex::encode(&diana_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 a group with Alice
67 let gide = mls_platform_api::mls_group_create(
68 &mut state_global,
69 &alice_id,
70 &alice_cred,
71 None,
72 None,
73 &Default::default(),
74 )?;
75
76 println!("\nGroup created by Alice: {}", hex::encode(&gide.group_id));
77
78 // List the members of the group
79 let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &alice_id)?;
80 println!("Members (alice, before adding bob): {members:?}");
81
82 //
83 // Alice adds Bob to a group
84 //
85 println!("\nAlice adds Bob to the Group");
86 let commit_output = mls_platform_api::mls_group_add(
87 &mut state_global,
88 &gide.group_id,
89 &alice_id,
90 vec![bob_kp],
91 )?;
92
93 let welcome = commit_output
94 .welcome
95 .first()
96 .expect("No welcome messages found")
97 .clone();
98
99 // Alice process her own commit
100 // println!("\nAlice process her commit to add Bob to the Group");
101 mls_platform_api::mls_receive(
102 &state_global,
103 &alice_id,
104 &MessageOrAck::MlsMessage(commit_output.commit.clone()),
105 )?;
106
107 // List the members of the group
108 let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &alice_id)?;
109 println!("Members (alice, after adding bob): {members:?}");
110
111 // Bob joins
112 println!("\nBob joins the group created by Alice");
113 mls_platform_api::mls_group_join(&state_global, &bob_id, &welcome, None)?;
114
115 // List the members of the group
116 let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &bob_id)?;
117 println!("Members (bob, after joining the group): {members:?}");
118
119 //
120 // Bob produces group info to allow an external join from Diana
121 //
122 let client_config = ClientConfig {
123 allow_external_commits: true,
124 ..Default::default()
125 };
126
127 println!("\nBob produce a group info so that someone can do an External join");
128 let commit_4_output = mls_platform_api::mls_group_update(
129 &mut state_global,
130 &gide.group_id,
131 &bob_id,
132 None,
133 None,
134 None,
135 &client_config,
136 )?;
137
138 // Alice receives Bob's commit
139 mls_platform_api::mls_receive(
140 &state_global,
141 &alice_id,
142 &MessageOrAck::MlsMessage(commit_4_output.commit.clone()),
143 )?;
144
145 let members_alice =
146 mls_platform_api::mls_group_details(&state_global, &gide.group_id, &alice_id)?;
147 println!(
148 "Members (alice, after receiving the commit allowing external join): {members_alice:?}"
149 );
150
151 // Bob receives own commit
152 mls_platform_api::mls_receive(
153 &state_global,
154 &bob_id,
155 &MessageOrAck::MlsMessage(commit_4_output.commit.clone()),
156 )?;
157
158 let members_bob = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &bob_id)?;
159 println!("Members (bob, after commit allowing external join): {members_bob:?}");
160
161 //
162 // Diana joins the group with an external commit
163 //
164 println!("\nDiana uses the group info created by Bob to do an External join");
165 let external_commit_output = mls_platform_api::mls_group_external_commit(
166 &state_global,
167 &diana_id,
168 &diana_cred,
169 &commit_4_output
170 .group_info
171 .expect("alice should produce group info"),
172 // use tree in extension for now
173 None,
174 )?;
175
176 println!("Externally joined group {:?}", &external_commit_output.gid);
177
178 let members_diana =
179 mls_platform_api::mls_group_details(&state_global, &gide.group_id, &diana_id)?;
180 println!("Members (diane, after joining): {members_diana:?}");
181
182 // Alice receives Diana's commit
183 println!("\nAlice receives the External Join from Diana");
184 mls_platform_api::mls_receive(
185 &state_global,
186 &alice_id,
187 &MessageOrAck::MlsMessage(external_commit_output.external_commit.clone()),
188 )?;
189
190 let members_alice =
191 mls_platform_api::mls_group_details(&state_global, &gide.group_id, &alice_id)?;
192 println!("Members (alice, after receiving the commit from Diana): {members_alice:?}");
193
194 // Bob receives Diana's commit
195 println!("\nBob receives the External Join from Diana");
196 mls_platform_api::mls_receive(
197 &state_global,
198 &bob_id,
199 &MessageOrAck::MlsMessage(external_commit_output.external_commit.clone()),
200 )?;
201
202 let members_bob = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &bob_id)?;
203 println!("Members (bob, after receiving the commit from Diana): {members_bob:?}");
204
205 // Check if Diana is in the members list
206 let diana_present = members_diana
207 .group_members
208 .iter()
209 .any(|ClientIdentifiers { identity, .. }| identity == &diana_id);
210
211 // Test that alice was removed from the group
212 assert!(
213 diana_present,
214 "Diana should be in the group members after external join"
215 );
216
217 // Test that membership are all the same
218 assert!(members_alice == members_bob);
219 assert!(members_diana == members_bob);
220
221 Ok(())
222 }