| 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 |
use mls_platform_api::Received; |
| 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 |
|
| 21 |
#[test] |
| 22 |
fn test_send_receive() -> Result<(), PlatformError> { |
| 23 |
// Default group configuration |
| 24 |
let group_config = mls_platform_api::GroupConfig::default(); |
| 25 |
|
| 26 |
// Storage states |
| 27 |
let mut state_global = mls_platform_api::state_access("global.db", &[0u8; 32])?; |
| 28 |
|
| 29 |
// Credentials |
| 30 |
let alice_cred = mls_platform_api::mls_generate_credential_basic("alice".as_bytes())?; |
| 31 |
let bob_cred = mls_platform_api::mls_generate_credential_basic("bob".as_bytes())?; |
| 32 |
|
| 33 |
println!("\nAlice credential: {}", hex::encode(&alice_cred)); |
| 34 |
println!("Bob credential: {}", hex::encode(&bob_cred)); |
| 35 |
|
| 36 |
// Create signature keypairs and store them in the state |
| 37 |
let alice_id = |
| 38 |
mls_platform_api::mls_generate_identity(&state_global, group_config.ciphersuite)?; |
| 39 |
|
| 40 |
let bob_id = mls_platform_api::mls_generate_identity(&state_global, group_config.ciphersuite)?; |
| 41 |
|
| 42 |
println!("\nAlice identifier: {}", hex::encode(&alice_id)); |
| 43 |
println!("Bob identifier: {}", hex::encode(&bob_id)); |
| 44 |
|
| 45 |
// Create Key Package for Bob |
| 46 |
let bob_kp = mls_platform_api::mls_generate_key_package( |
| 47 |
&state_global, |
| 48 |
&bob_id, |
| 49 |
&bob_cred, |
| 50 |
&Default::default(), |
| 51 |
)?; |
| 52 |
|
| 53 |
// Create a group with Alice |
| 54 |
let gide = mls_platform_api::mls_group_create( |
| 55 |
&mut state_global, |
| 56 |
&alice_id, |
| 57 |
&alice_cred, |
| 58 |
None, |
| 59 |
None, |
| 60 |
&Default::default(), |
| 61 |
)?; |
| 62 |
|
| 63 |
println!("\nGroup created by Alice: {}", hex::encode(&gide.group_id)); |
| 64 |
|
| 65 |
// List the members of the group |
| 66 |
let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &alice_id)?; |
| 67 |
println!("Members (alice, before adding bob): {members:?}"); |
| 68 |
|
| 69 |
// |
| 70 |
// Alice adds Bob to a group |
| 71 |
// |
| 72 |
println!("\nAlice adds Bob to the Group"); |
| 73 |
let commit_output = mls_platform_api::mls_group_add( |
| 74 |
&mut state_global, |
| 75 |
&gide.group_id, |
| 76 |
&alice_id, |
| 77 |
vec![bob_kp], |
| 78 |
)?; |
| 79 |
|
| 80 |
let welcome = commit_output |
| 81 |
.welcome |
| 82 |
.first() |
| 83 |
.expect("No welcome messages found") |
| 84 |
.clone(); |
| 85 |
|
| 86 |
// Alice process her own commit |
| 87 |
// println!("\nAlice process her commit to add Bob to the Group"); |
| 88 |
mls_platform_api::mls_receive( |
| 89 |
&state_global, |
| 90 |
&alice_id, |
| 91 |
&MessageOrAck::MlsMessage(commit_output.commit.clone()), |
| 92 |
)?; |
| 93 |
|
| 94 |
// Bob joins |
| 95 |
println!("\nBob joins the group created by Alice"); |
| 96 |
mls_platform_api::mls_group_join(&state_global, &bob_id, &welcome, None)?; |
| 97 |
|
| 98 |
// |
| 99 |
// Bob sends message to alice |
| 100 |
// |
| 101 |
println!("\nBob sends a message to Alice"); |
| 102 |
let message_str = "hello"; |
| 103 |
let ciphertext = mls_platform_api::mls_send( |
| 104 |
&state_global, |
| 105 |
&gide.group_id, |
| 106 |
&bob_id, |
| 107 |
message_str.as_bytes(), |
| 108 |
)?; |
| 109 |
|
| 110 |
// Alice receives the message |
| 111 |
let message = mls_platform_api::mls_receive( |
| 112 |
&state_global, |
| 113 |
&alice_id, |
| 114 |
&MessageOrAck::MlsMessage(ciphertext), |
| 115 |
)?; |
| 116 |
|
| 117 |
let (_, Received::ApplicationMessage(app_msg)) = message else { |
| 118 |
panic!("Expected an application message, but received a different type."); |
| 119 |
}; |
| 120 |
|
| 121 |
let message_2_str = String::from_utf8(app_msg).unwrap(); |
| 122 |
|
| 123 |
println!("\nAlice receives the message from Bob {:?}", message_2_str); |
| 124 |
|
| 125 |
assert!(message_str == message_2_str); |
| 126 |
Ok(()) |
| 127 |
} |