From b39ca24758b81ee241f19fc5cdcbfee77685a6d3 Mon Sep 17 00:00:00 2001
From: Xiaohe Yang <yangxiaohe2@huawei.com>
Date: Mon, 22 Feb 2021 10:48:24 +0800
Subject: [PATCH 1/2] address_space: update temp file creation in unit-test

1. Instead of creating file in `/tmp` directory, this commit update to use
TempFile in vmm-sys-util crate.
2. Update test functions' name in host_mmap.rs.

Signed-off-by: Xiaohe Yang <yangxiaohe2@huawei.com>
---
 address_space/src/host_mmap.rs | 34 +++++++++++++++++++++++++---------
 address_space/src/region.rs    | 12 +++++++-----
 2 files changed, 32 insertions(+), 14 deletions(-)

diff --git a/address_space/src/host_mmap.rs b/address_space/src/host_mmap.rs
index 7c8e283..77c3062 100644
--- a/address_space/src/host_mmap.rs
+++ b/address_space/src/host_mmap.rs
@@ -319,8 +319,15 @@ mod test {
     }
 
     #[test]
-    fn test_file_backend() {
-        let file_path = String::from("/tmp/");
+    fn test_file_backend_with_dir() {
+        // Create file backend in the current directory,
+        // and the file will be removed after test-thread exits.
+        let file_path = std::env::current_dir()
+            .unwrap()
+            .as_path()
+            .to_str()
+            .unwrap()
+            .to_string();
         let file_size = 100u64;
         let f_back = FileBackend::new(&file_path, file_size);
         assert!(f_back.is_ok());
@@ -328,7 +335,9 @@ mod test {
     }
 
     #[test]
-    fn test_create_file_backend() {
+    fn test_file_backend_with_file() {
+        // Create file backend in the current directory,
+        // and the file will be removed after test-thread exits.
         let file_path = String::from("back_mem_test1");
         let file_size = 100_u64;
         let f_back = FileBackend::new(&file_path, file_size);
@@ -341,17 +350,18 @@ mod test {
     }
 
     #[test]
-    fn test_exist_file_backend() {
+    fn test_file_backend_with_exist_file() {
+        // Create file backend in the current directory, and the file is removed manually.
         let file_path = String::from("back_mem_test2");
         let file = File::create(file_path.clone()).unwrap();
         file.set_len(50_u64).unwrap();
 
-        let file_size = 100_u64;
-        let f_back = FileBackend::new(&file_path, file_size);
+        let mem_size = 100_u64;
+        let f_back = FileBackend::new(&file_path, mem_size);
         assert!(f_back.is_err());
 
-        let file_size = 20_u64;
-        let f_back = FileBackend::new(&file_path, file_size);
+        let mem_size = 20_u64;
+        let f_back = FileBackend::new(&file_path, mem_size);
         assert!(f_back.is_ok());
         assert_eq!(f_back.as_ref().unwrap().offset, 0u64);
         assert_eq!(
@@ -365,9 +375,15 @@ mod test {
     #[test]
     fn test_create_host_mmaps() {
         let addr_ranges = [(0x0, 0x10_0000), (0x100000, 0x10_0000)];
+        let mem_path = std::env::current_dir()
+            .unwrap()
+            .as_path()
+            .to_str()
+            .unwrap()
+            .to_string();
         let mem_config = MachineMemConfig {
             mem_size: 0x20_0000,
-            mem_path: Some(String::from("/tmp")),
+            mem_path: Some(mem_path),
             dump_guest_core: false,
             mem_share: false,
         };
diff --git a/address_space/src/region.rs b/address_space/src/region.rs
index 679090f..86f807c 100644
--- a/address_space/src/region.rs
+++ b/address_space/src/region.rs
@@ -707,6 +707,7 @@ mod test {
 
     use libc::EFD_NONBLOCK;
     use vmm_sys_util::eventfd::EventFd;
+    use vmm_sys_util::tempfile::TempFile;
 
     use super::*;
 
@@ -778,8 +779,9 @@ mod test {
         let rgn_start = GuestAddress(0);
         let host_mmap = HostMemMapping::new(GuestAddress(0), 1024u64, None, false, false).unwrap();
         let ram_region = Region::init_ram_region(Arc::new(host_mmap));
-        let mut file = std::fs::File::create("/tmp/test_read_write_buffer.tmp").unwrap();
-        let mut file_read = std::fs::File::open("/tmp/test_read_write_buffer.tmp").unwrap();
+
+        let file = TempFile::new().unwrap();
+        let mut file_read = std::fs::File::open(file.as_path()).unwrap();
         let slice: [u8; 24] = [91; 24];
         let mut res_slice: [u8; 24] = [0; 24];
         let mut res_slice2: [u8; 24] = [0; 24];
@@ -790,7 +792,9 @@ mod test {
             .unwrap();
 
         // read the ram to the file, then check the file's content
-        assert!(ram_region.read(&mut file, rgn_start, 1000, 24).is_ok());
+        assert!(ram_region
+            .read(&mut file.as_file(), rgn_start, 1000, 24)
+            .is_ok());
         assert!(file_read.read(&mut res_slice).is_ok());
         assert_eq!(&slice, &mut res_slice);
 
@@ -802,8 +806,6 @@ mod test {
             .read(&mut res_slice2.as_mut(), rgn_start, 0, 24)
             .unwrap();
         assert_eq!(&slice, &mut res_slice2);
-
-        std::fs::remove_file("/tmp/test_read_write_buffer.tmp").unwrap();
     }
 
     #[test]
-- 
2.27.0

