Debian Codegraph @

third_party/rust/mls-platform-api/tests/group_propose_add.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::MessageOrAck;
5 use mls_platform_api::PlatformError;
6 use mls_platform_api::Received;
7
8 //
9 // Scenario
10 //
11 // * Alice, Bob, Charlie create signing identity (generate_signature_keypair)
12 // * Alice, Bob, Charlie 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 propose to add Charlie to the group
19 // - Alice receives the proposal
20 // * Alice adds Charlie to the group
21 // - Bob receives the add commit
22 // - Alice receives the add commit
23 // - Charlie joins the group
24
25 #[test]
26 fn test_propose_add() -> Result<(), PlatformError> {
27 // Default group configuration
28 let group_config = mls_platform_api::GroupConfig::default();
29
30 // Storage states
31 let mut state_global = mls_platform_api::state_access("global.db", &[0u8; 32])?;
32
33 // Credentials
34 let alice_cred = mls_platform_api::mls_generate_credential_basic("alice".as_bytes())?;
35 let bob_cred = mls_platform_api::mls_generate_credential_basic("bob".as_bytes())?;
36 let charlie_cred = mls_platform_api::mls_generate_credential_basic("charlie".as_bytes())?;
37
38 println!("\nAlice credential: {}", hex::encode(&alice_cred));
39 println!("Bob credential: {}", hex::encode(&bob_cred));
40 println!("Charlie credential: {}", hex::encode(&charlie_cred));
41
42 // Create signature keypairs and store them in the state
43 let alice_id =
44 mls_platform_api::mls_generate_identity(&state_global, group_config.ciphersuite)?;
45
46 let bob_id = mls_platform_api::mls_generate_identity(&state_global, group_config.ciphersuite)?;
47
48 let charlie_id =
49 mls_platform_api::mls_generate_identity(&state_global, group_config.ciphersuite)?;
50
51 println!("\nAlice identifier: {}", hex::encode(&alice_id));
52 println!("Bob identifier: {}", hex::encode(&bob_id));
53 println!("Charlie identifier: {}", hex::encode(&charlie_id));
54
55 // Create Key Package for Bob
56 let bob_kp = mls_platform_api::mls_generate_key_package(
57 &state_global,
58 &bob_id,
59 &bob_cred,
60 &Default::default(),
61 )?;
62
63 // Create Key Package for Charlie
64 let charlie_kp = mls_platform_api::mls_generate_key_package(
65 &state_global,
66 &charlie_id,
67 &charlie_cred,
68 &Default::default(),
69 )?;
70
71 // Create a group with Alice
72 let gide = mls_platform_api::mls_group_create(
73 &mut state_global,
74 &alice_id,
75 &alice_cred,
76 None,
77 None,
78 &Default::default(),
79 )?;
80
81 println!("\nGroup created by Alice: {}", hex::encode(&gide.group_id));
82
83 // List the members of the group
84 let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &alice_id)?;
85 println!("Members (alice, before adding bob): {members:?}");
86
87 //
88 // Alice adds Bob to a group
89 //
90 println!("\nAlice adds Bob to the Group");
91 let commit_output = mls_platform_api::mls_group_add(
92 &mut state_global,
93 &gide.group_id,
94 &alice_id,
95 vec![bob_kp],
96 )?;
97
98 let welcome = commit_output
99 .welcome
100 .first()
101 .expect("No welcome messages found")
102 .clone();
103
104 // Alice process her own commit
105 println!("\nAlice process her commit to add Bob to the Group");
106 mls_platform_api::mls_receive(
107 &state_global,
108 &alice_id,
109 &MessageOrAck::MlsMessage(commit_output.commit.clone()),
110 )?;
111
112 // List the members of the group
113 let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &alice_id)?;
114 println!("Members (alice, after adding bob): {members:?}");
115
116 // Bob joins
117 println!("\nBob joins the group created by Alice");
118 mls_platform_api::mls_group_join(&state_global, &bob_id, &welcome, None)?;
119
120 // List the members of the group
121 let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &bob_id)?;
122 println!("Members (bob, after joining the group): {members:?}");
123
124 //
125 // Bob propose to add Charlie
126 //
127 println!("\nBob proposes to add Charlie to the Group");
128 let proposal_add_bytes = mls_platform_api::mls_group_propose_add(
129 &mut state_global,
130 &gide.group_id,
131 &bob_id,
132 charlie_kp,
133 )?;
134
135 // Alice receives the Add proposal and commits to it
136 println!("\nAlice commits to the add");
137 let recv_commit_output_5 = mls_platform_api::mls_receive(
138 &state_global,
139 &alice_id,
140 &MessageOrAck::MlsMessage(proposal_add_bytes.clone()),
141 )?;
142
143 let (_, Received::CommitOutput(commit_output_5)) = recv_commit_output_5 else {
144 panic!("Expected a different type.");
145 };
146
147 // Bobs process the commit
148 println!("\nBob receives the commit");
149 mls_platform_api::mls_receive(
150 &state_global,
151 &bob_id,
152 &MessageOrAck::MlsMessage(commit_output_5.commit.clone()),
153 )?;
154
155 // List the members of the group
156 let members_bob = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &bob_id)?;
157 println!("Members (bob, after adding charlie): {members_bob:?}");
158
159 // Alice receives the commit
160 println!("\nAlice receives the commit");
161 mls_platform_api::mls_receive(
162 &state_global,
163 &alice_id,
164 &MessageOrAck::MlsMessage(commit_output_5.commit.clone()),
165 )?;
166
167 // List the members of the group
168 let members_alice =
169 mls_platform_api::mls_group_details(&state_global, &gide.group_id, &alice_id)?;
170 println!("Members (alice, after adding charlie): {members_alice:?}");
171
172 // Extract the welcome from the commit output
173 let welcome_5 = commit_output_5
174 .welcome
175 .first()
176 .expect("No welcome messages found")
177 .clone();
178
179 // Charlie joins
180 println!("\nCharlie joins the group");
181 mls_platform_api::mls_group_join(&state_global, &charlie_id, &welcome_5, None)?;
182
183 // List the members of the group
184 let members_charlie =
185 mls_platform_api::mls_group_details(&state_global, &gide.group_id, &charlie_id)?;
186 println!("Members (charlie, after joining the group): {members_charlie:?}");
187
188 // Test that Alice, Bob and Charlie are in the same group
189 assert!(members_alice == members_bob);
190 assert!(members_bob == members_charlie);
191
192 Ok(())
193 }