Skip to content

Commit 42eff43

Browse files
committed
add test and remove once_cell dependency
1 parent b3923d7 commit 42eff43

File tree

7 files changed

+79
-10
lines changed

7 files changed

+79
-10
lines changed

Cargo.lock

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ itertools = "0.13.0"
3838
hickory-resolver = { version = "0.24.0", features = ["dns-over-rustls"] }
3939
anyhow = "1.0.40"
4040
text_placeholder = { version = "0.5", features = ["struct_context"] }
41-
once_cell = "1.20.2"
4241

4342
[dev-dependencies]
4443
parameterized = "2.0.0"
@@ -59,6 +58,10 @@ strip = true
5958
name = "rustscan"
6059
path = "src/main.rs"
6160

61+
[[example]]
62+
name = "log_mode_test_binary"
63+
path = "tests/log_mode_test_binary/mod.rs"
64+
6265
[lints.rust]
6366
unexpected_cfgs = { level = "warn", check-cfg = ["cfg(tarpaulin_include)"] }
6467

build.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ fn generate_code(port_payload_map: BTreeMap<Vec<u16>, Vec<u8>>) {
6262
let dest_path = PathBuf::from("src/generated.rs");
6363

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

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

9493
generated_code.push_str(
95-
"static PARSED_DATA: Lazy<BTreeMap<Vec<u16>, Vec<u8>>> = Lazy::new(generated_data);\n",
94+
"static PARSED_DATA: LazyLock<BTreeMap<Vec<u16>, Vec<u8>>> = LazyLock::new(generated_data);\n",
9695
);
9796
generated_code.push_str("pub fn get_parsed_data() -> &'static BTreeMap<Vec<u16>, Vec<u8>> {\n");
9897
generated_code.push_str(" &PARSED_DATA\n");

src/generated.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use once_cell::sync::Lazy;
2-
use std::collections::BTreeMap;
1+
use std::{collections::BTreeMap, sync::LazyLock};
32

43
fn generated_data() -> BTreeMap<Vec<u16>, Vec<u8>> {
54
let mut map = BTreeMap::new();
@@ -2990,7 +2989,7 @@ fn generated_data() -> BTreeMap<Vec<u16>, Vec<u8>> {
29902989
map
29912990
}
29922991

2993-
static PARSED_DATA: Lazy<BTreeMap<Vec<u16>, Vec<u8>>> = Lazy::new(generated_data);
2992+
static PARSED_DATA: LazyLock<BTreeMap<Vec<u16>, Vec<u8>>> = LazyLock::new(generated_data);
29942993
pub fn get_parsed_data() -> &'static BTreeMap<Vec<u16>, Vec<u8>> {
29952994
&PARSED_DATA
29962995
}

src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
//! ```
4242
#![allow(clippy::needless_doctest_main)]
4343

44+
use std::sync::OnceLock;
45+
4446
pub mod tui;
4547

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

6567
/// Set IS_CLI_MODE to true.
6668
#[doc(hidden)]

tests/log_mode.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Test rustscan logging utilities, ensuring library doesn't log to stdout.
3+
*/
4+
use std::{
5+
io::Read,
6+
process::{Command, Stdio},
7+
};
8+
9+
// need to import here, otherwise cargo thinks I'm not using the test
10+
mod log_mode_test_binary;
11+
12+
#[test]
13+
fn no_logging_scanner() {
14+
let mut child = Command::new("target/debug/examples/log_mode_test_binary")
15+
.stderr(Stdio::piped())
16+
.stdout(Stdio::piped())
17+
.spawn()
18+
.unwrap();
19+
20+
child.wait().unwrap();
21+
22+
let buf = &mut Vec::new();
23+
child.stdout.take().unwrap().read_to_end(buf).unwrap();
24+
assert!(buf.is_empty());
25+
26+
child.stderr.take().unwrap().read_to_end(buf).unwrap();
27+
assert!(buf.is_empty());
28+
}

tests/log_mode_test_binary/mod.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//! File used just to build a binary for testing if stdout is being logged.
2+
//! Older versions of the library rustscan would write to stdout. This file
3+
//! helps ensure the library only writes to stdout if log is initialized,
4+
//! otherwise it shouldn't write at all.
5+
//!
6+
//! It was necessary to create this file because checking if some code write to
7+
//! stdout is very orthogonal to rust's testing tools. There are utilities but
8+
//! only on unstable rust. This file is used to create a binary that can
9+
//! be executed as child process for testing the behavior.
10+
11+
#![allow(unused)]
12+
13+
use std::{net::IpAddr, str::FromStr, time::Duration};
14+
15+
use futures::executor::block_on;
16+
use rustscan::{input::ScanOrder, port_strategy::PortStrategy, scanner::Scanner};
17+
18+
fn main() {
19+
// "open" tcp connection on random port
20+
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
21+
// get the port from above connection
22+
let port = listener.local_addr().unwrap().port();
23+
24+
// execute
25+
block_on(
26+
Scanner::new(
27+
&[IpAddr::from_str("127.0.0.1").unwrap()],
28+
100,
29+
Duration::from_secs(5),
30+
3,
31+
false,
32+
PortStrategy::pick(&None, Some(vec![port]), ScanOrder::Random),
33+
true,
34+
vec![],
35+
false,
36+
)
37+
.run(),
38+
);
39+
}

0 commit comments

Comments
 (0)