1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
//! This module tests the classification of remove operations with RemoveOperation
use super::utils::{generate_credential_bundle, generate_key_package_bundle};
use crate::{credentials::*, framing::*, group::*, test_utils::*, *};
use openmls_rust_crypto::OpenMlsRustCrypto;
// Tests the differen variants of the RemoveOperation enum.
#[apply(ciphersuites_and_backends)]
fn test_remove_operation_variants(ciphersuite: Ciphersuite, backend: &impl OpenMlsCryptoProvider) {
// We define two test cases, one where the member is removed by another member
// and one where the member leaves the group on its own
enum TestCase {
Remove,
Leave,
}
for test_case in [TestCase::Remove, TestCase::Leave] {
let group_id = GroupId::from_slice(b"Test Group");
// Generate credential bundles
let alice_credential = generate_credential_bundle(
"Alice".into(),
CredentialType::Basic,
ciphersuite.signature_algorithm(),
backend,
)
.expect("An unexpected error occurred.");
let bob_credential = generate_credential_bundle(
"Bob".into(),
CredentialType::Basic,
ciphersuite.signature_algorithm(),
backend,
)
.expect("An unexpected error occurred.");
let charlie_credential = generate_credential_bundle(
"Charlie".into(),
CredentialType::Basic,
ciphersuite.signature_algorithm(),
backend,
)
.expect("An unexpected error occurred.");
// Generate KeyPackages
let alice_key_package =
generate_key_package_bundle(&[ciphersuite], &alice_credential, vec![], backend)
.expect("An unexpected error occurred.");
let bob_key_package =
generate_key_package_bundle(&[ciphersuite], &bob_credential, vec![], backend)
.expect("An unexpected error occurred.");
let charlie_key_package =
generate_key_package_bundle(&[ciphersuite], &charlie_credential, vec![], backend)
.expect("An unexpected error occurred.");
// Define the MlsGroup configuration
let mls_group_config = MlsGroupConfig::default();
// === Alice creates a group ===
let mut alice_group = MlsGroup::new(
backend,
&mls_group_config,
group_id,
alice_key_package
.hash_ref(backend.crypto())
.expect("Could not hash KeyPackage.")
.as_slice(),
)
.expect("An unexpected error occurred.");
// === Alice adds Bob & Charlie ===
let (_message, welcome) = alice_group
.add_members(backend, &[bob_key_package, charlie_key_package])
.expect("An unexpected error occurred.");
alice_group
.merge_pending_commit()
.expect("error merging pending commit");
let mut bob_group = MlsGroup::new_from_welcome(
backend,
&mls_group_config,
welcome.clone(),
Some(alice_group.export_ratchet_tree()),
)
.expect("Error creating group from Welcome");
let mut charlie_group = MlsGroup::new_from_welcome(
backend,
&mls_group_config,
welcome,
Some(alice_group.export_ratchet_tree()),
)
.expect("Error creating group from Welcome");
// === Remove operation ===
let alice_kpr = *alice_group
.key_package_ref()
.expect("An unexpected error occurred.");
let bob_kpr = *bob_group
.key_package_ref()
.expect("An unexpected error occurred.");
// We differentiate between the two test cases here
let (message, _welcome) = match test_case {
// Alice removes Bob
TestCase::Remove => alice_group
.remove_members(backend, &[bob_kpr])
.expect("Could not remove members."),
// Bob leaves
TestCase::Leave => {
// Bob leaves the group
let message = bob_group
.leave_group(backend)
.expect("Could not leave group.");
// Alice & Charlie store the pending proposal
for group in [&mut alice_group, &mut charlie_group] {
let unverified_message = group
.parse_message(message.clone().into(), backend)
.expect("Could not parse message.");
let processed_message = group
.process_unverified_message(unverified_message, None, backend)
.expect("Could not process unverified message.");
match processed_message {
ProcessedMessage::ProposalMessage(proposal) => {
group.store_pending_proposal(*proposal);
}
_ => unreachable!(),
}
}
// Alice commits to Bob's proposal
alice_group
.commit_to_pending_proposals(backend)
.expect("An unexpected error occurred.")
}
};
// === Remove operation from Alice's perspective ===
let alice_staged_commit = alice_group.pending_commit().expect("No pending commit.");
let remove_proposal = alice_staged_commit
.remove_proposals()
.next()
.expect("An unexpected error occurred.");
let remove_operation = RemoveOperation::new(remove_proposal, &alice_group)
.expect("An unexpected Error occurred.");
match test_case {
TestCase::Remove => {
// We expect this variant, since Alice removed Bob
match remove_operation {
RemoveOperation::WeRemovedThem(removed) => {
// Check that it was indeed Bob who was removed
assert_eq!(removed, bob_kpr);
}
_ => unreachable!(),
}
}
TestCase::Leave => {
// We expect this variant, since Bob left
match remove_operation {
RemoveOperation::TheyLeft(removed) => {
// Check that it was indeed Bob who left
assert_eq!(removed, bob_kpr);
}
_ => unreachable!(),
}
}
}
// === Remove operation from Bob's perspective ===
let unverified_message = bob_group
.parse_message(message.clone().into(), backend)
.expect("Could not parse message.");
let bob_processed_message = bob_group
.process_unverified_message(unverified_message, None, backend)
.expect("Could not process unverified message.");
match bob_processed_message {
ProcessedMessage::StagedCommitMessage(bob_staged_commit) => {
let remove_proposal = bob_staged_commit
.remove_proposals()
.next()
.expect("An unexpected error occurred.");
let remove_operation = RemoveOperation::new(remove_proposal, &bob_group)
.expect("An unexpected Error occurred.");
match test_case {
TestCase::Remove => {
// We expect this variant, since Alice removed Bob
match remove_operation {
RemoveOperation::WeWereRemovedBy(sender) => {
// Make sure Alice is indeed a member
assert!(sender.is_member());
// Check Bob was removed
assert!(bob_staged_commit.self_removed());
match sender {
Sender::Member(member) => {
// Check that it was Alice who removed Bob
assert_eq!(member, alice_kpr);
}
_ => unreachable!(),
}
}
_ => unreachable!(),
}
}
TestCase::Leave => {
// We expect this variant, since Bob left
match remove_operation {
RemoveOperation::WeLeft => {
// Check that Bob is no longer part of the group
assert!(bob_staged_commit.self_removed());
}
_ => unreachable!(),
}
}
}
}
_ => unreachable!(),
}
// === Remove operation from Charlie's perspective ===
let unverified_message = charlie_group
.parse_message(message.into(), backend)
.expect("Could not parse message.");
let charlie_processed_message = charlie_group
.process_unverified_message(unverified_message, None, backend)
.expect("Could not process unverified message.");
match charlie_processed_message {
ProcessedMessage::StagedCommitMessage(charlie_staged_commit) => {
let remove_proposal = charlie_staged_commit
.remove_proposals()
.next()
.expect("An unexpected error occurred.");
let remove_operation = RemoveOperation::new(remove_proposal, &charlie_group)
.expect("An unexpected Error occurred.");
match test_case {
TestCase::Remove => {
// We expect this variant, since Alice removed Bob
match remove_operation {
RemoveOperation::TheyWereRemovedBy((removed, sender)) => {
// Make sure Alice is indeed a member
assert!(sender.is_member());
// Check that it was indeed Bob who was removed
assert_eq!(removed, bob_kpr);
match sender {
Sender::Member(member) => {
// Check that it was Alice who removed Bob
assert_eq!(member, alice_kpr);
}
_ => unreachable!(),
}
}
_ => unreachable!(),
}
}
TestCase::Leave => {
// We expect this variant, since Bob left
match remove_operation {
RemoveOperation::TheyLeft(removed) => {
// Check that it was indeed Bob who left
assert_eq!(removed, bob_kpr);
}
_ => unreachable!(),
}
}
}
}
_ => unreachable!(),
}
}
}