Debian Codegraph @

third_party/rust/mls-platform-api/tests/clear_pending_proposals.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 and Charlie create signing identity (generate_signature_keypair)
11 // * Alice, Bob and Charlie create credentials (generate_credential_basic)
12 // * Bob and Charlie create key packages (generate_key_package)
13 // * Alice creates a group (group_create)
14 // * Alice proposes to add Bob to the group (group_propose_add)
15 // - Alice decides to clear the proposal
16 // * Alice can propose to add Charlie to the group
17
18 #[test]
19 fn test_clear_pending_proposals() -> 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 let charlie_cred = mls_platform_api::mls_generate_credential_basic("charlie".as_bytes())?;
30
31 println!("\nAlice credential: {}", hex::encode(&alice_cred));
32 println!("Bob credential: {}", hex::encode(&bob_cred));
33 println!("Charlie credential: {}", hex::encode(&charlie_cred));
34
35 // Create signature keypairs and store them in the state
36 let alice_id =
37 mls_platform_api::mls_generate_identity(&state_global, group_config.ciphersuite)?;
38
39 let bob_id = mls_platform_api::mls_generate_identity(&state_global, group_config.ciphersuite)?;
40 let charlie_id =
41 mls_platform_api::mls_generate_identity(&state_global, group_config.ciphersuite)?;
42
43 println!("\nAlice identifier: {}", hex::encode(&alice_id));
44 println!("Bob identifier: {}", hex::encode(&bob_id));
45 println!("Charlie identifier: {}", hex::encode(&charlie_id));
46
47 // Create Key Package for Bob
48 let bob_kp = mls_platform_api::mls_generate_key_package(
49 &state_global,
50 &bob_id,
51 &bob_cred,
52 &Default::default(),
53 )?;
54
55 // Create Key Package for Charlie
56 let charlie_kp = mls_platform_api::mls_generate_key_package(
57 &state_global,
58 &charlie_id,
59 &charlie_cred,
60 &Default::default(),
61 )?;
62
63 // Create a group with Alice
64 let gide = mls_platform_api::mls_group_create(
65 &mut state_global,
66 &alice_id,
67 &alice_cred,
68 None,
69 None,
70 &Default::default(),
71 )?;
72
73 println!("\nGroup created by Alice: {}", hex::encode(&gide.group_id));
74
75 // List the members of the group
76 let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &alice_id)?;
77 println!("Members (alice, before adding bob): {members:?}");
78
79 //
80 // Alice proposes to add Bob to the group
81 //
82 println!("\nAlice proposes to add Bob to the Group");
83 let _commit_output = mls_platform_api::mls_group_propose_add(
84 &mut state_global,
85 &gide.group_id,
86 &alice_id,
87 bob_kp,
88 )?;
89
90 // Check if there's a pending proposal
91 let pending =
92 mls_platform_api::mls_has_pending_proposals(&state_global, &gide.group_id, &alice_id)?;
93 assert!(pending);
94
95 println!("\nAlice proposes to add Charlie to the Group");
96 let _commit_output = mls_platform_api::mls_group_propose_add(
97 &mut state_global,
98 &gide.group_id,
99 &alice_id,
100 charlie_kp.clone(),
101 )?;
102
103 // Check if there's a pending proposal
104 let pending =
105 mls_platform_api::mls_has_pending_proposals(&state_global, &gide.group_id, &alice_id)?;
106 assert!(pending);
107
108 // Try to add Charlie while there's a pending proposal - should fail
109 println!("\nAlice tries to add Charlie while there's a pending proposal");
110 let result = mls_platform_api::mls_group_add(
111 &mut state_global,
112 &gide.group_id,
113 &alice_id,
114 vec![charlie_kp.clone()],
115 );
116
117 // Verify we get an error
118 assert!(result.is_err());
119 println!("Got expected error when trying to add because of pending proposal: {result:?}");
120
121 // Discard the pending proposal
122 println!("\nAlice discards the pending proposal");
123 mls_platform_api::mls_clear_pending_proposals(&mut state_global, &gide.group_id, &alice_id)?;
124
125 // Check if there's a pending proposal again
126 let pending =
127 mls_platform_api::mls_has_pending_proposals(&state_global, &gide.group_id, &alice_id)?;
128 assert!(!pending);
129
130 // Alice can now add Charlie to the group
131 println!("\nAlice adds Charlie to the Group");
132 let commit_output = mls_platform_api::mls_group_add(
133 &mut state_global,
134 &gide.group_id,
135 &alice_id,
136 vec![charlie_kp.clone()],
137 )?;
138
139 // Alice process her own commit
140 println!("\nAlice process her commit to add Bob to the Group");
141 mls_platform_api::mls_receive(
142 &state_global,
143 &alice_id,
144 &MessageOrAck::MlsMessage(commit_output.commit),
145 )?;
146
147 // List the members of the group
148 let members = mls_platform_api::mls_group_details(&state_global, &gide.group_id, &alice_id)?;
149 println!("Members (alice, after adding charlie): {members:?}");
150
151 Ok(())
152 }