blob: f969cb6c7db6b2e833d8588a4601533e65ccf32c [file] [log] [blame]
Janis Danisevskis7d77a762020-07-20 13:03:31 -07001// Copyright 2020, The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Keystore error provides convenience methods and types for Keystore error handling.
16//! Clients of Keystore expect one of two error codes, i.e., a Keystore ResponseCode as
17//! defined by the Keystore AIDL interface, or a Keymint ErrorCode as defined by
18//! the Keymint HAL specification.
19//! This crate provides `Error` which can wrap both. It is to be used
20//! internally by Keystore to diagnose error conditions that need to be reported to
21//! the client. To report the error condition to the client the Keystore AIDL
22//! interface defines a wire type `Result` which is distinctly different from Rust's
23//! `enum Result<T,E>`.
24//!
25//! This crate provides the convenience method `map_or_log_err` to convert `anyhow::Error`
26//! into this wire type. In addition to handling the conversion of `Error`
27//! to the `Result` wire type it handles any other error by mapping it to
Janis Danisevskisc5b210b2020-09-11 13:27:37 -070028//! `ResponseCode::SYSTEM_ERROR` and logs any error condition.
Janis Danisevskis7d77a762020-07-20 13:03:31 -070029//!
30//! Keystore functions should use `anyhow::Result` to return error conditions, and
31//! context should be added every time an error is forwarded.
32
Shawn Willden708744a2020-12-11 13:05:27 +000033pub use android_hardware_security_keymint::aidl::android::hardware::security::keymint::ErrorCode::ErrorCode;
Janis Danisevskisc5b210b2020-09-11 13:27:37 -070034pub use android_system_keystore2::aidl::android::system::keystore2::ResponseCode::ResponseCode;
Janis Danisevskisc5b210b2020-09-11 13:27:37 -070035use android_system_keystore2::binder::{
Janis Danisevskisba998992020-12-29 16:08:40 -080036 ExceptionCode, Result as BinderResult, Status as BinderStatus, StatusCode,
Janis Danisevskis017d2092020-09-02 10:15:52 -070037};
Janis Danisevskis2ee014b2021-05-05 14:29:08 -070038use keystore2_selinux as selinux;
39use std::cmp::PartialEq;
Janis Danisevskis7d77a762020-07-20 13:03:31 -070040
41/// This is the main Keystore error type. It wraps the Keystore `ResponseCode` generated
42/// from AIDL in the `Rc` variant and Keymint `ErrorCode` in the Km variant.
43#[derive(Debug, thiserror::Error, PartialEq)]
44pub enum Error {
45 /// Wraps a Keystore `ResponseCode` as defined by the Keystore AIDL interface specification.
46 #[error("Error::Rc({0:?})")]
Janis Danisevskise24f3472020-08-12 17:58:49 -070047 Rc(ResponseCode),
Janis Danisevskis7d77a762020-07-20 13:03:31 -070048 /// Wraps a Keymint `ErrorCode` as defined by the Keymint AIDL interface specification.
49 #[error("Error::Km({0:?})")]
Janis Danisevskise24f3472020-08-12 17:58:49 -070050 Km(ErrorCode),
Janis Danisevskis017d2092020-09-02 10:15:52 -070051 /// Wraps a Binder exception code other than a service specific exception.
52 #[error("Binder exception code {0:?}, {1:?}")]
53 Binder(ExceptionCode, i32),
Janis Danisevskisba998992020-12-29 16:08:40 -080054 /// Wraps a Binder status code.
55 #[error("Binder transaction error {0:?}")]
56 BinderTransaction(StatusCode),
Max Biresb2e1d032021-02-08 21:35:05 -080057 /// Wraps a Remote Provisioning ErrorCode as defined by the IRemotelyProvisionedComponent
58 /// AIDL interface spec.
59 #[error("Error::Rp({0:?})")]
60 Rp(ErrorCode),
Janis Danisevskis7d77a762020-07-20 13:03:31 -070061}
62
63impl Error {
Janis Danisevskisc5b210b2020-09-11 13:27:37 -070064 /// Short hand for `Error::Rc(ResponseCode::SYSTEM_ERROR)`
Janis Danisevskis7d77a762020-07-20 13:03:31 -070065 pub fn sys() -> Self {
Janis Danisevskisc5b210b2020-09-11 13:27:37 -070066 Error::Rc(ResponseCode::SYSTEM_ERROR)
Janis Danisevskis7d77a762020-07-20 13:03:31 -070067 }
68
Janis Danisevskisc5b210b2020-09-11 13:27:37 -070069 /// Short hand for `Error::Rc(ResponseCode::PERMISSION_DENIED`
Janis Danisevskis7d77a762020-07-20 13:03:31 -070070 pub fn perm() -> Self {
Janis Danisevskisc5b210b2020-09-11 13:27:37 -070071 Error::Rc(ResponseCode::PERMISSION_DENIED)
Janis Danisevskis7d77a762020-07-20 13:03:31 -070072 }
73}
74
Janis Danisevskis017d2092020-09-02 10:15:52 -070075/// Helper function to map the binder status we get from calls into KeyMint
76/// to a Keystore Error. We don't create an anyhow error here to make
77/// it easier to evaluate KeyMint errors, which we must do in some cases, e.g.,
78/// when diagnosing authentication requirements, update requirements, and running
79/// out of operation slots.
80pub fn map_km_error<T>(r: BinderResult<T>) -> Result<T, Error> {
81 r.map_err(|s| {
82 match s.exception_code() {
83 ExceptionCode::SERVICE_SPECIFIC => {
84 let se = s.service_specific_error();
85 if se < 0 {
86 // Negative service specific errors are KM error codes.
Janis Danisevskisc5b210b2020-09-11 13:27:37 -070087 Error::Km(ErrorCode(s.service_specific_error()))
Janis Danisevskis017d2092020-09-02 10:15:52 -070088 } else {
89 // Non negative error codes cannot be KM error codes.
90 // So we create an `Error::Binder` variant to preserve
91 // the service specific error code for logging.
92 // `map_or_log_err` will map this on a system error,
93 // but not before logging the details to logcat.
94 Error::Binder(ExceptionCode::SERVICE_SPECIFIC, se)
95 }
96 }
97 // We create `Error::Binder` to preserve the exception code
98 // for logging.
99 // `map_or_log_err` will map this on a system error.
100 e_code => Error::Binder(e_code, 0),
101 }
102 })
103}
104
Max Biresb2e1d032021-02-08 21:35:05 -0800105/// Helper function to map the binder status we get from calls into a RemotelyProvisionedComponent
106/// to a Keystore Error. We don't create an anyhow error here to make
107/// it easier to evaluate service specific errors.
108pub fn map_rem_prov_error<T>(r: BinderResult<T>) -> Result<T, Error> {
109 r.map_err(|s| match s.exception_code() {
110 ExceptionCode::SERVICE_SPECIFIC => Error::Rp(ErrorCode(s.service_specific_error())),
111 e_code => Error::Binder(e_code, 0),
112 })
113}
114
Janis Danisevskisba998992020-12-29 16:08:40 -0800115/// This function is similar to map_km_error only that we don't expect
116/// any KeyMint error codes, we simply preserve the exception code and optional
117/// service specific exception.
118pub fn map_binder_status<T>(r: BinderResult<T>) -> Result<T, Error> {
119 r.map_err(|s| match s.exception_code() {
120 ExceptionCode::SERVICE_SPECIFIC => {
121 let se = s.service_specific_error();
122 Error::Binder(ExceptionCode::SERVICE_SPECIFIC, se)
123 }
124 ExceptionCode::TRANSACTION_FAILED => {
125 let e = s.transaction_error();
126 Error::BinderTransaction(e)
127 }
128 e_code => Error::Binder(e_code, 0),
129 })
130}
131
132/// This function maps a status code onto a Keystore Error.
133pub fn map_binder_status_code<T>(r: Result<T, StatusCode>) -> Result<T, Error> {
134 r.map_err(Error::BinderTransaction)
135}
136
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700137/// This function should be used by Keystore service calls to translate error conditions
Janis Danisevskis8ea5f552020-11-20 11:22:59 -0800138/// into service specific exceptions.
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700139///
Hasini Gunasinghee1d1bbd2021-04-20 18:13:25 +0000140/// All error conditions get logged by this function, except for KEY_NOT_FOUND error.
Janis Danisevskis8ea5f552020-11-20 11:22:59 -0800141///
142/// All `Error::Rc(x)` and `Error::Km(x)` variants get mapped onto a service specific error
143/// code of x. This is possible because KeyMint `ErrorCode` errors are always negative and
144/// `ResponseCode` codes are always positive.
145/// `selinux::Error::PermissionDenied` is mapped on `ResponseCode::PERMISSION_DENIED`.
146///
147/// All non `Error` error conditions and the Error::Binder variant get mapped onto
148/// ResponseCode::SYSTEM_ERROR`.
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700149///
150/// `handle_ok` will be called if `result` is `Ok(value)` where `value` will be passed
Janis Danisevskis8ea5f552020-11-20 11:22:59 -0800151/// as argument to `handle_ok`. `handle_ok` must generate a `BinderResult<T>`, but it
152/// typically returns Ok(value).
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700153///
154/// # Examples
155///
156/// ```
Janis Danisevskis8ea5f552020-11-20 11:22:59 -0800157/// fn loadKey() -> anyhow::Result<Vec<u8>> {
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700158/// if (good_but_auth_required) {
Janis Danisevskis8ea5f552020-11-20 11:22:59 -0800159/// Ok(vec!['k', 'e', 'y'])
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700160/// } else {
Janis Danisevskis8ea5f552020-11-20 11:22:59 -0800161/// Err(anyhow!(Error::Rc(ResponseCode::KEY_NOT_FOUND)))
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700162/// }
163/// }
164///
Janis Danisevskis8ea5f552020-11-20 11:22:59 -0800165/// map_or_log_err(loadKey(), Ok)
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700166/// ```
Janis Danisevskise24f3472020-08-12 17:58:49 -0700167pub fn map_or_log_err<T, U, F>(result: anyhow::Result<U>, handle_ok: F) -> BinderResult<T>
168where
169 F: FnOnce(U) -> BinderResult<T>,
170{
Janis Danisevskis778245c2021-03-04 15:40:23 -0800171 map_err_with(
172 result,
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700173 |e| {
Hasini Gunasinghee1d1bbd2021-04-20 18:13:25 +0000174 // Make the key not found errors silent.
175 if !matches!(
176 e.root_cause().downcast_ref::<Error>(),
177 Some(Error::Rc(ResponseCode::KEY_NOT_FOUND))
178 ) {
179 log::error!("{:?}", e);
180 }
Janis Danisevskis778245c2021-03-04 15:40:23 -0800181 e
182 },
183 handle_ok,
184 )
185}
186
187/// This function behaves similar to map_or_log_error, but it does not log the errors, instead
188/// it calls map_err on the error before mapping it to a binder result allowing callers to
189/// log or transform the error before mapping it.
190pub fn map_err_with<T, U, F1, F2>(
191 result: anyhow::Result<U>,
192 map_err: F1,
193 handle_ok: F2,
194) -> BinderResult<T>
195where
196 F1: FnOnce(anyhow::Error) -> anyhow::Error,
197 F2: FnOnce(U) -> BinderResult<T>,
198{
199 result.map_or_else(
200 |e| {
201 let e = map_err(e);
Hasini Gunasingheb7142972021-02-20 03:11:27 +0000202 let rc = get_error_code(&e);
Janis Danisevskise24f3472020-08-12 17:58:49 -0700203 Err(BinderStatus::new_service_specific_error(rc, None))
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700204 },
205 handle_ok,
206 )
207}
208
Hasini Gunasingheb7142972021-02-20 03:11:27 +0000209/// Returns the error code given a reference to the error
210pub fn get_error_code(e: &anyhow::Error) -> i32 {
211 let root_cause = e.root_cause();
212 match root_cause.downcast_ref::<Error>() {
213 Some(Error::Rc(rcode)) => rcode.0,
214 Some(Error::Km(ec)) => ec.0,
215 Some(Error::Rp(_)) => ResponseCode::SYSTEM_ERROR.0,
216 // If an Error::Binder reaches this stage we report a system error.
217 // The exception code and possible service specific error will be
218 // printed in the error log above.
219 Some(Error::Binder(_, _)) | Some(Error::BinderTransaction(_)) => {
220 ResponseCode::SYSTEM_ERROR.0
221 }
222 None => match root_cause.downcast_ref::<selinux::Error>() {
223 Some(selinux::Error::PermissionDenied) => ResponseCode::PERMISSION_DENIED.0,
224 _ => ResponseCode::SYSTEM_ERROR.0,
225 },
226 }
227}
228
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700229#[cfg(test)]
Hasini Gunasingheaf993662020-07-24 18:40:20 +0000230pub mod tests {
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700231
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700232 use super::*;
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700233 use android_system_keystore2::binder::{
Janis Danisevskis017d2092020-09-02 10:15:52 -0700234 ExceptionCode, Result as BinderResult, Status as BinderStatus,
235 };
Janis Danisevskise24f3472020-08-12 17:58:49 -0700236 use anyhow::{anyhow, Context};
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700237
Janis Danisevskise24f3472020-08-12 17:58:49 -0700238 fn nested_nested_rc(rc: ResponseCode) -> anyhow::Result<()> {
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700239 Err(anyhow!(Error::Rc(rc))).context("nested nested rc")
240 }
241
Janis Danisevskise24f3472020-08-12 17:58:49 -0700242 fn nested_rc(rc: ResponseCode) -> anyhow::Result<()> {
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700243 nested_nested_rc(rc).context("nested rc")
244 }
245
246 fn nested_nested_ec(ec: ErrorCode) -> anyhow::Result<()> {
247 Err(anyhow!(Error::Km(ec))).context("nested nested ec")
248 }
249
250 fn nested_ec(ec: ErrorCode) -> anyhow::Result<()> {
251 nested_nested_ec(ec).context("nested ec")
252 }
253
Janis Danisevskise24f3472020-08-12 17:58:49 -0700254 fn nested_nested_ok(rc: ResponseCode) -> anyhow::Result<ResponseCode> {
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700255 Ok(rc)
256 }
257
Janis Danisevskise24f3472020-08-12 17:58:49 -0700258 fn nested_ok(rc: ResponseCode) -> anyhow::Result<ResponseCode> {
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700259 nested_nested_ok(rc).context("nested ok")
260 }
261
Janis Danisevskisce995432020-07-21 12:22:34 -0700262 fn nested_nested_selinux_perm() -> anyhow::Result<()> {
263 Err(anyhow!(selinux::Error::perm())).context("nested nexted selinux permission denied")
264 }
265
266 fn nested_selinux_perm() -> anyhow::Result<()> {
267 nested_nested_selinux_perm().context("nested selinux permission denied")
268 }
269
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700270 #[derive(Debug, thiserror::Error)]
271 enum TestError {
272 #[error("TestError::Fail")]
273 Fail = 0,
274 }
275
276 fn nested_nested_other_error() -> anyhow::Result<()> {
277 Err(anyhow!(TestError::Fail)).context("nested nested other error")
278 }
279
280 fn nested_other_error() -> anyhow::Result<()> {
281 nested_nested_other_error().context("nested other error")
282 }
283
Janis Danisevskis017d2092020-09-02 10:15:52 -0700284 fn binder_sse_error(sse: i32) -> BinderResult<()> {
285 Err(BinderStatus::new_service_specific_error(sse, None))
286 }
287
288 fn binder_exception(ex: ExceptionCode) -> BinderResult<()> {
289 Err(BinderStatus::new_exception(ex, None))
290 }
291
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700292 #[test]
293 fn keystore_error_test() -> anyhow::Result<(), String> {
294 android_logger::init_once(
295 android_logger::Config::default()
296 .with_tag("keystore_error_tests")
297 .with_min_level(log::Level::Debug),
298 );
Janis Danisevskise24f3472020-08-12 17:58:49 -0700299 // All Error::Rc(x) get mapped on a service specific error
300 // code of x.
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700301 for rc in ResponseCode::LOCKED.0..ResponseCode::BACKEND_BUSY.0 {
Janis Danisevskise24f3472020-08-12 17:58:49 -0700302 assert_eq!(
303 Result::<(), i32>::Err(rc),
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700304 map_or_log_err(nested_rc(ResponseCode(rc)), |_| Err(BinderStatus::ok()))
Janis Danisevskise24f3472020-08-12 17:58:49 -0700305 .map_err(|s| s.service_specific_error())
306 );
307 }
308
Janis Danisevskis017d2092020-09-02 10:15:52 -0700309 // All Keystore Error::Km(x) get mapped on a service
Janis Danisevskise24f3472020-08-12 17:58:49 -0700310 // specific error of x.
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700311 for ec in ErrorCode::UNKNOWN_ERROR.0..ErrorCode::ROOT_OF_TRUST_ALREADY_SET.0 {
Janis Danisevskise24f3472020-08-12 17:58:49 -0700312 assert_eq!(
313 Result::<(), i32>::Err(ec),
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700314 map_or_log_err(nested_ec(ErrorCode(ec)), |_| Err(BinderStatus::ok()))
Janis Danisevskise24f3472020-08-12 17:58:49 -0700315 .map_err(|s| s.service_specific_error())
316 );
317 }
318
Janis Danisevskis017d2092020-09-02 10:15:52 -0700319 // All Keymint errors x received through a Binder Result get mapped on
320 // a service specific error of x.
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700321 for ec in ErrorCode::UNKNOWN_ERROR.0..ErrorCode::ROOT_OF_TRUST_ALREADY_SET.0 {
Janis Danisevskis017d2092020-09-02 10:15:52 -0700322 assert_eq!(
323 Result::<(), i32>::Err(ec),
324 map_or_log_err(
325 map_km_error(binder_sse_error(ec))
326 .with_context(|| format!("Km error code: {}.", ec)),
327 |_| Err(BinderStatus::ok())
328 )
329 .map_err(|s| s.service_specific_error())
330 );
331 }
332
333 // map_km_error creates an Error::Binder variant storing
334 // ExceptionCode::SERVICE_SPECIFIC and the given
335 // service specific error.
336 let sse = map_km_error(binder_sse_error(1));
337 assert_eq!(Err(Error::Binder(ExceptionCode::SERVICE_SPECIFIC, 1)), sse);
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700338 // map_or_log_err then maps it on a service specific error of ResponseCode::SYSTEM_ERROR.
Janis Danisevskis017d2092020-09-02 10:15:52 -0700339 assert_eq!(
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700340 Result::<(), ResponseCode>::Err(ResponseCode::SYSTEM_ERROR),
Janis Danisevskis017d2092020-09-02 10:15:52 -0700341 map_or_log_err(sse.context("Non negative service specific error."), |_| Err(
342 BinderStatus::ok()
343 ))
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700344 .map_err(|s| ResponseCode(s.service_specific_error()))
Janis Danisevskis017d2092020-09-02 10:15:52 -0700345 );
346
347 // map_km_error creates a Error::Binder variant storing the given exception code.
348 let binder_exception = map_km_error(binder_exception(ExceptionCode::TRANSACTION_FAILED));
349 assert_eq!(Err(Error::Binder(ExceptionCode::TRANSACTION_FAILED, 0)), binder_exception);
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700350 // map_or_log_err then maps it on a service specific error of ResponseCode::SYSTEM_ERROR.
Janis Danisevskis017d2092020-09-02 10:15:52 -0700351 assert_eq!(
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700352 Result::<(), ResponseCode>::Err(ResponseCode::SYSTEM_ERROR),
Janis Danisevskis017d2092020-09-02 10:15:52 -0700353 map_or_log_err(binder_exception.context("Binder Exception."), |_| Err(
354 BinderStatus::ok()
355 ))
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700356 .map_err(|s| ResponseCode(s.service_specific_error()))
Janis Danisevskis017d2092020-09-02 10:15:52 -0700357 );
358
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700359 // selinux::Error::Perm() needs to be mapped to ResponseCode::PERMISSION_DENIED
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700360 assert_eq!(
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700361 Result::<(), ResponseCode>::Err(ResponseCode::PERMISSION_DENIED),
Janis Danisevskise24f3472020-08-12 17:58:49 -0700362 map_or_log_err(nested_selinux_perm(), |_| Err(BinderStatus::ok()))
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700363 .map_err(|s| ResponseCode(s.service_specific_error()))
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700364 );
365
Janis Danisevskise24f3472020-08-12 17:58:49 -0700366 // All other errors get mapped on System Error.
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700367 assert_eq!(
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700368 Result::<(), ResponseCode>::Err(ResponseCode::SYSTEM_ERROR),
Janis Danisevskise24f3472020-08-12 17:58:49 -0700369 map_or_log_err(nested_other_error(), |_| Err(BinderStatus::ok()))
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700370 .map_err(|s| ResponseCode(s.service_specific_error()))
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700371 );
372
373 // Result::Ok variants get passed to the ok handler.
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700374 assert_eq!(Ok(ResponseCode::LOCKED), map_or_log_err(nested_ok(ResponseCode::LOCKED), Ok));
375 assert_eq!(
376 Ok(ResponseCode::SYSTEM_ERROR),
377 map_or_log_err(nested_ok(ResponseCode::SYSTEM_ERROR), Ok)
378 );
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700379
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700380 Ok(())
381 }
Hasini Gunasingheaf993662020-07-24 18:40:20 +0000382
383 //Helper function to test whether error cases are handled as expected.
Janis Danisevskise24f3472020-08-12 17:58:49 -0700384 pub fn check_result_contains_error_string<T>(
385 result: anyhow::Result<T>,
386 expected_error_string: &str,
387 ) {
Hasini Gunasingheaf993662020-07-24 18:40:20 +0000388 let error_str = format!(
389 "{:#?}",
390 result.err().unwrap_or_else(|| panic!("Expected the error: {}", expected_error_string))
391 );
392 assert!(
393 error_str.contains(expected_error_string),
394 "The string \"{}\" should contain \"{}\"",
395 error_str,
396 expected_error_string
397 );
398 }
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700399} // mod tests