core_crypto/transaction_context/e2e_identity/
enabled.rs

1//! Utility for clients to get the current state of E2EI when the app resumes
2
3use super::Result;
4use crate::{RecursiveError, transaction_context::TransactionContext};
5use openmls_traits::types::SignatureScheme;
6
7impl TransactionContext {
8    /// See [Client::e2ei_is_enabled]
9    pub async fn e2ei_is_enabled(&self, signature_scheme: SignatureScheme) -> Result<bool> {
10        let client = self
11            .session()
12            .await
13            .map_err(RecursiveError::transaction("getting mls client"))?;
14        client
15            .e2ei_is_enabled(signature_scheme)
16            .await
17            .map_err(RecursiveError::mls_client("is e2ei enabled for client?"))
18            .map_err(Into::into)
19    }
20}
21
22#[cfg(test)]
23mod tests {
24    use super::super::Error;
25    use crate::{RecursiveError, mls, prelude::MlsCredentialType, test_utils::*};
26    use openmls_traits::types::SignatureScheme;
27    use wasm_bindgen_test::*;
28
29    wasm_bindgen_test_configure!(run_in_browser);
30
31    #[apply(all_cred_cipher)]
32    #[wasm_bindgen_test]
33    async fn should_be_false_when_basic_and_true_when_x509(case: TestCase) {
34        run_test_with_client_ids(case.clone(), ["alice"], move |[cc]| {
35            Box::pin(async move {
36                let e2ei_is_enabled = cc.context.e2ei_is_enabled(case.signature_scheme()).await.unwrap();
37                match case.credential_type {
38                    MlsCredentialType::Basic => assert!(!e2ei_is_enabled),
39                    MlsCredentialType::X509 => assert!(e2ei_is_enabled),
40                };
41            })
42        })
43        .await
44    }
45
46    #[apply(all_cred_cipher)]
47    #[wasm_bindgen_test]
48    async fn should_fail_when_no_client(case: TestCase) {
49        run_test_wo_clients(case.clone(), move |cc| {
50            Box::pin(async move {
51                assert!(matches!(
52                    cc.context.e2ei_is_enabled(case.signature_scheme()).await.unwrap_err(),
53                    Error::Recursive(RecursiveError::MlsClient {  source, .. })
54                    if matches!(*source, mls::session::Error::MlsNotInitialized)
55                ));
56            })
57        })
58        .await
59    }
60
61    #[apply(all_cred_cipher)]
62    #[wasm_bindgen_test]
63    async fn should_fail_when_no_credential_for_given_signature_scheme(case: TestCase) {
64        run_test_with_client_ids(case.clone(), ["alice"], move |[cc]| {
65            Box::pin(async move {
66                // just return something different from the signature scheme the MlsCentral was initialized with
67                let other_sc = match case.signature_scheme() {
68                    SignatureScheme::ED25519 => SignatureScheme::ECDSA_SECP256R1_SHA256,
69                    _ => SignatureScheme::ED25519,
70                };
71                assert!(matches!(
72                    cc.context.e2ei_is_enabled(other_sc).await.unwrap_err(),
73                    Error::Recursive(RecursiveError::MlsClient {  source, .. })
74                    if matches!(*source, mls::session::Error::CredentialNotFound(_))
75                ));
76            })
77        })
78        .await
79    }
80}