Skip to content

Commit

Permalink
Use migration testing from trussed-staging
Browse files Browse the repository at this point in the history
  • Loading branch information
sosthene-nitrokey committed Feb 19, 2024
1 parent 3d3b17a commit 3ae816e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 77 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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" }
84 changes: 8 additions & 76 deletions src/state/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, E = littlefs2::io::Error> = core::result::Result<T, E>;

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)),
(
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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(&[(
Expand Down Expand Up @@ -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"))
});
}
}

0 comments on commit 3ae816e

Please sign in to comment.