blob: 84d741fe4d8787bdc4229427cb60253d1291bca5 [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
Seth Moore7ee79f92021-12-07 11:42:49 -080069 /// 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 }
Seth Moore7ee79f92021-12-07 11:42:49 -080073
74 /// Short hand for `Error::Rc(ResponseCode::OUT_OF_KEYS)`
75 pub fn out_of_keys() -> Self {
76 Error::Rc(ResponseCode::OUT_OF_KEYS)
77 }
Janis Danisevskis7d77a762020-07-20 13:03:31 -070078}
79
Janis Danisevskis017d2092020-09-02 10:15:52 -070080/// Helper function to map the binder status we get from calls into KeyMint
81/// to a Keystore Error. We don't create an anyhow error here to make
82/// it easier to evaluate KeyMint errors, which we must do in some cases, e.g.,
83/// when diagnosing authentication requirements, update requirements, and running
84/// out of operation slots.
85pub fn map_km_error<T>(r: BinderResult<T>) -> Result<T, Error> {
86 r.map_err(|s| {
87 match s.exception_code() {
88 ExceptionCode::SERVICE_SPECIFIC => {
89 let se = s.service_specific_error();
90 if se < 0 {
91 // Negative service specific errors are KM error codes.
Janis Danisevskisc5b210b2020-09-11 13:27:37 -070092 Error::Km(ErrorCode(s.service_specific_error()))
Janis Danisevskis017d2092020-09-02 10:15:52 -070093 } else {
94 // Non negative error codes cannot be KM error codes.
95 // So we create an `Error::Binder` variant to preserve
96 // the service specific error code for logging.
97 // `map_or_log_err` will map this on a system error,
98 // but not before logging the details to logcat.
99 Error::Binder(ExceptionCode::SERVICE_SPECIFIC, se)
100 }
101 }
102 // We create `Error::Binder` to preserve the exception code
103 // for logging.
104 // `map_or_log_err` will map this on a system error.
105 e_code => Error::Binder(e_code, 0),
106 }
107 })
108}
109
Max Biresb2e1d032021-02-08 21:35:05 -0800110/// Helper function to map the binder status we get from calls into a RemotelyProvisionedComponent
111/// to a Keystore Error. We don't create an anyhow error here to make
112/// it easier to evaluate service specific errors.
113pub fn map_rem_prov_error<T>(r: BinderResult<T>) -> Result<T, Error> {
114 r.map_err(|s| match s.exception_code() {
115 ExceptionCode::SERVICE_SPECIFIC => Error::Rp(ErrorCode(s.service_specific_error())),
116 e_code => Error::Binder(e_code, 0),
117 })
118}
119
Janis Danisevskisba998992020-12-29 16:08:40 -0800120/// This function is similar to map_km_error only that we don't expect
121/// any KeyMint error codes, we simply preserve the exception code and optional
122/// service specific exception.
123pub fn map_binder_status<T>(r: BinderResult<T>) -> Result<T, Error> {
124 r.map_err(|s| match s.exception_code() {
125 ExceptionCode::SERVICE_SPECIFIC => {
126 let se = s.service_specific_error();
127 Error::Binder(ExceptionCode::SERVICE_SPECIFIC, se)
128 }
129 ExceptionCode::TRANSACTION_FAILED => {
130 let e = s.transaction_error();
131 Error::BinderTransaction(e)
132 }
133 e_code => Error::Binder(e_code, 0),
134 })
135}
136
137/// This function maps a status code onto a Keystore Error.
138pub fn map_binder_status_code<T>(r: Result<T, StatusCode>) -> Result<T, Error> {
139 r.map_err(Error::BinderTransaction)
140}
141
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700142/// This function should be used by Keystore service calls to translate error conditions
Janis Danisevskis8ea5f552020-11-20 11:22:59 -0800143/// into service specific exceptions.
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700144///
Hasini Gunasinghee1d1bbd2021-04-20 18:13:25 +0000145/// All error conditions get logged by this function, except for KEY_NOT_FOUND error.
Janis Danisevskis8ea5f552020-11-20 11:22:59 -0800146///
147/// All `Error::Rc(x)` and `Error::Km(x)` variants get mapped onto a service specific error
148/// code of x. This is possible because KeyMint `ErrorCode` errors are always negative and
149/// `ResponseCode` codes are always positive.
150/// `selinux::Error::PermissionDenied` is mapped on `ResponseCode::PERMISSION_DENIED`.
151///
152/// All non `Error` error conditions and the Error::Binder variant get mapped onto
153/// ResponseCode::SYSTEM_ERROR`.
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700154///
155/// `handle_ok` will be called if `result` is `Ok(value)` where `value` will be passed
Janis Danisevskis8ea5f552020-11-20 11:22:59 -0800156/// as argument to `handle_ok`. `handle_ok` must generate a `BinderResult<T>`, but it
157/// typically returns Ok(value).
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700158///
159/// # Examples
160///
161/// ```
Janis Danisevskis8ea5f552020-11-20 11:22:59 -0800162/// fn loadKey() -> anyhow::Result<Vec<u8>> {
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700163/// if (good_but_auth_required) {
Janis Danisevskis8ea5f552020-11-20 11:22:59 -0800164/// Ok(vec!['k', 'e', 'y'])
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700165/// } else {
Janis Danisevskis8ea5f552020-11-20 11:22:59 -0800166/// Err(anyhow!(Error::Rc(ResponseCode::KEY_NOT_FOUND)))
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700167/// }
168/// }
169///
Janis Danisevskis8ea5f552020-11-20 11:22:59 -0800170/// map_or_log_err(loadKey(), Ok)
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700171/// ```
Janis Danisevskise24f3472020-08-12 17:58:49 -0700172pub fn map_or_log_err<T, U, F>(result: anyhow::Result<U>, handle_ok: F) -> BinderResult<T>
173where
174 F: FnOnce(U) -> BinderResult<T>,
175{
Janis Danisevskis778245c2021-03-04 15:40:23 -0800176 map_err_with(
177 result,
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700178 |e| {
Hasini Gunasinghee1d1bbd2021-04-20 18:13:25 +0000179 // Make the key not found errors silent.
180 if !matches!(
181 e.root_cause().downcast_ref::<Error>(),
182 Some(Error::Rc(ResponseCode::KEY_NOT_FOUND))
183 ) {
184 log::error!("{:?}", e);
185 }
Janis Danisevskis778245c2021-03-04 15:40:23 -0800186 e
187 },
188 handle_ok,
189 )
190}
191
192/// This function behaves similar to map_or_log_error, but it does not log the errors, instead
193/// it calls map_err on the error before mapping it to a binder result allowing callers to
194/// log or transform the error before mapping it.
195pub fn map_err_with<T, U, F1, F2>(
196 result: anyhow::Result<U>,
197 map_err: F1,
198 handle_ok: F2,
199) -> BinderResult<T>
200where
201 F1: FnOnce(anyhow::Error) -> anyhow::Error,
202 F2: FnOnce(U) -> BinderResult<T>,
203{
204 result.map_or_else(
205 |e| {
206 let e = map_err(e);
Hasini Gunasingheb7142972021-02-20 03:11:27 +0000207 let rc = get_error_code(&e);
Janis Danisevskise24f3472020-08-12 17:58:49 -0700208 Err(BinderStatus::new_service_specific_error(rc, None))
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700209 },
210 handle_ok,
211 )
212}
213
Hasini Gunasingheb7142972021-02-20 03:11:27 +0000214/// Returns the error code given a reference to the error
215pub fn get_error_code(e: &anyhow::Error) -> i32 {
216 let root_cause = e.root_cause();
217 match root_cause.downcast_ref::<Error>() {
218 Some(Error::Rc(rcode)) => rcode.0,
219 Some(Error::Km(ec)) => ec.0,
220 Some(Error::Rp(_)) => ResponseCode::SYSTEM_ERROR.0,
221 // If an Error::Binder reaches this stage we report a system error.
222 // The exception code and possible service specific error will be
223 // printed in the error log above.
224 Some(Error::Binder(_, _)) | Some(Error::BinderTransaction(_)) => {
225 ResponseCode::SYSTEM_ERROR.0
226 }
227 None => match root_cause.downcast_ref::<selinux::Error>() {
228 Some(selinux::Error::PermissionDenied) => ResponseCode::PERMISSION_DENIED.0,
229 _ => ResponseCode::SYSTEM_ERROR.0,
230 },
231 }
232}
233
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700234#[cfg(test)]
Hasini Gunasingheaf993662020-07-24 18:40:20 +0000235pub mod tests {
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700236
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700237 use super::*;
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700238 use android_system_keystore2::binder::{
Janis Danisevskis017d2092020-09-02 10:15:52 -0700239 ExceptionCode, Result as BinderResult, Status as BinderStatus,
240 };
Janis Danisevskise24f3472020-08-12 17:58:49 -0700241 use anyhow::{anyhow, Context};
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700242
Janis Danisevskise24f3472020-08-12 17:58:49 -0700243 fn nested_nested_rc(rc: ResponseCode) -> anyhow::Result<()> {
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700244 Err(anyhow!(Error::Rc(rc))).context("nested nested rc")
245 }
246
Janis Danisevskise24f3472020-08-12 17:58:49 -0700247 fn nested_rc(rc: ResponseCode) -> anyhow::Result<()> {
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700248 nested_nested_rc(rc).context("nested rc")
249 }
250
251 fn nested_nested_ec(ec: ErrorCode) -> anyhow::Result<()> {
252 Err(anyhow!(Error::Km(ec))).context("nested nested ec")
253 }
254
255 fn nested_ec(ec: ErrorCode) -> anyhow::Result<()> {
256 nested_nested_ec(ec).context("nested ec")
257 }
258
Janis Danisevskise24f3472020-08-12 17:58:49 -0700259 fn nested_nested_ok(rc: ResponseCode) -> anyhow::Result<ResponseCode> {
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700260 Ok(rc)
261 }
262
Janis Danisevskise24f3472020-08-12 17:58:49 -0700263 fn nested_ok(rc: ResponseCode) -> anyhow::Result<ResponseCode> {
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700264 nested_nested_ok(rc).context("nested ok")
265 }
266
Janis Danisevskisce995432020-07-21 12:22:34 -0700267 fn nested_nested_selinux_perm() -> anyhow::Result<()> {
268 Err(anyhow!(selinux::Error::perm())).context("nested nexted selinux permission denied")
269 }
270
271 fn nested_selinux_perm() -> anyhow::Result<()> {
272 nested_nested_selinux_perm().context("nested selinux permission denied")
273 }
274
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700275 #[derive(Debug, thiserror::Error)]
276 enum TestError {
277 #[error("TestError::Fail")]
278 Fail = 0,
279 }
280
281 fn nested_nested_other_error() -> anyhow::Result<()> {
282 Err(anyhow!(TestError::Fail)).context("nested nested other error")
283 }
284
285 fn nested_other_error() -> anyhow::Result<()> {
286 nested_nested_other_error().context("nested other error")
287 }
288
Janis Danisevskis017d2092020-09-02 10:15:52 -0700289 fn binder_sse_error(sse: i32) -> BinderResult<()> {
290 Err(BinderStatus::new_service_specific_error(sse, None))
291 }
292
293 fn binder_exception(ex: ExceptionCode) -> BinderResult<()> {
294 Err(BinderStatus::new_exception(ex, None))
295 }
296
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700297 #[test]
298 fn keystore_error_test() -> anyhow::Result<(), String> {
299 android_logger::init_once(
300 android_logger::Config::default()
301 .with_tag("keystore_error_tests")
302 .with_min_level(log::Level::Debug),
303 );
Janis Danisevskise24f3472020-08-12 17:58:49 -0700304 // All Error::Rc(x) get mapped on a service specific error
305 // code of x.
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700306 for rc in ResponseCode::LOCKED.0..ResponseCode::BACKEND_BUSY.0 {
Janis Danisevskise24f3472020-08-12 17:58:49 -0700307 assert_eq!(
308 Result::<(), i32>::Err(rc),
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700309 map_or_log_err(nested_rc(ResponseCode(rc)), |_| Err(BinderStatus::ok()))
Janis Danisevskise24f3472020-08-12 17:58:49 -0700310 .map_err(|s| s.service_specific_error())
311 );
312 }
313
Janis Danisevskis017d2092020-09-02 10:15:52 -0700314 // All Keystore Error::Km(x) get mapped on a service
Janis Danisevskise24f3472020-08-12 17:58:49 -0700315 // specific error of x.
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700316 for ec in ErrorCode::UNKNOWN_ERROR.0..ErrorCode::ROOT_OF_TRUST_ALREADY_SET.0 {
Janis Danisevskise24f3472020-08-12 17:58:49 -0700317 assert_eq!(
318 Result::<(), i32>::Err(ec),
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700319 map_or_log_err(nested_ec(ErrorCode(ec)), |_| Err(BinderStatus::ok()))
Janis Danisevskise24f3472020-08-12 17:58:49 -0700320 .map_err(|s| s.service_specific_error())
321 );
322 }
323
Janis Danisevskis017d2092020-09-02 10:15:52 -0700324 // All Keymint errors x received through a Binder Result get mapped on
325 // a service specific error of x.
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700326 for ec in ErrorCode::UNKNOWN_ERROR.0..ErrorCode::ROOT_OF_TRUST_ALREADY_SET.0 {
Janis Danisevskis017d2092020-09-02 10:15:52 -0700327 assert_eq!(
328 Result::<(), i32>::Err(ec),
329 map_or_log_err(
330 map_km_error(binder_sse_error(ec))
331 .with_context(|| format!("Km error code: {}.", ec)),
332 |_| Err(BinderStatus::ok())
333 )
334 .map_err(|s| s.service_specific_error())
335 );
336 }
337
338 // map_km_error creates an Error::Binder variant storing
339 // ExceptionCode::SERVICE_SPECIFIC and the given
340 // service specific error.
341 let sse = map_km_error(binder_sse_error(1));
342 assert_eq!(Err(Error::Binder(ExceptionCode::SERVICE_SPECIFIC, 1)), sse);
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700343 // map_or_log_err then maps it on a service specific error of ResponseCode::SYSTEM_ERROR.
Janis Danisevskis017d2092020-09-02 10:15:52 -0700344 assert_eq!(
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700345 Result::<(), ResponseCode>::Err(ResponseCode::SYSTEM_ERROR),
Janis Danisevskis017d2092020-09-02 10:15:52 -0700346 map_or_log_err(sse.context("Non negative service specific error."), |_| Err(
347 BinderStatus::ok()
348 ))
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700349 .map_err(|s| ResponseCode(s.service_specific_error()))
Janis Danisevskis017d2092020-09-02 10:15:52 -0700350 );
351
352 // map_km_error creates a Error::Binder variant storing the given exception code.
353 let binder_exception = map_km_error(binder_exception(ExceptionCode::TRANSACTION_FAILED));
354 assert_eq!(Err(Error::Binder(ExceptionCode::TRANSACTION_FAILED, 0)), binder_exception);
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700355 // map_or_log_err then maps it on a service specific error of ResponseCode::SYSTEM_ERROR.
Janis Danisevskis017d2092020-09-02 10:15:52 -0700356 assert_eq!(
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700357 Result::<(), ResponseCode>::Err(ResponseCode::SYSTEM_ERROR),
Janis Danisevskis017d2092020-09-02 10:15:52 -0700358 map_or_log_err(binder_exception.context("Binder Exception."), |_| Err(
359 BinderStatus::ok()
360 ))
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700361 .map_err(|s| ResponseCode(s.service_specific_error()))
Janis Danisevskis017d2092020-09-02 10:15:52 -0700362 );
363
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700364 // selinux::Error::Perm() needs to be mapped to ResponseCode::PERMISSION_DENIED
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700365 assert_eq!(
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700366 Result::<(), ResponseCode>::Err(ResponseCode::PERMISSION_DENIED),
Janis Danisevskise24f3472020-08-12 17:58:49 -0700367 map_or_log_err(nested_selinux_perm(), |_| Err(BinderStatus::ok()))
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700368 .map_err(|s| ResponseCode(s.service_specific_error()))
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700369 );
370
Janis Danisevskise24f3472020-08-12 17:58:49 -0700371 // All other errors get mapped on System Error.
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700372 assert_eq!(
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700373 Result::<(), ResponseCode>::Err(ResponseCode::SYSTEM_ERROR),
Janis Danisevskise24f3472020-08-12 17:58:49 -0700374 map_or_log_err(nested_other_error(), |_| Err(BinderStatus::ok()))
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700375 .map_err(|s| ResponseCode(s.service_specific_error()))
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700376 );
377
378 // Result::Ok variants get passed to the ok handler.
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700379 assert_eq!(Ok(ResponseCode::LOCKED), map_or_log_err(nested_ok(ResponseCode::LOCKED), Ok));
380 assert_eq!(
381 Ok(ResponseCode::SYSTEM_ERROR),
382 map_or_log_err(nested_ok(ResponseCode::SYSTEM_ERROR), Ok)
383 );
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700384
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700385 Ok(())
386 }
Hasini Gunasingheaf993662020-07-24 18:40:20 +0000387
388 //Helper function to test whether error cases are handled as expected.
Janis Danisevskise24f3472020-08-12 17:58:49 -0700389 pub fn check_result_contains_error_string<T>(
390 result: anyhow::Result<T>,
391 expected_error_string: &str,
392 ) {
Hasini Gunasingheaf993662020-07-24 18:40:20 +0000393 let error_str = format!(
394 "{:#?}",
395 result.err().unwrap_or_else(|| panic!("Expected the error: {}", expected_error_string))
396 );
397 assert!(
398 error_str.contains(expected_error_string),
399 "The string \"{}\" should contain \"{}\"",
400 error_str,
401 expected_error_string
402 );
403 }
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700404} // mod tests