@@ -9,3 +9,179 @@ mod resource_manager_tpm_tests;
9
9
mod sapi_tests;
10
10
mod tcti_tests;
11
11
mod tpm_tests;
12
+
13
+ use tss_esapi:: {
14
+ constants:: tss:: {
15
+ TPM2_RC_INITIALIZE , TSS2_BASE_RC_BAD_REFERENCE , TSS2_BASE_RC_BAD_SEQUENCE ,
16
+ TSS2_ESYS_RC_LAYER , TSS2_FEATURE_RC_LAYER , TSS2_MU_RC_LAYER , TSS2_RESMGR_RC_LAYER ,
17
+ TSS2_RESMGR_TPM_RC_LAYER , TSS2_SYS_RC_LAYER , TSS2_TCTI_RC_LAYER , TSS2_TPM_RC_LAYER ,
18
+ } ,
19
+ error:: {
20
+ BaseReturnCode , EsapiReturnCode , FapiReturnCode , MuapiReturnCode , ReturnCode ,
21
+ SapiReturnCode , TctiReturnCode , TpmResponseCode ,
22
+ } ,
23
+ } ;
24
+
25
+ use std:: { convert:: TryFrom , error:: Error } ;
26
+
27
+ macro_rules! test_error_trait_impl {
28
+ ( $native_rc: ident, $tss_rc_layer: ident, $tss_rc: ident) => {
29
+ let return_code = ReturnCode :: try_from( $tss_rc_layer | $tss_rc) . unwrap_or_else( |_| {
30
+ panic!(
31
+ "Failed to convert {} error in {} layer return code into a ReturnCode object." ,
32
+ std:: stringify!( $tss_rc) ,
33
+ std:: stringify!( $tss_rc_layer)
34
+ )
35
+ } ) ;
36
+
37
+ let response_code = $native_rc:: try_from( u16 :: try_from( $tss_rc) . unwrap_or_else( |_| {
38
+ panic!(
39
+ "Failed to convert {} into a u16 value." ,
40
+ std:: stringify!( $tss_rc)
41
+ )
42
+ } ) )
43
+ . unwrap_or_else( |_| {
44
+ panic!(
45
+ "Failed to convert {} into a {}." ,
46
+ std:: stringify!( $tss_rc) ,
47
+ std:: any:: type_name:: <$native_rc>( )
48
+ )
49
+ } ) ;
50
+
51
+ assert_eq!(
52
+ format!(
53
+ "{}" ,
54
+ return_code. source( ) . unwrap_or_else( || {
55
+ panic!(
56
+ "`source` function for a {} layer return code should not return None." ,
57
+ std:: stringify!( $tss_rc_layer)
58
+ )
59
+ } )
60
+ ) ,
61
+ format!( "{}" , response_code) ,
62
+ "Tss2_RC with from {} error in {} layer did not convert into the expected type {}" ,
63
+ std:: stringify!( $tss_rc) ,
64
+ std:: stringify!( $tss_rc_layer) ,
65
+ std:: any:: type_name:: <$native_rc>( )
66
+ ) ;
67
+ } ;
68
+ }
69
+
70
+ #[ test]
71
+ fn test_error_trait_implementation ( ) {
72
+ test_error_trait_impl ! ( TpmResponseCode , TSS2_TPM_RC_LAYER , TPM2_RC_INITIALIZE ) ;
73
+ test_error_trait_impl ! (
74
+ FapiReturnCode ,
75
+ TSS2_FEATURE_RC_LAYER ,
76
+ TSS2_BASE_RC_BAD_SEQUENCE
77
+ ) ;
78
+ test_error_trait_impl ! (
79
+ EsapiReturnCode ,
80
+ TSS2_ESYS_RC_LAYER ,
81
+ TSS2_BASE_RC_BAD_SEQUENCE
82
+ ) ;
83
+ test_error_trait_impl ! ( SapiReturnCode , TSS2_SYS_RC_LAYER , TSS2_BASE_RC_BAD_SEQUENCE ) ;
84
+ test_error_trait_impl ! (
85
+ MuapiReturnCode ,
86
+ TSS2_MU_RC_LAYER ,
87
+ TSS2_BASE_RC_BAD_REFERENCE
88
+ ) ;
89
+ test_error_trait_impl ! (
90
+ TctiReturnCode ,
91
+ TSS2_TCTI_RC_LAYER ,
92
+ TSS2_BASE_RC_BAD_REFERENCE
93
+ ) ;
94
+ test_error_trait_impl ! (
95
+ BaseReturnCode ,
96
+ TSS2_RESMGR_RC_LAYER ,
97
+ TSS2_BASE_RC_BAD_SEQUENCE
98
+ ) ;
99
+ test_error_trait_impl ! (
100
+ TpmResponseCode ,
101
+ TSS2_RESMGR_TPM_RC_LAYER ,
102
+ TPM2_RC_INITIALIZE
103
+ ) ;
104
+ }
105
+
106
+ macro_rules! test_display_trait_impl {
107
+ ( $expected_error_message: tt, $native_rc: ident, $tss_rc_layer: ident, $tss_rc: ident) => {
108
+ let return_code = ReturnCode :: try_from( $tss_rc_layer | $tss_rc) . unwrap_or_else( |_| {
109
+ panic!(
110
+ "Failed to convert {} error in {} layer return code into a ReturnCode object." ,
111
+ std:: stringify!( $tss_rc) ,
112
+ std:: stringify!( $tss_rc_layer)
113
+ )
114
+ } ) ;
115
+
116
+ let response_code = $native_rc:: try_from( u16 :: try_from( $tss_rc) . unwrap_or_else( |_| {
117
+ panic!(
118
+ "Failed to convert {} into a u16 value." ,
119
+ std:: stringify!( $tss_rc)
120
+ )
121
+ } ) )
122
+ . unwrap_or_else( |_| {
123
+ panic!(
124
+ "Failed to convert {} into a {}." ,
125
+ std:: stringify!( $tss_rc) ,
126
+ std:: any:: type_name:: <$native_rc>( )
127
+ )
128
+ } ) ;
129
+
130
+ assert_eq!(
131
+ format!( "{} {}" , $expected_error_message, response_code) ,
132
+ format!( "{}" , return_code)
133
+ ) ;
134
+ } ;
135
+ }
136
+
137
+ #[ test]
138
+ fn test_display_trait_implementation ( ) {
139
+ test_display_trait_impl ! (
140
+ "TSS Layer: TPM, Code: 0x00000100, Message:" ,
141
+ TpmResponseCode ,
142
+ TSS2_TPM_RC_LAYER ,
143
+ TPM2_RC_INITIALIZE
144
+ ) ;
145
+ test_display_trait_impl ! (
146
+ "TSS Layer: FAPI, Code: 0x00060007, Message:" ,
147
+ FapiReturnCode ,
148
+ TSS2_FEATURE_RC_LAYER ,
149
+ TSS2_BASE_RC_BAD_SEQUENCE
150
+ ) ;
151
+ test_display_trait_impl ! (
152
+ "TSS Layer: ESAPI, Code: 0x00070007, Message:" ,
153
+ EsapiReturnCode ,
154
+ TSS2_ESYS_RC_LAYER ,
155
+ TSS2_BASE_RC_BAD_SEQUENCE
156
+ ) ;
157
+ test_display_trait_impl ! (
158
+ "TSS Layer: SAPI, Code: 0x00080007, Message:" ,
159
+ SapiReturnCode ,
160
+ TSS2_SYS_RC_LAYER ,
161
+ TSS2_BASE_RC_BAD_SEQUENCE
162
+ ) ;
163
+ test_display_trait_impl ! (
164
+ "TSS Layer: MUAPI, Code: 0x00090005, Message:" ,
165
+ MuapiReturnCode ,
166
+ TSS2_MU_RC_LAYER ,
167
+ TSS2_BASE_RC_BAD_REFERENCE
168
+ ) ;
169
+ test_display_trait_impl ! (
170
+ "TSS Layer: TCTI, Code: 0x000A0005, Message:" ,
171
+ TctiReturnCode ,
172
+ TSS2_TCTI_RC_LAYER ,
173
+ TSS2_BASE_RC_BAD_REFERENCE
174
+ ) ;
175
+ test_display_trait_impl ! (
176
+ "TSS Layer: RESOURCE MANAGER, Code: 0x000B0007, Message:" ,
177
+ BaseReturnCode ,
178
+ TSS2_RESMGR_RC_LAYER ,
179
+ TSS2_BASE_RC_BAD_SEQUENCE
180
+ ) ;
181
+ test_display_trait_impl ! (
182
+ "TSS Layer: TPM RESOURCE MANAGER, Code: 0x000C0100, Message:" ,
183
+ TpmResponseCode ,
184
+ TSS2_RESMGR_TPM_RC_LAYER ,
185
+ TPM2_RC_INITIALIZE
186
+ ) ;
187
+ }
0 commit comments