diff --git a/cmd/implants/eldritch/Cargo.toml b/cmd/implants/eldritch/Cargo.toml index 86534905d..f57eb09de 100644 --- a/cmd/implants/eldritch/Cargo.toml +++ b/cmd/implants/eldritch/Cargo.toml @@ -5,6 +5,7 @@ edition = "2021" [dependencies] starlark = "0.6.0" +tempfile = "3.3.0" anyhow = "1.0.55" derive_more = "0.99.17" sha256 = "1.0.3" \ No newline at end of file diff --git a/cmd/implants/eldritch/src/file/append_impl.rs b/cmd/implants/eldritch/src/file/append_impl.rs index f4a9285af..895deb30b 100644 --- a/cmd/implants/eldritch/src/file/append_impl.rs +++ b/cmd/implants/eldritch/src/file/append_impl.rs @@ -21,51 +21,53 @@ mod tests { use std::io::BufReader; use std::fs::File; use std::fs::remove_file; + use tempfile::NamedTempFile; + #[test] fn test_append_nonexisting() -> anyhow::Result<()> { - // In Case Cleanup failed - let _ = remove_file(String::from("/tmp/win")); + // Create file and then delete it (so we know it doesnt exist) + let tmp_file = NamedTempFile::new()?; + let path = String::from(tmp_file.path().to_str().unwrap()).clone(); + tmp_file.close()?; // Run our code - append(String::from("/tmp/win"), String::from("Hi2!"))?; + append(path.clone(), String::from("Hi2!"))?; // Read the file - let file = BufReader::new(File::open("/tmp/win")?); + let file_reader = BufReader::new(File::open(path.clone())?); // Get Last Line - let last_line = file.lines().last().unwrap()?; + let last_line = file_reader.lines().last().unwrap()?; // Make sure the last line equals == Hi2! assert_eq!(last_line, "Hi2!"); // Cleanup - remove_file(String::from("/tmp/win"))?; + remove_file(path)?; Ok(()) } #[test] fn test_append_existing() -> anyhow::Result<()> { - // In Case Cleanup failed - let _ = remove_file(String::from("/tmp/win")); + // Create file + let mut tmp_file = NamedTempFile::new()?; + let path = String::from(tmp_file.path().to_str().unwrap()); - // Make New File - let mut file = File::create("/tmp/win")?; - file.write_all(b"Hello, world!\n")?; + // Write to New File + tmp_file.write_all(b"Hello, world!\n")?; // Run our code - append(String::from("/tmp/win"), String::from("Hi2!"))?; + append(path, String::from("Hi2!"))?; // Read the file - let file = BufReader::new(File::open("/tmp/win")?); + let file_reader = BufReader::new(tmp_file); // Get Last Line - let last_line = file.lines().last().unwrap()?; + let last_line = file_reader.lines().last().unwrap()?; // Make sure the last line equals == Hi2! assert_eq!(last_line, "Hi2!"); - // Cleanup - let _ = remove_file(String::from("/tmp/win")); Ok(()) } } diff --git a/cmd/implants/eldritch/src/file/copy_impl.rs b/cmd/implants/eldritch/src/file/copy_impl.rs index 7f9de535d..d78f4343b 100644 --- a/cmd/implants/eldritch/src/file/copy_impl.rs +++ b/cmd/implants/eldritch/src/file/copy_impl.rs @@ -10,33 +10,29 @@ pub fn copy(src: String, dst: String) -> Result<()> { #[cfg(test)] mod tests { use super::*; - use std::fs::File; use std::io::prelude::*; - use std::fs::remove_file; + use tempfile::NamedTempFile; #[test] fn test_copy() -> anyhow::Result<()>{ - let _ = remove_file(String::from("/tmp/win_copy1")); - let _ = remove_file(String::from("/tmp/win_copy2")); + // Create files + let mut tmp_file_src = NamedTempFile::new()?; + let path_src = String::from(tmp_file_src.path().to_str().unwrap()); + let mut tmp_file_dst = NamedTempFile::new()?; + let path_dst = String::from(tmp_file_dst.path().to_str().unwrap()); - // Create file - let mut file = File::create("/tmp/win_copy1")?; // Write to file - file.write_all(b"Hello, world!")?; + tmp_file_src.write_all(b"Hello, world!")?; // Run our code - copy(String::from("/tmp/win_copy1"), String::from("/tmp/win_copy2"))?; + copy(path_src, path_dst)?; - // Open copied file - let mut winfile = File::open("/tmp/win_copy2")?; // Read let mut contents = String::new(); - winfile.read_to_string(&mut contents)?; + tmp_file_dst.read_to_string(&mut contents)?; // Compare assert_eq!(contents, "Hello, world!"); - remove_file(String::from("/tmp/win_copy1"))?; - remove_file(String::from("/tmp/win_copy2"))?; Ok(()) } } diff --git a/cmd/implants/eldritch/src/file/exists_impl.rs b/cmd/implants/eldritch/src/file/exists_impl.rs index 3de399c3d..99d12d097 100644 --- a/cmd/implants/eldritch/src/file/exists_impl.rs +++ b/cmd/implants/eldritch/src/file/exists_impl.rs @@ -9,56 +9,61 @@ pub fn exists(path: String) -> Result { #[cfg(test)] mod tests { use super::*; - use std::fs::File; use std::io::prelude::*; - use std::fs::remove_file; + use tempfile::{NamedTempFile,tempdir}; + #[test] fn test_exists_file() -> anyhow::Result<()>{ - let winfile = "/tmp/win_test_exists_does"; - let _ = remove_file(String::from(winfile)); - // Create file - let mut file = File::create(winfile)?; + // Create files + let mut tmp_file = NamedTempFile::new()?; + let path = String::from(tmp_file.path().to_str().unwrap()); + // Write to file - file.write_all(b"Hello, world!")?; + tmp_file.write_all(b"Hello, world!")?; // Run our code - let res = exists(String::from(winfile))?; + let res = exists(path)?; assert_eq!(res, true); - remove_file(String::from(winfile))?; Ok(()) } #[test] fn test_exists_no_file() -> anyhow::Result<()>{ - let winfile = "/tmp/win_test_exists_doesnt"; - let _ = remove_file(String::from(winfile)); + // Create file and then delete it (so we know it doesnt exist) + let tmp_file = NamedTempFile::new()?; + let path = String::from(tmp_file.path().to_str().unwrap()).clone(); + tmp_file.close()?; // Run our code - let res = exists(String::from(winfile))?; + let res = exists(path)?; assert_eq!(res, false); - let _ = remove_file(String::from(winfile)); Ok(()) } #[test] fn test_exists_dir() -> anyhow::Result<()>{ - let winfile = "/tmp/"; + // Create Dir + let dir = tempdir()?; + let path = String::from(dir.path().to_str().unwrap()); // Run our code - let res = exists(String::from(winfile))?; + let res = exists(path)?; assert_eq!(res, true); Ok(()) } #[test] fn test_exists_no_dir() -> anyhow::Result<()>{ - let winfile = "/aoeu/"; + // Create Dir and then delete it (so we know it doesnt exist) + let dir = tempdir()?; + let path = String::from(dir.path().to_str().unwrap()).clone(); + dir.close()?; // Run our code - let res = exists(String::from(winfile))?; + let res = exists(path)?; assert_eq!(res, false); Ok(()) diff --git a/cmd/implants/eldritch/src/file/hash_impl.rs b/cmd/implants/eldritch/src/file/hash_impl.rs index 05a932a3b..9d81d7639 100644 --- a/cmd/implants/eldritch/src/file/hash_impl.rs +++ b/cmd/implants/eldritch/src/file/hash_impl.rs @@ -9,25 +9,23 @@ pub fn hash(path: String) -> Result { #[cfg(test)] mod tests { use super::*; - use std::fs::File; use std::io::prelude::*; - use std::fs::remove_file; + use tempfile::NamedTempFile; #[test] fn test_hash() -> anyhow::Result<()>{ - let winfile = "/tmp/win_test_exists_does"; - let _ = remove_file(String::from(winfile)); // Create file - let mut file = File::create(winfile)?; + let mut tmp_file = NamedTempFile::new()?; + let path = String::from(tmp_file.path().to_str().unwrap()); + // Write to file - file.write_all(b"aoeu")?; + tmp_file.write_all(b"aoeu")?; // Run our code - let res = hash(String::from(winfile))?; + let res = hash(path)?; assert_eq!(res, "bc4c24181ed3ce6666444deeb95e1f61940bffee70dd13972beb331f5d111e9b"); - remove_file(String::from(winfile))?; Ok(()) } } \ No newline at end of file