| 1 |
// Copyright (c) 2024 Mozilla Corporation and contributors. |
| 2 |
// SPDX-License-Identifier: (Apache-2.0 OR MIT) |
| 3 |
|
| 4 |
use mls_platform_api::mls_group_propose_remove; |
| 5 |
use mls_platform_api::ClientIdentifiers; |
| 6 |
use mls_platform_api::MessageOrAck; |
| 7 |
use mls_platform_api::PlatformError; |
| 8 |
use mls_platform_api::Received; |
| 9 |
|
| 10 |
// |
| 11 |
// Scenario |
| 12 |
// |
| 13 |
// * Alice, Bob, Charlie create signing identity (generate_signature_keypair) |
| 14 |
// * Alice, Bob, Charlie create credentials (generate_credential_basic) |
| 15 |
// * Bob and Charlie create key packages (generate_key_package) |
| 16 |
// * Alice creates a group (group_create) |
| 17 |
// * Alice adds Bob to the group (group_add) |
| 18 |
// - Alice receives her add commit (receive for commit) |
| 19 |
// - Bob joins the group (group_join) |
| 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 |
// * Bob sends a self remove proposal to the group (group_propose_remove) |
| 25 |
// - Charlie receives the proposal (receive for proposal) |
| 26 |
// * Charlie produces the commit for the remove proposal |
| 27 |
// - Alice receives her remove commit |
| 28 |
// - Charlie receives her remove commit |
| 29 |
// - Bob receives her remove commit |
| 30 |
|
| 31 |
#[test] |
| 32 |
fn test_group_propose_self_remove() -> Result<(), PlatformError> { |
| 33 |
// Default group configuration |
| 34 |
let group_config = mls_platform_api::GroupConfig::default(); |
| 35 |
|
| 36 |
// Storage states |
| 37 |
let mut state_global = mls_platform_api::state_access("global.db", &[0u8; 32])?; |
| 38 |
|
| 39 |
// Credentials |
| 40 |
let alice_cred = mls_platform_api::mls_generate_credential_basic("alice".as_bytes())?; |
| 41 |
let bob_cred = mls_platform_api::mls_generate_credential_basic("bob".as_bytes())?; |
| 42 |
let charlie_cred = mls_platform_api::mls_generate_credential_basic("charlie".as_bytes())?; |
| 43 |
|
| 44 |
println!("\nAlice credential: {}", hex::encode(&alice_cred)); |
| 45 |
println!("Bob credential: {}", hex::encode(&bob_cred)); |
| 46 |
println!("Charlie credential: {}", hex::encode(&charlie_cred)); |
| 47 |
|
| 48 |
// Create signature keypairs and store them in the state |
| 49 |
let alice_id = |
| 50 |
mls_platform_api::mls_generate_identity(&state_global, group_config.ciphersuite)?; |
| 51 |
|
| 52 |
let bob_id = mls_platform_api::mls_generate_identity(&state_global, group_config.ciphersuite)?; |
| 53 |
|
| 54 |
let charlie_id = |
| 55 |
mls_platform_api::mls_generate_identity(&state_global, group_config.ciphersuite)?; |
| 56 |
|
| 57 |
println!("\nAlice identifier: {}", hex::encode(&alice_id)); |
| 58 |
println!("Bob identifier: {}", hex::encode(&bob_id)); |
| 59 |
println!("Charlie identifier: {}", hex::encode(&charlie_id)); |
| 60 |
|
| 61 |
// Create Key Package for Bob |
| 62 |
let bob_kp = mls_platform_api::mls_generate_key_package( |
| 63 |
&state_global, |
| 64 |
&bob_id, |
| 65 |
&bob_cred, |
| 66 |
&Default::default(), |
| 67 |
)?; |
| 68 |
|
| 69 |
// Create Key Package for Charlie |
| 70 |
let charlie_kp = mls_platform_api::mls_generate_key_package( |
| 71 |
&state_global, |
| 72 |
&charlie_id, |
| 73 |
&charlie_cred, |
| 74 |
&Default::default(), |
| 75 |
)?; |
| 76 |
|
| 77 |
// Create a group with Alice |
| 78 |
let gide = mls_platform_api::mls_group_create( |
| 79 |
&mut state_global, |
| 80 |
&alice_id, |
| 81 |
&alice_cred, |
| 82 |
None, |
| 83 |
None, |
| 84 |
&Default::default(), |
| 85 |
)?; |
| 86 |
|
| 87 |
println!("\nGroup created by Alice: {}", hex::encode(&gide.group_id)); |
| 88 |
|
| 89 |
// List the members of the group |
| 90 |
let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &alice_id)?; |
| 91 |
println!("Members (alice, before adding bob): {members:?}"); |
| 92 |
|
| 93 |
// |
| 94 |
// Alice adds Bob to a group |
| 95 |
// |
| 96 |
println!("\nAlice adds Bob to the Group"); |
| 97 |
let commit_output = mls_platform_api::mls_group_add( |
| 98 |
&mut state_global, |
| 99 |
&gide.group_id, |
| 100 |
&alice_id, |
| 101 |
vec![bob_kp], |
| 102 |
)?; |
| 103 |
|
| 104 |
let welcome = commit_output |
| 105 |
.welcome |
| 106 |
.first() |
| 107 |
.expect("No welcome messages found") |
| 108 |
.clone(); |
| 109 |
|
| 110 |
// Alice process her own commit |
| 111 |
println!("\nAlice process her commit to add Bob to the Group"); |
| 112 |
mls_platform_api::mls_receive( |
| 113 |
&state_global, |
| 114 |
&alice_id, |
| 115 |
&MessageOrAck::MlsMessage(commit_output.commit.clone()), |
| 116 |
)?; |
| 117 |
|
| 118 |
// List the members of the group |
| 119 |
let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &alice_id)?; |
| 120 |
println!("Members (alice, after adding bob): {members:?}"); |
| 121 |
|
| 122 |
// Bob joins |
| 123 |
println!("\nBob joins the group created by Alice"); |
| 124 |
mls_platform_api::mls_group_join(&state_global, &bob_id, &welcome, None)?; |
| 125 |
|
| 126 |
// List the members of the group |
| 127 |
let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &bob_id)?; |
| 128 |
println!("Members (bob, after joining the group): {members:?}"); |
| 129 |
|
| 130 |
// |
| 131 |
// Bob adds Charlie |
| 132 |
// |
| 133 |
println!("\nBob adds Charlie to the Group"); |
| 134 |
let commit_2_output = mls_platform_api::mls_group_add( |
| 135 |
&mut state_global, |
| 136 |
&gide.group_id, |
| 137 |
&bob_id, |
| 138 |
vec![charlie_kp], |
| 139 |
)?; |
| 140 |
|
| 141 |
let commit_2 = commit_2_output.commit; |
| 142 |
let welcome_2 = commit_2_output |
| 143 |
.welcome |
| 144 |
.first() |
| 145 |
.expect("No welcome messages found") |
| 146 |
.clone(); |
| 147 |
|
| 148 |
// Bobs process its commit |
| 149 |
println!("\nBob process their Commit"); |
| 150 |
mls_platform_api::mls_receive( |
| 151 |
&state_global, |
| 152 |
&bob_id, |
| 153 |
&MessageOrAck::MlsMessage(commit_2.clone()), |
| 154 |
)?; |
| 155 |
|
| 156 |
// List the members of the group |
| 157 |
let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &bob_id)?; |
| 158 |
println!("Members (bob, after adding charlie): {members:?}"); |
| 159 |
|
| 160 |
// Alice receives the commit |
| 161 |
println!("\nAlice receives the commit from Bob to add Charlie"); |
| 162 |
mls_platform_api::mls_receive( |
| 163 |
&state_global, |
| 164 |
&alice_id, |
| 165 |
&MessageOrAck::MlsMessage(commit_2), |
| 166 |
)?; |
| 167 |
|
| 168 |
// Charlie joins |
| 169 |
println!("\nCharlie joins the group"); |
| 170 |
mls_platform_api::mls_group_join(&state_global, &charlie_id, &welcome_2, None)?; |
| 171 |
|
| 172 |
// List the members of the group |
| 173 |
let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &charlie_id)?; |
| 174 |
println!("Members (charlie, after joining the group): {members:?}"); |
| 175 |
|
| 176 |
// |
| 177 |
// Bob proposes to remove itself |
| 178 |
// |
| 179 |
println!("\nBob proposes a self remove"); |
| 180 |
let self_remove_proposal = |
| 181 |
mls_group_propose_remove(&state_global, &gide.group_id, &bob_id, &bob_id)?; |
| 182 |
|
| 183 |
// |
| 184 |
// Charlie receives the proposal from Bob |
| 185 |
// |
| 186 |
println!("\nCharlie commits to the remove"); |
| 187 |
let recv_commit_output_5 = mls_platform_api::mls_receive( |
| 188 |
&state_global, |
| 189 |
&charlie_id, |
| 190 |
&MessageOrAck::MlsMessage(self_remove_proposal.clone()), |
| 191 |
)?; |
| 192 |
|
| 193 |
let (_, Received::CommitOutput(commit_output_5)) = recv_commit_output_5 else { |
| 194 |
panic!("Expected a different type."); |
| 195 |
}; |
| 196 |
|
| 197 |
let commit_5_msg = MessageOrAck::MlsMessage(commit_output_5.commit); |
| 198 |
|
| 199 |
// Alice processes the remove commit |
| 200 |
println!("\nAlice processes the remove commit"); |
| 201 |
mls_platform_api::mls_receive( |
| 202 |
&state_global, |
| 203 |
&alice_id, |
| 204 |
&MessageOrAck::MlsMessage(self_remove_proposal.clone()), |
| 205 |
)?; |
| 206 |
mls_platform_api::mls_receive(&state_global, &alice_id, &commit_5_msg)?; |
| 207 |
|
| 208 |
let members_alice = |
| 209 |
mls_platform_api::mls_group_details(&state_global, &gide.group_id, &alice_id)?; |
| 210 |
println!("Members (alice, after removing Bob): {members_alice:?}"); |
| 211 |
|
| 212 |
// Charlie processes the remove commit |
| 213 |
println!("\nCharlie processes the remove commit"); |
| 214 |
mls_platform_api::mls_receive(&state_global, &charlie_id, &commit_5_msg)?; |
| 215 |
|
| 216 |
let members_charlie = |
| 217 |
mls_platform_api::mls_group_details(&state_global, &gide.group_id, &charlie_id)?; |
| 218 |
println!("Members (charlie, after removing bob): {members_charlie:?}"); |
| 219 |
|
| 220 |
// Bob processes the remove commit |
| 221 |
println!("\nBob processes the remove commit"); |
| 222 |
|
| 223 |
// https://github.com/awslabs/mls-rs/pull/154 |
| 224 |
// mls_platform_api::mls_receive( |
| 225 |
// &state_global, |
| 226 |
// &bob_id, |
| 227 |
// MlsMessageOrAck::MlsMessage(self_remove_proposal), |
| 228 |
// )?; |
| 229 |
|
| 230 |
let out_5_bob = mls_platform_api::mls_receive(&state_global, &bob_id, &commit_5_msg)?; |
| 231 |
|
| 232 |
println!("Bob, out_commit_5 {out_5_bob:?}"); |
| 233 |
println!("Bob's state for the group has been removed"); |
| 234 |
// Note: Bob cannot look at its own group state because it was already removed |
| 235 |
|
| 236 |
// Check if Bob is present in the group |
| 237 |
let bob_present = members_alice |
| 238 |
.group_members |
| 239 |
.iter() |
| 240 |
.any(|ClientIdentifiers { identity, .. }| identity == &bob_id); |
| 241 |
|
| 242 |
// Test that bob was removed from the group |
| 243 |
assert!( |
| 244 |
!bob_present, |
| 245 |
"Bob should not be in the group members after removal" |
| 246 |
); |
| 247 |
|
| 248 |
// Test that Alice and Charlie have the same group |
| 249 |
assert!(members_alice == members_charlie); |
| 250 |
|
| 251 |
Ok(()) |
| 252 |
} |