Skip to content

Commit b234176

Browse files
authored
Merge pull request #408 from Superhepper/base-error-from-tss-layer-return-code
Adds tests for `base_error` method.
2 parents 079d6d8 + 0295267 commit b234176

File tree

6 files changed

+212
-18
lines changed

6 files changed

+212
-18
lines changed

tss-esapi/tests/integration_tests/error_tests/return_code_tests/esapi_tests.rs

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ use tss_esapi::{
1313
TSS2_BASE_RC_INCOMPATIBLE_TCTI, TSS2_BASE_RC_INSUFFICIENT_RESPONSE,
1414
TSS2_BASE_RC_MALFORMED_RESPONSE, TSS2_BASE_RC_MEMORY,
1515
TSS2_BASE_RC_MULTIPLE_DECRYPT_SESSIONS, TSS2_BASE_RC_MULTIPLE_ENCRYPT_SESSIONS,
16-
TSS2_BASE_RC_NOT_IMPLEMENTED, TSS2_BASE_RC_NO_DECRYPT_PARAM,
17-
TSS2_BASE_RC_NO_ENCRYPT_PARAM, TSS2_BASE_RC_TRY_AGAIN, TSS2_ESYS_RC_LAYER,
16+
TSS2_BASE_RC_NOT_IMPLEMENTED, TSS2_BASE_RC_NOT_SUPPORTED,
17+
TSS2_BASE_RC_NO_DECRYPT_PARAM, TSS2_BASE_RC_NO_ENCRYPT_PARAM, TSS2_BASE_RC_TRY_AGAIN,
18+
TSS2_ESYS_RC_LAYER,
1819
},
1920
BaseError, SessionType,
2021
},
@@ -113,13 +114,14 @@ fn test_valid_conversions() {
113114
TSS2_BASE_RC_MULTIPLE_ENCRYPT_SESSIONS,
114115
BaseError::MultipleEncryptSessions
115116
);
117+
test_valid_conversion!(TSS2_BASE_RC_NOT_SUPPORTED, BaseError::NotSupported);
116118
}
117119

118120
#[test]
119121
fn test_invalid_conversions() {
120-
let tss_invalid_fapi_rc = TSS2_ESYS_RC_LAYER | TSS2_BASE_RC_BAD_TEMPLATE;
122+
let tss_invalid_esapi_rc = TSS2_ESYS_RC_LAYER | TSS2_BASE_RC_BAD_TEMPLATE;
121123
assert_eq!(
122-
ReturnCode::try_from(tss_invalid_fapi_rc),
124+
ReturnCode::try_from(tss_invalid_esapi_rc),
123125
Err(Error::WrapperError(WrapperErrorKind::InvalidParam)),
124126
"Converting invalid ESAPI layer response code did not produce the expected error"
125127
);
@@ -187,3 +189,41 @@ fn test_esapi_error_from_context_method() {
187189
panic!("Calling 'create_primary' with two encrypt session did not result in an error");
188190
}
189191
}
192+
193+
macro_rules! test_base_error {
194+
(BaseError::$base_error:ident) => {
195+
let esapi_rc = EsapiReturnCode::try_from(BaseError::$base_error).expect(&format!(
196+
"Failed to convert {} into EsapiReturnCode",
197+
std::stringify!(BaseError::$base_error)
198+
));
199+
200+
assert_eq!(
201+
BaseError::$base_error,
202+
esapi_rc.base_error(),
203+
"`base_error` method did not return the expected value."
204+
);
205+
};
206+
}
207+
208+
#[test]
209+
fn test_base_error_method() {
210+
test_base_error!(BaseError::GeneralFailure);
211+
test_base_error!(BaseError::NotImplemented);
212+
test_base_error!(BaseError::BadContext);
213+
test_base_error!(BaseError::AbiMismatch);
214+
test_base_error!(BaseError::BadReference);
215+
test_base_error!(BaseError::BadSequence);
216+
test_base_error!(BaseError::TryAgain);
217+
test_base_error!(BaseError::BadValue);
218+
test_base_error!(BaseError::NoDecryptParam);
219+
test_base_error!(BaseError::NoEncryptParam);
220+
test_base_error!(BaseError::MalformedResponse);
221+
test_base_error!(BaseError::InsufficientResponse);
222+
test_base_error!(BaseError::IncompatibleTcti);
223+
test_base_error!(BaseError::BadTctiStructure);
224+
test_base_error!(BaseError::Memory);
225+
test_base_error!(BaseError::BadTr);
226+
test_base_error!(BaseError::MultipleDecryptSessions);
227+
test_base_error!(BaseError::MultipleEncryptSessions);
228+
test_base_error!(BaseError::NotSupported);
229+
}

