blob: b60b64f86d676474175bc859e7dbc4aabdef304a [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 Danisevskisea03cff2021-12-16 08:10:17 -080040use std::ffi::CString;
Janis Danisevskis7d77a762020-07-20 13:03:31 -070041
42/// This is the main Keystore error type. It wraps the Keystore `ResponseCode` generated
43/// from AIDL in the `Rc` variant and Keymint `ErrorCode` in the Km variant.
Chris Wailes263de9f2022-08-11 15:00:51 -070044#[derive(Debug, thiserror::Error, PartialEq, Eq)]
Janis Danisevskis7d77a762020-07-20 13:03:31 -070045pub enum Error {
46 /// Wraps a Keystore `ResponseCode` as defined by the Keystore AIDL interface specification.
47 #[error("Error::Rc({0:?})")]
Janis Danisevskise24f3472020-08-12 17:58:49 -070048 Rc(ResponseCode),
Janis Danisevskis7d77a762020-07-20 13:03:31 -070049 /// Wraps a Keymint `ErrorCode` as defined by the Keymint AIDL interface specification.
50 #[error("Error::Km({0:?})")]
Janis Danisevskise24f3472020-08-12 17:58:49 -070051 Km(ErrorCode),
Janis Danisevskis017d2092020-09-02 10:15:52 -070052 /// Wraps a Binder exception code other than a service specific exception.
53 #[error("Binder exception code {0:?}, {1:?}")]
54 Binder(ExceptionCode, i32),
Janis Danisevskisba998992020-12-29 16:08:40 -080055 /// Wraps a Binder status code.
56 #[error("Binder transaction error {0:?}")]
57 BinderTransaction(StatusCode),
Max Biresb2e1d032021-02-08 21:35:05 -080058 /// Wraps a Remote Provisioning ErrorCode as defined by the IRemotelyProvisionedComponent
59 /// AIDL interface spec.
60 #[error("Error::Rp({0:?})")]
61 Rp(ErrorCode),
Janis Danisevskis7d77a762020-07-20 13:03:31 -070062}
63
64impl Error {
Janis Danisevskisc5b210b2020-09-11 13:27:37 -070065 /// Short hand for `Error::Rc(ResponseCode::SYSTEM_ERROR)`
Janis Danisevskis7d77a762020-07-20 13:03:31 -070066 pub fn sys() -> Self {
Janis Danisevskisc5b210b2020-09-11 13:27:37 -070067 Error::Rc(ResponseCode::SYSTEM_ERROR)
Janis Danisevskis7d77a762020-07-20 13:03:31 -070068 }
69
Seth Moore7ee79f92021-12-07 11:42:49 -080070 /// Short hand for `Error::Rc(ResponseCode::PERMISSION_DENIED)`
Janis Danisevskis7d77a762020-07-20 13:03:31 -070071 pub fn perm() -> Self {
Janis Danisevskisc5b210b2020-09-11 13:27:37 -070072 Error::Rc(ResponseCode::PERMISSION_DENIED)
Janis Danisevskis7d77a762020-07-20 13:03:31 -070073 }
Seth Moore7ee79f92021-12-07 11:42:49 -080074
75 /// Short hand for `Error::Rc(ResponseCode::OUT_OF_KEYS)`
76 pub fn out_of_keys() -> Self {
77 Error::Rc(ResponseCode::OUT_OF_KEYS)
78 }
Janis Danisevskis7d77a762020-07-20 13:03:31 -070079}
80
Janis Danisevskis017d2092020-09-02 10:15:52 -070081/// Helper function to map the binder status we get from calls into KeyMint
82/// to a Keystore Error. We don't create an anyhow error here to make
83/// it easier to evaluate KeyMint errors, which we must do in some cases, e.g.,
84/// when diagnosing authentication requirements, update requirements, and running
85/// out of operation slots.
86pub fn map_km_error<T>(r: BinderResult<T>) -> Result<T, Error> {
87 r.map_err(|s| {
88 match s.exception_code() {
89 ExceptionCode::SERVICE_SPECIFIC => {
90 let se = s.service_specific_error();
91 if se < 0 {
92 // Negative service specific errors are KM error codes.
Janis Danisevskisc5b210b2020-09-11 13:27:37 -070093 Error::Km(ErrorCode(s.service_specific_error()))
Janis Danisevskis017d2092020-09-02 10:15:52 -070094 } else {
95 // Non negative error codes cannot be KM error codes.
96 // So we create an `Error::Binder` variant to preserve
97 // the service specific error code for logging.
98 // `map_or_log_err` will map this on a system error,
99 // but not before logging the details to logcat.
100 Error::Binder(ExceptionCode::SERVICE_SPECIFIC, se)
101 }
102 }
103 // We create `Error::Binder` to preserve the exception code
104 // for logging.
105 // `map_or_log_err` will map this on a system error.
106 e_code => Error::Binder(e_code, 0),
107 }
108 })
109}
110
Max Biresb2e1d032021-02-08 21:35:05 -0800111/// Helper function to map the binder status we get from calls into a RemotelyProvisionedComponent
112/// to a Keystore Error. We don't create an anyhow error here to make
113/// it easier to evaluate service specific errors.
114pub fn map_rem_prov_error<T>(r: BinderResult<T>) -> Result<T, Error> {
115 r.map_err(|s| match s.exception_code() {
116 ExceptionCode::SERVICE_SPECIFIC => Error::Rp(ErrorCode(s.service_specific_error())),
117 e_code => Error::Binder(e_code, 0),
118 })
119}
120
Janis Danisevskisba998992020-12-29 16:08:40 -0800121/// This function is similar to map_km_error only that we don't expect
122/// any KeyMint error codes, we simply preserve the exception code and optional
123/// service specific exception.
124pub fn map_binder_status<T>(r: BinderResult<T>) -> Result<T, Error> {
125 r.map_err(|s| match s.exception_code() {
126 ExceptionCode::SERVICE_SPECIFIC => {
127 let se = s.service_specific_error();
128 Error::Binder(ExceptionCode::SERVICE_SPECIFIC, se)
129 }
130 ExceptionCode::TRANSACTION_FAILED => {
131 let e = s.transaction_error();
132 Error::BinderTransaction(e)
133 }
134 e_code => Error::Binder(e_code, 0),
135 })
136}
137
138/// This function maps a status code onto a Keystore Error.
139pub fn map_binder_status_code<T>(r: Result<T, StatusCode>) -> Result<T, Error> {
140 r.map_err(Error::BinderTransaction)
141}
142
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700143/// This function should be used by Keystore service calls to translate error conditions
Janis Danisevskis8ea5f552020-11-20 11:22:59 -0800144/// into service specific exceptions.
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700145///
Hasini Gunasinghee1d1bbd2021-04-20 18:13:25 +0000146/// All error conditions get logged by this function, except for KEY_NOT_FOUND error.
Janis Danisevskis8ea5f552020-11-20 11:22:59 -0800147///
148/// All `Error::Rc(x)` and `Error::Km(x)` variants get mapped onto a service specific error
149/// code of x. This is possible because KeyMint `ErrorCode` errors are always negative and
150/// `ResponseCode` codes are always positive.
151/// `selinux::Error::PermissionDenied` is mapped on `ResponseCode::PERMISSION_DENIED`.
152///
153/// All non `Error` error conditions and the Error::Binder variant get mapped onto
154/// ResponseCode::SYSTEM_ERROR`.
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700155///
156/// `handle_ok` will be called if `result` is `Ok(value)` where `value` will be passed
Janis Danisevskis8ea5f552020-11-20 11:22:59 -0800157/// as argument to `handle_ok`. `handle_ok` must generate a `BinderResult<T>`, but it
158/// typically returns Ok(value).
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700159///
160/// # Examples
161///
162/// ```
Janis Danisevskis8ea5f552020-11-20 11:22:59 -0800163/// fn loadKey() -> anyhow::Result<Vec<u8>> {
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700164/// if (good_but_auth_required) {
Janis Danisevskis8ea5f552020-11-20 11:22:59 -0800165/// Ok(vec!['k', 'e', 'y'])
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700166/// } else {
Janis Danisevskis8ea5f552020-11-20 11:22:59 -0800167/// Err(anyhow!(Error::Rc(ResponseCode::KEY_NOT_FOUND)))
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700168/// }
169/// }
170///
Janis Danisevskis8ea5f552020-11-20 11:22:59 -0800171/// map_or_log_err(loadKey(), Ok)
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700172/// ```
Janis Danisevskise24f3472020-08-12 17:58:49 -0700173pub fn map_or_log_err<T, U, F>(result: anyhow::Result<U>, handle_ok: F) -> BinderResult<T>
174where
175 F: FnOnce(U) -> BinderResult<T>,
176{
Janis Danisevskis778245c2021-03-04 15:40:23 -0800177 map_err_with(
178 result,
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700179 |e| {
Hasini Gunasinghee1d1bbd2021-04-20 18:13:25 +0000180 // Make the key not found errors silent.
181 if !matches!(
182 e.root_cause().downcast_ref::<Error>(),
183 Some(Error::Rc(ResponseCode::KEY_NOT_FOUND))
184 ) {
185 log::error!("{:?}", e);
186 }
Janis Danisevskis778245c2021-03-04 15:40:23 -0800187 e
188 },
189 handle_ok,
190 )
191}
192
Janis Danisevskisea03cff2021-12-16 08:10:17 -0800193/// This function turns an anyhow error into an optional CString.
194/// This is especially useful to add a message string to a service specific error.
195/// If the formatted string was not convertible because it contained a nul byte,
196/// None is returned and a warning is logged.
197pub fn anyhow_error_to_cstring(e: &anyhow::Error) -> Option<CString> {
198 match CString::new(format!("{:?}", e)) {
199 Ok(msg) => Some(msg),
200 Err(_) => {
201 log::warn!("Cannot convert error message to CStr. It contained a nul byte.");
202 None
203 }
204 }
205}
206
Janis Danisevskis778245c2021-03-04 15:40:23 -0800207/// This function behaves similar to map_or_log_error, but it does not log the errors, instead
208/// it calls map_err on the error before mapping it to a binder result allowing callers to
209/// log or transform the error before mapping it.
210pub fn map_err_with<T, U, F1, F2>(
211 result: anyhow::Result<U>,
212 map_err: F1,
213 handle_ok: F2,
214) -> BinderResult<T>
215where
216 F1: FnOnce(anyhow::Error) -> anyhow::Error,
217 F2: FnOnce(U) -> BinderResult<T>,
218{
219 result.map_or_else(
220 |e| {
221 let e = map_err(e);
Hasini Gunasingheb7142972021-02-20 03:11:27 +0000222 let rc = get_error_code(&e);
Janis Danisevskisea03cff2021-12-16 08:10:17 -0800223 Err(BinderStatus::new_service_specific_error(
224 rc,
225 anyhow_error_to_cstring(&e).as_deref(),
226 ))
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700227 },
228 handle_ok,
229 )
230}
231
Hasini Gunasingheb7142972021-02-20 03:11:27 +0000232/// Returns the error code given a reference to the error
233pub fn get_error_code(e: &anyhow::Error) -> i32 {
234 let root_cause = e.root_cause();
235 match root_cause.downcast_ref::<Error>() {
236 Some(Error::Rc(rcode)) => rcode.0,
237 Some(Error::Km(ec)) => ec.0,
238 Some(Error::Rp(_)) => ResponseCode::SYSTEM_ERROR.0,
239 // If an Error::Binder reaches this stage we report a system error.
240 // The exception code and possible service specific error will be
241 // printed in the error log above.
242 Some(Error::Binder(_, _)) | Some(Error::BinderTransaction(_)) => {
243 ResponseCode::SYSTEM_ERROR.0
244 }
245 None => match root_cause.downcast_ref::<selinux::Error>() {
246 Some(selinux::Error::PermissionDenied) => ResponseCode::PERMISSION_DENIED.0,
247 _ => ResponseCode::SYSTEM_ERROR.0,
248 },
249 }
250}
251
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700252#[cfg(test)]
Hasini Gunasingheaf993662020-07-24 18:40:20 +0000253pub mod tests {
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700254
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700255 use super::*;
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700256 use android_system_keystore2::binder::{
Janis Danisevskis017d2092020-09-02 10:15:52 -0700257 ExceptionCode, Result as BinderResult, Status as BinderStatus,
258 };
Janis Danisevskise24f3472020-08-12 17:58:49 -0700259 use anyhow::{anyhow, Context};
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700260
Janis Danisevskise24f3472020-08-12 17:58:49 -0700261 fn nested_nested_rc(rc: ResponseCode) -> anyhow::Result<()> {
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700262 Err(anyhow!(Error::Rc(rc))).context("nested nested rc")
263 }
264
Janis Danisevskise24f3472020-08-12 17:58:49 -0700265 fn nested_rc(rc: ResponseCode) -> anyhow::Result<()> {
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700266 nested_nested_rc(rc).context("nested rc")
267 }
268
269 fn nested_nested_ec(ec: ErrorCode) -> anyhow::Result<()> {
270 Err(anyhow!(Error::Km(ec))).context("nested nested ec")
271 }
272
273 fn nested_ec(ec: ErrorCode) -> anyhow::Result<()> {
274 nested_nested_ec(ec).context("nested ec")
275 }
276
Janis Danisevskise24f3472020-08-12 17:58:49 -0700277 fn nested_nested_ok(rc: ResponseCode) -> anyhow::Result<ResponseCode> {
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700278 Ok(rc)
279 }
280
Janis Danisevskise24f3472020-08-12 17:58:49 -0700281 fn nested_ok(rc: ResponseCode) -> anyhow::Result<ResponseCode> {
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700282 nested_nested_ok(rc).context("nested ok")
283 }
284
Janis Danisevskisce995432020-07-21 12:22:34 -0700285 fn nested_nested_selinux_perm() -> anyhow::Result<()> {
286 Err(anyhow!(selinux::Error::perm())).context("nested nexted selinux permission denied")
287 }
288
289 fn nested_selinux_perm() -> anyhow::Result<()> {
290 nested_nested_selinux_perm().context("nested selinux permission denied")
291 }
292
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700293 #[derive(Debug, thiserror::Error)]
294 enum TestError {
295 #[error("TestError::Fail")]
296 Fail = 0,
297 }
298
299 fn nested_nested_other_error() -> anyhow::Result<()> {
300 Err(anyhow!(TestError::Fail)).context("nested nested other error")
301 }
302
303 fn nested_other_error() -> anyhow::Result<()> {
304 nested_nested_other_error().context("nested other error")
305 }
306
Janis Danisevskis017d2092020-09-02 10:15:52 -0700307 fn binder_sse_error(sse: i32) -> BinderResult<()> {
308 Err(BinderStatus::new_service_specific_error(sse, None))
309 }
310
311 fn binder_exception(ex: ExceptionCode) -> BinderResult<()> {
312 Err(BinderStatus::new_exception(ex, None))
313 }
314
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700315 #[test]
316 fn keystore_error_test() -> anyhow::Result<(), String> {
317 android_logger::init_once(
318 android_logger::Config::default()
319 .with_tag("keystore_error_tests")
320 .with_min_level(log::Level::Debug),
321 );
Janis Danisevskise24f3472020-08-12 17:58:49 -0700322 // All Error::Rc(x) get mapped on a service specific error
323 // code of x.
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700324 for rc in ResponseCode::LOCKED.0..ResponseCode::BACKEND_BUSY.0 {
Janis Danisevskise24f3472020-08-12 17:58:49 -0700325 assert_eq!(
326 Result::<(), i32>::Err(rc),
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700327 map_or_log_err(nested_rc(ResponseCode(rc)), |_| Err(BinderStatus::ok()))
Janis Danisevskise24f3472020-08-12 17:58:49 -0700328 .map_err(|s| s.service_specific_error())
329 );
330 }
331
Janis Danisevskis017d2092020-09-02 10:15:52 -0700332 // All Keystore Error::Km(x) get mapped on a service
Janis Danisevskise24f3472020-08-12 17:58:49 -0700333 // specific error of x.
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700334 for ec in ErrorCode::UNKNOWN_ERROR.0..ErrorCode::ROOT_OF_TRUST_ALREADY_SET.0 {
Janis Danisevskise24f3472020-08-12 17:58:49 -0700335 assert_eq!(
336 Result::<(), i32>::Err(ec),
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700337 map_or_log_err(nested_ec(ErrorCode(ec)), |_| Err(BinderStatus::ok()))
Janis Danisevskise24f3472020-08-12 17:58:49 -0700338 .map_err(|s| s.service_specific_error())
339 );
340 }
341
Janis Danisevskis017d2092020-09-02 10:15:52 -0700342 // All Keymint errors x received through a Binder Result get mapped on
343 // a service specific error of x.
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700344 for ec in ErrorCode::UNKNOWN_ERROR.0..ErrorCode::ROOT_OF_TRUST_ALREADY_SET.0 {
Janis Danisevskis017d2092020-09-02 10:15:52 -0700345 assert_eq!(
346 Result::<(), i32>::Err(ec),
347 map_or_log_err(
348 map_km_error(binder_sse_error(ec))
349 .with_context(|| format!("Km error code: {}.", ec)),
350 |_| Err(BinderStatus::ok())
351 )
352 .map_err(|s| s.service_specific_error())
353 );
354 }
355
356 // map_km_error creates an Error::Binder variant storing
357 // ExceptionCode::SERVICE_SPECIFIC and the given
358 // service specific error.
359 let sse = map_km_error(binder_sse_error(1));
360 assert_eq!(Err(Error::Binder(ExceptionCode::SERVICE_SPECIFIC, 1)), sse);
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700361 // map_or_log_err then maps it on a service specific error of ResponseCode::SYSTEM_ERROR.
Janis Danisevskis017d2092020-09-02 10:15:52 -0700362 assert_eq!(
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700363 Result::<(), ResponseCode>::Err(ResponseCode::SYSTEM_ERROR),
Janis Danisevskis017d2092020-09-02 10:15:52 -0700364 map_or_log_err(sse.context("Non negative service specific error."), |_| Err(
365 BinderStatus::ok()
366 ))
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700367 .map_err(|s| ResponseCode(s.service_specific_error()))
Janis Danisevskis017d2092020-09-02 10:15:52 -0700368 );
369
370 // map_km_error creates a Error::Binder variant storing the given exception code.
371 let binder_exception = map_km_error(binder_exception(ExceptionCode::TRANSACTION_FAILED));
372 assert_eq!(Err(Error::Binder(ExceptionCode::TRANSACTION_FAILED, 0)), binder_exception);
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700373 // map_or_log_err then maps it on a service specific error of ResponseCode::SYSTEM_ERROR.
Janis Danisevskis017d2092020-09-02 10:15:52 -0700374 assert_eq!(
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700375 Result::<(), ResponseCode>::Err(ResponseCode::SYSTEM_ERROR),
Janis Danisevskis017d2092020-09-02 10:15:52 -0700376 map_or_log_err(binder_exception.context("Binder Exception."), |_| Err(
377 BinderStatus::ok()
378 ))
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700379 .map_err(|s| ResponseCode(s.service_specific_error()))
Janis Danisevskis017d2092020-09-02 10:15:52 -0700380 );
381
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700382 // selinux::Error::Perm() needs to be mapped to ResponseCode::PERMISSION_DENIED
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700383 assert_eq!(
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700384 Result::<(), ResponseCode>::Err(ResponseCode::PERMISSION_DENIED),
Janis Danisevskise24f3472020-08-12 17:58:49 -0700385 map_or_log_err(nested_selinux_perm(), |_| Err(BinderStatus::ok()))
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700386 .map_err(|s| ResponseCode(s.service_specific_error()))
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700387 );
388
Janis Danisevskise24f3472020-08-12 17:58:49 -0700389 // All other errors get mapped on System Error.
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700390 assert_eq!(
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700391 Result::<(), ResponseCode>::Err(ResponseCode::SYSTEM_ERROR),
Janis Danisevskise24f3472020-08-12 17:58:49 -0700392 map_or_log_err(nested_other_error(), |_| Err(BinderStatus::ok()))
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700393 .map_err(|s| ResponseCode(s.service_specific_error()))
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700394 );
395
396 // Result::Ok variants get passed to the ok handler.
Janis Danisevskisc5b210b2020-09-11 13:27:37 -0700397 assert_eq!(Ok(ResponseCode::LOCKED), map_or_log_err(nested_ok(ResponseCode::LOCKED), Ok));
398 assert_eq!(
399 Ok(ResponseCode::SYSTEM_ERROR),
400 map_or_log_err(nested_ok(ResponseCode::SYSTEM_ERROR), Ok)
401 );
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700402
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700403 Ok(())
404 }
Hasini Gunasingheaf993662020-07-24 18:40:20 +0000405
406 //Helper function to test whether error cases are handled as expected.
Janis Danisevskise24f3472020-08-12 17:58:49 -0700407 pub fn check_result_contains_error_string<T>(
408 result: anyhow::Result<T>,
409 expected_error_string: &str,
410 ) {
Hasini Gunasingheaf993662020-07-24 18:40:20 +0000411 let error_str = format!(
412 "{:#?}",
413 result.err().unwrap_or_else(|| panic!("Expected the error: {}", expected_error_string))
414 );
415 assert!(
416 error_str.contains(expected_error_string),
417 "The string \"{}\" should contain \"{}\"",
418 error_str,
419 expected_error_string
420 );
421 }
Janis Danisevskis7d77a762020-07-20 13:03:31 -0700422} // mod tests