Skip to content

Commit db0992c

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

File tree

6 files changed

+51
-10
lines changed

6 files changed

+51
-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: 0 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"

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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Test rustscan logging utilities, ensuring library doesn't log to stdout.
3+
*/
4+
#![feature(internal_output_capture)]
5+
6+
use std::{net::IpAddr, str::FromStr, time::Duration};
7+
8+
use futures::executor::block_on;
9+
use rustscan::{input::ScanOrder, port_strategy::PortStrategy, scanner::Scanner};
10+
use std::io;
11+
use std::sync::{Arc, Mutex};
12+
13+
#[test]
14+
fn no_logging_scanner() {
15+
// "open" tcp connection on random port
16+
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
17+
// get the port from above connection
18+
let port = listener.local_addr().unwrap().port();
19+
20+
let stdout_buf = Arc::new(Mutex::new(vec![]));
21+
22+
// capture stdout
23+
io::set_output_capture(Some(stdout_buf.clone()));
24+
block_on(
25+
Scanner::new(
26+
&[IpAddr::from_str("127.0.0.1").unwrap()],
27+
100,
28+
Duration::from_secs(5),
29+
3,
30+
false,
31+
PortStrategy::pick(&None, Some(vec![port]), ScanOrder::Random),
32+
true,
33+
vec![],
34+
false,
35+
)
36+
.run(),
37+
);
38+
// stop capturing stdout
39+
io::set_output_capture(None);
40+
41+
let captured_stdout = stdout_buf.lock().unwrap();
42+
assert!(captured_stdout.is_empty());
43+
}

0 commit comments

Comments
 (0)