tss-esapi/tests/integration_tests/error_tests/return_code_tests/fapi_tests.rs

Lines changed: 70 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ use std::convert::TryFrom;
55
use tss_esapi::{
66
constants::{
77
tss::{
8-
TSS2_BASE_RC_ABI_MISMATCH, TSS2_BASE_RC_AUTHORIZATION_FAILED,
9-
TSS2_BASE_RC_AUTHORIZATION_UNKNOWN, TSS2_BASE_RC_BAD_CONTEXT, TSS2_BASE_RC_BAD_KEY,
10-
TSS2_BASE_RC_BAD_PATH, TSS2_BASE_RC_BAD_REFERENCE, TSS2_BASE_RC_BAD_SEQUENCE,
11-
TSS2_BASE_RC_BAD_TEMPLATE, TSS2_BASE_RC_BAD_VALUE, TSS2_BASE_RC_GENERAL_FAILURE,
12-
TSS2_BASE_RC_HASH_MISMATCH, TSS2_BASE_RC_IO_ERROR, TSS2_BASE_RC_KEY_NOT_DUPLICABLE,
13-
TSS2_BASE_RC_KEY_NOT_FOUND, TSS2_BASE_RC_MEMORY, TSS2_BASE_RC_NAME_ALREADY_EXISTS,
14-
TSS2_BASE_RC_NOT_DELETABLE, TSS2_BASE_RC_NOT_IMPLEMENTED, TSS2_BASE_RC_NO_CERT,
8+
TSS2_BASE_RC_ABI_MISMATCH, TSS2_BASE_RC_ALREADY_PROVISIONED,
9+
TSS2_BASE_RC_AUTHORIZATION_FAILED, TSS2_BASE_RC_AUTHORIZATION_UNKNOWN,
10+
TSS2_BASE_RC_BAD_CONTEXT, TSS2_BASE_RC_BAD_KEY, TSS2_BASE_RC_BAD_PATH,
11+
TSS2_BASE_RC_BAD_REFERENCE, TSS2_BASE_RC_BAD_SEQUENCE, TSS2_BASE_RC_BAD_TEMPLATE,
12+
TSS2_BASE_RC_BAD_VALUE, TSS2_BASE_RC_GENERAL_FAILURE, TSS2_BASE_RC_HASH_MISMATCH,
13+
TSS2_BASE_RC_IO_ERROR, TSS2_BASE_RC_KEY_NOT_DUPLICABLE, TSS2_BASE_RC_KEY_NOT_FOUND,
14+
TSS2_BASE_RC_MEMORY, TSS2_BASE_RC_NAME_ALREADY_EXISTS, TSS2_BASE_RC_NOT_DELETABLE,
15+
TSS2_BASE_RC_NOT_IMPLEMENTED, TSS2_BASE_RC_NOT_PROVISIONED, TSS2_BASE_RC_NO_CERT,
1516
TSS2_BASE_RC_NO_CONFIG, TSS2_BASE_RC_NO_DECRYPT_PARAM, TSS2_BASE_RC_NO_ENCRYPT_PARAM,
1617
TSS2_BASE_RC_NO_HANDLE, TSS2_BASE_RC_NO_PCR, TSS2_BASE_RC_NO_TPM,
1718
TSS2_BASE_RC_NV_NOT_READABLE, TSS2_BASE_RC_NV_NOT_WRITEABLE, TSS2_BASE_RC_NV_TOO_SMALL,
@@ -135,6 +136,11 @@ fn test_valid_conversions() {
135136
test_valid_conversion!(TSS2_BASE_RC_TRY_AGAIN, BaseError::TryAgain);
136137
test_valid_conversion!(TSS2_BASE_RC_BAD_KEY, BaseError::BadKey);
137138
test_valid_conversion!(TSS2_BASE_RC_NO_HANDLE, BaseError::NoHandle);
139+
test_valid_conversion!(TSS2_BASE_RC_NOT_PROVISIONED, BaseError::NotProvisioned);
140+
test_valid_conversion!(
141+
TSS2_BASE_RC_ALREADY_PROVISIONED,
142+
BaseError::AlreadyProvisioned
143+
);
138144
}
139145

140146
#[test]
@@ -143,6 +149,62 @@ fn test_invalid_conversions() {
143149
assert_eq!(
144150
ReturnCode::try_from(tss_invalid_fapi_rc),
145151
Err(Error::WrapperError(WrapperErrorKind::InvalidParam)),
146-
"Converting invalid FAPI layer resposne code did not produce the expected error"
152+
"Converting invalid FAPI layer response code did not produce the expected error"
147153
);
148154
}
155+
156+
macro_rules! test_base_error {
157+
(BaseError::$base_error:ident) => {
158+
let fapi_rc = FapiReturnCode::try_from(BaseError::$base_error).expect(&format!(
159+
"Failed to convert {} into FapiReturnCode",
160+
std::stringify!(BaseError::$base_error)
161+
));
162+
163+
assert_eq!(
164+
BaseError::$base_error,
165+
fapi_rc.base_error(),
166+
"`base_error` method did not return the expected value."
167+
);
168+
};
169+
}
170+
171+
#[test]
172+
fn test_base_error_method() {
173+
test_base_error!(BaseError::GeneralFailure);
174+
test_base_error!(BaseError::NotImplemented);
175+
test_base_error!(BaseError::BadReference);
176+
test_base_error!(BaseError::BadSequence);
177+
test_base_error!(BaseError::IoError);
178+
test_base_error!(BaseError::BadValue);
179+
test_base_error!(BaseError::NoDecryptParam);
180+
test_base_error!(BaseError::NoEncryptParam);
181+
test_base_error!(BaseError::Memory);
182+
test_base_error!(BaseError::BadContext);
183+
test_base_error!(BaseError::NoConfig);
184+
test_base_error!(BaseError::BadPath);
185+
test_base_error!(BaseError::NotDeletable);
186+
test_base_error!(BaseError::PathAlreadyExists);
187+
test_base_error!(BaseError::KeyNotFound);
188+
test_base_error!(BaseError::SignatureVerificationFailed);
189+
test_base_error!(BaseError::HashMismatch);
190+
test_base_error!(BaseError::KeyNotDuplicable);
191+
test_base_error!(BaseError::PathNotFound);
192+
test_base_error!(BaseError::NoCert);
193+
test_base_error!(BaseError::NoPcr);
194+
test_base_error!(BaseError::PcrNotResettable);
195+
test_base_error!(BaseError::BadTemplate);
196+
test_base_error!(BaseError::AuthorizationFailed);
197+
test_base_error!(BaseError::AuthorizationUnknown);
198+
test_base_error!(BaseError::NvNotReadable);
199+
test_base_error!(BaseError::NvTooSmall);
200+
test_base_error!(BaseError::NvNotWriteable);
201+
test_base_error!(BaseError::PolicyUnknown);
202+
test_base_error!(BaseError::NvWrongType);
203+
test_base_error!(BaseError::NameAlreadyExists);
204+
test_base_error!(BaseError::NoTpm);
205+
test_base_error!(BaseError::TryAgain);
206+
test_base_error!(BaseError::BadKey);
207+
test_base_error!(BaseError::NoHandle);
208+
test_base_error!(BaseError::NotProvisioned);
209+
test_base_error!(BaseError::AlreadyProvisioned);
210+
}

tss-esapi/tests/integration_tests/error_tests/return_code_tests/muapi_tests.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,30 @@ fn test_invalid_conversions() {
9090
assert_eq!(
9191
ReturnCode::try_from(tss_invalid_fapi_rc),
9292
Err(Error::WrapperError(WrapperErrorKind::InvalidParam)),
93-
"Converting invalid MUAPI layer resposne code did not produce the expected error"
93+
"Converting invalid MUAPI layer response code did not produce the expected error"
9494
);
9595
}
96+
97+
macro_rules! test_base_error {
98+
(BaseError::$base_error:ident) => {
99+
let muapi_rc = MuapiReturnCode::try_from(BaseError::$base_error).expect(&format!(
100+
"Failed to convert {} into MuapiReturnCode",
101+
std::stringify!(BaseError::$base_error)
102+
));
103+
104+
assert_eq!(
105+
BaseError::$base_error,
106+
muapi_rc.base_error(),
107+
"`base_error` method did not return the expected value."
108+
);
109+
};
110+
}
111+
112+
#[test]
113+
fn test_base_error_method() {
114+
test_base_error!(BaseError::GeneralFailure);
115+
test_base_error!(BaseError::BadReference);
116+
test_base_error!(BaseError::InsufficientBuffer);
117+
test_base_error!(BaseError::BadSize);
118+
test_base_error!(BaseError::BadValue);
119+
}

tss-esapi/tests/integration_tests/error_tests/return_code_tests/resource_manager_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,6 @@ fn test_invalid_conversions() {
172172
assert_eq!(
173173
ReturnCode::try_from(tss_invalid_resmgr_rc),
174174
Err(Error::WrapperError(WrapperErrorKind::InvalidParam)),
175-
"Converting invalid RESMGR layer resposne code did not produce the expected error"
175+
"Converting invalid RESMGR layer response code did not produce the expected error"
176176
);
177177
}

tss-esapi/tests/integration_tests/error_tests/return_code_tests/sapi_tests.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ use tss_esapi::{
99
TSS2_BASE_RC_BAD_TCTI_STRUCTURE, TSS2_BASE_RC_BAD_TEMPLATE, TSS2_BASE_RC_BAD_VALUE,
1010
TSS2_BASE_RC_GENERAL_FAILURE, TSS2_BASE_RC_INCOMPATIBLE_TCTI,
1111
TSS2_BASE_RC_INSUFFICIENT_BUFFER, TSS2_BASE_RC_INSUFFICIENT_CONTEXT,
12-
TSS2_BASE_RC_INSUFFICIENT_RESPONSE, TSS2_BASE_RC_MALFORMED_RESPONSE,
13-
TSS2_BASE_RC_NO_DECRYPT_PARAM, TSS2_BASE_RC_NO_ENCRYPT_PARAM, TSS2_SYS_RC_LAYER,
12+
TSS2_BASE_RC_INSUFFICIENT_RESPONSE, TSS2_BASE_RC_INVALID_SESSIONS,
13+
TSS2_BASE_RC_MALFORMED_RESPONSE, TSS2_BASE_RC_NO_DECRYPT_PARAM,
14+
TSS2_BASE_RC_NO_ENCRYPT_PARAM, TSS2_SYS_RC_LAYER,
1415
},
1516
BaseError,
1617
},
@@ -86,6 +87,7 @@ fn test_valid_conversions() {
8687
);
8788
test_valid_conversion!(TSS2_BASE_RC_BAD_SEQUENCE, BaseError::BadSequence);
8889
test_valid_conversion!(TSS2_BASE_RC_BAD_VALUE, BaseError::BadValue);
90+
test_valid_conversion!(TSS2_BASE_RC_INVALID_SESSIONS, BaseError::InvalidSessions);
8991
test_valid_conversion!(TSS2_BASE_RC_NO_DECRYPT_PARAM, BaseError::NoDecryptParam);
9092
test_valid_conversion!(TSS2_BASE_RC_NO_ENCRYPT_PARAM, BaseError::NoEncryptParam);
9193
test_valid_conversion!(
@@ -110,6 +112,39 @@ fn test_invalid_conversions() {
110112
assert_eq!(
111113
ReturnCode::try_from(tss_invalid_fapi_rc),
112114
Err(Error::WrapperError(WrapperErrorKind::InvalidParam)),
113-
"Converting invalid SAPI layer resposne code did not produce the expected error"
115+
"Converting invalid SAPI layer response code did not produce the expected error"
114116
);
115117
}
118+
119+
macro_rules! test_base_error {
120+
(BaseError::$base_error:ident) => {
121+
let sapi_rc = SapiReturnCode::try_from(BaseError::$base_error).expect(&format!(
122+
"Failed to convert {} into SapiReturnCode",
123+
std::stringify!(BaseError::$base_error)
124+
));
125+
126+
assert_eq!(
127+
BaseError::$base_error,
128+
sapi_rc.base_error(),
129+
"`base_error` method did not return the expected value."
130+
);
131+
};
132+
}
133+
134+
#[test]
135+
fn test_base_error_method() {
136+
test_base_error!(BaseError::GeneralFailure);
137+
test_base_error!(BaseError::AbiMismatch);
138+
test_base_error!(BaseError::BadReference);
139+
test_base_error!(BaseError::InsufficientBuffer);
140+
test_base_error!(BaseError::BadSequence);
141+
test_base_error!(BaseError::BadValue);
142+
test_base_error!(BaseError::InvalidSessions);
143+
test_base_error!(BaseError::NoDecryptParam);
144+
test_base_error!(BaseError::NoEncryptParam);
145+
test_base_error!(BaseError::MalformedResponse);
146+
test_base_error!(BaseError::InsufficientContext);
147+
test_base_error!(BaseError::InsufficientResponse);
148+
test_base_error!(BaseError::IncompatibleTcti);
149+
test_base_error!(BaseError::BadTctiStructure);
150+
}

tss-esapi/tests/integration_tests/error_tests/return_code_tests/tcti_tests.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,39 @@ fn test_invalid_conversions() {
105105
assert_eq!(
106106
ReturnCode::try_from(tss_invalid_fapi_rc),
107107
Err(Error::WrapperError(WrapperErrorKind::InvalidParam)),
108-
"Converting invalid TCTI layer resposne code did not produce the expected error"
108+
"Converting invalid TCTI layer response code did not produce the expected error"
109109
);
110110
}
111+
112+
macro_rules! test_base_error {
113+
(BaseError::$base_error:ident) => {
114+
let tcti_rc = TctiReturnCode::try_from(BaseError::$base_error).expect(&format!(
115+
"Failed to convert {} into TctiReturnCode",
116+
std::stringify!(BaseError::$base_error)
117+
));
118+
119+
assert_eq!(
120+
BaseError::$base_error,
121+
tcti_rc.base_error(),
122+
"`base_error` method did not return the expected value."
123+
);
124+
};
125+
}
126+
127+
#[test]
128+
fn test_base_error_method() {
129+
test_base_error!(BaseError::GeneralFailure);
130+
test_base_error!(BaseError::NotImplemented);
131+
test_base_error!(BaseError::BadContext);
132+
test_base_error!(BaseError::AbiMismatch);
133+
test_base_error!(BaseError::BadReference);
134+
test_base_error!(BaseError::InsufficientBuffer);
135+
test_base_error!(BaseError::BadSequence);
136+
test_base_error!(BaseError::NoConnection);
137+
test_base_error!(BaseError::TryAgain);
138+
test_base_error!(BaseError::IoError);
139+
test_base_error!(BaseError::BadValue);
140+
test_base_error!(BaseError::NotPermitted);
141+
test_base_error!(BaseError::MalformedResponse);
142+
test_base_error!(BaseError::NotSupported);
143+
}

0 commit comments

Comments
 (0)