use crate::key::MasterKey;
use crate::traits::{AuthBoundKeyStore, Availability};
use crate::{Error, Result};
#[derive(Debug, Clone, Copy)]
pub struct UnsupportedKeyStore {
reason: &'static str,
}
impl UnsupportedKeyStore {
#[must_use]
pub const fn new(reason: &'static str) -> Self {
Self { reason }
}
#[must_use]
pub const fn reason(&self) -> &'static str {
self.reason
}
}
impl AuthBoundKeyStore for UnsupportedKeyStore {
fn availability(&self) -> Availability {
Availability::Unsupported {
reason: self.reason,
}
}
fn create(&self) -> Result<()> {
Err(Error::Unavailable {
reason: self.reason,
})
}
fn wrap(&self, _master: &MasterKey) -> Result<Vec<u8>> {
Err(Error::Unavailable {
reason: self.reason,
})
}
fn unwrap(&self, _blob: &[u8]) -> Result<MasterKey> {
Err(Error::Unavailable {
reason: self.reason,
})
}
fn delete_blob(&self, _blob: &[u8]) -> Result<()> {
Ok(())
}
fn delete(&self) -> Result<()> {
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_operation_reports_unavailable() {
let store = UnsupportedKeyStore::new("no backend in this build");
assert_eq!(
store.availability(),
Availability::Unsupported {
reason: "no backend in this build",
}
);
assert!(matches!(
store.create(),
Err(Error::Unavailable {
reason: "no backend in this build"
})
));
let master = MasterKey::from_array([1_u8; crate::MASTER_KEY_LEN]);
assert!(store.wrap(&master).is_err());
assert!(store.unwrap(&[0_u8; 32]).is_err());
assert!(store.delete().is_ok());
}
}