From 84c6fb214bba89cda7c2331bd685931947978044 Mon Sep 17 00:00:00 2001 From: Mahdi Robatipoor Date: Tue, 5 Mar 2024 02:02:04 +0330 Subject: [PATCH] refactor test input --- Cargo.toml | 4 +- cli/src/command.rs | 23 +++-- cli/src/main.rs | 6 +- cli/src/util/crypto.rs | 5 +- cli/tests/cli/copy_and_paste_cli_test.rs | 92 +++++++++++++++++++ cli/tests/cli/encrypt_and_decrypt_cli_test.rs | 15 ++- cli/tests/cli/helper/mod.rs | 14 ++- cli/tests/cli/main.rs | 1 + cli/tests/cli/upload_and_download_cli_test.rs | 8 +- sdk/Cargo.toml | 1 - sdk/src/client.rs | 12 +-- sdk/src/util/crypto.rs | 6 +- 12 files changed, 144 insertions(+), 43 deletions(-) create mode 100644 cli/tests/cli/copy_and_paste_cli_test.rs diff --git a/Cargo.toml b/Cargo.toml index 88dbb99..4706e4e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,13 +37,14 @@ tokio = { version = "1.36.0", features = [ "process", "net", "rt-multi-thread", + "io-std", ] } tokio-util = { version = "0.7.10", features = ["io"] } tokio-rustls = "0.25.0" tower = { version = "0.4.13", features = ["util", "make"] } tower-http = { version = "0.5.2", features = ["fs", "cors"] } tower-service = "0.3.2" -base64 = "0.21.7" +base64 = "0.22.0" bincode = "1.3.3" build_html = "2.4.0" chrono = { version = "0.4.34", features = ["serde"] } @@ -55,7 +56,6 @@ fake = { version = "2.9.2", features = ['derive', 'uuid', 'chrono'] } futures-util = "0.3.30" indicatif = "0.17.8" log = "0.4.20" -log-derive = "0.4.1" mime_guess = "2.0.4" once_cell = { version = "1.19.0" } qrcode = "0.13" diff --git a/cli/src/command.rs b/cli/src/command.rs index 5610bf4..422d17d 100644 --- a/cli/src/command.rs +++ b/cli/src/command.rs @@ -10,6 +10,7 @@ use sdk::{ }, }; use std::path::{Path, PathBuf}; +use tokio::io::{AsyncRead, AsyncWrite}; use url::Url; use crate::{args::UploadOutput, client::CommandLineClient}; @@ -81,7 +82,10 @@ pub async fn upload(args: UploadArguments) { }; } -pub async fn copy(args: CopyArguments) { +pub async fn copy(reader: R, args: CopyArguments) +where + R: AsyncRead + Send + Unpin + 'static, +{ let client = CommandLineClient::new(args.server_addr); let param = UploadQueryParam { max_download: args.max_download, @@ -90,21 +94,20 @@ pub async fn copy(args: CopyArguments) { allow_manual_deletion: args.allow_manual_deletion, qr_code_format: None, }; - let stdin = tokio::io::stdin(); let (_, resp) = if let Some(key_nonce) = args.key_nonce.as_ref() { client .upload_encrypt( key_nonce, args.file_name, "text/plain", - stdin, + reader, ¶m, args.auth, ) .await } else { client - .upload_reader(args.file_name, "text/plain", stdin, ¶m, args.auth) + .upload_reader(args.file_name, "text/plain", reader, ¶m, args.auth) .await } .unwrap(); @@ -162,20 +165,22 @@ pub async fn download( } } -pub async fn paste( +pub async fn paste( server_addr: String, auth: Option<(String, String)>, url_path: FileUrlPath, key_nonce: Option, -) { + writer: W, +) where + W: AsyncWrite + Unpin, +{ let client = CommandLineClient::new(server_addr); - let stdout = tokio::io::stdout(); let (_, resp) = if let Some(key_nonce) = key_nonce.as_ref() { client - .download_and_decrypt(key_nonce, &url_path, auth, stdout) + .download_and_decrypt(key_nonce, &url_path, auth, writer) .await } else { - client.download_and_write(&url_path, auth, stdout).await + client.download_and_write(&url_path, auth, writer).await } .unwrap(); diff --git a/cli/src/main.rs b/cli/src/main.rs index 459b1e2..9e78a8c 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -51,6 +51,7 @@ async fn main() { key_nonce, } => { let server_addr = args.server_addr.expect("Server address should be set."); + let stdin = tokio::io::stdin(); let args = CopyArguments { server_addr, auth: args.auth, @@ -62,7 +63,7 @@ async fn main() { output, key_nonce, }; - command::copy(args).await; + command::copy(stdin, args).await; } SubCommand::Download { progress_bar, @@ -86,7 +87,8 @@ async fn main() { key_nonce, } => { let server_addr = args.server_addr.expect("Server address should be set."); - command::paste(server_addr, args.auth, url_path, key_nonce).await; + let stdout: tokio::io::Stdout = tokio::io::stdout(); + command::paste(server_addr, args.auth, url_path, key_nonce, stdout).await; } SubCommand::Info { url_path } => { let server_addr = args.server_addr.expect("Server address should be set."); diff --git a/cli/src/util/crypto.rs b/cli/src/util/crypto.rs index 2e70a44..da9b115 100644 --- a/cli/src/util/crypto.rs +++ b/cli/src/util/crypto.rs @@ -45,6 +45,7 @@ mod tests { use sdk::util::{ crypto::{KeyType, NonceType}, + random::generate_random_string, test::FileTestContext, }; @@ -54,8 +55,8 @@ mod tests { #[tokio::test] pub async fn test_encrypt_upload_file_and_decrypt_download_file(ctx: &mut FileTestContext) { let key_nonce = KeyNonce { - key: KeyType::new("01234567890123456789012345678912").unwrap(), - nonce: NonceType::new("1234567891213141516").unwrap(), + key: KeyType::new(&generate_random_string(32)).unwrap(), + nonce: NonceType::new(&generate_random_string(19)).unwrap(), }; let contents: String = Faker.fake::(); let plaintext_file = ctx.temp_path.join("file.txt"); diff --git a/cli/tests/cli/copy_and_paste_cli_test.rs b/cli/tests/cli/copy_and_paste_cli_test.rs new file mode 100644 index 0000000..7687af0 --- /dev/null +++ b/cli/tests/cli/copy_and_paste_cli_test.rs @@ -0,0 +1,92 @@ +use assert_cmd::Command; +use fake::{Fake, Faker}; + +use crate::helper::{generate_random_key_nonce, CliTestContext}; + +#[test_context::test_context(CliTestContext)] +#[tokio::test] +async fn test_copy_and_paste_command(ctx: &mut CliTestContext) { + let (file, expected_content) = ctx.create_dummy_file().await.unwrap(); + let url_path = Command::cargo_bin("cli") + .unwrap() + .args([ + "--server-addr", + &ctx.server_addr, + "copy", + "--source-file", + "--output", + "url-path", + ]) + // .pipe_stdin(file) + .output() + .unwrap() + .stdout; + let url_path = std::str::from_utf8(&url_path).unwrap().trim(); + let destination_dir = ctx.workspace.join("destination_dir"); + tokio::fs::create_dir_all(&destination_dir).await.unwrap(); + Command::cargo_bin("cli") + .unwrap() + .args([ + "--server-addr", + &ctx.server_addr, + "download", + "--url-path", + url_path, + "--destination", + destination_dir.to_str().unwrap(), + ]) + .assert() + .success(); + + let destination_file_path = destination_dir.join(file.file_name().unwrap()); + let actual_content = tokio::fs::read_to_string(destination_file_path) + .await + .unwrap(); + assert_eq!(actual_content, expected_content); +} + +#[test_context::test_context(CliTestContext)] +#[tokio::test] +async fn test_copy_encrypt_and_paste_decrypt_command(ctx: &mut CliTestContext) { + let key_nonce = &generate_random_key_nonce(); + let (file, expected_content) = ctx.create_dummy_file().await.unwrap(); + let url_path = Command::cargo_bin("cli") + .unwrap() + .args([ + "--server-addr", + &ctx.server_addr, + "upload", + "--source-file", + file.to_str().unwrap(), + "--key-nonce", + key_nonce, + "--output", + "url-path", + ]) + .output() + .unwrap() + .stdout; + let url_path = std::str::from_utf8(&url_path).unwrap().trim(); + let destination_dir = ctx.workspace.join("destination_dir"); + tokio::fs::create_dir_all(&destination_dir).await.unwrap(); + Command::cargo_bin("cli") + .unwrap() + .args([ + "--server-addr", + &ctx.server_addr, + "download", + "--url-path", + url_path, + "--destination", + destination_dir.to_str().unwrap(), + "--key-nonce", + key_nonce, + ]) + .assert() + .success(); + let destination_file_path = destination_dir.join(file.file_name().unwrap()); + let actual_content = tokio::fs::read_to_string(destination_file_path) + .await + .unwrap(); + assert_eq!(actual_content, expected_content); +} diff --git a/cli/tests/cli/encrypt_and_decrypt_cli_test.rs b/cli/tests/cli/encrypt_and_decrypt_cli_test.rs index a3408a9..aeef4e0 100644 --- a/cli/tests/cli/encrypt_and_decrypt_cli_test.rs +++ b/cli/tests/cli/encrypt_and_decrypt_cli_test.rs @@ -1,12 +1,11 @@ +use crate::helper::{generate_random_key_nonce, CliTestContext}; use assert_cmd::Command; -use crate::helper::CliTestContext; - #[test_context::test_context(CliTestContext)] #[tokio::test] async fn test_encrypt_and_decrypt_to_the_destination_file(ctx: &mut CliTestContext) { let (file, expected_content) = ctx.create_dummy_file().await.unwrap(); - let key_nonce = "12345678901234567890123456789012:1234567890123456789"; + let key_nonce = generate_random_key_nonce(); Command::cargo_bin("cli") .unwrap() .args([ @@ -18,7 +17,7 @@ async fn test_encrypt_and_decrypt_to_the_destination_file(ctx: &mut CliTestConte "--destination", ctx.workspace.to_str().unwrap(), "--key-nonce", - key_nonce, + &key_nonce, ]) .assert() .success(); @@ -34,7 +33,7 @@ async fn test_encrypt_and_decrypt_to_the_destination_file(ctx: &mut CliTestConte "--destination", destination_file_path.to_str().unwrap(), "--key-nonce", - key_nonce, + &key_nonce, ]) .assert() .success(); @@ -49,7 +48,7 @@ async fn test_encrypt_and_decrypt_to_the_destination_file(ctx: &mut CliTestConte #[tokio::test] async fn test_encrypt_file_and_decrypt_to_the_destination_dir(ctx: &mut CliTestContext) { let (file, expected_content) = ctx.create_dummy_file().await.unwrap(); - let key_nonce = "12345678901234567890123456789012:1234567890123456789"; + let key_nonce = generate_random_key_nonce(); Command::cargo_bin("cli") .unwrap() .args([ @@ -61,7 +60,7 @@ async fn test_encrypt_file_and_decrypt_to_the_destination_dir(ctx: &mut CliTestC "--destination", ctx.workspace.to_str().unwrap(), "--key-nonce", - key_nonce, + &key_nonce, ]) .assert() .success(); @@ -78,7 +77,7 @@ async fn test_encrypt_file_and_decrypt_to_the_destination_dir(ctx: &mut CliTestC "--destination", destination_dir.to_str().unwrap(), "--key-nonce", - key_nonce, + &key_nonce, ]) .assert() .success(); diff --git a/cli/tests/cli/helper/mod.rs b/cli/tests/cli/helper/mod.rs index 3e2b81f..51b4eb4 100644 --- a/cli/tests/cli/helper/mod.rs +++ b/cli/tests/cli/helper/mod.rs @@ -1,6 +1,10 @@ use fake::{Fake, Faker}; use once_cell::sync::Lazy; -use sdk::{dto::FileUrlPath, retry, util::dir::get_cargo_project_root}; +use sdk::{ + dto::FileUrlPath, + retry, + util::{dir::get_cargo_project_root, random::generate_random_string}, +}; use std::{ io, @@ -129,3 +133,11 @@ async fn ping(root_dir: &Path, server_addr: &str) -> Result String { + format!( + "{}:{}", + generate_random_string(32), + generate_random_string(19) + ) +} diff --git a/cli/tests/cli/main.rs b/cli/tests/cli/main.rs index 69a3863..2b1bfb1 100644 --- a/cli/tests/cli/main.rs +++ b/cli/tests/cli/main.rs @@ -7,3 +7,4 @@ pub(crate) mod helper; pub(crate) mod info_cli_test; pub(crate) mod ping_cli_test; pub(crate) mod upload_and_download_cli_test; +// pub(crate) mod copy_and_paste_cli_test; diff --git a/cli/tests/cli/upload_and_download_cli_test.rs b/cli/tests/cli/upload_and_download_cli_test.rs index a274d2b..6c7ad4a 100644 --- a/cli/tests/cli/upload_and_download_cli_test.rs +++ b/cli/tests/cli/upload_and_download_cli_test.rs @@ -1,6 +1,6 @@ use assert_cmd::Command; -use crate::helper::CliTestContext; +use crate::helper::{generate_random_key_nonce, CliTestContext}; #[test_context::test_context(CliTestContext)] #[tokio::test] @@ -47,7 +47,7 @@ async fn test_upload_and_download_command(ctx: &mut CliTestContext) { #[test_context::test_context(CliTestContext)] #[tokio::test] async fn test_upload_encrypt_and_download_decrypt_command(ctx: &mut CliTestContext) { - let key_nonce = "12345678901234567890123456789012:1234567890123456789"; + let key_nonce = generate_random_key_nonce(); let (file, expected_content) = ctx.create_dummy_file().await.unwrap(); let url_path = Command::cargo_bin("cli") .unwrap() @@ -58,7 +58,7 @@ async fn test_upload_encrypt_and_download_decrypt_command(ctx: &mut CliTestConte "--source-file", file.to_str().unwrap(), "--key-nonce", - key_nonce, + &key_nonce, "--output", "url-path", ]) @@ -79,7 +79,7 @@ async fn test_upload_encrypt_and_download_decrypt_command(ctx: &mut CliTestConte "--destination", destination_dir.to_str().unwrap(), "--key-nonce", - key_nonce, + &key_nonce, ]) .assert() .success(); diff --git a/sdk/Cargo.toml b/sdk/Cargo.toml index c7dd2be..a0e6cd1 100644 --- a/sdk/Cargo.toml +++ b/sdk/Cargo.toml @@ -17,7 +17,6 @@ futures-util = { workspace = true } tracing = { workspace = true } garde = { workspace = true } base64 = { workspace = true } -log-derive = { workspace = true } log = { workspace = true } async-stream = { workspace = true } mime_guess = { workspace = true } diff --git a/sdk/src/client.rs b/sdk/src/client.rs index bcd6f6e..46f18c9 100644 --- a/sdk/src/client.rs +++ b/sdk/src/client.rs @@ -17,8 +17,7 @@ use chacha20poly1305::{ KeyInit, XChaCha20Poly1305, }; -use futures_util::{Stream, StreamExt, TryStreamExt}; -use log_derive::logfn; +use futures_util::{StreamExt, TryStreamExt}; use once_cell::sync::Lazy; use reqwest::StatusCode; use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt}; @@ -45,13 +44,11 @@ impl PasteFileClient { } } - #[logfn(Info)] pub async fn health_check(&self) -> anyhow::Result<(StatusCode, ApiResponseResult)> { let resp = self.get(format!("{}/healthz", self.addr)).send().await?; Ok((resp.status(), resp.json().await?)) } - #[logfn(Info)] pub async fn upload_file_part( &self, file_part: reqwest::multipart::Part, @@ -70,7 +67,6 @@ impl PasteFileClient { Ok((resp.status(), resp.json().await?)) } - #[logfn(Info)] pub async fn upload( &self, file_name: String, @@ -143,7 +139,6 @@ impl PasteFileClient { self.upload_file_part(file_part, param, auth).await } - #[logfn(Info)] pub async fn upload_file( &self, source: &Path, @@ -181,7 +176,6 @@ impl PasteFileClient { Ok((status, ApiResponseResult::Ok(()))) } - #[logfn(Info)] pub async fn download_bytes( &self, url_path: &FileUrlPath, @@ -196,7 +190,6 @@ impl PasteFileClient { Ok((status, ApiResponseResult::Ok(resp.bytes().await?.to_vec()))) } - #[logfn(Info)] pub async fn download( &self, url_path: &FileUrlPath, @@ -254,7 +247,6 @@ impl PasteFileClient { Ok((status, ApiResponseResult::Ok(()))) } - #[logfn(Info)] pub async fn download_file( &self, url_path: &FileUrlPath, @@ -282,7 +274,6 @@ impl PasteFileClient { Ok((status, ApiResponseResult::Ok(destination))) } - #[logfn(Info)] pub async fn info( &self, url_path: &FileUrlPath, @@ -296,7 +287,6 @@ impl PasteFileClient { Ok((resp.status(), resp.json().await?)) } - #[logfn(Info)] pub async fn delete( &self, url_path: &FileUrlPath, diff --git a/sdk/src/util/crypto.rs b/sdk/src/util/crypto.rs index b4d814d..c3c8eac 100644 --- a/sdk/src/util/crypto.rs +++ b/sdk/src/util/crypto.rs @@ -165,7 +165,7 @@ mod tests { use fake::{Fake, Faker}; use test_context::test_context; - use crate::util::test::FileTestContext; + use crate::util::{random::generate_random_string, test::FileTestContext}; use super::*; @@ -173,8 +173,8 @@ mod tests { #[tokio::test] pub async fn test_encrypt_file_and_decrypt_file(ctx: &mut FileTestContext) { let key_nonce = KeyNonce { - key: KeyType::new("01234567890123456789012345678912").unwrap(), - nonce: NonceType::new("1234567891213141516").unwrap(), + key: KeyType::new(&generate_random_string(32)).unwrap(), + nonce: NonceType::new(&generate_random_string(19)).unwrap(), }; let contents = Faker.fake::();