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