Skip to content

Commit da28f83

Browse files
authored
Merge pull request #331 from Superhepper/session-attributes-test-improvement
Adds more session attributes tests.
2 parents 6444109 + 50f213e commit da28f83

File tree

2 files changed

+180
-0
lines changed

2 files changed

+180
-0
lines changed

tss-esapi/tests/integration_tests/attributes_tests/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ mod algorithm_attributes_tests;
44
mod command_code_attributes_tests;
55
mod locality_attributes_tests;
66
mod nv_index_attributes_tests;
7+
mod session_attributes_tests;
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
// Copyright 2022 Contributors to the Parsec project.
2+
// SPDX-License-Identifier: Apache-2.0
3+
use tss_esapi::{
4+
attributes::{SessionAttributes, SessionAttributesBuilder, SessionAttributesMask},
5+
tss2_esys::TPMA_SESSION,
6+
};
7+
8+
macro_rules! test_valid_conversion {
9+
($tpm_value:expr, $method:ident) => {
10+
let tpma_session_attribute: TPMA_SESSION = $tpm_value;
11+
let session_attributes = SessionAttributes::from(tpma_session_attribute);
12+
assert_eq!(
13+
true,
14+
session_attributes.$method(),
15+
"SessionAttributes converted from TPMA_SESSION = {} did not produce the expected result with regard to {}.", tpma_session_attribute, stringify!($method),
16+
);
17+
assert_eq!(
18+
tpma_session_attribute,
19+
session_attributes.into(),
20+
"Converting session attributes with {} set did not convert into the expected TPMA_SESSION value", std::stringify!($method),
21+
);
22+
};
23+
}
24+
25+
macro_rules! test_valid_mask_conversion {
26+
($tpm_value:expr, $attribute:tt) => {
27+
let tpma_session_attribute: TPMA_SESSION = $tpm_value;
28+
let session_attributes_mask = SessionAttributesMask::from(tpma_session_attribute);
29+
assert_eq!(
30+
tpma_session_attribute,
31+
session_attributes_mask.into(),
32+
"Converting session attributes mask with {} set did not convert into the expected TPMA_SESSION value", $attribute,
33+
);
34+
};
35+
}
36+
37+
#[test]
38+
fn test_valid_session_attributes_conversions() {
39+
test_valid_conversion!(
40+
1u8.checked_shl(0)
41+
.expect("Failed to create 'continue session' value"),
42+
continue_session
43+
);
44+
test_valid_conversion!(
45+
1u8.checked_shl(1)
46+
.expect("Failed to create 'audit exclusive' value"),
47+
audit_exclusive
48+
);
49+
test_valid_conversion!(
50+
1u8.checked_shl(2)
51+
.expect("Failed to create 'audit reset' value"),
52+
audit_reset
53+
);
54+
test_valid_conversion!(
55+
1u8.checked_shl(5)
56+
.expect("Failed to create 'decrypt' value"),
57+
decrypt
58+
);
59+
test_valid_conversion!(
60+
1u8.checked_shl(6)
61+
.expect("Failed to create 'encrypt' value"),
62+
encrypt
63+
);
64+
test_valid_conversion!(
65+
1u8.checked_shl(7).expect("Failed to create 'audit' value"),
66+
audit
67+
);
68+
}
69+
70+
#[ignore]
71+
#[test]
72+
fn test_invalid_session_attributes_conversions() {
73+
// bit 3 and 4 are reserved and shall be cleared.
74+
// No error for this is implemented.
75+
// See https://github.com/parallaxsecond/rust-tss-esapi/issues/330
76+
}
77+
78+
#[test]
79+
fn test_valid_session_attributes_mask_conversions() {
80+
test_valid_mask_conversion!(
81+
1u8.checked_shl(0)
82+
.expect("Failed to create 'use continue session' mask value"),
83+
"use_continue_session"
84+
);
85+
test_valid_mask_conversion!(
86+
1u8.checked_shl(1)
87+
.expect("Failed to create 'use audit exclusive' mask value"),
88+
"use_audit_exclusive"
89+
);
90+
test_valid_mask_conversion!(
91+
1u8.checked_shl(2)
92+
.expect("Failed to create 'use audit reset' mask value"),
93+
"use_audit_reset"
94+
);
95+
test_valid_mask_conversion!(
96+
1u8.checked_shl(5)
97+
.expect("Failed to create 'use decrypt' mask value"),
98+
"use_decrypt"
99+
);
100+
test_valid_mask_conversion!(
101+
1u8.checked_shl(6)
102+
.expect("Failed to 'use encrypt' mask value"),
103+
"use encrypt"
104+
);
105+
test_valid_mask_conversion!(
106+
1u8.checked_shl(7)
107+
.expect("Failed to create 'use audit' session value"),
108+
"use audit"
109+
);
110+
}
111+
112+
#[ignore]
113+
#[test]
114+
fn test_invalid_session_attributes_mask_conversions() {
115+
// bit 3 and 4 are reserved and shall be cleared.
116+
// No error for this is implemented.
117+
// See https://github.com/parallaxsecond/rust-tss-esapi/issues/330
118+
}
119+
120+
#[test]
121+
fn test_session_attributes_builder_constructing() {
122+
let _b1 = SessionAttributes::builder();
123+
let _b2 = SessionAttributesMask::builder();
124+
let _b3 = SessionAttributesBuilder::default();
125+
let _b4 = SessionAttributesBuilder::new();
126+
}
127+
128+
#[test]
129+
fn test_builder_from_session_attributes() {
130+
let (attributes, mask) = SessionAttributes::builder().build();
131+
assert_eq!(SessionAttributes::from(0), attributes, "Building session attributes without anything set using SessionAttributes::builder() did not produce expected result");
132+
assert_eq!(SessionAttributesMask::from(0), mask, "Building sesssion attributes mask without anything set using SessionAttributes::builder() did not produce expected result")
133+
}
134+
135+
#[test]
136+
fn test_builder_from_session_attributes_mask() {
137+
let (attributes, mask) = SessionAttributesMask::builder().build();
138+
assert_eq!(SessionAttributes::from(0), attributes, "Building session attributes without anything set using SessionAttributesMask::builder() did not produce expected result");
139+
assert_eq!(SessionAttributesMask::from(0), mask, "Building sesssion attributes mask without anything set using SessionAttributesMask::builder() did not produce expected result")
140+
}
141+
142+
#[test]
143+
fn test_builder_from_session_attributes_builder_default() {
144+
let (attributes, mask) = SessionAttributesBuilder::default().build();
145+
assert_eq!(SessionAttributes::from(0), attributes, "Building session attributes without anything set using SessionAttributesBuilder::default() did not produce expected result");
146+
assert_eq!(SessionAttributesMask::from(0), mask, "Building sesssion attributes mask without anything set using SessionAttributesBuilder::default() did not produce expected result")
147+
}
148+
149+
#[test]
150+
fn test_builder_from_session_attributes_builder_new() {
151+
let (attributes, mask) = SessionAttributesBuilder::new().build();
152+
assert_eq!(SessionAttributes::from(0), attributes, "Building session attributes without anything set using SessionAttributesBuilder::new() did not produce expected result");
153+
assert_eq!(SessionAttributesMask::from(0), mask, "Building sesssion attributes mask without anything set using SessionAttributesBuilder::new() did not produce expected result")
154+
}
155+
156+
#[test]
157+
fn test_session_attributes_builder() {
158+
let expected_session_attributes = SessionAttributes::from(0b11100111u8);
159+
let expected_session_attributes_mask = SessionAttributesMask::from(0b11100111u8);
160+
161+
let (actual_session_attributes, actual_session_attributes_mask) =
162+
SessionAttributesBuilder::new()
163+
.with_continue_session(true)
164+
.with_audit_exclusive(true)
165+
.with_audit_reset(true)
166+
.with_decrypt(true)
167+
.with_encrypt(true)
168+
.with_audit(true)
169+
.build();
170+
171+
assert_eq!(
172+
expected_session_attributes, actual_session_attributes,
173+
"SessionAttributes builder did not produce the expected session attributes value"
174+
);
175+
assert_eq!(
176+
expected_session_attributes_mask, actual_session_attributes_mask,
177+
"SessionAttributes builder did not produce the expected session attributes mask value"
178+
)
179+
}

0 commit comments

Comments
 (0)