Skip to content

Commit

Permalink
add test and remove once_cell dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
Dauaaa committed Dec 6, 2024
1 parent b3923d7 commit 42eff43
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 10 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

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

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ itertools = "0.13.0"
hickory-resolver = { version = "0.24.0", features = ["dns-over-rustls"] }
anyhow = "1.0.40"
text_placeholder = { version = "0.5", features = ["struct_context"] }
once_cell = "1.20.2"

[dev-dependencies]
parameterized = "2.0.0"
Expand All @@ -59,6 +58,10 @@ strip = true
name = "rustscan"
path = "src/main.rs"

[[example]]
name = "log_mode_test_binary"
path = "tests/log_mode_test_binary/mod.rs"

[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ["cfg(tarpaulin_include)"] }

Expand Down
5 changes: 2 additions & 3 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ fn generate_code(port_payload_map: BTreeMap<Vec<u16>, Vec<u8>>) {
let dest_path = PathBuf::from("src/generated.rs");

let mut generated_code = String::new();
generated_code.push_str("use std::collections::BTreeMap;\n");
generated_code.push_str("use once_cell::sync::Lazy;\n\n");
generated_code.push_str("use std::{collections::BTreeMap, sync::LazyLock};\n\n");

generated_code.push_str("fn generated_data() -> BTreeMap<Vec<u16>, Vec<u8>> {\n");
generated_code.push_str(" let mut map = BTreeMap::new();\n");
Expand Down Expand Up @@ -92,7 +91,7 @@ fn generate_code(port_payload_map: BTreeMap<Vec<u16>, Vec<u8>>) {
generated_code.push_str("}\n\n");

generated_code.push_str(
"static PARSED_DATA: Lazy<BTreeMap<Vec<u16>, Vec<u8>>> = Lazy::new(generated_data);\n",
"static PARSED_DATA: LazyLock<BTreeMap<Vec<u16>, Vec<u8>>> = LazyLock::new(generated_data);\n",
);
generated_code.push_str("pub fn get_parsed_data() -> &'static BTreeMap<Vec<u16>, Vec<u8>> {\n");
generated_code.push_str(" &PARSED_DATA\n");
Expand Down
5 changes: 2 additions & 3 deletions src/generated.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use once_cell::sync::Lazy;
use std::collections::BTreeMap;
use std::{collections::BTreeMap, sync::LazyLock};

fn generated_data() -> BTreeMap<Vec<u16>, Vec<u8>> {
let mut map = BTreeMap::new();
Expand Down Expand Up @@ -2990,7 +2989,7 @@ fn generated_data() -> BTreeMap<Vec<u16>, Vec<u8>> {
map
}

static PARSED_DATA: Lazy<BTreeMap<Vec<u16>, Vec<u8>>> = Lazy::new(generated_data);
static PARSED_DATA: LazyLock<BTreeMap<Vec<u16>, Vec<u8>>> = LazyLock::new(generated_data);
pub fn get_parsed_data() -> &'static BTreeMap<Vec<u16>, Vec<u8>> {
&PARSED_DATA
}
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
//! ```
#![allow(clippy::needless_doctest_main)]

use std::sync::OnceLock;

pub mod tui;

pub mod input;
Expand All @@ -60,7 +62,7 @@ pub mod generated;
/// Static variable defining the current state of execution. The cli binary should
/// set it to true by calling set_cli_mode.
#[doc(hidden)]
pub static IS_CLI_MODE: once_cell::sync::OnceCell<bool> = once_cell::sync::OnceCell::new();
pub static IS_CLI_MODE: OnceLock<bool> = OnceLock::new();

/// Set IS_CLI_MODE to true.
#[doc(hidden)]
Expand Down
28 changes: 28 additions & 0 deletions tests/log_mode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Test rustscan logging utilities, ensuring library doesn't log to stdout.
*/
use std::{
io::Read,
process::{Command, Stdio},
};

// need to import here, otherwise cargo thinks I'm not using the test
mod log_mode_test_binary;

#[test]
fn no_logging_scanner() {
let mut child = Command::new("target/debug/examples/log_mode_test_binary")
.stderr(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.unwrap();

child.wait().unwrap();

let buf = &mut Vec::new();
child.stdout.take().unwrap().read_to_end(buf).unwrap();
assert!(buf.is_empty());

child.stderr.take().unwrap().read_to_end(buf).unwrap();
assert!(buf.is_empty());
}
39 changes: 39 additions & 0 deletions tests/log_mode_test_binary/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//! File used just to build a binary for testing if stdout is being logged.
//! Older versions of the library rustscan would write to stdout. This file
//! helps ensure the library only writes to stdout if log is initialized,
//! otherwise it shouldn't write at all.
//!
//! It was necessary to create this file because checking if some code write to
//! stdout is very orthogonal to rust's testing tools. There are utilities but
//! only on unstable rust. This file is used to create a binary that can
//! be executed as child process for testing the behavior.
#![allow(unused)]

use std::{net::IpAddr, str::FromStr, time::Duration};

use futures::executor::block_on;
use rustscan::{input::ScanOrder, port_strategy::PortStrategy, scanner::Scanner};

fn main() {
// "open" tcp connection on random port
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
// get the port from above connection
let port = listener.local_addr().unwrap().port();

// execute
block_on(
Scanner::new(
&[IpAddr::from_str("127.0.0.1").unwrap()],
100,
Duration::from_secs(5),
3,
false,
PortStrategy::pick(&None, Some(vec![port]), ScanOrder::Random),
true,
vec![],
false,
)
.run(),
);
}

0 comments on commit 42eff43

Please sign in to comment.