From 3ae816ed62517a864b87f4933154bb87f9c71c1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Mon, 19 Feb 2024 14:29:17 +0100 Subject: [PATCH] Use migration testing from trussed-staging --- Cargo.toml | 3 +- src/state/migration.rs | 84 ++++-------------------------------------- 2 files changed, 10 insertions(+), 77 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e4241e9..225d3e4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -53,6 +53,7 @@ hex-literal = "0.4.1" rand = "0.8.4" trussed = { version = "0.1", features = ["virt"] } trussed-usbip = { version = "0.0.1", default-features = false, features = ["ctaphid"] } +trussed-staging = { version = "0.1.0", features = ["migration-tests"] } usbd-ctaphid = "0.1.0" [package.metadata.docs.rs] @@ -65,7 +66,7 @@ ctaphid-dispatch = { git = "https://github.com/trussed-dev/ctaphid-dispatch.git" apdu-dispatch = { git = "https://github.com/trussed-dev/apdu-dispatch.git", rev = "915fc237103fcecc29d0f0b73391f19abf6576de" } trussed = { git = "https://github.com/nitrokey/trussed.git", rev = "907805eb84c8eb34ed99c922b24433602440ff9f" } littlefs2 = { git = "https://github.com/sosthene-nitrokey/littlefs2.git", rev = "99b1a9832c46c9097e73ca1fa43e119026e2068f" } -trussed-staging = { git = "https://github.com/trussed-dev/trussed-staging", rev = "f6c93e7b50813289f48cb659b39eaa3e7d274dcd" } +trussed-staging = { git = "https://github.com/trussed-dev/trussed-staging", rev = "93801d3b4aefe469806a213e7a49aa540059abb1" } serde-indexed = { git = "https://github.com/sosthene-nitrokey/serde-indexed.git", rev = "5005d23cb4ee8622e62188ea0f9466146f851f0d" } trussed-usbip = { git = "https://github.com/Nitrokey/pc-usbip-runner.git", tag = "v0.0.1-nitrokey.1" } usbd-ctaphid = { git = "https://github.com/Nitrokey/usbd-ctaphid.git", tag = "v0.1.0-nitrokey.2" } diff --git a/src/state/migration.rs b/src/state/migration.rs index c473045..95cc1a6 100644 --- a/src/state/migration.rs +++ b/src/state/migration.rs @@ -52,63 +52,10 @@ fn migrate_rp_dir( #[allow(clippy::unwrap_used)] #[cfg(test)] mod tests { - use littlefs2::{fs::Filesystem, ram_storage}; + use trussed_staging::manage::test_utils::{test_migration_one, FsValues}; use super::*; - type Result = core::result::Result; - - ram_storage!( - name=NoBackendStorage, - backend=RamDirect, - trait=littlefs2::driver::Storage, - erase_value=0xff, - read_size=16, - write_size=16, - cache_size_ty=littlefs2::consts::U512, - block_size=512, - block_count=128, - lookahead_size_ty=littlefs2::consts::U8, - filename_max_plus_one_ty=littlefs2::consts::U256, - path_max_plus_one_ty=littlefs2::consts::U256, - result=Result, - ); - - enum FsValues { - Dir(&'static [(&'static Path, FsValues)]), - File(usize), - } - - fn test_fs_equality(fs: &dyn DynFilesystem, value: &FsValues, path: &Path) { - match value { - FsValues::Dir(d) => { - let mut expected_iter = d.iter(); - fs.read_dir_and_then(path, &mut |dir| { - // skip . and .. - dir.next().unwrap().unwrap(); - dir.next().unwrap().unwrap(); - for (expected_path, expected_values) in expected_iter.by_ref() { - let entry = dir.next().unwrap().unwrap(); - assert_eq!(entry.file_name(), *expected_path); - test_fs_equality(fs, expected_values, &path.join(expected_path)); - } - assert!(dir.next().is_none()); - Ok(()) - }) - .unwrap(); - } - FsValues::File(f_data_len) => { - fs.open_file_and_then(path, &mut |f| { - let mut buf = [0; 512]; - let data = f.read(&mut buf).unwrap(); - assert_eq!(data, *f_data_len); - Ok(()) - }) - .unwrap(); - } - } - } - const FIDO_DAT_DIR_BEFORE: FsValues = FsValues::Dir(&[ (path!("persistent-state.cbor"), FsValues::File(137)), ( @@ -184,10 +131,6 @@ mod tests { #[test] fn migration_no_auth() { - let mut storage = RamDirect { - buf: *include_bytes!("../../test_fs/fido-trussed.lfs"), - }; - const TEST_VALUES_BEFORE: FsValues = FsValues::Dir(&[ ( path!("fido"), @@ -222,21 +165,13 @@ mod tests { ), ]); - Filesystem::mount_and_then(&mut NoBackendStorage::new(&mut storage), |fs| { - test_fs_equality(fs, &TEST_VALUES_BEFORE, path!("/")); - migrate(fs, path!("fido/dat")).unwrap(); - test_fs_equality(fs, &TEST_VALUES_AFTER, path!("/")); - Ok(()) - }) - .unwrap(); + test_migration_one(&TEST_VALUES_BEFORE, &TEST_VALUES_AFTER, |fs| { + migrate(fs, path!("fido/dat")) + }); } #[test] fn migration_auth() { - let mut storage = RamDirect { - buf: *include_bytes!("../../test_fs/fido-trussed-auth.lfs"), - }; - const AUTH_SECRETS_DIR: (&Path, FsValues) = ( path!("secrets"), FsValues::Dir(&[( @@ -292,12 +227,9 @@ mod tests { AUTH_SECRETS_DIR, TRUSSED_DIR, ]); - Filesystem::mount_and_then(&mut NoBackendStorage::new(&mut storage), |fs| { - test_fs_equality(fs, &TEST_BEFORE, path!("/")); - migrate(fs, path!("/fido/dat")).unwrap(); - test_fs_equality(fs, &TEST_AFTER, path!("/")); - Ok(()) - }) - .unwrap(); + + test_migration_one(&TEST_BEFORE, &TEST_AFTER, |fs| { + migrate(fs, path!("fido/dat")) + }); } }