Skip to content

Commit

Permalink
Add reflexive test
Browse files Browse the repository at this point in the history
  • Loading branch information
Firstyear committed Dec 14, 2023
1 parent ea4b3da commit fae4a03
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 22 deletions.
1 change: 1 addition & 0 deletions tss-esapi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ paste = "1.0.14"
[dev-dependencies]
env_logger = "0.9.0"
sha2 = "0.10.1"
serde_json = "^1.0.108"

[build-dependencies]
semver = "1.0.7"
Expand Down
18 changes: 7 additions & 11 deletions tss-esapi/src/structures/buffers/private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
// SPDX-License-Identifier: Apache-2.0

use crate::traits::impl_mu_standard;
use tss_esapi_sys::_PRIVATE;
use crate::traits::{Marshall, UnMarshall};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use tss_esapi_sys::_PRIVATE;

buffer_type!(Private, ::std::mem::size_of::<_PRIVATE>(), TPM2B_PRIVATE);

Expand All @@ -16,10 +17,9 @@ impl Serialize for Private {
where
S: Serializer,
{
let bytes = self.marshall()
.map_err(|err| {
serde::ser::Error::custom(err)
})?;
let bytes = self
.marshall()
.map_err(|err| serde::ser::Error::custom(err))?;
serializer.serialize_bytes(&bytes)
}
}
Expand All @@ -31,11 +31,7 @@ impl<'de> Deserialize<'de> for Private {
where
D: Deserializer<'de>,
{
let bytes = <&[u8]>::deserialize(deserializer)?;
Self::unmarshall(bytes)
.map_err(|err| {
serde::de::Error::custom(err)
})
let bytes = <Vec<u8>>::deserialize(deserializer)?;
Self::unmarshall(&bytes).map_err(|err| serde::de::Error::custom(err))
}
}

18 changes: 7 additions & 11 deletions tss-esapi/src/structures/tagged/public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
attributes::ObjectAttributes,
interface_types::algorithm::{HashingAlgorithm, PublicAlgorithm},
structures::{Digest, EccPoint, PublicKeyRsa, SymmetricCipherParameters},
traits::{impl_mu_standard, Marshall},
traits::{impl_mu_standard, Marshall, UnMarshall},
tss2_esys::{TPM2B_PUBLIC, TPMT_PUBLIC},
Error, Result, ReturnCode, WrapperErrorKind,
};
Expand All @@ -18,9 +18,9 @@ use keyed_hash::PublicKeyedHashParameters;
use rsa::PublicRsaParameters;

use log::error;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::convert::{TryFrom, TryInto};
use tss_esapi_sys::{TPMU_PUBLIC_ID, TPMU_PUBLIC_PARMS};
use serde::{Deserialize, Deserializer, Serialize, Serializer};

/// A builder for the [Public] type.
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -501,10 +501,9 @@ impl Serialize for Public {
where
S: Serializer,
{
let bytes = self.marshall()
.map_err(|err| {
serde::ser::Error::custom(err)
})?;
let bytes = self
.marshall()
.map_err(|err| serde::ser::Error::custom(err))?;
serializer.serialize_bytes(&bytes)
}
}
Expand All @@ -516,11 +515,8 @@ impl<'de> Deserialize<'de> for Public {
where
D: Deserializer<'de>,
{
let bytes = <&[u8]>::deserialize(deserializer)?;
Self::unmarshall(bytes)
.map_err(|err| {
serde::de::Error::custom(err)
})
let bytes = <Vec<u8>>::deserialize(deserializer)?;
Self::unmarshall(&bytes).map_err(|err| serde::de::Error::custom(err))
}
}

Expand Down
2 changes: 2 additions & 0 deletions tss-esapi/tests/integration_tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ use tss_esapi::{
};

mod marshall;
mod serde;
mod tpm2b_types_equality_checks;
mod tpma_types_equality_checks;
mod tpml_types_equality_checks;
mod tpms_types_equality_checks;
mod tpmt_types_equality_checks;
pub use self::serde::*;
pub use marshall::*;
pub use tpm2b_types_equality_checks::*;
pub use tpma_types_equality_checks::*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ fn marshall_unmarshall() {
crate::common::check_marshall_unmarshall(&private);
}

#[test]
fn serialise_deserialise() {
crate::common::check_serialise_deserialise(&Private::default());
let private = Private::try_from([0xff; 100].to_vec()).unwrap();
crate::common::check_serialise_deserialise(&private);
}

#[test]
fn marshall_unmarshall_offset() {
crate::common::check_marshall_unmarshall_offset(&Private::default());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ fn marshall_unmarshall() {
.for_each(crate::common::check_marshall_unmarshall);
}

#[test]
fn serialise_deserialise() {
crate::common::publics()
.iter()
.for_each(crate::common::check_serialise_deserialise);
}

#[test]
fn tpm2b_conversion() {
crate::common::publics().iter().for_each(|public| {
Expand Down

0 comments on commit fae4a03

Please sign in to comment.