Skip to content

Commit

Permalink
refactor test input
Browse files Browse the repository at this point in the history
  • Loading branch information
robatipoor committed Mar 4, 2024
1 parent 16aad20 commit 84c6fb2
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 43 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand All @@ -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"
Expand Down
23 changes: 14 additions & 9 deletions cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -81,7 +82,10 @@ pub async fn upload(args: UploadArguments) {
};
}

pub async fn copy(args: CopyArguments) {
pub async fn copy<R>(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,
Expand All @@ -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,
&param,
args.auth,
)
.await
} else {
client
.upload_reader(args.file_name, "text/plain", stdin, &param, args.auth)
.upload_reader(args.file_name, "text/plain", reader, &param, args.auth)
.await
}
.unwrap();
Expand Down Expand Up @@ -162,20 +165,22 @@ pub async fn download(
}
}

pub async fn paste(
pub async fn paste<W>(
server_addr: String,
auth: Option<(String, String)>,
url_path: FileUrlPath,
key_nonce: Option<KeyNonce>,
) {
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();

Expand Down
6 changes: 4 additions & 2 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -62,7 +63,7 @@ async fn main() {
output,
key_nonce,
};
command::copy(args).await;
command::copy(stdin, args).await;
}
SubCommand::Download {
progress_bar,
Expand All @@ -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.");
Expand Down
5 changes: 3 additions & 2 deletions cli/src/util/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ mod tests {

use sdk::util::{
crypto::{KeyType, NonceType},
random::generate_random_string,
test::FileTestContext,
};

Expand All @@ -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::<String>();
let plaintext_file = ctx.temp_path.join("file.txt");
Expand Down
92 changes: 92 additions & 0 deletions cli/tests/cli/copy_and_paste_cli_test.rs
Original file line number Diff line number Diff line change
@@ -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);
}
15 changes: 7 additions & 8 deletions cli/tests/cli/encrypt_and_decrypt_cli_test.rs
Original file line number Diff line number Diff line change
@@ -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([
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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([
Expand All @@ -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();
Expand All @@ -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();
Expand Down
14 changes: 13 additions & 1 deletion cli/tests/cli/helper/mod.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -129,3 +133,11 @@ async fn ping(root_dir: &Path, server_addr: &str) -> Result<ExitStatus, io::Erro
.wait()
.await
}

pub fn generate_random_key_nonce() -> String {
format!(
"{}:{}",
generate_random_string(32),
generate_random_string(19)
)
}
1 change: 1 addition & 0 deletions cli/tests/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
8 changes: 4 additions & 4 deletions cli/tests/cli/upload_and_download_cli_test.rs
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -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()
Expand All @@ -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",
])
Expand All @@ -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();
Expand Down
1 change: 0 additions & 1 deletion sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
Loading

0 comments on commit 84c6fb2

Please sign in to comment.