Merge "Keystore 2.0: Move test utils to separate library."

This commit is contained in:
Treehugger Robot 2021-01-27 01:22:00 +00:00 committed by Gerrit Code Review
commit b0b1b0a049
5 changed files with 33 additions and 7 deletions

View file

@ -41,6 +41,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",
@ -62,6 +72,7 @@ rust_test {
"libkeystore2_crypto_rust",
"libkeystore2_km_compat",
"libkeystore2_selinux",
"libkeystore2_test_utils",
"liblazy_static",
"liblibc",
"liblibsqlite3_sys",

View file

@ -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,

View file

@ -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() {

View file

@ -34,8 +34,3 @@ mod async_task;
mod db_utils;
mod gc;
mod super_key;
#[cfg(test)]
mod test {
pub mod utils;
}

View file

@ -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 <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 @@ 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 "<tdir.path()>/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