Debian Codegraph @

third_party/rust/mls-platform-api/tests/group_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
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
17 #[test]
18 fn test_group_add() -> Result<(), PlatformError> {
19 // Default group configuration
20 let group_config = mls_platform_api::GroupConfig::default();
21
22 // Storage states
23 let mut state_global = mls_platform_api::state_access("global.db", &[0u8; 32])?;
24
25 // Credentials
26 let alice_cred = mls_platform_api::mls_generate_credential_basic("alice".as_bytes())?;
27 let bob_cred = mls_platform_api::mls_generate_credential_basic("bob".as_bytes())?;
28
29 println!("\nAlice credential: {}", hex::encode(&alice_cred));
30 println!("Bob credential: {}", hex::encode(&bob_cred));
31
32 // Create signature keypairs and store them in the state
33 let alice_id =
34 mls_platform_api::mls_generate_identity(&state_global, group_config.ciphersuite)?;
35
36 let bob_id = mls_platform_api::mls_generate_identity(&state_global, group_config.ciphersuite)?;
37
38 println!("\nAlice identifier: {}", hex::encode(&alice_id));
39 println!("Bob identifier: {}", hex::encode(&bob_id));
40
41 // Create Key Package for Bob
42 let bob_kp = mls_platform_api::mls_generate_key_package(
43 &state_global,
44 &bob_id,
45 &bob_cred,
46 &Default::default(),
47 )?;
48
49 // Create a group with Alice
50 let gide = mls_platform_api::mls_group_create(
51 &mut state_global,
52 &alice_id,
53 &alice_cred,
54 None,
55 None,
56 &Default::default(),
57 )?;
58
59 println!("\nGroup created by Alice: {}", hex::encode(&gide.group_id));
60
61 // List the members of the group
62 let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &alice_id)?;
63 println!("Members (alice, before adding bob): {members:?}");
64
65 //
66 // Alice adds Bob to a group
67 //
68 println!("\nAlice adds Bob to the Group");
69 let commit_output = mls_platform_api::mls_group_add(
70 &mut state_global,
71 &gide.group_id,
72 &alice_id,
73 vec![bob_kp],
74 )?;
75
76 // Alice process her own commit
77 println!("\nAlice process her commit to add Bob to the Group");
78 mls_platform_api::mls_receive(
79 &state_global,
80 &alice_id,
81 &MessageOrAck::MlsMessage(commit_output.commit),
82 )?;
83
84 // List the members of the group
85 let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &alice_id)?;
86 println!("Members (alice, after adding bob): {members:?}");
87
88 Ok(())
89 }
90
91 #[test]
92 fn test_group_add_multiple() -> Result<(), PlatformError> {
93 // Default group configuration
94 let group_config = mls_platform_api::GroupConfig::default();
95
96 // Storage states
97 let mut state_global = mls_platform_api::state_access("global.db", &[0u8; 32])?;
98
99 // Credentials
100 let alice_cred = mls_platform_api::mls_generate_credential_basic("alice".as_bytes())?;
101 let bob_cred = mls_platform_api::mls_generate_credential_basic("bob".as_bytes())?;
102 let charlie_cred = mls_platform_api::mls_generate_credential_basic("charlie".as_bytes())?;
103
104 println!("\nAlice credential: {}", hex::encode(&alice_cred));
105 println!("Bob credential: {}", hex::encode(&bob_cred));
106 println!("Charlie credential: {}", hex::encode(&charlie_cred));
107
108 // Create signature keypairs and store them in the state
109 let alice_id =
110 mls_platform_api::mls_generate_identity(&state_global, group_config.ciphersuite)?;
111
112 let bob_id = mls_platform_api::mls_generate_identity(&state_global, group_config.ciphersuite)?;
113 let charlie_id =
114 mls_platform_api::mls_generate_identity(&state_global, group_config.ciphersuite)?;
115
116 println!("\nAlice identifier: {}", hex::encode(&alice_id));
117 println!("Bob identifier: {}", hex::encode(&bob_id));
118 println!("Charlie identifier: {}", hex::encode(&charlie_id));
119
120 // Create Key Package for Bob
121 let bob_kp = mls_platform_api::mls_generate_key_package(
122 &state_global,
123 &bob_id,
124 &bob_cred,
125 &Default::default(),
126 )?;
127
128 // Create Key Package for Charlie
129 let charlie_kp = mls_platform_api::mls_generate_key_package(
130 &state_global,
131 &charlie_id,
132 &charlie_cred,
133 &Default::default(),
134 )?;
135
136 // Create a group with Alice
137 let gide = mls_platform_api::mls_group_create(
138 &mut state_global,
139 &alice_id,
140 &alice_cred,
141 None,
142 None,
143 &Default::default(),
144 )?;
145
146 println!("\nGroup created by Alice: {}", hex::encode(&gide.group_id));
147
148 // List the members of the group
149 let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &alice_id)?;
150 println!("Members (alice, before adding bob): {members:?}");
151
152 //
153 // Alice adds Bob to a group
154 //
155 println!("\nAlice adds Bob and Charlie to the Group");
156 let commit_output = mls_platform_api::mls_group_add(
157 &mut state_global,
158 &gide.group_id,
159 &alice_id,
160 vec![bob_kp, charlie_kp],
161 )?;
162
163 // Alice process her own commit
164 println!("\nAlice process her commit to add Bob and Charlie to the Group");
165 mls_platform_api::mls_receive(
166 &state_global,
167 &alice_id,
168 &MessageOrAck::MlsMessage(commit_output.commit),
169 )?;
170
171 // List the members of the group
172 let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &alice_id)?;
173 println!("Members (alice, after adding bob and charlie): {members:?}");
174
175 Ok(())
176 }