Skip to content

Commit

Permalink
add tests for NSSKeyManager
Browse files Browse the repository at this point in the history
TODO: executing those tests with `cargo test --features keydb` fails
because nss will be initialized without profile dir first:
```
thread 'encryption::keydb_test::test_nss_key_manager' panicked at components/logins/src/encryption.rs:401:87:
called `Result::unwrap()` on an `Err` value: NSSInitializationFailed { reason: "NSS could not be initialized: NSS has been already initialized without profile" }
```
  • Loading branch information
jo committed Feb 12, 2025
1 parent e3ac09e commit a0b6eda
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions components/logins/src/encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,34 @@ mod test {
));
}
}

#[cfg(feature = "keydb")]
#[cfg(test)]
mod keydb_test {
use super::*;
use anyhow::Result;
use std::env;

struct TestPrimaryPasswordAuthenticator {}

impl PrimaryPasswordAuthenticator for TestPrimaryPasswordAuthenticator {
fn get_primary_password(&self) -> Result<String, LoginsApiError> {
panic!("no primary password should be used in test")
}
}

#[test]
fn test_nss_key_manager() {
let profile_path = env::current_dir().unwrap();
let primary_password_authenticator = Arc::new(TestPrimaryPasswordAuthenticator {});
let key_manager =
Arc::new(NSSKeyManager::new(profile_path, primary_password_authenticator).unwrap());
let encdec = ManagedEncryptorDecryptor { key_manager };
let cleartext = "secret";
let ciphertext = encdec.encrypt(cleartext.as_bytes().into()).unwrap();
assert_eq!(
encdec.decrypt(ciphertext.clone()).unwrap(),
cleartext.as_bytes()
);
}
}

0 comments on commit a0b6eda

Please sign in to comment.