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

added tests in rust #332

Closed
wants to merge 3 commits into from
Closed
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
14 changes: 14 additions & 0 deletions frontend/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions frontend/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ tauri-plugin-dialog = "2.0.0-beta.9"
tauri-plugin-store = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v2" }
ring = "0.16.20"
data-encoding = "2.3.2"
tokio = { version = "1", features = ["macros"] }
tempfile = "3"
arrayref = "0.3.6"
directories = "4.0"
chrono = { version = "0.4.26", features = ["serde"] }
Expand Down
4 changes: 4 additions & 0 deletions frontend/src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod repositories;
pub mod services;
pub mod models;
pub mod utils;
24 changes: 12 additions & 12 deletions frontend/src-tauri/src/services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use directories::ProjectDirs;
use rand::seq::SliceRandom;
use std::collections::HashSet;

const SECURE_FOLDER_NAME: &str = "secure_folder";
pub const SECURE_FOLDER_NAME: &str = "secure_folder";
const SALT_LENGTH: usize = 16;
const NONCE_LENGTH: usize = 12;

Expand Down Expand Up @@ -276,7 +276,7 @@ pub async fn save_edited_image(
Ok(())
}

fn apply_sepia(img: &DynamicImage) -> DynamicImage {
pub fn apply_sepia(img: &DynamicImage) -> DynamicImage {
let (width, height) = img.dimensions();
let mut sepia_img = ImageBuffer::new(width, height);

Expand All @@ -295,7 +295,7 @@ fn apply_sepia(img: &DynamicImage) -> DynamicImage {
DynamicImage::ImageRgba8(sepia_img)
}

fn adjust_brightness_contrast(img: &DynamicImage, brightness: i32, contrast: i32) -> DynamicImage {
pub fn adjust_brightness_contrast(img: &DynamicImage, brightness: i32, contrast: i32) -> DynamicImage {
let (width, height) = img.dimensions();
let mut adjusted_img = ImageBuffer::new(width, height);

Expand All @@ -320,15 +320,15 @@ fn adjust_brightness_contrast(img: &DynamicImage, brightness: i32, contrast: i32
DynamicImage::ImageRgba8(adjusted_img)
}

fn get_secure_folder_path() -> Result<PathBuf, String> {
pub fn get_secure_folder_path() -> Result<PathBuf, String> {
let project_dirs = ProjectDirs::from("com", "AOSSIE", "Pictopy")
.ok_or_else(|| "Failed to get project directories".to_string())?;
let mut path = project_dirs.data_dir().to_path_buf();
path.push(SECURE_FOLDER_NAME);
Ok(path)
}

fn generate_salt() -> [u8; SALT_LENGTH] {
pub fn generate_salt() -> [u8; SALT_LENGTH] {
let mut salt = [0u8; SALT_LENGTH];
SystemRandom::new().fill(&mut salt).unwrap();
salt
Expand Down Expand Up @@ -455,7 +455,7 @@ pub async fn get_secure_media(password: String) -> Result<Vec<SecureMedia>, Stri
Ok(secure_media)
}

fn hash_password(password: &str, salt: &[u8]) -> Vec<u8> {
pub fn hash_password(password: &str, salt: &[u8]) -> Vec<u8> {
let mut hash = [0u8; digest::SHA256_OUTPUT_LEN];
pbkdf2::derive(
pbkdf2::PBKDF2_HMAC_SHA256,
Expand All @@ -467,7 +467,7 @@ fn hash_password(password: &str, salt: &[u8]) -> Vec<u8> {
hash.to_vec()
}

fn encrypt_data(data: &[u8], password: &str) -> Result<Vec<u8>, ring::error::Unspecified> {
pub fn encrypt_data(data: &[u8], password: &str) -> Result<Vec<u8>, ring::error::Unspecified> {
let salt = generate_salt();
let key = derive_key(password, &salt);
let nonce = generate_nonce();
Expand All @@ -488,7 +488,7 @@ fn encrypt_data(data: &[u8], password: &str) -> Result<Vec<u8>, ring::error::Uns
Ok(result)
}

fn decrypt_data(encrypted: &[u8], password: &str) -> Result<Vec<u8>, String> {
pub fn decrypt_data(encrypted: &[u8], password: &str) -> Result<Vec<u8>, String> {
println!("Decrypting data...");

if encrypted.len() < SALT_LENGTH + NONCE_LENGTH + 16 {
Expand Down Expand Up @@ -539,7 +539,7 @@ pub async fn unlock_secure_folder(password: String) -> Result<bool, String> {
Ok(input_hash == stored_hash)
}

fn derive_key(password: &str, salt: &[u8]) -> LessSafeKey {
pub fn derive_key(password: &str, salt: &[u8]) -> LessSafeKey {
let mut key_bytes = [0u8; 32];
pbkdf2::derive(
pbkdf2::PBKDF2_HMAC_SHA256,
Expand All @@ -560,7 +560,7 @@ pub async fn check_secure_folder_status() -> Result<bool, String> {
Ok(config_path.exists())
}

fn generate_nonce() -> [u8; NONCE_LENGTH] {
pub fn generate_nonce() -> [u8; NONCE_LENGTH] {
let mut nonce = [0u8; NONCE_LENGTH];
SystemRandom::new().fill(&mut nonce).unwrap();
nonce
Expand Down Expand Up @@ -588,7 +588,7 @@ pub fn get_random_memories(directories: Vec<String>, count: usize) -> Result<Vec
Ok(selected_images)
}

fn get_images_from_directory(dir: &str) -> Result<Vec<MemoryImage>, String> {
pub fn get_images_from_directory(dir: &str) -> Result<Vec<MemoryImage>, String> {
let path = Path::new(dir);
if !path.is_dir() {
return Err(format!("{} is not a directory", dir));
Expand Down Expand Up @@ -620,7 +620,7 @@ fn get_images_from_directory(dir: &str) -> Result<Vec<MemoryImage>, String> {
Ok(images)
}

fn is_image_file(path: &Path) -> bool {
pub fn is_image_file(path: &Path) -> bool {
let extensions = ["jpg", "jpeg", "png", "gif"];
path.extension()
.and_then(|ext| ext.to_str())
Expand Down
Loading
Loading