From 2a8330a90dab5c7dd6aa76bfb376fd97eccd00e5 Mon Sep 17 00:00:00 2001 From: Janis Danisevskis Date: Wed, 20 Jan 2021 15:34:26 -0800 Subject: [PATCH] Keystore 2.0: Move test utils to separate library. Move TempDir test utils to separate library for easier reuse. Test: keystore2_test Change-Id: If1edfde39b66efa43f8a5ed32a500fad57291512 --- keystore2/Android.bp | 11 ++++++++++ keystore2/src/database.rs | 2 +- keystore2/src/legacy_blob.rs | 2 +- keystore2/src/lib.rs | 5 ----- .../{src/test/utils.rs => test_utils/lib.rs} | 20 +++++++++++++++++++ 5 files changed, 33 insertions(+), 7 deletions(-) rename keystore2/{src/test/utils.rs => test_utils/lib.rs} (67%) diff --git a/keystore2/Android.bp b/keystore2/Android.bp index 2ed2a60f..70363ef4 100644 --- a/keystore2/Android.bp +++ b/keystore2/Android.bp @@ -40,6 +40,16 @@ rust_library { ], } +rust_library { + name: "libkeystore2_test_utils", + crate_name: "keystore2_test_utils", + srcs: ["test_utils/lib.rs"], + rustlibs: [ + "liblog_rust", + "librand", + ] +} + rust_test { name: "keystore2_test", crate_name: "keystore2", @@ -60,6 +70,7 @@ rust_test { "libkeystore2_crypto_rust", "libkeystore2_km_compat", "libkeystore2_selinux", + "libkeystore2_test_utils", "liblazy_static", "liblibc", "liblibsqlite3_sys", diff --git a/keystore2/src/database.rs b/keystore2/src/database.rs index 344fe2fc..225378f4 100644 --- a/keystore2/src/database.rs +++ b/keystore2/src/database.rs @@ -1792,7 +1792,7 @@ mod tests { }; use crate::key_perm_set; use crate::permission::{KeyPerm, KeyPermSet}; - use crate::test::utils::TempDir; + use keystore2_test_utils::TempDir; use android_hardware_security_keymint::aidl::android::hardware::security::keymint::{ HardwareAuthToken::HardwareAuthToken, HardwareAuthenticatorType::HardwareAuthenticatorType as kmhw_authenticator_type, diff --git a/keystore2/src/legacy_blob.rs b/keystore2/src/legacy_blob.rs index 34a0ecab..230a82cb 100644 --- a/keystore2/src/legacy_blob.rs +++ b/keystore2/src/legacy_blob.rs @@ -775,7 +775,7 @@ mod test { mod legacy_blob_test_vectors; use crate::error; use crate::legacy_blob::test::legacy_blob_test_vectors::*; - use crate::test::utils::TempDir; + use keystore2_test_utils::TempDir; #[test] fn decode_encode_alias_test() { diff --git a/keystore2/src/lib.rs b/keystore2/src/lib.rs index 0475d6ff..811db91b 100644 --- a/keystore2/src/lib.rs +++ b/keystore2/src/lib.rs @@ -34,8 +34,3 @@ mod async_task; mod db_utils; mod gc; mod super_key; - -#[cfg(test)] -mod test { - pub mod utils; -} diff --git a/keystore2/src/test/utils.rs b/keystore2/test_utils/lib.rs similarity index 67% rename from keystore2/src/test/utils.rs rename to keystore2/test_utils/lib.rs index 8c938596..627af20c 100644 --- a/keystore2/src/test/utils.rs +++ b/keystore2/test_utils/lib.rs @@ -12,11 +12,14 @@ // See the License for the specific language governing permissions and // limitations under the License. +//! Implements TempDir which aids in creating an cleaning up temporary directories for testing. + use std::fs::{create_dir, remove_dir_all}; use std::io::ErrorKind; use std::path::{Path, PathBuf}; use std::{env::temp_dir, ops::Deref}; +/// Represents the lifecycle of a temporary directory for testing. #[derive(Debug)] pub struct TempDir { path: std::path::PathBuf, @@ -24,6 +27,11 @@ pub struct TempDir { } impl TempDir { + /// Creates a temporary directory with a name of the form _NNNNN where NNNNN is a zero + /// padded random number with 5 figures. The prefix must not contain file system separators. + /// The location of the directory cannot be chosen. + /// The directory with all of its content is removed from the file system when the resulting + /// object gets dropped. pub fn new(prefix: &str) -> std::io::Result { let tmp = loop { let mut tmp = temp_dir(); @@ -40,10 +48,20 @@ impl TempDir { Ok(Self { path: tmp, do_drop: true }) } + /// Returns the absolute path of the temporary directory. pub fn path(&self) -> &Path { &self.path } + /// Returns a path builder for convenient extension of the path. + /// + /// ## Example: + /// + /// ``` + /// let tdir = TempDir::new("my_test")?; + /// let temp_foo_bar = tdir.build().push("foo").push("bar"); + /// ``` + /// `temp_foo_bar` derefs to a Path that represents "/foo/bar" pub fn build(&self) -> PathBuilder { PathBuilder(self.path.clone()) } @@ -66,9 +84,11 @@ impl Drop for TempDir { } } +/// Allows for convenient building of paths from a TempDir. See TempDir.build() for more details. pub struct PathBuilder(PathBuf); impl PathBuilder { + /// Adds another segment to the end of the path. Consumes, modifies and returns self. pub fn push(mut self, segment: &str) -> Self { self.0.push(segment); self