diff --git a/firmware/Cargo.toml b/firmware/Cargo.toml index 2aac4df..379685b 100644 --- a/firmware/Cargo.toml +++ b/firmware/Cargo.toml @@ -29,3 +29,6 @@ hex = { workspace = true, optional = true } nom = { workspace = true } secp256k1 = { workspace = true } anyhow = { workspace = true, optional = true } + +[dev-dependencies] +foundation-test-vectors = { workspace = true, features = ["firmware"] } diff --git a/firmware/tests/test-vectors.rs b/firmware/tests/test-vectors.rs new file mode 100644 index 0000000..eccd0bd --- /dev/null +++ b/firmware/tests/test-vectors.rs @@ -0,0 +1,63 @@ +// SPDX-FileCopyrightText: © 2024 Foundation Devices, Inc. +// SPDX-License-Identifier: GPL-3.0-or-later + +use foundation_firmware::{header, VerifyHeaderError}; +use foundation_test_vectors::firmware::{ + INVALID_MAGIC, INVALID_MAX_LENGTH, INVALID_MIN_LENGTH, INVALID_PUBLIC_KEY1, + INVALID_PUBLIC_KEY2, INVALID_TIMESTAMP, VALID_HEADER, +}; +use nom::Finish; + +#[test] +pub fn valid_header() { + let (_, header) = header(VALID_HEADER).finish().unwrap(); + header.verify().unwrap(); +} + +#[test] +pub fn invalid_magic() { + let (_, header) = header(INVALID_MAGIC).finish().unwrap(); + assert_eq!(header.verify(), Err(VerifyHeaderError::UnknownMagic(0))); +} + +#[test] +pub fn invalid_min_length() { + let (_, header) = header(INVALID_MIN_LENGTH).finish().unwrap(); + assert_eq!( + header.verify(), + Err(VerifyHeaderError::FirmwareTooSmall(2047)) + ); +} + +#[test] +pub fn invalid_max_length() { + let (_, header) = header(INVALID_MAX_LENGTH).finish().unwrap(); + assert_eq!( + header.verify(), + Err(VerifyHeaderError::FirmwareTooBig(1834753)) + ); +} + +#[test] +pub fn invalid_public_key1() { + let (_, header) = header(INVALID_PUBLIC_KEY1).finish().unwrap(); + assert_eq!( + header.verify(), + Err(VerifyHeaderError::InvalidPublicKey1Index(5)) + ); +} + +#[test] +pub fn invalid_public_key2() { + let (_, header) = header(INVALID_PUBLIC_KEY2).finish().unwrap(); + assert_eq!( + header.verify(), + Err(VerifyHeaderError::InvalidPublicKey2Index(5)) + ); +} + +#[test] +pub fn invalid_timestamp() { + let (_, header) = header(INVALID_TIMESTAMP).finish().unwrap(); + assert_eq!(header.verify(), Err(VerifyHeaderError::InvalidTimestamp)); +}