Janis Danisevskis | bf15d73 | 2020-12-08 10:35:26 -0800 | [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 | |
Janis Danisevskis | 2a8330a | 2021-01-20 15:34:26 -0800 | [diff] [blame] | 15 | //! Implements TempDir which aids in creating an cleaning up temporary directories for testing. |
| 16 | |
Janis Danisevskis | bf15d73 | 2020-12-08 10:35:26 -0800 | [diff] [blame] | 17 | use std::fs::{create_dir, remove_dir_all}; |
| 18 | use std::io::ErrorKind; |
Janis Danisevskis | a51ccbc | 2020-11-25 21:04:24 -0800 | [diff] [blame] | 19 | use std::path::{Path, PathBuf}; |
| 20 | use std::{env::temp_dir, ops::Deref}; |
Janis Danisevskis | bf15d73 | 2020-12-08 10:35:26 -0800 | [diff] [blame] | 21 | |
Rajesh Nyamagoud | 901386c | 2022-03-21 20:35:18 +0000 | [diff] [blame] | 22 | use android_system_keystore2::aidl::android::system::keystore2::IKeystoreService::IKeystoreService; |
| 23 | |
| 24 | pub mod authorizations; |
Rajesh Nyamagoud | 10f02e7 | 2023-08-17 22:27:40 +0000 | [diff] [blame^] | 25 | pub mod ffi_test_utils; |
Rajesh Nyamagoud | 901386c | 2022-03-21 20:35:18 +0000 | [diff] [blame] | 26 | pub mod key_generations; |
Janis Danisevskis | a578d39 | 2021-09-20 15:44:06 -0700 | [diff] [blame] | 27 | pub mod run_as; |
| 28 | |
Rajesh Nyamagoud | 901386c | 2022-03-21 20:35:18 +0000 | [diff] [blame] | 29 | static KS2_SERVICE_NAME: &str = "android.system.keystore2.IKeystoreService/default"; |
| 30 | |
Janis Danisevskis | 2a8330a | 2021-01-20 15:34:26 -0800 | [diff] [blame] | 31 | /// Represents the lifecycle of a temporary directory for testing. |
Janis Danisevskis | bf15d73 | 2020-12-08 10:35:26 -0800 | [diff] [blame] | 32 | #[derive(Debug)] |
| 33 | pub struct TempDir { |
| 34 | path: std::path::PathBuf, |
| 35 | do_drop: bool, |
| 36 | } |
| 37 | |
| 38 | impl TempDir { |
Janis Danisevskis | 2a8330a | 2021-01-20 15:34:26 -0800 | [diff] [blame] | 39 | /// Creates a temporary directory with a name of the form <prefix>_NNNNN where NNNNN is a zero |
| 40 | /// padded random number with 5 figures. The prefix must not contain file system separators. |
| 41 | /// The location of the directory cannot be chosen. |
| 42 | /// The directory with all of its content is removed from the file system when the resulting |
| 43 | /// object gets dropped. |
Janis Danisevskis | bf15d73 | 2020-12-08 10:35:26 -0800 | [diff] [blame] | 44 | pub fn new(prefix: &str) -> std::io::Result<Self> { |
| 45 | let tmp = loop { |
| 46 | let mut tmp = temp_dir(); |
| 47 | let number: u16 = rand::random(); |
| 48 | tmp.push(format!("{}_{:05}", prefix, number)); |
| 49 | match create_dir(&tmp) { |
| 50 | Err(e) => match e.kind() { |
| 51 | ErrorKind::AlreadyExists => continue, |
| 52 | _ => return Err(e), |
| 53 | }, |
| 54 | Ok(()) => break tmp, |
| 55 | } |
| 56 | }; |
| 57 | Ok(Self { path: tmp, do_drop: true }) |
| 58 | } |
| 59 | |
Janis Danisevskis | 2a8330a | 2021-01-20 15:34:26 -0800 | [diff] [blame] | 60 | /// Returns the absolute path of the temporary directory. |
Janis Danisevskis | bf15d73 | 2020-12-08 10:35:26 -0800 | [diff] [blame] | 61 | pub fn path(&self) -> &Path { |
| 62 | &self.path |
| 63 | } |
| 64 | |
Janis Danisevskis | 2a8330a | 2021-01-20 15:34:26 -0800 | [diff] [blame] | 65 | /// Returns a path builder for convenient extension of the path. |
| 66 | /// |
| 67 | /// ## Example: |
| 68 | /// |
| 69 | /// ``` |
| 70 | /// let tdir = TempDir::new("my_test")?; |
| 71 | /// let temp_foo_bar = tdir.build().push("foo").push("bar"); |
| 72 | /// ``` |
| 73 | /// `temp_foo_bar` derefs to a Path that represents "<tdir.path()>/foo/bar" |
Janis Danisevskis | a51ccbc | 2020-11-25 21:04:24 -0800 | [diff] [blame] | 74 | pub fn build(&self) -> PathBuilder { |
| 75 | PathBuilder(self.path.clone()) |
| 76 | } |
| 77 | |
Janis Danisevskis | bf15d73 | 2020-12-08 10:35:26 -0800 | [diff] [blame] | 78 | /// When a test is failing you can set this to false in order to inspect |
| 79 | /// the directory structure after the test failed. |
| 80 | #[allow(dead_code)] |
| 81 | pub fn do_not_drop(&mut self) { |
| 82 | println!("Disabled automatic cleanup for: {:?}", self.path); |
| 83 | log::info!("Disabled automatic cleanup for: {:?}", self.path); |
| 84 | self.do_drop = false; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | impl Drop for TempDir { |
| 89 | fn drop(&mut self) { |
| 90 | if self.do_drop { |
| 91 | remove_dir_all(&self.path).expect("Cannot delete temporary dir."); |
| 92 | } |
| 93 | } |
| 94 | } |
Janis Danisevskis | a51ccbc | 2020-11-25 21:04:24 -0800 | [diff] [blame] | 95 | |
Janis Danisevskis | 2a8330a | 2021-01-20 15:34:26 -0800 | [diff] [blame] | 96 | /// Allows for convenient building of paths from a TempDir. See TempDir.build() for more details. |
Janis Danisevskis | a51ccbc | 2020-11-25 21:04:24 -0800 | [diff] [blame] | 97 | pub struct PathBuilder(PathBuf); |
| 98 | |
| 99 | impl PathBuilder { |
Janis Danisevskis | 2a8330a | 2021-01-20 15:34:26 -0800 | [diff] [blame] | 100 | /// Adds another segment to the end of the path. Consumes, modifies and returns self. |
Janis Danisevskis | a51ccbc | 2020-11-25 21:04:24 -0800 | [diff] [blame] | 101 | pub fn push(mut self, segment: &str) -> Self { |
| 102 | self.0.push(segment); |
| 103 | self |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | impl Deref for PathBuilder { |
| 108 | type Target = Path; |
| 109 | |
| 110 | fn deref(&self) -> &Self::Target { |
| 111 | &self.0 |
| 112 | } |
| 113 | } |
Rajesh Nyamagoud | 901386c | 2022-03-21 20:35:18 +0000 | [diff] [blame] | 114 | |
| 115 | /// Get Keystore2 service. |
| 116 | pub fn get_keystore_service() -> binder::Strong<dyn IKeystoreService> { |
| 117 | binder::get_interface(KS2_SERVICE_NAME).unwrap() |
| 118 | } |