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
diff --git a/keystore2/Android.bp b/keystore2/Android.bp
index 2ed2a60..70363ef 100644
--- a/keystore2/Android.bp
+++ b/keystore2/Android.bp
@@ -40,6 +40,16 @@
],
}
+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 @@
"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 344fe2f..225378f 100644
--- a/keystore2/src/database.rs
+++ b/keystore2/src/database.rs
@@ -1792,7 +1792,7 @@
};
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 34a0eca..230a82c 100644
--- a/keystore2/src/legacy_blob.rs
+++ b/keystore2/src/legacy_blob.rs
@@ -775,7 +775,7 @@
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 0475d6f..811db91 100644
--- a/keystore2/src/lib.rs
+++ b/keystore2/src/lib.rs
@@ -34,8 +34,3 @@
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 8c93859..627af20 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 @@
}
impl TempDir {
+ /// Creates a temporary directory with a name of the form <prefix>_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<Self> {
let tmp = loop {
let mut tmp = temp_dir();
@@ -40,10 +48,20 @@
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 "<tdir.path()>/foo/bar"
pub fn build(&self) -> PathBuilder {
PathBuilder(self.path.clone())
}
@@ -66,9 +84,11 @@
}
}
+/// 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