Module keyring::mock

source ·
Expand description

§Mock credential store

To facilitate testing of clients, this crate provides a Mock credential store that is platform-independent, provides no persistence, and allows the client to specify the return values (including errors) for each call.

To use this credential store instead of the default, make this call during application startup before creating any entries:

set_default_credential_builder(mock::default_credential_builder());

You can then create entries as you usually do, and call their usual methods to set, get, and delete passwords. There is no persistence other than in the entry itself, so getting a password before setting it will always result in a NotFound error.

If you want a method call on an entry to fail in a specific way, you can downcast the entry to a MockCredential and then call set_error with the appropriate error. The next entry method called on the credential will fail with the error you set. The error will then be cleared, so the next call on the mock will operate as usual. Here’s a complete example:

let entry = Entry::new("service", "user").unwrap();
let mock: &MockCredential = entry.get_credential().downcast_ref().unwrap();
mock.set_error(Error::Invalid("mock error".to_string(), "takes precedence".to_string()));
entry.set_password("test").expect_err("error will override");
entry.set_password("test").expect("error has been cleared");

Structs§

Functions§