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/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/src/test/utils.rs
deleted file mode 100644
index 8c93859..0000000
--- a/keystore2/src/test/utils.rs
+++ /dev/null
@@ -1,84 +0,0 @@
-// Copyright 2020, The Android Open Source Project
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-use std::fs::{create_dir, remove_dir_all};
-use std::io::ErrorKind;
-use std::path::{Path, PathBuf};
-use std::{env::temp_dir, ops::Deref};
-
-#[derive(Debug)]
-pub struct TempDir {
- path: std::path::PathBuf,
- do_drop: bool,
-}
-
-impl TempDir {
- pub fn new(prefix: &str) -> std::io::Result<Self> {
- let tmp = loop {
- let mut tmp = temp_dir();
- let number: u16 = rand::random();
- tmp.push(format!("{}_{:05}", prefix, number));
- match create_dir(&tmp) {
- Err(e) => match e.kind() {
- ErrorKind::AlreadyExists => continue,
- _ => return Err(e),
- },
- Ok(()) => break tmp,
- }
- };
- Ok(Self { path: tmp, do_drop: true })
- }
-
- pub fn path(&self) -> &Path {
- &self.path
- }
-
- pub fn build(&self) -> PathBuilder {
- PathBuilder(self.path.clone())
- }
-
- /// When a test is failing you can set this to false in order to inspect
- /// the directory structure after the test failed.
- #[allow(dead_code)]
- pub fn do_not_drop(&mut self) {
- println!("Disabled automatic cleanup for: {:?}", self.path);
- log::info!("Disabled automatic cleanup for: {:?}", self.path);
- self.do_drop = false;
- }
-}
-
-impl Drop for TempDir {
- fn drop(&mut self) {
- if self.do_drop {
- remove_dir_all(&self.path).expect("Cannot delete temporary dir.");
- }
- }
-}
-
-pub struct PathBuilder(PathBuf);
-
-impl PathBuilder {
- pub fn push(mut self, segment: &str) -> Self {
- self.0.push(segment);
- self
- }
-}
-
-impl Deref for PathBuilder {
- type Target = Path;
-
- fn deref(&self) -> &Self::Target {
- &self.0
- }
-}