Skip to content

Commit

Permalink
Merge pull request #513 from Raspirus/testcov
Browse files Browse the repository at this point in the history
Adding test cases and increasing coverage
  • Loading branch information
Benji377 authored Dec 22, 2023
2 parents af2a31d + 7dcd255 commit 2a09cf5
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 23 deletions.
6 changes: 6 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ component_management:
name: tests
paths:
- src-tauri/src/tests/**

ignore:
- "**/tests/**" # ignore all files in the tests folder
- "**/tests" # ignore the tests folder itself
- "src-tauri/src/backend/utils/**" # ignore all files in the utils folder
- "src-tauri/src/main.rs" # ignore the main.rs file
2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ sysinfo = "0.29"
reqwest = { version = "0.11.23", features = ["blocking"] }
rusqlite = { version = "0.30.0", features = ["bundled"] }
terminal_size = "0.3.0"
tokio = "1.35.1"
tokio = {version = "1.35.1", features = ["rt", "macros"]}
#walkdir = "2.4.0"
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
Expand Down
Binary file added src-tauri/signatures.db
Binary file not shown.
6 changes: 2 additions & 4 deletions src-tauri/src/backend/scanner.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{
fs::{self, File},
path::Path,
process::exit, time, io::Read,
time, io::Read,
};

use chrono::{DateTime, Local};
Expand Down Expand Up @@ -43,13 +43,11 @@ impl Scanner {
// Creates a FileScanner object
pub fn new(db_file: &str, t_win: Option<tauri::Window>) -> Result<Self, String> {
//check if the path that should be scanned exists


let tmpconf = match DBOps::new(db_file, None) {
Ok(db_conn) => db_conn,
Err(err) => {
error!("{err}");
exit(-1);
return Err(format!("Failed to initialize database: {err}"));
}
};

Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/backend/utils/scanner_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::backend::{config_file::Config, scanner};

static DB_NAME: &str = "signatures.db";

// There are tow equal functions here, one is async and gets called from the GUI to ensure the main thread doesn't stop
// There are two equal functions here, one is async and gets called from the GUI to ensure the main thread doesn't stop
// The second one is sync and is called from the CLI. The second one can probably be rewritten

pub async fn start_scanner(window: Option<tauri::Window>, path: String) -> Result<String, String> {
Expand Down
33 changes: 25 additions & 8 deletions src-tauri/src/tests/db_ops_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,6 @@ mod tests {
"ec2112c9c243d837247217baf351ab79".to_owned(),
)])
.unwrap();

// Check if the hash exists in the database
// let exists_in_db = db_ops.hash_exists(hash_to_insert).unwrap();
// let does_not_exist = db_ops.hash_exists(hash_not_in_db).unwrap();

// Assert the results
//assert_eq!(exists_in_db, Some(true));
//assert_eq!(does_not_exist, Some(false));
}

#[test]
Expand Down Expand Up @@ -108,6 +100,31 @@ mod tests {
assert!(db_ops.get_db_files().is_some());
}

#[test]
fn test_update_db_timeout() {
// Run the update_db function for 10 seconds, then kill it if no errors occurred before
let mut db_ops = DBOps::new(DB_FILE_LOC, None).unwrap();
let _ = std::thread::spawn(move || {
let result = db_ops.update_db();
if let Err(err) = result {
error!("Error occurred: {}", err);
assert!(false);
}
});
std::thread::sleep(std::time::Duration::from_secs(10));

// If we got here, it means that the update_db function ran for 10 seconds without errors
// So we can safely assume that the function works as expected
assert!(true);
}

#[test]
fn test_db_diff_func() {
let db_ops = DBOps::new(DB_FILE_LOC, None).unwrap();
let diff = db_ops.get_diff_file();
assert!(!diff.is_empty());
}

#[cfg(test)]
#[ctor::dtor]
fn teardown() {
Expand Down
62 changes: 53 additions & 9 deletions src-tauri/src/tests/file_scanner_test.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
#[cfg(test)]
mod tests {
use log::debug;

use crate::backend::scanner::Scanner;
use std::env;

const DB_FILE_LOC: &str = "signatures.db";

#[test]
fn test_new_filescanner_valid_path() {
let scanner = Scanner::new(DB_FILE_LOC, None);
assert!(scanner.is_ok());
}

#[test]
fn test_new_filescanner_invalid_path() {
// Invalid path to the database file
let db_file_loc = "non-existent-dir/signatures.db";
let scanner = Scanner::new(db_file_loc, None);
assert!(scanner.is_err());
}

#[test]
fn test_init_invalid_path() {
let t_win = None;
// Invalid path to the directory to scan
let scanloc = "non-existent-dir";
let scanner = Scanner::new(DB_FILE_LOC, t_win).unwrap();
let dirty_files = scanner.init(false, scanloc);
assert!(dirty_files.is_err());
}

#[test]
fn test_search_files() {
let t_win = None;

// Get the parent directory of the current test file as the scan location
Expand All @@ -17,24 +39,46 @@ mod tests {
.to_string_lossy()
.to_string();

debug!("SCAN LOCATION: {:?}", scanloc);
let scanner = Scanner::new(DB_FILE_LOC, t_win).unwrap();
let dirty_files = scanner.init(false, &scanloc).unwrap();
// Assert that the list of dirty_files is empty since we didn't add any malicious files
assert_eq!(dirty_files.len(), 0);
}

let scanner = Scanner::new( DB_FILE_LOC, t_win).unwrap();
// Check if the scanner is initialized properly
assert_eq!(scanner.dirty_files.len(), 0);
#[test]
fn test_scan_single_file() {
let t_win = None;

// Get the parent directory of the current test file as the scan location
let mut scanloc = env::current_dir()
.expect("Failed to get current directory")
.to_string_lossy()
.to_string();

// Scan the current test file
scanloc.push_str("/src/tests/file_scanner_test.rs");

let scanner = Scanner::new(DB_FILE_LOC, t_win).unwrap();
let dirty_files = scanner.init(false, &scanloc).unwrap();
// Assert that the list of dirty_files is empty since we didn't add any malicious files
assert_eq!(dirty_files.len(), 0);
}

#[test]
fn test_search_files() {
fn test_scan_zip_file() {
let t_win = None;

// Get the parent directory of the current test file as the scan location
let scanloc = env::current_dir()
let mut scanloc = env::current_dir()
.expect("Failed to get current directory")
.to_string_lossy()
.to_string();
// Warning, this returns the src-tauri folder

// Scan the zip sample file
scanloc.push_str("/src/tests/test_sample.zip");

let scanner = Scanner::new( DB_FILE_LOC, t_win).unwrap();
let scanner = Scanner::new(DB_FILE_LOC, t_win).unwrap();
let dirty_files = scanner.init(false, &scanloc).unwrap();
// Assert that the list of dirty_files is empty since we didn't add any malicious files
assert_eq!(dirty_files.len(), 0);
Expand Down
Binary file added src-tauri/src/tests/test_sample.zip
Binary file not shown.

0 comments on commit 2a09cf5

Please sign in to comment.