Add regression tests for Display of ParseError
What changed, and why it matters
This commit only adds new unit tests that check how a checksum error message is displayed to users. It does not change any production code, fix a bug, or alter behavior. There is no security issue in this commit itself.
No security action needed. Review the follow-up patch that changes the Display implementation, since this commit explicitly prepares for that change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff adds three regression tests in bitcoin/src/consensus/error.rs for the Display implementation of ParseError::InvalidChecksum. The tests verify that expected and actual 4-byte checksums are formatted as lowercase hex strings, including with leading zeros preserved. No implementation code is modified.
Changed components
bitcoin/src/consensus/error.rs (tests only)Inspect captured patch +41 / −0
diff --git a/bitcoin/src/consensus/error.rs b/bitcoin/src/consensus/error.rs
index 184daafc..16f6330f 100644
--- a/bitcoin/src/consensus/error.rs
+++ b/bitcoin/src/consensus/error.rs
@@ -257,3 +257,44 @@ impl From<OddLengthStringError> for FromHexError {
pub(crate) fn parse_failed_error(msg: &'static str) -> Error {
Error::Parse(ParseError::ParseFailed(msg))
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn invalid_checksum_display() {
+ let e = ParseError::InvalidChecksum {
+ expected: [0xde, 0xad, 0xbe, 0xef],
+ actual: [0xca, 0xfe, 0xba, 0xbe],
+ };
+
+ let want = "invalid checksum: expected deadbeef, actual cafebabe";
+ let got = format!("{}", e);
+ assert_eq!(got, want);
+ }
+
+ #[test]
+ fn invalid_checksum_display_expected_leading_zeros() {
+ let e = ParseError::InvalidChecksum {
+ expected: [0x00, 0x00, 0x00, 0x0f],
+ actual: [0xca, 0xfe, 0xba, 0xbe],
+ };
+
+ let want = "invalid checksum: expected 0000000f, actual cafebabe";
+ let got = format!("{}", e);
+ assert_eq!(got, want);
+ }
+
+ #[test]
+ fn invalid_checksum_display_actual_leading_zeros() {
+ let e = ParseError::InvalidChecksum {
+ expected: [0xde, 0xad, 0xbe, 0xef],
+ actual: [0x00, 0x00, 0x00, 0x0e],
+ };
+
+ let want = "invalid checksum: expected deadbeef, actual 0000000e";
+ let got = format!("{}", e);
+ assert_eq!(got, want);
+ }
+}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.