From bffcb2334797f6c807d7aa8c338f5854d6495eca Mon Sep 17 00:00:00 2001 From: Dan Moore Date: Tue, 1 Mar 2022 14:41:39 +0000 Subject: [PATCH] Revert "[PRMT-2531] Use [REDACTED] replacement for obviousness" This reverts commit d54805aaf24231fcb876f2dbba82a12c2e398c31. --- .talismanrc | 2 -- redactor/src/main.rs | 74 +++++++++++++++++++------------------------- 2 files changed, 31 insertions(+), 45 deletions(-) diff --git a/.talismanrc b/.talismanrc index 423c319..eba24af 100644 --- a/.talismanrc +++ b/.talismanrc @@ -3,7 +3,5 @@ fileignoreconfig: checksum: f2e4da4dfc22981641fe21b13c8cef755785845c1431427b3312ca6558251a21 - filename: test/helpers/bats-support checksum: 2076897e3d8d1a27b5d649ebdf9637ea9de70464a8c45346e9054689157c1db3 -- filename: redactor/src/main.rs - checksum: ee591091d4fd625a98dd33fe1ec8a31a15e2a57a3123c3595722834e63d9df0c allowed_patterns: - pwd diff --git a/redactor/src/main.rs b/redactor/src/main.rs index 9f102d5..8b58e44 100644 --- a/redactor/src/main.rs +++ b/redactor/src/main.rs @@ -1,31 +1,28 @@ use std::io::{self, BufRead, Write, Result}; -pub fn redactor(input: R, mut output: W) +pub fn redactor(input: R, mut output: W, num_digits: usize) where R: BufRead, W: Write, { - const NUM_DIGITS: usize = 10; - let mask = "[REDACTED]"; + let mut mask = String::new(); + for _ in 0..num_digits { + mask.push('#'); + } for maybe_line in input.lines() { let mut line = fetch_next_line(maybe_line); - let sequence_positions = find_digit_sequences(NUM_DIGITS, &line); + let sequences = find_digit_sequences(num_digits, &line); - for sequence_position in &sequence_positions { - if is_not_in_uuid(sequence_position, &line) { - line.replace_range(sequence_position..&(sequence_position + NUM_DIGITS), &mask); - } + for sequence in &sequences { + let redaction_end = sequence + num_digits; + line.replace_range(sequence..&redaction_end, &mask); } write!(&mut output, "{}", line).expect("failed to write"); } () } -pub fn is_not_in_uuid(ten_digit_start_position: &usize, line: &String) -> bool { - true -} - fn fetch_next_line(maybe_line: Result) -> String { let mut line = maybe_line.expect("Failed to read line"); line.push('\n'); @@ -55,7 +52,7 @@ fn main() { let output = io::stdout(); - redactor(input, output); + redactor(input, output, 10); } #[cfg(test)] @@ -65,21 +62,21 @@ mod tests { #[test] fn test_replaces_digits_with_redaction_message() { - let input = b"1234567890\n"; + let input = b"123\n"; let mut output = Vec::new(); - redactor(&input[..], &mut output); + redactor(&input[..], &mut output, 3); let output_str = String::from_utf8(output).expect("Not UTF-8"); - assert_eq!("[REDACTED]\n", output_str); + assert_eq!("###\n", output_str); } #[test] fn test_does_not_replace_non_digits() { let mut output = Vec::new(); - redactor(&b"abc\n"[..], &mut output); + redactor(&b"abc\n"[..], &mut output, 3); assert_eq!("abc\n", String::from_utf8(output).unwrap()); } @@ -88,89 +85,80 @@ mod tests { fn test_replaces_only_digits() { let mut output = Vec::new(); - redactor(&b"a1234567890b\n"[..], &mut output); + redactor(&b"a123b\n"[..], &mut output, 3); - assert_eq!("a[REDACTED]b\n", String::from_utf8(output).unwrap()); + assert_eq!("a###b\n", String::from_utf8(output).unwrap()); } #[test] fn test_does_not_replace_fewer_digits_than_intended() { let mut output = Vec::new(); - redactor(&b"a123456789bc\n"[..], &mut output); + redactor(&b"a12bc\n"[..], &mut output, 3); - assert_eq!("a123456789bc\n", String::from_utf8(output).unwrap()); + assert_eq!("a12bc\n", String::from_utf8(output).unwrap()); } #[test] fn test_does_not_replace_more_digits_than_intended() { let mut output = Vec::new(); - redactor(&b"a12345678904bc\n"[..], &mut output); + redactor(&b"a1234bc\n"[..], &mut output, 3); - assert_eq!("a12345678904bc\n", String::from_utf8(output).unwrap()); + assert_eq!("a1234bc\n", String::from_utf8(output).unwrap()); } #[test] fn test_replaces_multiple_occurrences_on_line() { let mut output = Vec::new(); - redactor(&b"abc1234567890de1112223334fgh7778889990xyz\n"[..], &mut output); + redactor(&b"abc123de456fgh789xyz\n"[..], &mut output, 3); - assert_eq!("abc[REDACTED]de[REDACTED]fgh[REDACTED]xyz\n", String::from_utf8(output).unwrap()); + assert_eq!("abc###de###fgh###xyz\n", String::from_utf8(output).unwrap()); } #[test] fn test_replaces_digits_at_start_of_line() { let mut output = Vec::new(); - redactor(&b"1234567890abc\n"[..], &mut output); + redactor(&b"123abc\n"[..], &mut output, 3); - assert_eq!("[REDACTED]abc\n", String::from_utf8(output).unwrap()); + assert_eq!("###abc\n", String::from_utf8(output).unwrap()); } #[test] fn test_replaces_digits_at_end_of_line() { let mut output = Vec::new(); - redactor(&b"abc1234567890\n"[..], &mut output); + redactor(&b"abc123\n"[..], &mut output, 3); - assert_eq!("abc[REDACTED]\n", String::from_utf8(output).unwrap()); + assert_eq!("abc###\n", String::from_utf8(output).unwrap()); } #[test] fn test_includes_fewer_that_intended_digits_at_end_of_line() { let mut output = Vec::new(); - redactor(&b"abc123456789\n"[..], &mut output); + redactor(&b"abc12\n"[..], &mut output, 3); - assert_eq!("abc123456789\n", String::from_utf8(output).unwrap()); + assert_eq!("abc12\n", String::from_utf8(output).unwrap()); } #[test] fn test_replaces_digits_on_multiple_lines() { let mut output = Vec::new(); - redactor(&b"abc1234567890\n4567890123def7890123456\n"[..], &mut output); + redactor(&b"abc123\n456def789\n"[..], &mut output, 3); - assert_eq!("abc[REDACTED]\n[REDACTED]def[REDACTED]\n", String::from_utf8(output).unwrap()); + assert_eq!("abc###\n###def###\n", String::from_utf8(output).unwrap()); } #[test] fn test_copes_with_multiple_runs_of_shorter_digits() { let mut output = Vec::new(); - redactor(&b"abc12__34__56\n"[..], &mut output); + redactor(&b"abc12__34__56\n"[..], &mut output, 3); assert_eq!("abc12__34__56\n", String::from_utf8(output).unwrap()); } - - // #[test] - // fn test_recognises_that_ten_digit_sequence_is_in_uuid_when_position_is_at_start_of_last_sequence_of_a_uuid() { - // let single_uuid_line = String::from("2523fa52-1719-4300-9bb0-321612e7393a"); - // let in_uuid = !is_not_in_uuid(&24, &single_uuid_line); - // - // assert_eq!(single_uuid_line.chars().nth(24).unwrap(), '3'); - // assert_eq!(in_uuid, true); - // } }