Skip to content

Commit 3b40a58

Browse files
committed
Rust: Add test cases (generated by LLM).
1 parent c68579b commit 3b40a58

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

rust/ql/test/query-tests/security/CWE-312/test_logging.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
use log::{debug, error, info, trace, warn, log, Level};
33
use std::io::Write as _;
44
use std::fmt::Write as _;
5+
use log_err::{LogErrOption, LogErrResult};
56

67
// --- tests ---
78

@@ -146,6 +147,32 @@ fn test_log(harmless: String, password: String, encrypted_password: String) {
146147
warn!("message = {}", s2); // (this implementation does not output the password field)
147148
warn!("message = {:?}", s2); // $ MISSING: Alert[rust/cleartext-logging]=s2
148149
warn!("message = {:#?}", s2); // $ MISSING: Alert[rust/cleartext-logging]=s2
150+
151+
// test log_expect with sensitive data
152+
let password2 = "123456".to_string(); // Create new password for this test
153+
let sensitive_opt: Option<String> = Some(password2.clone());
154+
155+
// log_expect tests with LogErrOption trait
156+
let _ = sensitive_opt.log_expect("Option is None"); // $ Alert[rust/cleartext-logging]
157+
158+
// log_expect tests with LogErrResult trait
159+
let sensitive_result: Result<String, &str> = Ok(password2.clone());
160+
let _ = sensitive_result.log_expect("Result failed"); // $ Alert[rust/cleartext-logging]
161+
162+
// log_unwrap tests with LogErrOption trait
163+
let sensitive_opt2: Option<String> = Some(password2.clone());
164+
let _ = sensitive_opt2.log_unwrap(); // $ Alert[rust/cleartext-logging]
165+
166+
// log_unwrap tests with LogErrResult trait
167+
let sensitive_result2: Result<String, &str> = Ok(password2.clone());
168+
let _ = sensitive_result2.log_unwrap(); // $ Alert[rust/cleartext-logging]
169+
170+
// Negative cases that should fail and log
171+
let none_opt: Option<String> = None;
172+
let _ = none_opt.log_expect(&format!("Failed with password: {}", password2)); // $ Alert[rust/cleartext-logging]
173+
174+
let err_result: Result<String, String> = Err(password2);
175+
let _ = err_result.log_unwrap(); // $ Alert[rust/cleartext-logging]
149176
}
150177

151178
fn test_std(password: String, i: i32, opt_i: Option<i32>) {

0 commit comments

Comments
 (0)