Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 1 | // 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 Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 28 | //! `ResponseCode::SYSTEM_ERROR` and logs any error condition. |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 29 | //! |
| 30 | //! Keystore functions should use `anyhow::Result` to return error conditions, and |
| 31 | //! context should be added every time an error is forwarded. |
| 32 | |
| 33 | use std::cmp::PartialEq; |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 34 | |
Shawn Willden | 708744a | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 35 | pub use android_hardware_security_keymint::aidl::android::hardware::security::keymint::ErrorCode::ErrorCode; |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 36 | pub use android_system_keystore2::aidl::android::system::keystore2::ResponseCode::ResponseCode; |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 37 | |
Janis Danisevskis | ce99543 | 2020-07-21 12:22:34 -0700 | [diff] [blame] | 38 | use keystore2_selinux as selinux; |
| 39 | |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 40 | use android_system_keystore2::binder::{ |
Janis Danisevskis | ba99899 | 2020-12-29 16:08:40 -0800 | [diff] [blame] | 41 | ExceptionCode, Result as BinderResult, Status as BinderStatus, StatusCode, |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 42 | }; |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 43 | |
| 44 | /// This is the main Keystore error type. It wraps the Keystore `ResponseCode` generated |
| 45 | /// from AIDL in the `Rc` variant and Keymint `ErrorCode` in the Km variant. |
| 46 | #[derive(Debug, thiserror::Error, PartialEq)] |
| 47 | pub enum Error { |
| 48 | /// Wraps a Keystore `ResponseCode` as defined by the Keystore AIDL interface specification. |
| 49 | #[error("Error::Rc({0:?})")] |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 50 | Rc(ResponseCode), |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 51 | /// Wraps a Keymint `ErrorCode` as defined by the Keymint AIDL interface specification. |
| 52 | #[error("Error::Km({0:?})")] |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 53 | Km(ErrorCode), |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 54 | /// Wraps a Binder exception code other than a service specific exception. |
| 55 | #[error("Binder exception code {0:?}, {1:?}")] |
| 56 | Binder(ExceptionCode, i32), |
Janis Danisevskis | ba99899 | 2020-12-29 16:08:40 -0800 | [diff] [blame] | 57 | /// Wraps a Binder status code. |
| 58 | #[error("Binder transaction error {0:?}")] |
| 59 | BinderTransaction(StatusCode), |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | impl Error { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 63 | /// Short hand for `Error::Rc(ResponseCode::SYSTEM_ERROR)` |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 64 | pub fn sys() -> Self { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 65 | Error::Rc(ResponseCode::SYSTEM_ERROR) |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 68 | /// Short hand for `Error::Rc(ResponseCode::PERMISSION_DENIED` |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 69 | pub fn perm() -> Self { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 70 | Error::Rc(ResponseCode::PERMISSION_DENIED) |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 74 | /// Helper function to map the binder status we get from calls into KeyMint |
| 75 | /// to a Keystore Error. We don't create an anyhow error here to make |
| 76 | /// it easier to evaluate KeyMint errors, which we must do in some cases, e.g., |
| 77 | /// when diagnosing authentication requirements, update requirements, and running |
| 78 | /// out of operation slots. |
| 79 | pub fn map_km_error<T>(r: BinderResult<T>) -> Result<T, Error> { |
| 80 | r.map_err(|s| { |
| 81 | match s.exception_code() { |
| 82 | ExceptionCode::SERVICE_SPECIFIC => { |
| 83 | let se = s.service_specific_error(); |
| 84 | if se < 0 { |
| 85 | // Negative service specific errors are KM error codes. |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 86 | Error::Km(ErrorCode(s.service_specific_error())) |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 87 | } else { |
| 88 | // Non negative error codes cannot be KM error codes. |
| 89 | // So we create an `Error::Binder` variant to preserve |
| 90 | // the service specific error code for logging. |
| 91 | // `map_or_log_err` will map this on a system error, |
| 92 | // but not before logging the details to logcat. |
| 93 | Error::Binder(ExceptionCode::SERVICE_SPECIFIC, se) |
| 94 | } |
| 95 | } |
| 96 | // We create `Error::Binder` to preserve the exception code |
| 97 | // for logging. |
| 98 | // `map_or_log_err` will map this on a system error. |
| 99 | e_code => Error::Binder(e_code, 0), |
| 100 | } |
| 101 | }) |
| 102 | } |
| 103 | |
Janis Danisevskis | ba99899 | 2020-12-29 16:08:40 -0800 | [diff] [blame] | 104 | /// This function is similar to map_km_error only that we don't expect |
| 105 | /// any KeyMint error codes, we simply preserve the exception code and optional |
| 106 | /// service specific exception. |
| 107 | pub fn map_binder_status<T>(r: BinderResult<T>) -> Result<T, Error> { |
| 108 | r.map_err(|s| match s.exception_code() { |
| 109 | ExceptionCode::SERVICE_SPECIFIC => { |
| 110 | let se = s.service_specific_error(); |
| 111 | Error::Binder(ExceptionCode::SERVICE_SPECIFIC, se) |
| 112 | } |
| 113 | ExceptionCode::TRANSACTION_FAILED => { |
| 114 | let e = s.transaction_error(); |
| 115 | Error::BinderTransaction(e) |
| 116 | } |
| 117 | e_code => Error::Binder(e_code, 0), |
| 118 | }) |
| 119 | } |
| 120 | |
| 121 | /// This function maps a status code onto a Keystore Error. |
| 122 | pub fn map_binder_status_code<T>(r: Result<T, StatusCode>) -> Result<T, Error> { |
| 123 | r.map_err(Error::BinderTransaction) |
| 124 | } |
| 125 | |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 126 | /// This function should be used by Keystore service calls to translate error conditions |
Janis Danisevskis | 8ea5f55 | 2020-11-20 11:22:59 -0800 | [diff] [blame] | 127 | /// into service specific exceptions. |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 128 | /// |
Janis Danisevskis | 8ea5f55 | 2020-11-20 11:22:59 -0800 | [diff] [blame] | 129 | /// All error conditions get logged by this function. |
| 130 | /// |
| 131 | /// All `Error::Rc(x)` and `Error::Km(x)` variants get mapped onto a service specific error |
| 132 | /// code of x. This is possible because KeyMint `ErrorCode` errors are always negative and |
| 133 | /// `ResponseCode` codes are always positive. |
| 134 | /// `selinux::Error::PermissionDenied` is mapped on `ResponseCode::PERMISSION_DENIED`. |
| 135 | /// |
| 136 | /// All non `Error` error conditions and the Error::Binder variant get mapped onto |
| 137 | /// ResponseCode::SYSTEM_ERROR`. |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 138 | /// |
| 139 | /// `handle_ok` will be called if `result` is `Ok(value)` where `value` will be passed |
Janis Danisevskis | 8ea5f55 | 2020-11-20 11:22:59 -0800 | [diff] [blame] | 140 | /// as argument to `handle_ok`. `handle_ok` must generate a `BinderResult<T>`, but it |
| 141 | /// typically returns Ok(value). |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 142 | /// |
| 143 | /// # Examples |
| 144 | /// |
| 145 | /// ``` |
Janis Danisevskis | 8ea5f55 | 2020-11-20 11:22:59 -0800 | [diff] [blame] | 146 | /// fn loadKey() -> anyhow::Result<Vec<u8>> { |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 147 | /// if (good_but_auth_required) { |
Janis Danisevskis | 8ea5f55 | 2020-11-20 11:22:59 -0800 | [diff] [blame] | 148 | /// Ok(vec!['k', 'e', 'y']) |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 149 | /// } else { |
Janis Danisevskis | 8ea5f55 | 2020-11-20 11:22:59 -0800 | [diff] [blame] | 150 | /// Err(anyhow!(Error::Rc(ResponseCode::KEY_NOT_FOUND))) |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 151 | /// } |
| 152 | /// } |
| 153 | /// |
Janis Danisevskis | 8ea5f55 | 2020-11-20 11:22:59 -0800 | [diff] [blame] | 154 | /// map_or_log_err(loadKey(), Ok) |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 155 | /// ``` |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 156 | pub fn map_or_log_err<T, U, F>(result: anyhow::Result<U>, handle_ok: F) -> BinderResult<T> |
| 157 | where |
| 158 | F: FnOnce(U) -> BinderResult<T>, |
| 159 | { |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 160 | result.map_or_else( |
| 161 | |e| { |
| 162 | log::error!("{:?}", e); |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 163 | let root_cause = e.root_cause(); |
| 164 | let rc = match root_cause.downcast_ref::<Error>() { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 165 | Some(Error::Rc(rcode)) => rcode.0, |
| 166 | Some(Error::Km(ec)) => ec.0, |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 167 | // If an Error::Binder reaches this stage we report a system error. |
| 168 | // The exception code and possible service specific error will be |
| 169 | // printed in the error log above. |
Janis Danisevskis | ba99899 | 2020-12-29 16:08:40 -0800 | [diff] [blame] | 170 | Some(Error::Binder(_, _)) | Some(Error::BinderTransaction(_)) => { |
| 171 | ResponseCode::SYSTEM_ERROR.0 |
| 172 | } |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 173 | None => match root_cause.downcast_ref::<selinux::Error>() { |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 174 | Some(selinux::Error::PermissionDenied) => ResponseCode::PERMISSION_DENIED.0, |
| 175 | _ => ResponseCode::SYSTEM_ERROR.0, |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 176 | }, |
| 177 | }; |
| 178 | Err(BinderStatus::new_service_specific_error(rc, None)) |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 179 | }, |
| 180 | handle_ok, |
| 181 | ) |
| 182 | } |
| 183 | |
| 184 | #[cfg(test)] |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 185 | pub mod tests { |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 186 | |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 187 | use super::*; |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 188 | use android_system_keystore2::binder::{ |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 189 | ExceptionCode, Result as BinderResult, Status as BinderStatus, |
| 190 | }; |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 191 | use anyhow::{anyhow, Context}; |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 192 | |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 193 | fn nested_nested_rc(rc: ResponseCode) -> anyhow::Result<()> { |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 194 | Err(anyhow!(Error::Rc(rc))).context("nested nested rc") |
| 195 | } |
| 196 | |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 197 | fn nested_rc(rc: ResponseCode) -> anyhow::Result<()> { |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 198 | nested_nested_rc(rc).context("nested rc") |
| 199 | } |
| 200 | |
| 201 | fn nested_nested_ec(ec: ErrorCode) -> anyhow::Result<()> { |
| 202 | Err(anyhow!(Error::Km(ec))).context("nested nested ec") |
| 203 | } |
| 204 | |
| 205 | fn nested_ec(ec: ErrorCode) -> anyhow::Result<()> { |
| 206 | nested_nested_ec(ec).context("nested ec") |
| 207 | } |
| 208 | |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 209 | fn nested_nested_ok(rc: ResponseCode) -> anyhow::Result<ResponseCode> { |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 210 | Ok(rc) |
| 211 | } |
| 212 | |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 213 | fn nested_ok(rc: ResponseCode) -> anyhow::Result<ResponseCode> { |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 214 | nested_nested_ok(rc).context("nested ok") |
| 215 | } |
| 216 | |
Janis Danisevskis | ce99543 | 2020-07-21 12:22:34 -0700 | [diff] [blame] | 217 | fn nested_nested_selinux_perm() -> anyhow::Result<()> { |
| 218 | Err(anyhow!(selinux::Error::perm())).context("nested nexted selinux permission denied") |
| 219 | } |
| 220 | |
| 221 | fn nested_selinux_perm() -> anyhow::Result<()> { |
| 222 | nested_nested_selinux_perm().context("nested selinux permission denied") |
| 223 | } |
| 224 | |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 225 | #[derive(Debug, thiserror::Error)] |
| 226 | enum TestError { |
| 227 | #[error("TestError::Fail")] |
| 228 | Fail = 0, |
| 229 | } |
| 230 | |
| 231 | fn nested_nested_other_error() -> anyhow::Result<()> { |
| 232 | Err(anyhow!(TestError::Fail)).context("nested nested other error") |
| 233 | } |
| 234 | |
| 235 | fn nested_other_error() -> anyhow::Result<()> { |
| 236 | nested_nested_other_error().context("nested other error") |
| 237 | } |
| 238 | |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 239 | fn binder_sse_error(sse: i32) -> BinderResult<()> { |
| 240 | Err(BinderStatus::new_service_specific_error(sse, None)) |
| 241 | } |
| 242 | |
| 243 | fn binder_exception(ex: ExceptionCode) -> BinderResult<()> { |
| 244 | Err(BinderStatus::new_exception(ex, None)) |
| 245 | } |
| 246 | |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 247 | #[test] |
| 248 | fn keystore_error_test() -> anyhow::Result<(), String> { |
| 249 | android_logger::init_once( |
| 250 | android_logger::Config::default() |
| 251 | .with_tag("keystore_error_tests") |
| 252 | .with_min_level(log::Level::Debug), |
| 253 | ); |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 254 | // All Error::Rc(x) get mapped on a service specific error |
| 255 | // code of x. |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 256 | for rc in ResponseCode::LOCKED.0..ResponseCode::BACKEND_BUSY.0 { |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 257 | assert_eq!( |
| 258 | Result::<(), i32>::Err(rc), |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 259 | map_or_log_err(nested_rc(ResponseCode(rc)), |_| Err(BinderStatus::ok())) |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 260 | .map_err(|s| s.service_specific_error()) |
| 261 | ); |
| 262 | } |
| 263 | |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 264 | // All Keystore Error::Km(x) get mapped on a service |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 265 | // specific error of x. |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 266 | for ec in ErrorCode::UNKNOWN_ERROR.0..ErrorCode::ROOT_OF_TRUST_ALREADY_SET.0 { |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 267 | assert_eq!( |
| 268 | Result::<(), i32>::Err(ec), |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 269 | map_or_log_err(nested_ec(ErrorCode(ec)), |_| Err(BinderStatus::ok())) |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 270 | .map_err(|s| s.service_specific_error()) |
| 271 | ); |
| 272 | } |
| 273 | |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 274 | // All Keymint errors x received through a Binder Result get mapped on |
| 275 | // a service specific error of x. |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 276 | for ec in ErrorCode::UNKNOWN_ERROR.0..ErrorCode::ROOT_OF_TRUST_ALREADY_SET.0 { |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 277 | assert_eq!( |
| 278 | Result::<(), i32>::Err(ec), |
| 279 | map_or_log_err( |
| 280 | map_km_error(binder_sse_error(ec)) |
| 281 | .with_context(|| format!("Km error code: {}.", ec)), |
| 282 | |_| Err(BinderStatus::ok()) |
| 283 | ) |
| 284 | .map_err(|s| s.service_specific_error()) |
| 285 | ); |
| 286 | } |
| 287 | |
| 288 | // map_km_error creates an Error::Binder variant storing |
| 289 | // ExceptionCode::SERVICE_SPECIFIC and the given |
| 290 | // service specific error. |
| 291 | let sse = map_km_error(binder_sse_error(1)); |
| 292 | assert_eq!(Err(Error::Binder(ExceptionCode::SERVICE_SPECIFIC, 1)), sse); |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 293 | // map_or_log_err then maps it on a service specific error of ResponseCode::SYSTEM_ERROR. |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 294 | assert_eq!( |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 295 | Result::<(), ResponseCode>::Err(ResponseCode::SYSTEM_ERROR), |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 296 | map_or_log_err(sse.context("Non negative service specific error."), |_| Err( |
| 297 | BinderStatus::ok() |
| 298 | )) |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 299 | .map_err(|s| ResponseCode(s.service_specific_error())) |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 300 | ); |
| 301 | |
| 302 | // map_km_error creates a Error::Binder variant storing the given exception code. |
| 303 | let binder_exception = map_km_error(binder_exception(ExceptionCode::TRANSACTION_FAILED)); |
| 304 | assert_eq!(Err(Error::Binder(ExceptionCode::TRANSACTION_FAILED, 0)), binder_exception); |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 305 | // map_or_log_err then maps it on a service specific error of ResponseCode::SYSTEM_ERROR. |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 306 | assert_eq!( |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 307 | Result::<(), ResponseCode>::Err(ResponseCode::SYSTEM_ERROR), |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 308 | map_or_log_err(binder_exception.context("Binder Exception."), |_| Err( |
| 309 | BinderStatus::ok() |
| 310 | )) |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 311 | .map_err(|s| ResponseCode(s.service_specific_error())) |
Janis Danisevskis | 017d209 | 2020-09-02 10:15:52 -0700 | [diff] [blame] | 312 | ); |
| 313 | |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 314 | // selinux::Error::Perm() needs to be mapped to ResponseCode::PERMISSION_DENIED |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 315 | assert_eq!( |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 316 | Result::<(), ResponseCode>::Err(ResponseCode::PERMISSION_DENIED), |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 317 | map_or_log_err(nested_selinux_perm(), |_| Err(BinderStatus::ok())) |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 318 | .map_err(|s| ResponseCode(s.service_specific_error())) |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 319 | ); |
| 320 | |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 321 | // All other errors get mapped on System Error. |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 322 | assert_eq!( |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 323 | Result::<(), ResponseCode>::Err(ResponseCode::SYSTEM_ERROR), |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 324 | map_or_log_err(nested_other_error(), |_| Err(BinderStatus::ok())) |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 325 | .map_err(|s| ResponseCode(s.service_specific_error())) |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 326 | ); |
| 327 | |
| 328 | // Result::Ok variants get passed to the ok handler. |
Janis Danisevskis | c5b210b | 2020-09-11 13:27:37 -0700 | [diff] [blame] | 329 | assert_eq!(Ok(ResponseCode::LOCKED), map_or_log_err(nested_ok(ResponseCode::LOCKED), Ok)); |
| 330 | assert_eq!( |
| 331 | Ok(ResponseCode::SYSTEM_ERROR), |
| 332 | map_or_log_err(nested_ok(ResponseCode::SYSTEM_ERROR), Ok) |
| 333 | ); |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 334 | |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 335 | Ok(()) |
| 336 | } |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 337 | |
| 338 | //Helper function to test whether error cases are handled as expected. |
Janis Danisevskis | e24f347 | 2020-08-12 17:58:49 -0700 | [diff] [blame] | 339 | pub fn check_result_contains_error_string<T>( |
| 340 | result: anyhow::Result<T>, |
| 341 | expected_error_string: &str, |
| 342 | ) { |
Hasini Gunasinghe | af99366 | 2020-07-24 18:40:20 +0000 | [diff] [blame] | 343 | let error_str = format!( |
| 344 | "{:#?}", |
| 345 | result.err().unwrap_or_else(|| panic!("Expected the error: {}", expected_error_string)) |
| 346 | ); |
| 347 | assert!( |
| 348 | error_str.contains(expected_error_string), |
| 349 | "The string \"{}\" should contain \"{}\"", |
| 350 | error_str, |
| 351 | expected_error_string |
| 352 | ); |
| 353 | } |
Janis Danisevskis | 7d77a76 | 2020-07-20 13:03:31 -0700 | [diff] [blame] | 354 | } // mod tests |