|
| 1 | +// Copyright 2022 Contributors to the Parsec project. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use std::convert::{TryFrom, TryInto}; |
| 5 | +use tss_esapi::{ |
| 6 | + attributes::CommandCodeAttributes, constants::CommandCode, |
| 7 | + structures::CommandCodeAttributesList, tss2_esys::TPML_CCA, Error, WrapperErrorKind, |
| 8 | +}; |
| 9 | + |
| 10 | +#[test] |
| 11 | +fn test_valid_conversions() { |
| 12 | + let expected_command_code_attributes = vec![ |
| 13 | + CommandCodeAttributes::try_from(u32::from(CommandCode::NvUndefineSpaceSpecial)).expect( |
| 14 | + "Failed to create CommandCodeAttributes using CommandCode::NvUndefineSpaceSpecia", |
| 15 | + ), |
| 16 | + CommandCodeAttributes::try_from(u32::from(CommandCode::EvictControl)) |
| 17 | + .expect("Failed to create CommandCodeAttributes using CommandCode::EvictControl"), |
| 18 | + CommandCodeAttributes::try_from(u32::from(CommandCode::PcrRead)) |
| 19 | + .expect("Failed to create CommandCodeAttributes using CommandCode::PcrRead"), |
| 20 | + ]; |
| 21 | + |
| 22 | + let expected_tpml_cca: TPML_CCA = |
| 23 | + expected_command_code_attributes |
| 24 | + .iter() |
| 25 | + .fold(Default::default(), |mut acc, &v| { |
| 26 | + acc.commandAttributes[acc.count as usize] = v.into(); |
| 27 | + acc.count += 1; |
| 28 | + acc |
| 29 | + }); |
| 30 | + |
| 31 | + let command_code_attributes_list_from_vec: CommandCodeAttributesList = |
| 32 | + expected_command_code_attributes |
| 33 | + .clone() |
| 34 | + .try_into() |
| 35 | + .expect("Failed to convert Vec<CommandCodeAttributes> to CommandCodeAttributesList"); |
| 36 | + |
| 37 | + assert_eq!( |
| 38 | + expected_command_code_attributes.len(), |
| 39 | + command_code_attributes_list_from_vec.len(), |
| 40 | + "Mismatch in 'len()' between the Vec<CommandCodeAttributes> and the CommandCodeAttributesList(from vec)" |
| 41 | + ); |
| 42 | + |
| 43 | + expected_command_code_attributes |
| 44 | + .iter() |
| 45 | + .zip(command_code_attributes_list_from_vec.as_ref()) |
| 46 | + .for_each(|(expected, actual)| { |
| 47 | + assert_eq!(expected, actual, "Mismatch between an expected CommandCodeAttributes in the Vec<CommandCodeAttributes> the actual command code attributes in CommandCodeAttributesList(from vec)"); |
| 48 | + }); |
| 49 | + |
| 50 | + let command_code_attributes_list_from_tss: CommandCodeAttributesList = expected_tpml_cca |
| 51 | + .try_into() |
| 52 | + .expect("Failed to convert expected_tpml_cca into CommandCodeAttributesList"); |
| 53 | + |
| 54 | + assert_eq!( |
| 55 | + expected_command_code_attributes.len(), |
| 56 | + command_code_attributes_list_from_tss.len(), |
| 57 | + "Mismatch in 'len()' between the Vec<CommandCodeAttributes> and the CommandCodeAttributesList(from tss)" |
| 58 | + ); |
| 59 | + |
| 60 | + expected_command_code_attributes |
| 61 | + .iter() |
| 62 | + .zip(command_code_attributes_list_from_tss.as_ref()) |
| 63 | + .for_each(|(expected, actual)| { |
| 64 | + assert_eq!(expected, actual, "Mismatch between an expected CommandCodeAttributes in the Vec<CommandCodeAttributes> the actual tagged property in CommandCodeAttributesList(from tss)"); |
| 65 | + }); |
| 66 | + |
| 67 | + let actual_tpml_cca: TPML_CCA = command_code_attributes_list_from_vec.into(); |
| 68 | + |
| 69 | + crate::common::ensure_tpml_cca_equality(&expected_tpml_cca, &actual_tpml_cca); |
| 70 | +} |
| 71 | + |
| 72 | +#[test] |
| 73 | +fn test_invalid_conversions() { |
| 74 | + assert_eq!( |
| 75 | + Err(Error::WrapperError(WrapperErrorKind::InvalidParam)), |
| 76 | + CommandCodeAttributesList::try_from(vec![CommandCodeAttributes::try_from(u32::from(CommandCode::NvUndefineSpaceSpecial)).expect( |
| 77 | + "Failed to create CommandCodeAttributes using CommandCode::NvUndefineSpaceSpecia", |
| 78 | + ); CommandCodeAttributesList::MAX_SIZE + 1]), |
| 79 | + "Converting a vector with to many elements into a CommandCodeAttributesList did not produce the expected error", |
| 80 | + ); |
| 81 | + |
| 82 | + assert_eq!( |
| 83 | + Err(Error::WrapperError(WrapperErrorKind::InvalidParam)), |
| 84 | + CommandCodeAttributesList::try_from(TPML_CCA { |
| 85 | + count: CommandCodeAttributesList::MAX_SIZE as u32 + 1u32, |
| 86 | + commandAttributes: [Default::default(); 256], |
| 87 | + }), |
| 88 | + "Converting a TPML_CCA with an invalid 'count' value into a CommandCodeAttributesList did not produce the expected error", |
| 89 | + ); |
| 90 | +} |
| 91 | + |
| 92 | +#[test] |
| 93 | +fn test_find() { |
| 94 | + let command_code_attributes_list = CommandCodeAttributesList::try_from(vec![ |
| 95 | + CommandCodeAttributes::try_from(u32::from(CommandCode::NvUndefineSpaceSpecial)).expect( |
| 96 | + "Failed to create CommandCodeAttributes using CommandCode::NvUndefineSpaceSpecial", |
| 97 | + ), |
| 98 | + CommandCodeAttributes::try_from(u32::from(CommandCode::EvictControl)) |
| 99 | + .expect("Failed to create CommandCodeAttributes using CommandCode::EvictControl"), |
| 100 | + CommandCodeAttributes::try_from(u32::from(CommandCode::PcrRead)) |
| 101 | + .expect("Failed to create CommandCodeAttributes using CommandCode::PcrRead"), |
| 102 | + ]) |
| 103 | + .expect("Failed to convert Vec<CommandCodeAttributes> to CommandCodeAttributesList"); |
| 104 | + |
| 105 | + assert_eq!( |
| 106 | + &CommandCodeAttributes::try_from(u32::from(CommandCode::NvUndefineSpaceSpecial)).expect( |
| 107 | + "Failed to create CommandCodeAttributes using CommandCode::NvUndefineSpaceSpecial", |
| 108 | + ), |
| 109 | + command_code_attributes_list |
| 110 | + .find( |
| 111 | + u16::try_from(u32::from(CommandCode::NvUndefineSpaceSpecial)) |
| 112 | + .expect("Failed to convert CommandCode::NvUndefineSpaceSpecial into command code index") |
| 113 | + ) |
| 114 | + .expect("Calling 'find' using CommandCode::NvUndefineSpaceSpecial as command code index returned an unexpected 'None'"), |
| 115 | + "Calling 'find' using CommandCode::NvUndefineSpaceSpecial as command code index did not return the expected CommandCodeAttributes value" |
| 116 | + ); |
| 117 | + |
| 118 | + assert_eq!( |
| 119 | + &CommandCodeAttributes::try_from(u32::from(CommandCode::EvictControl)).expect( |
| 120 | + "Failed to create CommandCodeAttributes using CommandCode::EvictControl", |
| 121 | + ), |
| 122 | + command_code_attributes_list |
| 123 | + .find( |
| 124 | + u16::try_from(u32::from(CommandCode::EvictControl)) |
| 125 | + .expect("Failed to convert CommandCode::EvictControl into command code index") |
| 126 | + ) |
| 127 | + .expect("Calling 'find' using CommandCode::NvUndefineSpaceSpecial as command code index returned an unexpected 'None'"), |
| 128 | + "Calling 'find' using CommandCode::EvictControl as command code index did not return the expected CommandCodeAttributes value" |
| 129 | + ); |
| 130 | + |
| 131 | + assert!( |
| 132 | + command_code_attributes_list |
| 133 | + .find( |
| 134 | + u16::try_from(u32::from(CommandCode::PcrAllocate)) |
| 135 | + .expect("Failed to convert CommandCode::PcrRead into command code index") |
| 136 | + ) |
| 137 | + .is_none(), |
| 138 | + "A value that should not exist was found in the CommandCodeAttributesList" |
| 139 | + ); |
| 140 | +} |
0 commit comments