From a360ec9db8a3f4da753aa37e39c9cad153b9dc6b Mon Sep 17 00:00:00 2001 From: Jesper Brynolf Date: Thu, 27 Jul 2023 02:17:20 +0200 Subject: [PATCH] Adds strum as a integration test requirement. -Adds integration-test feature in order to conditionally add dependencies. -Updates the TpmFormatZeroWarning so it is possible to iterate over all the items when creating integration tests. Signed-off-by: Jesper Brynolf --- tss-esapi/Cargo.toml | 3 ++ .../return_code/tpm/format_zero/warning.rs | 1 + tss-esapi/tests/all-fedora.sh | 4 +- tss-esapi/tests/all-ubuntu.sh | 4 +- tss-esapi/tests/coverage.sh | 2 +- .../tpm_format_zero_warning_tests.rs | 42 ++++--------------- tss-esapi/tests/valgrind.sh | 2 +- 7 files changed, 17 insertions(+), 41 deletions(-) diff --git a/tss-esapi/Cargo.toml b/tss-esapi/Cargo.toml index 6c260efe..a00e030a 100644 --- a/tss-esapi/Cargo.toml +++ b/tss-esapi/Cargo.toml @@ -27,6 +27,8 @@ oid = { version = "0.2.1", optional = true } picky-asn1 = { version = "0.3.0", optional = true } picky-asn1-x509 = { version = "0.6.1", optional = true } cfg-if = "1.0.0" +strum = { version = "0.25.0", optional = true } +strum_macros = { version = "0.25.0", optional = true } [dev-dependencies] env_logger = "0.9.0" @@ -39,3 +41,4 @@ semver = "1.0.7" default = ["abstraction"] generate-bindings = ["tss-esapi-sys/generate-bindings"] abstraction = ["oid", "picky-asn1", "picky-asn1-x509"] +integration-tests = ["strum", "strum_macros"] diff --git a/tss-esapi/src/constants/return_code/tpm/format_zero/warning.rs b/tss-esapi/src/constants/return_code/tpm/format_zero/warning.rs index 9ea34c2c..5f6fbacf 100644 --- a/tss-esapi/src/constants/return_code/tpm/format_zero/warning.rs +++ b/tss-esapi/src/constants/return_code/tpm/format_zero/warning.rs @@ -23,6 +23,7 @@ use std::convert::TryFrom; /// These are the values from the specification without /// the indicator that indicates that it is a TPM format /// zero warning (i.e. [TPM2_RC_WARN]). +#[cfg_attr(feature = "integration-tests", derive(strum_macros::EnumIter))] #[derive(FromPrimitive, ToPrimitive, Copy, Clone, Debug, PartialEq, Eq, Hash)] #[repr(u8)] pub enum TpmFormatZeroWarning { diff --git a/tss-esapi/tests/all-fedora.sh b/tss-esapi/tests/all-fedora.sh index 349286cd..01bdba2a 100755 --- a/tss-esapi/tests/all-fedora.sh +++ b/tss-esapi/tests/all-fedora.sh @@ -41,9 +41,9 @@ cargo clippy --all-targets --all-features -- -D clippy::all -D clippy::cargo ################### # Build the crate # ################### -RUST_BACKTRACE=1 cargo build --features generate-bindings +RUST_BACKTRACE=1 cargo build --features "generate-bindings integration-tests" ################# # Run the tests # ################# -TEST_TCTI=tabrmd:bus_type=session RUST_BACKTRACE=1 RUST_LOG=info cargo test --features generate-bindings -- --test-threads=1 --nocapture +TEST_TCTI=tabrmd:bus_type=session RUST_BACKTRACE=1 RUST_LOG=info cargo test --features "generate-bindings integration-tests" -- --test-threads=1 --nocapture diff --git a/tss-esapi/tests/all-ubuntu.sh b/tss-esapi/tests/all-ubuntu.sh index 37a4716d..b77fecec 100755 --- a/tss-esapi/tests/all-ubuntu.sh +++ b/tss-esapi/tests/all-ubuntu.sh @@ -21,9 +21,9 @@ fi # Generate bindings for non-"standard" versions # ################################################# if [[ "$TPM2_TSS_VERSION" != "2.3.3" ]]; then - FEATURES="--features=generate-bindings" + FEATURES="--features=\"generate-bindings integration-tests\"" else - FEATURES="" + FEATURES="--features=integration-tests" fi ################################# diff --git a/tss-esapi/tests/coverage.sh b/tss-esapi/tests/coverage.sh index 8147ce82..f800510e 100755 --- a/tss-esapi/tests/coverage.sh +++ b/tss-esapi/tests/coverage.sh @@ -16,4 +16,4 @@ tpm2_startup -c -T mssim # Install and run tarpaulin # ############################# cargo install cargo-tarpaulin -cargo tarpaulin --tests --out Xml --exclude-files="tests/*,../*" -- --test-threads=1 --nocapture +cargo tarpaulin --features integration-tests --tests --out Xml --exclude-files="tests/*,../*" -- --test-threads=1 --nocapture diff --git a/tss-esapi/tests/integration_tests/error_tests/return_code_tests/tpm_tests/tpm_format_zero_tests/tpm_format_zero_warning_tests.rs b/tss-esapi/tests/integration_tests/error_tests/return_code_tests/tpm_tests/tpm_format_zero_tests/tpm_format_zero_warning_tests.rs index 5f069878..56a55786 100644 --- a/tss-esapi/tests/integration_tests/error_tests/return_code_tests/tpm_tests/tpm_format_zero_tests/tpm_format_zero_warning_tests.rs +++ b/tss-esapi/tests/integration_tests/error_tests/return_code_tests/tpm_tests/tpm_format_zero_tests/tpm_format_zero_warning_tests.rs @@ -1,6 +1,7 @@ // Copyright 2022 Contributors to the Parsec project. // SPDX-License-Identifier: Apache-2. use std::convert::TryFrom; +use strum::IntoEnumIterator; use tss_esapi::{ constants::{ return_code::TpmFormatZeroWarning, @@ -231,43 +232,14 @@ fn test_display_implementation() { ); } -macro_rules! test_error_number_method { - (TpmFormatZeroWarning::$zero_warning:ident) => { - let zero_warning_rc = TpmFormatZeroWarningResponseCode::from(TpmFormatZeroWarning::$zero_warning); +#[test] +fn test_error_number() { + TpmFormatZeroWarning::iter().for_each(|item| { + let zero_warning_rc = TpmFormatZeroWarningResponseCode::from(item); assert_eq!( - TpmFormatZeroWarning::$zero_warning, + item, zero_warning_rc.error_number(), "The TpmFormatZeroWarningResponseCode `error_number()` method did not return the expected TpmFormatZeroWarning" ); - }; -} - -#[test] -fn test_error_number() { - test_error_number_method!(TpmFormatZeroWarning::ContextGap); - test_error_number_method!(TpmFormatZeroWarning::ObjectMemory); - test_error_number_method!(TpmFormatZeroWarning::SessionMemory); - test_error_number_method!(TpmFormatZeroWarning::ObjectHandles); - test_error_number_method!(TpmFormatZeroWarning::Locality); - test_error_number_method!(TpmFormatZeroWarning::Yielded); - test_error_number_method!(TpmFormatZeroWarning::Canceled); - test_error_number_method!(TpmFormatZeroWarning::Testing); - test_error_number_method!(TpmFormatZeroWarning::ReferenceH0); - test_error_number_method!(TpmFormatZeroWarning::ReferenceH1); - test_error_number_method!(TpmFormatZeroWarning::ReferenceH2); - test_error_number_method!(TpmFormatZeroWarning::ReferenceH3); - test_error_number_method!(TpmFormatZeroWarning::ReferenceH4); - test_error_number_method!(TpmFormatZeroWarning::ReferenceH5); - test_error_number_method!(TpmFormatZeroWarning::ReferenceH6); - test_error_number_method!(TpmFormatZeroWarning::ReferenceS0); - test_error_number_method!(TpmFormatZeroWarning::ReferenceS1); - test_error_number_method!(TpmFormatZeroWarning::ReferenceS2); - test_error_number_method!(TpmFormatZeroWarning::ReferenceS3); - test_error_number_method!(TpmFormatZeroWarning::ReferenceS4); - test_error_number_method!(TpmFormatZeroWarning::ReferenceS5); - test_error_number_method!(TpmFormatZeroWarning::ReferenceS6); - test_error_number_method!(TpmFormatZeroWarning::NvRate); - test_error_number_method!(TpmFormatZeroWarning::Lockout); - test_error_number_method!(TpmFormatZeroWarning::Retry); - test_error_number_method!(TpmFormatZeroWarning::NvUnavailable); + }); } diff --git a/tss-esapi/tests/valgrind.sh b/tss-esapi/tests/valgrind.sh index 2035b5d9..e3fe6db8 100755 --- a/tss-esapi/tests/valgrind.sh +++ b/tss-esapi/tests/valgrind.sh @@ -24,4 +24,4 @@ cargo install cargo-valgrind ################# # Run the tests # ################# -TEST_TCTI=mssim: RUST_BACKTRACE=1 RUST_LOG=info cargo valgrind test -- --test-threads=1 --nocapture +TEST_TCTI=mssim: RUST_BACKTRACE=1 RUST_LOG=info cargo valgrind --features integration-tests test -- --test-threads=1 --nocapture