Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use tempfile for Eldritch Tests #12

Merged
merged 1 commit into from
Mar 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/implants/eldritch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
34 changes: 18 additions & 16 deletions cmd/implants/eldritch/src/file/append_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
}
22 changes: 9 additions & 13 deletions cmd/implants/eldritch/src/file/copy_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
}
39 changes: 22 additions & 17 deletions cmd/implants/eldritch/src/file/exists_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,56 +9,61 @@ pub fn exists(path: String) -> Result<bool> {
#[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(())
Expand Down
14 changes: 6 additions & 8 deletions cmd/implants/eldritch/src/file/hash_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,23 @@ pub fn hash(path: String) -> Result<String> {
#[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(())
}
}