From 4c988a446d5d3bb706113231c91152d0b5916474 Mon Sep 17 00:00:00 2001 From: orhun Date: Sat, 24 Jul 2021 13:47:24 +0300 Subject: [PATCH] test(project): add tests --- Cargo.lock | 115 +++++++++++++++++++++++++++++++++++++++++--------- Cargo.toml | 3 ++ src/config.rs | 32 +++++++++++--- src/file.rs | 55 ++++++++++++++++++++++++ src/header.rs | 31 ++++++++++++++ src/server.rs | 24 +++++++++++ 6 files changed, 235 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a5bda72..c002443 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14,7 +14,7 @@ dependencies = [ "futures-sink", "log", "pin-project 0.4.28", - "tokio", + "tokio 0.2.25", "tokio-util", ] @@ -25,7 +25,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "177837a10863f15ba8d3ae3ec12fac1099099529ed20083a27fdfe247381d0dc" dependencies = [ "actix-codec", - "actix-rt", + "actix-rt 1.1.1", "actix-service", "actix-utils", "derive_more", @@ -65,7 +65,7 @@ checksum = "452299e87817ae5673910e53c243484ca38be3828db819b6011736fc6982e874" dependencies = [ "actix-codec", "actix-connect", - "actix-rt", + "actix-rt 1.1.1", "actix-service", "actix-threadpool", "actix-utils", @@ -114,6 +114,16 @@ dependencies = [ "syn", ] +[[package]] +name = "actix-macros" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2f86cd6857c135e6e9fe57b1619a88d1f94a7df34c00e11fe13e64fd3438837" +dependencies = [ + "quote", + "syn", +] + [[package]] name = "actix-multipart" version = "0.3.0" @@ -151,13 +161,24 @@ version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "143fcc2912e0d1de2bcf4e2f720d2a60c28652ab4179685a1ee159e0fb3db227" dependencies = [ - "actix-macros", + "actix-macros 0.1.3", "actix-threadpool", "copyless", "futures-channel", "futures-util", "smallvec", - "tokio", + "tokio 0.2.25", +] + +[[package]] +name = "actix-rt" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc7d7cd957c9ed92288a7c3c96af81fa5291f65247a76a34dac7b6af74e52ba0" +dependencies = [ + "actix-macros 0.2.1", + "futures-core", + "tokio 1.9.0", ] [[package]] @@ -167,13 +188,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "45407e6e672ca24784baa667c5d32ef109ccdd8d5e0b5ebb9ef8a67f4dfb708e" dependencies = [ "actix-codec", - "actix-rt", + "actix-rt 1.1.1", "actix-service", "actix-utils", "futures-channel", "futures-util", "log", - "mio", + "mio 0.6.23", "mio-uds", "num_cpus", "slab", @@ -196,8 +217,8 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "47239ca38799ab74ee6a8a94d1ce857014b2ac36f242f70f3f75a66f691e791c" dependencies = [ - "actix-macros", - "actix-rt", + "actix-macros 0.1.3", + "actix-rt 1.1.1", "actix-server", "actix-service", "log", @@ -238,7 +259,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2e9022dec56632d1d7979e59af14f0597a28a830a9c1c7fec8b2327eb9f16b5a" dependencies = [ "actix-codec", - "actix-rt", + "actix-rt 1.1.1", "actix-service", "bitflags", "bytes 0.5.6", @@ -259,9 +280,9 @@ checksum = "e641d4a172e7faa0862241a20ff4f1f5ab0ab7c279f00c2d4587b77483477b86" dependencies = [ "actix-codec", "actix-http", - "actix-macros", + "actix-macros 0.1.3", "actix-router", - "actix-rt", + "actix-rt 1.1.1", "actix-server", "actix-service", "actix-testing", @@ -358,7 +379,7 @@ checksum = "b381e490e7b0cfc37ebc54079b0413d8093ef43d14a4e4747083f7fa47a9e691" dependencies = [ "actix-codec", "actix-http", - "actix-rt", + "actix-rt 1.1.1", "actix-service", "base64", "bytes 0.5.6", @@ -814,7 +835,7 @@ dependencies = [ "http", "indexmap", "slab", - "tokio", + "tokio 0.2.25", "tokio-util", "tracing", "tracing-futures", @@ -1075,12 +1096,25 @@ dependencies = [ "kernel32-sys", "libc", "log", - "miow", + "miow 0.2.2", "net2", "slab", "winapi 0.2.8", ] +[[package]] +name = "mio" +version = "0.7.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c2bdb6314ec10835cd3293dd268473a835c02b7b352e788be788b3c6ca6bb16" +dependencies = [ + "libc", + "log", + "miow 0.3.7", + "ntapi", + "winapi 0.3.9", +] + [[package]] name = "mio-uds" version = "0.6.8" @@ -1089,7 +1123,7 @@ checksum = "afcb699eb26d4332647cc848492bbc15eafb26f08d0304550d5aa1f612e066f0" dependencies = [ "iovec", "libc", - "mio", + "mio 0.6.23", ] [[package]] @@ -1104,6 +1138,15 @@ dependencies = [ "ws2_32-sys", ] +[[package]] +name = "miow" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9f1c5b025cda876f66ef43a113f91ebc9f4ccef34843000e0adf6ebbab84e21" +dependencies = [ + "winapi 0.3.9", +] + [[package]] name = "net2" version = "0.2.37" @@ -1136,6 +1179,15 @@ dependencies = [ "version_check 0.9.3", ] +[[package]] +name = "ntapi" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f6bb902e437b6d86e03cce10a7e2af662292c5dfef23b65899ea3ac9354ad44" +dependencies = [ + "winapi 0.3.9", +] + [[package]] name = "num-traits" version = "0.1.43" @@ -1164,12 +1216,19 @@ dependencies = [ "libc", ] +[[package]] +name = "once_cell" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "692fcb63b64b1758029e0a96ee63e049ce8c5948587f2f7208df04625e5f6b56" + [[package]] name = "oops" version = "0.1.0" dependencies = [ "actix-files", "actix-multipart", + "actix-rt 2.2.0", "actix-web", "byte-unit", "config", @@ -1823,7 +1882,7 @@ dependencies = [ "lazy_static", "libc", "memchr", - "mio", + "mio 0.6.23", "mio-uds", "pin-project-lite 0.1.12", "signal-hook-registry", @@ -1831,6 +1890,22 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "tokio" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b7b349f11a7047e6d1276853e612d152f5e8a352c61917887cc2169e2366b4c" +dependencies = [ + "autocfg", + "libc", + "mio 0.7.13", + "once_cell", + "parking_lot", + "pin-project-lite 0.2.7", + "signal-hook-registry", + "winapi 0.3.9", +] + [[package]] name = "tokio-util" version = "0.3.1" @@ -1842,7 +1917,7 @@ dependencies = [ "futures-sink", "log", "pin-project-lite 0.1.12", - "tokio", + "tokio 0.2.25", ] [[package]] @@ -1901,7 +1976,7 @@ dependencies = [ "rand 0.7.3", "smallvec", "thiserror", - "tokio", + "tokio 0.2.25", "url", ] @@ -1920,7 +1995,7 @@ dependencies = [ "resolv-conf", "smallvec", "thiserror", - "tokio", + "tokio 0.2.25", "trust-dns-proto", ] diff --git a/Cargo.toml b/Cargo.toml index baec813..c14d04e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,9 @@ rand = "0.8.4" version = "4.0.12" features = ["serde"] +[dev-dependencies] +actix-rt = "2.2.0" + [profile.dev] opt-level = 0 debug = true diff --git a/src/config.rs b/src/config.rs index 3e130c1..4d25741 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,7 +3,7 @@ use config::{self, ConfigError}; use std::path::PathBuf; /// Configuration values. -#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] +#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)] pub struct Config { /// Server configuration. pub server: ServerConfig, @@ -12,7 +12,7 @@ pub struct Config { } /// Server configuration. -#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] +#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)] pub struct ServerConfig { /// The socket address to bind. pub address: String, @@ -27,7 +27,7 @@ pub struct ServerConfig { } /// Paste configuration. -#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] +#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)] pub struct PasteConfig { /// Pet names configuration. pub pet_names: PetNamesConfig, @@ -38,7 +38,7 @@ pub struct PasteConfig { } /// Pet names configuration. -#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] +#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)] pub struct PetNamesConfig { /// Use pet names instead of original file names. pub enabled: bool, @@ -49,7 +49,7 @@ pub struct PetNamesConfig { } /// Random string configuration. -#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] +#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)] pub struct RandomConfig { /// Use random strings instead of original file names. pub enabled: bool, @@ -67,3 +67,25 @@ impl Config { config.try_into() } } + +#[cfg(test)] +mod test { + use super::*; + use std::env; + + #[test] + fn test_parse_config() -> Result<(), ConfigError> { + let file_name = PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("config.toml") + .to_str() + .unwrap() + .to_string(); + env::set_var( + format!("{}_SERVER__ADDRESS", env!("CARGO_PKG_NAME")), + "0.0.1.1", + ); + let config = Config::parse(&file_name)?; + assert_eq!("0.0.1.1", config.server.address); + Ok(()) + } +} diff --git a/src/file.rs b/src/file.rs index d6bd8b4..3ebdc1d 100644 --- a/src/file.rs +++ b/src/file.rs @@ -63,3 +63,58 @@ pub fn save(mut file_name: &str, bytes: &[u8], config: &Config) -> IoResult IoResult<()> { + let mut config = Config::default(); + config.server.upload_path = env::current_dir()?; + config.paste.pet_names = PetNamesConfig { + enabled: true, + words: 3, + separator: String::from("_"), + }; + let file_name = save("test.txt", &[65, 66, 67], &config)?; + assert_eq!("ABC", fs::read_to_string(&file_name)?); + assert_eq!( + Some("txt"), + PathBuf::from(&file_name) + .extension() + .map(|v| v.to_str()) + .flatten() + ); + fs::remove_file(file_name)?; + + config.paste.default_extension = String::from("bin"); + config.paste.pet_names.enabled = false; + config.paste.random = RandomConfig { + enabled: true, + length: 10, + }; + let file_name = save("random", &[120, 121, 122], &config)?; + assert_eq!("xyz", fs::read_to_string(&file_name)?); + assert_eq!( + Some("bin"), + PathBuf::from(&file_name) + .extension() + .map(|v| v.to_str()) + .flatten() + ); + fs::remove_file(file_name)?; + + config.paste.random.enabled = false; + let file_name = save("test.file", &[116, 101, 115, 116], &config)?; + assert_eq!("test.file", &file_name); + assert_eq!("test", fs::read_to_string(&file_name)?); + fs::remove_file(file_name)?; + + Ok(()) + } +} diff --git a/src/header.rs b/src/header.rs index 7f9f7cb..2f031c9 100644 --- a/src/header.rs +++ b/src/header.rs @@ -45,3 +45,34 @@ impl ContentDisposition { .ok_or_else(|| error::ErrorBadRequest("file data not present")) } } + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_content_disposition() -> Result<(), ActixError> { + assert!(ContentDisposition::try_from(None).is_err()); + + let actix_content_disposition = Some(ActixContentDisposition { + disposition: DispositionType::FormData, + parameters: vec![ + DispositionParam::Name(String::from("file")), + DispositionParam::Filename(String::from("x.txt")), + ], + }); + let content_disposition = ContentDisposition::try_from(actix_content_disposition)?; + assert!(content_disposition.has_form_field("file")); + assert!(!content_disposition.has_form_field("test")); + assert_eq!("x.txt", content_disposition.get_file_name()?); + + let actix_content_disposition = Some(ActixContentDisposition { + disposition: DispositionType::Attachment, + parameters: vec![DispositionParam::Name(String::from("file"))], + }); + let content_disposition = ContentDisposition::try_from(actix_content_disposition)?; + assert!(!content_disposition.has_form_field("file")); + assert!(content_disposition.get_file_name().is_err()); + Ok(()) + } +} diff --git a/src/server.rs b/src/server.rs index d70bb91..c75c136 100644 --- a/src/server.rs +++ b/src/server.rs @@ -91,3 +91,27 @@ pub fn configure_routes(cfg: &mut web::ServiceConfig) { .service(upload) .route("", web::head().to(HttpResponse::MethodNotAllowed)); } + +#[cfg(test)] +mod tests { + use super::*; + use actix_web::{http, test, App}; + + #[actix_rt::test] + async fn test_index() { + let mut app = test::init_service(App::new().service(index)).await; + let req = test::TestRequest::with_header("content-type", "text/plain").to_request(); + let resp = test::call_service(&mut app, req).await; + assert!(resp.status().is_success()); + } + + #[actix_rt::test] + async fn test_serve() { + let mut app = test::init_service(App::new().service(serve)).await; + let req = test::TestRequest::default().to_request(); + let resp = test::call_service(&mut app, req).await; + assert_eq!(http::StatusCode::NOT_FOUND, resp.status()); + } + + // TODO: add test for upload +}