T-126: M1.2al — tools/curate-etl: sealed read-only MongoDB raw-BSON cursor adapter (mongo_stream.rs: ValidatedReadOnlyMongoUri → allowlisted find → impl io::Read feeding the existing bson_stream decoders) with offline tests and local benchmark (adds the mongodb crate only; no live MongoDB in tests)
This commit is contained in:
parent
76fc415db4
commit
91f10312e9
1 changed files with 610 additions and 0 deletions
|
|
@ -1294,3 +1294,613 @@ fn sealed_surface_source_scan() {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 7. Additional edge-case coverage
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
struct CountingSource {
|
||||
docs: Vec<Vec<u8>>,
|
||||
index: usize,
|
||||
polls: usize,
|
||||
}
|
||||
|
||||
impl CountingSource {
|
||||
fn new(docs: Vec<Vec<u8>>) -> Self {
|
||||
Self {
|
||||
docs,
|
||||
index: 0,
|
||||
polls: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl RawDocumentSource for CountingSource {
|
||||
fn next_raw_document(&mut self) -> Result<Option<&[u8]>, MongoStreamError> {
|
||||
self.polls += 1;
|
||||
if self.index >= self.docs.len() {
|
||||
return Ok(None);
|
||||
}
|
||||
let doc = self.docs[self.index].as_slice();
|
||||
self.index += 1;
|
||||
Ok(Some(doc))
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn source_is_not_polled_again_after_eof_across_repeated_reads() {
|
||||
let docs = vec![
|
||||
build_eod_doc("AAPL.US", "2024-01-02", 185.5, 1000.0),
|
||||
build_eod_doc("MSFT.US", "2024-01-03", 370.0, 2000.0),
|
||||
];
|
||||
let expected = concat_docs(&docs);
|
||||
let mut reader = RawBsonCursorReader::new(CountingSource::new(docs));
|
||||
|
||||
let got = read_all_with_step(&mut reader, 64).expect("initial read must succeed");
|
||||
assert_eq!(got, expected);
|
||||
assert_eq!(reader.docs_read(), 2);
|
||||
assert_eq!(reader.bytes_read(), expected.len() as u64);
|
||||
|
||||
let mut buf = [0u8; 32];
|
||||
for _ in 0..5 {
|
||||
assert_eq!(
|
||||
reader.read(&mut buf).expect("post-EOF read must return Ok(0)"),
|
||||
0
|
||||
);
|
||||
}
|
||||
|
||||
let inner = reader.into_inner();
|
||||
assert_eq!(
|
||||
inner.polls, 3,
|
||||
"source must be polled only once per doc plus one EOF poll"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn source_error_after_split_document_delivers_all_doc_bytes_then_sticky_error() {
|
||||
let doc0 = build_eod_doc("AAPL.US", "2024-01-02", 185.5, 1000.0);
|
||||
assert!(doc0.len() > 25, "test doc must span multiple small reads");
|
||||
let expected_err = MongoStreamError::Source {
|
||||
detail: "cursor dropped after split doc".to_owned(),
|
||||
};
|
||||
let source = FailingSource {
|
||||
docs: vec![doc0.clone()],
|
||||
index: 0,
|
||||
fail_at: 1,
|
||||
err: expected_err.clone(),
|
||||
};
|
||||
|
||||
let mut reader = RawBsonCursorReader::new(source);
|
||||
let mut received = Vec::new();
|
||||
let mut buf = [0u8; 13];
|
||||
|
||||
let first_err = loop {
|
||||
match reader.read(&mut buf) {
|
||||
Ok(0) => panic!("expected source error after split document, got EOF"),
|
||||
Ok(n) => received.extend_from_slice(&buf[..n]),
|
||||
Err(err) => break err,
|
||||
}
|
||||
};
|
||||
|
||||
assert_eq!(received, doc0, "all split document bytes must be delivered before error");
|
||||
assert_eq!(reader.docs_read(), 1);
|
||||
assert_eq!(reader.bytes_read(), doc0.len() as u64);
|
||||
|
||||
let downcast = first_err
|
||||
.get_ref()
|
||||
.and_then(|e| e.downcast_ref::<MongoStreamError>())
|
||||
.expect("io::Error must wrap MongoStreamError");
|
||||
assert_eq!(downcast, &expected_err);
|
||||
|
||||
// Subsequent reads stay sticky with the same error.
|
||||
let second_err = reader
|
||||
.read(&mut buf)
|
||||
.expect_err("reader must remain in sticky error state");
|
||||
let downcast2 = second_err
|
||||
.get_ref()
|
||||
.and_then(|e| e.downcast_ref::<MongoStreamError>())
|
||||
.expect("sticky io::Error must wrap MongoStreamError");
|
||||
assert_eq!(downcast2, &expected_err);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_first_document_fails_immediately_with_zero_counts_and_stays_sticky() {
|
||||
let valid_doc = build_eod_doc("AAPL.US", "2024-01-02", 100.0, 10.0);
|
||||
let mut bad_first = valid_doc.clone();
|
||||
*bad_first.last_mut().unwrap() = 0x42; // missing trailing 0x00
|
||||
|
||||
let mut reader =
|
||||
RawBsonCursorReader::new(VecRawDocumentSource::new(vec![bad_first, valid_doc]));
|
||||
let mut buf = [0u8; 256];
|
||||
|
||||
let first_err = reader
|
||||
.read(&mut buf)
|
||||
.expect_err("malformed doc #0 must fail on first read");
|
||||
let first_mongo_err = first_err
|
||||
.get_ref()
|
||||
.and_then(|e| e.downcast_ref::<MongoStreamError>())
|
||||
.expect("must downcast to MongoStreamError")
|
||||
.clone();
|
||||
assert!(
|
||||
matches!(
|
||||
first_mongo_err,
|
||||
MongoStreamError::InvalidBsonFrame { doc_index: 0, .. }
|
||||
),
|
||||
"expected InvalidBsonFrame at doc_index 0, got {first_mongo_err:?}"
|
||||
);
|
||||
assert_eq!(reader.docs_read(), 0);
|
||||
assert_eq!(reader.bytes_read(), 0);
|
||||
|
||||
for _ in 0..3 {
|
||||
let sticky_err = reader
|
||||
.read(&mut buf)
|
||||
.expect_err("subsequent reads must stay sticky after malformed doc #0");
|
||||
let sticky_mongo_err = sticky_err
|
||||
.get_ref()
|
||||
.and_then(|e| e.downcast_ref::<MongoStreamError>())
|
||||
.expect("sticky error must downcast to MongoStreamError");
|
||||
assert_eq!(sticky_mongo_err, &first_mongo_err);
|
||||
assert_eq!(reader.docs_read(), 0);
|
||||
assert_eq!(reader.bytes_read(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn boundary_document_sizes_min_five_bytes_and_exact_max_doc_bytes() {
|
||||
// Minimum valid 5-byte empty BSON document succeeds at max_doc_bytes == MIN_BSON_DOC_BYTES.
|
||||
let empty_bson = vec![5u8, 0, 0, 0, 0];
|
||||
assert_eq!(empty_bson.len(), MIN_BSON_DOC_BYTES);
|
||||
let mut min_reader = RawBsonCursorReader::with_max_doc_bytes(
|
||||
VecRawDocumentSource::new(vec![empty_bson.clone()]),
|
||||
MIN_BSON_DOC_BYTES,
|
||||
)
|
||||
.expect("MIN_BSON_DOC_BYTES must be accepted");
|
||||
let min_out = read_all_with_step(&mut min_reader, 2).expect("5-byte doc must succeed");
|
||||
assert_eq!(min_out, empty_bson);
|
||||
assert_eq!(min_reader.docs_read(), 1);
|
||||
assert_eq!(min_reader.bytes_read(), MIN_BSON_DOC_BYTES as u64);
|
||||
|
||||
// Exact max_doc_bytes succeeds while max_doc_bytes + 1 fails with InvalidBsonFrame.
|
||||
let exact_doc = build_eod_doc("AAPL.US", "2024-01-02", 185.5, 1000.0);
|
||||
let one_byte_larger = build_eod_doc("AAPLX.US", "2024-01-02", 185.5, 1000.0);
|
||||
assert_eq!(one_byte_larger.len(), exact_doc.len() + 1);
|
||||
let ceiling = exact_doc.len();
|
||||
|
||||
let mut exact_reader = RawBsonCursorReader::with_max_doc_bytes(
|
||||
VecRawDocumentSource::new(vec![exact_doc.clone()]),
|
||||
ceiling,
|
||||
)
|
||||
.expect("exact ceiling must be valid");
|
||||
let exact_out = read_all_with_step(&mut exact_reader, 37).expect("exact-size doc must succeed");
|
||||
assert_eq!(exact_out, exact_doc);
|
||||
assert_eq!(exact_reader.docs_read(), 1);
|
||||
assert_eq!(exact_reader.bytes_read(), ceiling as u64);
|
||||
|
||||
let mut over_reader = RawBsonCursorReader::with_max_doc_bytes(
|
||||
VecRawDocumentSource::new(vec![one_byte_larger]),
|
||||
ceiling,
|
||||
)
|
||||
.expect("ceiling must be valid");
|
||||
let mut buf = [0u8; 512];
|
||||
let err = over_reader
|
||||
.read(&mut buf)
|
||||
.expect_err("max_doc_bytes + 1 must fail");
|
||||
let mongo_err = err
|
||||
.get_ref()
|
||||
.and_then(|e| e.downcast_ref::<MongoStreamError>())
|
||||
.expect("must downcast to MongoStreamError");
|
||||
assert!(matches!(
|
||||
mongo_err,
|
||||
MongoStreamError::InvalidBsonFrame { doc_index: 0, .. }
|
||||
));
|
||||
assert_eq!(over_reader.docs_read(), 0);
|
||||
assert_eq!(over_reader.bytes_read(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn large_200kib_document_parity_and_bounded_pending_capacity() {
|
||||
let payload = "X".repeat(200 * 1024);
|
||||
let mut large_doc = RawDocumentBuf::new();
|
||||
large_doc.append(cstr!("eodSearchCode"), "BIG.US");
|
||||
large_doc.append(cstr!("date"), "2024-08-15");
|
||||
large_doc.append(cstr!("open"), 42.0_f64);
|
||||
large_doc.append(cstr!("high"), 43.0_f64);
|
||||
large_doc.append(cstr!("low"), 41.0_f64);
|
||||
large_doc.append(cstr!("close"), 42.5_f64);
|
||||
large_doc.append(cstr!("volume"), 999.0_f64);
|
||||
large_doc.append(cstr!("padding"), payload.as_str());
|
||||
let large_bytes = large_doc.into_bytes();
|
||||
assert!(large_bytes.len() > 200 * 1024);
|
||||
|
||||
let small_doc = build_eod_doc("SMALL.US", "2024-08-16", 12.25, 50.0);
|
||||
let docs = vec![large_bytes.clone(), small_doc];
|
||||
let concat = concat_docs(&docs);
|
||||
let max_doc_len = docs.iter().map(Vec::len).max().unwrap();
|
||||
|
||||
let mut expected_rows = Vec::new();
|
||||
let expected_stats = decode_eod_price_stream(
|
||||
Cursor::new(&concat),
|
||||
BsonStreamOptions::default(),
|
||||
|bar| {
|
||||
expected_rows.push((
|
||||
bar.eod_search_code.to_owned(),
|
||||
bar.session_date,
|
||||
bar.close.to_bits(),
|
||||
));
|
||||
},
|
||||
|offset, err| panic!("unexpected baseline error at {offset}: {err:?}"),
|
||||
)
|
||||
.expect("baseline large-doc decode must succeed");
|
||||
assert_eq!(expected_rows.len(), 2);
|
||||
|
||||
for step in [13usize, 37, 4099] {
|
||||
let mut cursor_reader = RawBsonCursorReader::new(VecRawDocumentSource::new(docs.clone()));
|
||||
let mut rows = Vec::new();
|
||||
let stats = decode_eod_price_stream(
|
||||
CappedRead::new(&mut cursor_reader, step),
|
||||
BsonStreamOptions::default(),
|
||||
|bar| {
|
||||
rows.push((
|
||||
bar.eod_search_code.to_owned(),
|
||||
bar.session_date,
|
||||
bar.close.to_bits(),
|
||||
));
|
||||
},
|
||||
|offset, err| panic!("unexpected stream error at {offset} (step={step}): {err:?}"),
|
||||
)
|
||||
.expect("large-doc stream decode must succeed");
|
||||
|
||||
assert_eq!(stats, expected_stats, "stats mismatch at step={step}");
|
||||
assert_eq!(rows, expected_rows, "rows mismatch at step={step}");
|
||||
assert_eq!(cursor_reader.docs_read(), 2);
|
||||
assert_eq!(cursor_reader.bytes_read(), concat.len() as u64);
|
||||
assert!(
|
||||
cursor_reader.pending_capacity() <= max_doc_len,
|
||||
"pending_capacity ({}) exceeded single large doc length ({max_doc_len}) at step={step}",
|
||||
cursor_reader.pending_capacity()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn read_spec_validate_accepts_all_boundary_limits() {
|
||||
let min_spec = MongoReadSpec {
|
||||
batch_size: 1,
|
||||
limit: Some(1),
|
||||
max_doc_bytes: MIN_BSON_DOC_BYTES,
|
||||
..MongoReadSpec::default()
|
||||
};
|
||||
min_spec
|
||||
.validate(SourceCollection::EodPrices)
|
||||
.expect("min boundary spec must validate");
|
||||
assert!(
|
||||
build_filter_document(SourceCollection::EodPrices, &min_spec)
|
||||
.expect("min boundary filter must succeed")
|
||||
.is_empty()
|
||||
);
|
||||
|
||||
let max_spec = MongoReadSpec {
|
||||
batch_size: MAX_MONGO_BATCH_SIZE,
|
||||
limit: Some(i64::MAX as u64),
|
||||
max_doc_bytes: MIN_BSON_DOC_BYTES,
|
||||
..MongoReadSpec::default()
|
||||
};
|
||||
max_spec
|
||||
.validate(SourceCollection::EodPrices)
|
||||
.expect("max boundary spec must validate");
|
||||
assert!(
|
||||
build_filter_document(SourceCollection::EodPrices, &max_spec)
|
||||
.expect("max boundary filter must succeed")
|
||||
.is_empty()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn one_sided_date_range_bounds_and_equal_bound_rejection() {
|
||||
// ISO string: gte-only
|
||||
let iso_gte = build_filter_document(
|
||||
SourceCollection::EodPrices,
|
||||
&MongoReadSpec {
|
||||
date_range: Some(MongoDateRange {
|
||||
gte: Some(FilterDateValue::IsoString("2024-01-01".to_owned())),
|
||||
lt: None,
|
||||
}),
|
||||
..MongoReadSpec::default()
|
||||
},
|
||||
)
|
||||
.expect("ISO gte-only must succeed");
|
||||
let sub = iso_gte.get_document("date").unwrap();
|
||||
assert_eq!(sub.get("$gte"), Some(&Bson::String("2024-01-01".to_owned())));
|
||||
assert_eq!(sub.get("$lt"), None);
|
||||
|
||||
// ISO string: lt-only
|
||||
let iso_lt = build_filter_document(
|
||||
SourceCollection::EodPrices,
|
||||
&MongoReadSpec {
|
||||
date_range: Some(MongoDateRange {
|
||||
gte: None,
|
||||
lt: Some(FilterDateValue::IsoString("2025-01-01".to_owned())),
|
||||
}),
|
||||
..MongoReadSpec::default()
|
||||
},
|
||||
)
|
||||
.expect("ISO lt-only must succeed");
|
||||
let sub = iso_lt.get_document("date").unwrap();
|
||||
assert_eq!(sub.get("$gte"), None);
|
||||
assert_eq!(sub.get("$lt"), Some(&Bson::String("2025-01-01".to_owned())));
|
||||
|
||||
// BSON DateTime millis: gte-only
|
||||
let dt_gte = build_filter_document(
|
||||
SourceCollection::CeoShorts,
|
||||
&MongoReadSpec {
|
||||
date_range: Some(MongoDateRange {
|
||||
gte: Some(FilterDateValue::BsonDateTimeMillis(1_704_067_200_000)),
|
||||
lt: None,
|
||||
}),
|
||||
..MongoReadSpec::default()
|
||||
},
|
||||
)
|
||||
.expect("DateTime gte-only must succeed");
|
||||
let sub = dt_gte.get_document("timestamp").unwrap();
|
||||
assert_eq!(
|
||||
sub.get("$gte"),
|
||||
Some(&Bson::DateTime(DateTime::from_millis(1_704_067_200_000)))
|
||||
);
|
||||
assert_eq!(sub.get("$lt"), None);
|
||||
|
||||
// BSON DateTime millis: lt-only
|
||||
let dt_lt = build_filter_document(
|
||||
SourceCollection::CeoShorts,
|
||||
&MongoReadSpec {
|
||||
date_range: Some(MongoDateRange {
|
||||
gte: None,
|
||||
lt: Some(FilterDateValue::BsonDateTimeMillis(1_735_689_600_000)),
|
||||
}),
|
||||
..MongoReadSpec::default()
|
||||
},
|
||||
)
|
||||
.expect("DateTime lt-only must succeed");
|
||||
let sub = dt_lt.get_document("timestamp").unwrap();
|
||||
assert_eq!(sub.get("$gte"), None);
|
||||
assert_eq!(
|
||||
sub.get("$lt"),
|
||||
Some(&Bson::DateTime(DateTime::from_millis(1_735_689_600_000)))
|
||||
);
|
||||
|
||||
// Equal bounds (gte == lt) must be rejected for both variants.
|
||||
assert!(matches!(
|
||||
build_filter_document(
|
||||
SourceCollection::EodPrices,
|
||||
&MongoReadSpec {
|
||||
date_range: Some(MongoDateRange {
|
||||
gte: Some(FilterDateValue::IsoString("2024-06-01".to_owned())),
|
||||
lt: Some(FilterDateValue::IsoString("2024-06-01".to_owned())),
|
||||
}),
|
||||
..MongoReadSpec::default()
|
||||
}
|
||||
),
|
||||
Err(MongoStreamError::InvalidDateRange {
|
||||
collection: SourceCollection::EodPrices,
|
||||
..
|
||||
})
|
||||
));
|
||||
assert!(matches!(
|
||||
build_filter_document(
|
||||
SourceCollection::CeoShorts,
|
||||
&MongoReadSpec {
|
||||
date_range: Some(MongoDateRange {
|
||||
gte: Some(FilterDateValue::BsonDateTimeMillis(1_704_067_200_000)),
|
||||
lt: Some(FilterDateValue::BsonDateTimeMillis(1_704_067_200_000)),
|
||||
}),
|
||||
..MongoReadSpec::default()
|
||||
}
|
||||
),
|
||||
Err(MongoStreamError::InvalidDateRange {
|
||||
collection: SourceCollection::CeoShorts,
|
||||
..
|
||||
})
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qm_filings_rejects_invalid_eastern_calendar_day_bounds() {
|
||||
let invalid_days = [
|
||||
"2023-02-29", // non-leap year Feb 29
|
||||
"2024-13-01", // month > 12
|
||||
"2024-00-10", // month == 0
|
||||
"2024-01-00", // day == 0
|
||||
"2024-04-31", // April has 30 days
|
||||
"2024-01-01 ", // trailing space
|
||||
"2024-01-01Z", // trailing UTC marker
|
||||
"2024-01-0é", // non-ASCII byte
|
||||
"2024-01-01", // fullwidth digits (non-ASCII)
|
||||
];
|
||||
|
||||
for bad in invalid_days {
|
||||
assert!(
|
||||
matches!(
|
||||
build_filter_document(
|
||||
SourceCollection::QmFilings,
|
||||
&MongoReadSpec {
|
||||
date_range: Some(MongoDateRange {
|
||||
gte: Some(FilterDateValue::IsoString(bad.to_owned())),
|
||||
lt: None,
|
||||
}),
|
||||
..MongoReadSpec::default()
|
||||
}
|
||||
),
|
||||
Err(MongoStreamError::InvalidDateRange {
|
||||
collection: SourceCollection::QmFilings,
|
||||
..
|
||||
})
|
||||
),
|
||||
"expected gte={bad:?} to be rejected for qmFilings"
|
||||
);
|
||||
assert!(
|
||||
matches!(
|
||||
build_filter_document(
|
||||
SourceCollection::QmFilings,
|
||||
&MongoReadSpec {
|
||||
date_range: Some(MongoDateRange {
|
||||
gte: None,
|
||||
lt: Some(FilterDateValue::IsoString(bad.to_owned())),
|
||||
}),
|
||||
..MongoReadSpec::default()
|
||||
}
|
||||
),
|
||||
Err(MongoStreamError::InvalidDateRange {
|
||||
collection: SourceCollection::QmFilings,
|
||||
..
|
||||
})
|
||||
),
|
||||
"expected lt={bad:?} to be rejected for qmFilings"
|
||||
);
|
||||
}
|
||||
|
||||
// Leap-year Feb 29 is valid.
|
||||
build_filter_document(
|
||||
SourceCollection::QmFilings,
|
||||
&MongoReadSpec {
|
||||
date_range: Some(MongoDateRange {
|
||||
gte: Some(FilterDateValue::IsoString("2024-02-29".to_owned())),
|
||||
lt: Some(FilterDateValue::IsoString("2024-03-01".to_owned())),
|
||||
}),
|
||||
..MongoReadSpec::default()
|
||||
},
|
||||
)
|
||||
.expect("valid leap-year calendar day 2024-02-29 must succeed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn source_collection_exhaustive_key_date_and_display_mappings() {
|
||||
let expected: [(SourceCollection, &str, &str, Option<&str>); 8] = [
|
||||
(
|
||||
SourceCollection::EodPrices,
|
||||
"eodPrices",
|
||||
"eodSearchCode",
|
||||
Some("date"),
|
||||
),
|
||||
(
|
||||
SourceCollection::QmSplits,
|
||||
"qmSplits",
|
||||
"qmSearchCode",
|
||||
Some("date"),
|
||||
),
|
||||
(
|
||||
SourceCollection::QmDividends,
|
||||
"qmDividends",
|
||||
"qmSearchCode",
|
||||
Some("date"),
|
||||
),
|
||||
(
|
||||
SourceCollection::QmPrices,
|
||||
"qmPrices",
|
||||
"qmSearchCode",
|
||||
Some("date"),
|
||||
),
|
||||
(
|
||||
SourceCollection::TeMarketData,
|
||||
"teMarketData",
|
||||
"teSymbol",
|
||||
None,
|
||||
),
|
||||
(
|
||||
SourceCollection::CeoShorts,
|
||||
"ceoShorts",
|
||||
"symbol",
|
||||
Some("timestamp"),
|
||||
),
|
||||
(
|
||||
SourceCollection::QmFilings,
|
||||
"qmFilings",
|
||||
"symbol",
|
||||
Some("timefiled"),
|
||||
),
|
||||
(
|
||||
SourceCollection::QmEarnings,
|
||||
"qmEarnings",
|
||||
"qmSearchCode",
|
||||
Some("reportDate"),
|
||||
),
|
||||
];
|
||||
|
||||
assert_eq!(SourceCollection::ALL.len(), expected.len());
|
||||
for (actual, (exp_variant, exp_name, exp_key, exp_date)) in
|
||||
SourceCollection::ALL.into_iter().zip(expected)
|
||||
{
|
||||
assert_eq!(actual, exp_variant);
|
||||
assert_eq!(actual.as_str(), exp_name);
|
||||
assert_eq!(format!("{actual}"), exp_name);
|
||||
assert_eq!(actual.key_field(), exp_key);
|
||||
assert_eq!(actual.date_field(), exp_date);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn error_redaction_handles_uppercase_and_mixed_case_mongo_schemes() {
|
||||
let primary_uri = validate_uri(
|
||||
"mongodb://studio_stock_bot_read:PRIMARY_CASE_PASS@localhost:27017/stock?authSource=stock",
|
||||
);
|
||||
let upper_secret = "UPPER_SCHEME_SECRET_777";
|
||||
let mixed_secret = "MIXED_SRV_SECRET_888";
|
||||
let diagnostic = format!(
|
||||
"tried MONGODB://reader:{upper_secret}@10.0.0.51:27017/stock then MongoDB+SRV://srvuser:{mixed_secret}@cluster.internal/stock"
|
||||
);
|
||||
|
||||
let err = MongoStreamError::redacted_source(&diagnostic, &primary_uri);
|
||||
let display_out = format!("{err}");
|
||||
let debug_out = format!("{err:?}");
|
||||
|
||||
for secret in ["PRIMARY_CASE_PASS", upper_secret, mixed_secret] {
|
||||
assert!(
|
||||
!display_out.contains(secret),
|
||||
"Display leaked `{secret}` under uppercase/mixed-case scheme: {display_out}"
|
||||
);
|
||||
assert!(
|
||||
!debug_out.contains(secret),
|
||||
"Debug leaked `{secret}` under uppercase/mixed-case scheme: {debug_out}"
|
||||
);
|
||||
}
|
||||
assert!(
|
||||
display_out.contains("[REDACTED]@10.0.0.51:27017/stock"),
|
||||
"expected redacted MONGODB:// URI in Display: {display_out}"
|
||||
);
|
||||
assert!(
|
||||
display_out.contains("[REDACTED]@cluster.internal/stock"),
|
||||
"expected redacted MongoDB+SRV:// URI in Display: {display_out}"
|
||||
);
|
||||
assert!(debug_out.contains("[REDACTED]"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn error_redaction_scrubs_repeated_password_occurrences_across_message() {
|
||||
let repeated_pass = "REPEATED_SENTINEL_PASS_4242";
|
||||
let primary_uri_str = format!(
|
||||
"mongodb://studio_stock_bot_read:{repeated_pass}@localhost:27017/stock?authSource=stock"
|
||||
);
|
||||
let primary_uri = validate_uri(&primary_uri_str);
|
||||
let diagnostic = format!(
|
||||
"attempt1={primary_uri_str}; raw1={repeated_pass}; fallback=mongodb://alt:{repeated_pass}@10.0.0.51:27017/stock; raw2={repeated_pass}; attempt2={primary_uri_str}"
|
||||
);
|
||||
|
||||
let err = MongoStreamError::redacted_source(&diagnostic, &primary_uri);
|
||||
let display_out = format!("{err}");
|
||||
let debug_out = format!("{err:?}");
|
||||
|
||||
assert!(
|
||||
!display_out.contains(repeated_pass),
|
||||
"Display leaked repeated password: {display_out}"
|
||||
);
|
||||
assert!(
|
||||
!debug_out.contains(repeated_pass),
|
||||
"Debug leaked repeated password: {debug_out}"
|
||||
);
|
||||
assert!(
|
||||
display_out.matches("[REDACTED]").count() >= 5,
|
||||
"expected every occurrence to be redacted in Display: {display_out}"
|
||||
);
|
||||
assert!(
|
||||
debug_out.matches("[REDACTED]").count() >= 5,
|
||||
"expected every occurrence to be redacted in Debug: {debug_out}"
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue