| 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::ClientConfig; |
| 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 and Diana create signing identity (generate_signature_keypair) |
| 14 |
// * Alice, Bob, Charlie and Diana 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 sends an application message (send) |
| 21 |
// - Alice receives the application message (receive for application message) |
| 22 |
// * Bob adds Charlie to the group |
| 23 |
// - Bob receives the add commit |
| 24 |
// - Alice receives the add commit |
| 25 |
// - Charlie joins the group |
| 26 |
// * Charlie removes Alice from the group (group_remove) |
| 27 |
// - Charlie receives her remove commit |
| 28 |
// - Alice receives the remove commit |
| 29 |
// - Bob receives the remove commit |
| 30 |
// * Bob produces group update with group info (group_update with external join) |
| 31 |
// - Bob receives his update commit |
| 32 |
// - Charlie receives the commit |
| 33 |
// * Diana sends a commit to do an external join (group_update for external join) |
| 34 |
// * Diana sends an application message |
| 35 |
// - Bob receives the commit |
| 36 |
// - Bob receives Diana's application message |
| 37 |
// - Charlie receives the commit |
| 38 |
// - Charlie receives Diana's application message |
| 39 |
// * Bob sends a self remove proposal to the group (group_propose_remove) |
| 40 |
// - Diana receives the proposal (receive for proposal) |
| 41 |
// * Diana produces the commit for the remove proposal |
| 42 |
// - Diana receives her remove commit |
| 43 |
// - Charlie receives her remove commit |
| 44 |
// - Bob receives her remove commit |
| 45 |
// * Charlie decides that it's enough and closes the group (group_close) |
| 46 |
// - Diana processes the close commit |
| 47 |
// - Charlie processes the close commit |
| 48 |
// * Charlie removes her state for the group (state_delete_group) |
| 49 |
|
| 50 |
#[test] |
| 51 |
fn main() -> Result<(), PlatformError> { |
| 52 |
// Default group configuration |
| 53 |
let group_config = mls_platform_api::GroupConfig::default(); |
| 54 |
|
| 55 |
// Storage states |
| 56 |
let mut state_global = mls_platform_api::state_access("global.db", &[0u8; 32])?; |
| 57 |
|
| 58 |
// Credentials |
| 59 |
let alice_cred = mls_platform_api::mls_generate_credential_basic("alice".as_bytes())?; |
| 60 |
let bob_cred = mls_platform_api::mls_generate_credential_basic("bob".as_bytes())?; |
| 61 |
let charlie_cred = mls_platform_api::mls_generate_credential_basic("charlie".as_bytes())?; |
| 62 |
let diana_cred = mls_platform_api::mls_generate_credential_basic("diana".as_bytes())?; |
| 63 |
|
| 64 |
println!("\nAlice credential: {}", hex::encode(&alice_cred)); |
| 65 |
println!("Bob credential: {}", hex::encode(&bob_cred)); |
| 66 |
println!("Charlie credential: {}", hex::encode(&charlie_cred)); |
| 67 |
println!("Diana credential: {}", hex::encode(&diana_cred)); |
| 68 |
|
| 69 |
// Create signature keypairs and store them in the state |
| 70 |
let alice_id = |
| 71 |
mls_platform_api::mls_generate_identity(&state_global, group_config.ciphersuite)?; |
| 72 |
|
| 73 |
let bob_id = mls_platform_api::mls_generate_identity(&state_global, group_config.ciphersuite)?; |
| 74 |
|
| 75 |
let charlie_id = |
| 76 |
mls_platform_api::mls_generate_identity(&state_global, group_config.ciphersuite)?; |
| 77 |
|
| 78 |
let diana_id = |
| 79 |
mls_platform_api::mls_generate_identity(&state_global, group_config.ciphersuite)?; |
| 80 |
|
| 81 |
println!("\nAlice identifier: {}", hex::encode(&alice_id)); |
| 82 |
println!("Bob identifier: {}", hex::encode(&bob_id)); |
| 83 |
println!("Charlie identifier: {}", hex::encode(&charlie_id)); |
| 84 |
println!("Diana identifier: {}", hex::encode(&diana_id)); |
| 85 |
|
| 86 |
// Create Key Package for Bob |
| 87 |
let bob_kp = mls_platform_api::mls_generate_key_package( |
| 88 |
&state_global, |
| 89 |
&bob_id, |
| 90 |
&bob_cred, |
| 91 |
&Default::default(), |
| 92 |
)?; |
| 93 |
|
| 94 |
// Create Key Package for Charlie |
| 95 |
let charlie_kp = mls_platform_api::mls_generate_key_package( |
| 96 |
&state_global, |
| 97 |
&charlie_id, |
| 98 |
&charlie_cred, |
| 99 |
&Default::default(), |
| 100 |
)?; |
| 101 |
|
| 102 |
// Create a group with Alice |
| 103 |
let gide = mls_platform_api::mls_group_create( |
| 104 |
&mut state_global, |
| 105 |
&alice_id, |
| 106 |
&alice_cred, |
| 107 |
None, |
| 108 |
None, |
| 109 |
&Default::default(), |
| 110 |
)?; |
| 111 |
|
| 112 |
println!("\nGroup created by Alice: {}", hex::encode(&gide.group_id)); |
| 113 |
|
| 114 |
// List the members of the group |
| 115 |
let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &alice_id)?; |
| 116 |
println!("Members (alice, before adding bob): {members:?}"); |
| 117 |
|
| 118 |
// |
| 119 |
// Alice adds Bob to a group |
| 120 |
// |
| 121 |
println!("\nAlice adds Bob to the Group"); |
| 122 |
let commit_output = mls_platform_api::mls_group_add( |
| 123 |
&mut state_global, |
| 124 |
&gide.group_id, |
| 125 |
&alice_id, |
| 126 |
vec![bob_kp], |
| 127 |
)?; |
| 128 |
|
| 129 |
let welcome = commit_output |
| 130 |
.welcome |
| 131 |
.first() |
| 132 |
.expect("No welcome messages found") |
| 133 |
.clone(); |
| 134 |
|
| 135 |
// Alice process her own commit |
| 136 |
println!("\nAlice process her commit to add Bob to the Group"); |
| 137 |
mls_platform_api::mls_receive( |
| 138 |
&state_global, |
| 139 |
&alice_id, |
| 140 |
&MessageOrAck::MlsMessage(commit_output.commit.clone()), |
| 141 |
)?; |
| 142 |
|
| 143 |
// List the members of the group |
| 144 |
let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &alice_id)?; |
| 145 |
println!("Members (alice, after adding bob): {members:?}"); |
| 146 |
|
| 147 |
// Bob joins |
| 148 |
println!("\nBob joins the group created by Alice"); |
| 149 |
mls_platform_api::mls_group_join(&state_global, &bob_id, &welcome, None)?; |
| 150 |
|
| 151 |
// List the members of the group |
| 152 |
let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &bob_id)?; |
| 153 |
println!("Members (bob, after joining the group): {members:?}"); |
| 154 |
|
| 155 |
// |
| 156 |
// Bob sends message to alice |
| 157 |
// |
| 158 |
println!("\nBob sends a message to Alice"); |
| 159 |
let ciphertext = mls_platform_api::mls_send(&state_global, &gide.group_id, &bob_id, b"hello")?; |
| 160 |
|
| 161 |
// Alice receives the message |
| 162 |
let message = mls_platform_api::mls_receive( |
| 163 |
&state_global, |
| 164 |
&alice_id, |
| 165 |
&MessageOrAck::MlsMessage(ciphertext), |
| 166 |
)?; |
| 167 |
let (_, Received::ApplicationMessage(app_msg)) = message else { |
| 168 |
panic!("Expected an application message, but received a different type."); |
| 169 |
}; |
| 170 |
|
| 171 |
println!( |
| 172 |
"\nAlice receives the message from Bob: {:?}", |
| 173 |
String::from_utf8(app_msg).unwrap() |
| 174 |
); |
| 175 |
|
| 176 |
// |
| 177 |
// Bob adds Charlie |
| 178 |
// |
| 179 |
println!("\nBob adds Charlie to the Group"); |
| 180 |
let commit_2_output = mls_platform_api::mls_group_add( |
| 181 |
&mut state_global, |
| 182 |
&gide.group_id, |
| 183 |
&bob_id, |
| 184 |
vec![charlie_kp], |
| 185 |
)?; |
| 186 |
|
| 187 |
let commit_2 = commit_2_output.commit; |
| 188 |
let welcome_2 = commit_2_output |
| 189 |
.welcome |
| 190 |
.first() |
| 191 |
.expect("No welcome messages found") |
| 192 |
.clone(); |
| 193 |
|
| 194 |
// Bobs process its commit |
| 195 |
println!("\nBob process their Commit"); |
| 196 |
mls_platform_api::mls_receive( |
| 197 |
&state_global, |
| 198 |
&bob_id, |
| 199 |
&MessageOrAck::MlsMessage(commit_2.clone()), |
| 200 |
)?; |
| 201 |
|
| 202 |
// List the members of the group |
| 203 |
let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &bob_id)?; |
| 204 |
println!("Members (bob, after adding charlie): {members:?}"); |
| 205 |
|
| 206 |
// Alice receives the commit |
| 207 |
println!("\nAlice receives the commit from Bob to add Charlie"); |
| 208 |
mls_platform_api::mls_receive( |
| 209 |
&state_global, |
| 210 |
&alice_id, |
| 211 |
&MessageOrAck::MlsMessage(commit_2), |
| 212 |
)?; |
| 213 |
|
| 214 |
// Charlie joins |
| 215 |
println!("\nCharlie joins the group"); |
| 216 |
mls_platform_api::mls_group_join(&state_global, &charlie_id, &welcome_2, None)?; |
| 217 |
|
| 218 |
// List the members of the group |
| 219 |
let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &charlie_id)?; |
| 220 |
println!("Members (charlie, after joining the group): {members:?}"); |
| 221 |
|
| 222 |
// |
| 223 |
// Charlie removes Alice from the group |
| 224 |
// |
| 225 |
println!("\nCharlie removes Alice from the Group"); |
| 226 |
let commit_3_output = |
| 227 |
mls_platform_api::mls_group_remove(&state_global, &gide.group_id, &charlie_id, &alice_id)?; |
| 228 |
|
| 229 |
let commit_3 = commit_3_output.commit; |
| 230 |
|
| 231 |
// Charlie receives the commit |
| 232 |
mls_platform_api::mls_receive( |
| 233 |
&state_global, |
| 234 |
&charlie_id, |
| 235 |
&MessageOrAck::Ack(gide.group_id.to_vec()), |
| 236 |
)?; |
| 237 |
|
| 238 |
let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &charlie_id)?; |
| 239 |
println!("Members (charlie, after removing alice): {members:?}"); |
| 240 |
|
| 241 |
// Alice receives the commit from Charlie |
| 242 |
println!("\nAlice receives the remove commit from Charlie"); |
| 243 |
let _ = mls_platform_api::mls_receive( |
| 244 |
&state_global, |
| 245 |
&alice_id, |
| 246 |
&MessageOrAck::MlsMessage(commit_3.clone()), |
| 247 |
)?; |
| 248 |
println!("Members (alice, after receiving alice's removal the group): {members:?}"); |
| 249 |
println!("Alice's state for the group has been removed"); |
| 250 |
|
| 251 |
// Bob receives the commit from Charlie |
| 252 |
println!("\nBob receives the remove commit from Charlie"); |
| 253 |
let _ = mls_platform_api::mls_receive( |
| 254 |
&state_global, |
| 255 |
&bob_id, |
| 256 |
&MessageOrAck::MlsMessage(commit_3.clone()), |
| 257 |
)?; |
| 258 |
|
| 259 |
let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &bob_id)?; |
| 260 |
println!("Members (bob, after receiving alice's removal the group): {members:?}"); |
| 261 |
|
| 262 |
// |
| 263 |
// Bob produces group info to allow an external join from Diana |
| 264 |
// |
| 265 |
let client_config = ClientConfig { |
| 266 |
allow_external_commits: true, |
| 267 |
..Default::default() |
| 268 |
}; |
| 269 |
|
| 270 |
println!("\nBob produce a group info so that someone can do an External join"); |
| 271 |
let commit_4_output = mls_platform_api::mls_group_update( |
| 272 |
&mut state_global, |
| 273 |
&gide.group_id, |
| 274 |
&bob_id, |
| 275 |
None, |
| 276 |
None, |
| 277 |
None, |
| 278 |
&client_config, |
| 279 |
)?; |
| 280 |
|
| 281 |
// Bob receives own commit |
| 282 |
mls_platform_api::mls_receive( |
| 283 |
&state_global, |
| 284 |
&bob_id, |
| 285 |
&MessageOrAck::MlsMessage(commit_4_output.commit.clone()), |
| 286 |
)?; |
| 287 |
|
| 288 |
let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &bob_id)?; |
| 289 |
println!("Members (bob, after commit allowing external join): {members:?}"); |
| 290 |
|
| 291 |
// Charlie receives Bob's commit with GroupInfo for External Join |
| 292 |
mls_platform_api::mls_receive( |
| 293 |
&state_global, |
| 294 |
&charlie_id, |
| 295 |
&MessageOrAck::MlsMessage(commit_4_output.commit), |
| 296 |
)?; |
| 297 |
|
| 298 |
let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &charlie_id)?; |
| 299 |
println!("Members (charlie, after bob's commit allowing external join): {members:?}"); |
| 300 |
|
| 301 |
// |
| 302 |
// Diana joins the group with an external commit |
| 303 |
// |
| 304 |
println!("\nDiana uses the group info created by Bob to do an External join"); |
| 305 |
let external_commit_output = mls_platform_api::mls_group_external_commit( |
| 306 |
&state_global, |
| 307 |
&diana_id, |
| 308 |
&diana_cred, |
| 309 |
&commit_4_output |
| 310 |
.group_info |
| 311 |
.expect("alice should produce group info"), |
| 312 |
// use tree in extension for now |
| 313 |
None, |
| 314 |
)?; |
| 315 |
|
| 316 |
println!("Externally joined group {:?}", &external_commit_output.gid); |
| 317 |
|
| 318 |
let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &diana_id)?; |
| 319 |
println!("Members (diane, after joining): {members:?}"); |
| 320 |
|
| 321 |
// |
| 322 |
// Diana sends a message to the group |
| 323 |
// |
| 324 |
println!("\nDiana sends a message to the group"); |
| 325 |
let ctx = mls_platform_api::mls_send( |
| 326 |
&state_global, |
| 327 |
&gide.group_id, |
| 328 |
&diana_id, |
| 329 |
b"hello from diana", |
| 330 |
)?; |
| 331 |
|
| 332 |
// Bob receives Diana's commit |
| 333 |
println!("\nBob receives the External Join from Diana"); |
| 334 |
mls_platform_api::mls_receive( |
| 335 |
&state_global, |
| 336 |
&bob_id, |
| 337 |
&MessageOrAck::MlsMessage(external_commit_output.external_commit.clone()), |
| 338 |
)?; |
| 339 |
|
| 340 |
let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &diana_id)?; |
| 341 |
println!("Members (bob, after diane joined externally): {members:?}"); |
| 342 |
|
| 343 |
// Bob receives Diana's application message |
| 344 |
let ptx = mls_platform_api::mls_receive( |
| 345 |
&state_global, |
| 346 |
&bob_id, |
| 347 |
&MessageOrAck::MlsMessage(ctx.clone()), |
| 348 |
)?; |
| 349 |
|
| 350 |
let (_, Received::ApplicationMessage(app_msg)) = ptx else { |
| 351 |
panic!("Expected an application message, but received a different type."); |
| 352 |
}; |
| 353 |
|
| 354 |
println!( |
| 355 |
"\nBob receives Diana's message {:?}", |
| 356 |
String::from_utf8(app_msg).unwrap() |
| 357 |
); |
| 358 |
|
| 359 |
// Charlie receives Diana's commit |
| 360 |
println!("\nCharlie receives the External Join from Diana"); |
| 361 |
mls_platform_api::mls_receive( |
| 362 |
&state_global, |
| 363 |
&charlie_id, |
| 364 |
&MessageOrAck::MlsMessage(external_commit_output.external_commit), |
| 365 |
)?; |
| 366 |
|
| 367 |
let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &charlie_id)?; |
| 368 |
println!("Members (charlie, after diane joined externally): {members:?}"); |
| 369 |
|
| 370 |
// Charlie receives Diana's application message |
| 371 |
let ptx = |
| 372 |
mls_platform_api::mls_receive(&state_global, &charlie_id, &MessageOrAck::MlsMessage(ctx))?; |
| 373 |
|
| 374 |
let (_, Received::ApplicationMessage(app_msg)) = ptx else { |
| 375 |
panic!("Expected an application message, but received a different type."); |
| 376 |
}; |
| 377 |
|
| 378 |
println!( |
| 379 |
"\nCharlie receives Diana's message {:?}", |
| 380 |
String::from_utf8(app_msg).unwrap() |
| 381 |
); |
| 382 |
|
| 383 |
// |
| 384 |
// Bob proposes to remove itself |
| 385 |
// |
| 386 |
println!("\nBob proposes a self remove"); |
| 387 |
let self_remove_proposal = |
| 388 |
mls_group_propose_remove(&state_global, &gide.group_id, &bob_id, &bob_id)?; |
| 389 |
|
| 390 |
// |
| 391 |
// Diana receives the proposal from Bob |
| 392 |
// |
| 393 |
println!("\nDiana commits to the remove"); |
| 394 |
let received_5_output = mls_platform_api::mls_receive( |
| 395 |
&state_global, |
| 396 |
&diana_id, |
| 397 |
&MessageOrAck::MlsMessage(self_remove_proposal.clone()), |
| 398 |
)?; |
| 399 |
|
| 400 |
let (_, Received::CommitOutput(commit_5_output)) = received_5_output else { |
| 401 |
panic!("Expected an application message, but received a different type."); |
| 402 |
}; |
| 403 |
|
| 404 |
let commit_5_msg = MessageOrAck::MlsMessage(commit_5_output.commit); |
| 405 |
|
| 406 |
// Diana processes the remove commit |
| 407 |
println!("\nDiana processes the remove commit"); |
| 408 |
mls_platform_api::mls_receive(&state_global, &diana_id, &commit_5_msg)?; |
| 409 |
|
| 410 |
let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &diana_id)?; |
| 411 |
println!("Members (diana, after removing bob): {members:?}"); |
| 412 |
|
| 413 |
// Charlie processes the remove commit |
| 414 |
println!("\nCharlie processes the remove commit"); |
| 415 |
mls_platform_api::mls_receive( |
| 416 |
&state_global, |
| 417 |
&charlie_id, |
| 418 |
&MessageOrAck::MlsMessage(self_remove_proposal), |
| 419 |
)?; |
| 420 |
mls_platform_api::mls_receive(&state_global, &charlie_id, &commit_5_msg)?; |
| 421 |
|
| 422 |
let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &charlie_id)?; |
| 423 |
println!("Members (charlie, after removing bob): {members:?}"); |
| 424 |
|
| 425 |
// Bob processes the remove commit |
| 426 |
println!("\nBob processes the remove commit"); |
| 427 |
let received_5_bob = mls_platform_api::mls_receive(&state_global, &bob_id, &commit_5_msg)?; |
| 428 |
|
| 429 |
let (_, Received::GroupIdEpoch(out_commit_5_bob)) = received_5_bob else { |
| 430 |
panic!("Expected a different type."); |
| 431 |
}; |
| 432 |
|
| 433 |
println!("Bob, out_commit_5 {out_commit_5_bob:?}"); |
| 434 |
println!("Bob's state for the group has been removed"); |
| 435 |
|
| 436 |
// Note: Bob cannot look at its own group state because it was already removed |
| 437 |
// let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &bob_id)?; |
| 438 |
// let members_str = mls_platform_api::utils_json_bytes_to_string_custom(&members)?; |
| 439 |
// println!("Members (bob, after its removal): {members_str:?}"); |
| 440 |
|
| 441 |
// |
| 442 |
// Charlie decides that it's enough and closes the group |
| 443 |
// |
| 444 |
println!("\nCharlie decides that it's enough and closes the group"); |
| 445 |
let commit_output_6_bytes = |
| 446 |
mls_platform_api::mls_group_close(&state_global, &gide.group_id, &charlie_id)?; |
| 447 |
|
| 448 |
let commit_6_msg = MessageOrAck::MlsMessage(commit_output_6_bytes.commit); |
| 449 |
|
| 450 |
// Diana processes the close commit |
| 451 |
println!("\nDiana processes the close commit"); |
| 452 |
let received_6_diana = mls_platform_api::mls_receive(&state_global, &diana_id, &commit_6_msg)?; |
| 453 |
|
| 454 |
let (_, Received::GroupIdEpoch(out_commit_6_diana)) = received_6_diana else { |
| 455 |
panic!("Expected a different type."); |
| 456 |
}; |
| 457 |
|
| 458 |
println!("Diana, out_commit_6 {out_commit_6_diana:?}"); |
| 459 |
println!("Diana's state for the group has been removed"); |
| 460 |
// Note: Diana cannot look at its own group state because it was already removed |
| 461 |
|
| 462 |
// Charlie processes the close commit |
| 463 |
println!("\nCharlie processes the close commit"); |
| 464 |
mls_platform_api::mls_receive(&state_global, &charlie_id, &commit_6_msg)?; |
| 465 |
|
| 466 |
let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &charlie_id)?; |
| 467 |
println!("Members (charlie, after processing their group_close commit): {members:?}"); |
| 468 |
|
| 469 |
// Charlie deletes her state for the group |
| 470 |
println!("\nCharlie deletes her state"); |
| 471 |
let out = mls_platform_api::state_delete_group(&state_global, &gide.group_id, &charlie_id)?; |
| 472 |
|
| 473 |
println!("Charlie, group deletion confirmation {out:?}"); |
| 474 |
|
| 475 |
Ok(()) |
| 476 |
} |