mod convert;
mod from_buffer;
mod migrate;
mod to_buffer;
use nuts_memory::{MemoryBackend, Settings};
use crate::header::plain_secret::{Magics, PlainRev0, PlainRev1, PlainRev2, PlainSecret};
use crate::migrate::Migration;
const REV0: [u8; 49] = [
0x00, 0x00, 0x12, 0x67, 0x00, 0x00, 0x12, 0x67, 0, 0, 0, 0, 0, 0, 0, 2, 1, 2, 0, 0, 0, 0, 0, 0, 0, 3, 3, 4, 5, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x12, 0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ];
const REV1: [u8; 22] = [
0x00, 0x00, 0x12, 0x67, 0x00, 0x00, 0x12, 0x67, 2, 1, 2, 3, 3, 4, 5, 4, 0, 0, 2, 154, 0, 0, ];
const REV1_NO_TOP_ID: [u8; 18] = [
0x00, 0x00, 0x12, 0x67, 0x00, 0x00, 0x12, 0x67, 2, 1, 2, 3, 3, 4, 5, 0, 0, 0, ];
const REV2_SID: [u8; 22] = [
0x00, 0x00, 0x12, 0x67, 0x00, 0x00, 0x12, 0x67, 2, 1, 2, 3, 3, 4, 5, 0, 0, 0x12, 0x67, 0, 0, 0, ];
const REV2_TOP_ID: [u8; 26] = [
0x00, 0x00, 0x12, 0x67, 0x00, 0x00, 0x12, 0x67, 2, 1, 2, 3, 3, 4, 5, 0, 0, 0, 0, 4, 0, 0, 2, 154, 0, 0, ];
const REV2_NONE: [u8; 22] = [
0x00, 0x00, 0x12, 0x67, 0x00, 0x00, 0x12, 0x67, 2, 1, 2, 3, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, ];
fn rev0() -> PlainRev0<MemoryBackend> {
PlainRev0 {
magics: Magics([4711, 4711]),
key: vec![1, 2].into(),
iv: vec![3, 4, 5].into(),
userdata: vec![0x00, 0x00, 0x12, 0x67].into(),
settings: Settings,
sid: None,
top_id: None,
}
}
fn rev1() -> PlainRev1<MemoryBackend> {
PlainRev1 {
magics: Magics([4711, 4711]),
key: vec![1, 2].into(),
iv: vec![3, 4, 5].into(),
top_id: Some("666".parse().unwrap()),
settings: Settings,
}
}
fn rev1_no_top_id() -> PlainRev1<MemoryBackend> {
PlainRev1 {
top_id: None,
..rev1()
}
}
fn rev2(sid: Option<u32>, top_id: Option<&str>) -> PlainRev2<MemoryBackend> {
PlainRev2 {
magics: Magics([4711, 4711]),
key: vec![1, 2].into(),
iv: vec![3, 4, 5].into(),
sid,
top_id: top_id.map(|id| id.parse().unwrap()),
settings: Settings,
}
}
struct SampleMigration;
impl Migration for SampleMigration {
fn migrate_rev0(&self, userdata: &[u8]) -> Result<(u32, Vec<u8>), String> {
assert_eq!(userdata, [0x00, 0x00, 0x12, 0x67]);
Ok((666, userdata.to_vec()))
}
}
struct ErrMigration;
impl Migration for ErrMigration {
fn migrate_rev0(&self, _userdata: &[u8]) -> Result<(u32, Vec<u8>), String> {
Err("foo".to_string())
}
}
#[test]
fn create_latest() {
let (revision, plain_secret) =
PlainSecret::<MemoryBackend>::create_latest(vec![1].into(), vec![2, 3].into(), Settings)
.unwrap();
let expected = PlainRev2::<MemoryBackend> {
magics: Magics([0x91C0B2CF; 2]),
key: vec![1].into(),
iv: vec![2, 3].into(),
sid: None,
top_id: None,
settings: Settings,
};
assert_eq!(revision, 2);
assert!(matches!(plain_secret, PlainSecret::Rev2(data) if data == expected));
}