What changed, and why it matters
This commit only adds new automated tests for an existing data type called BlockTime. It does not change any production code, fix bugs, or alter behavior. There is no security issue here.
No action required. This is a test-only change with no security relevance.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff adds unit tests in units/src/time.rs covering serde round-trip serialization and the custom encoding/decoding (BlockTimeEncoder/BlockTimeDecoder) for BlockTime. No implementation code is modified; only test code is added under #[cfg(test)].
Changed components
units/src/time.rs (test module only)Inspect captured patch +57 / −0
diff --git a/units/src/time.rs b/units/src/time.rs
index c15cc3e6..c93609a6 100644
--- a/units/src/time.rs
+++ b/units/src/time.rs
@@ -170,9 +170,66 @@ impl<'a> Arbitrary<'a> for BlockTime {
mod tests {
use super::*;
+ #[cfg(feature = "encoding")]
+ use encoding::{Decodable as _, Decoder as _};
+ #[cfg(all(feature = "encoding", feature = "alloc"))]
+ use encoding::UnexpectedEofError;
+
#[test]
fn block_time_round_trip() {
let t = BlockTime::from(1_742_979_600); // 26 Mar 2025 9:00 UTC
assert_eq!(u32::from(t), 1_742_979_600);
}
+
+ #[test]
+ #[cfg(feature = "serde")]
+ fn block_time_serde_round_trip() {
+ let t = BlockTime::from(1_765_364_400); // 10 Dec 2025 11:00 UTC
+
+ let json = serde_json::to_string(&t).unwrap();
+ assert_eq!(json, "1765364400"); // ASCII number representation
+
+ let roundtrip = serde_json::from_str::<BlockTime>(&json).unwrap();
+ assert_eq!(t, roundtrip);
+ }
+
+ #[test]
+ #[cfg(all(feature = "encoding", feature = "alloc"))]
+ fn block_time_encoding_round_trip() {
+ let t = BlockTime::from(1_742_979_600); // 26 Mar 2025 9:00 UTC
+ let expected_bytes = alloc::vec![0x10, 0xc2, 0xe3, 0x67];
+
+ let encoded = encoding::encode_to_vec(&t);
+ assert_eq!(encoded, expected_bytes);
+
+ let decoded = encoding::decode_from_slice::<BlockTime>(encoded.as_slice()).unwrap();
+ assert_eq!(decoded, t);
+ }
+
+ #[test]
+ #[cfg(feature = "encoding")]
+ fn block_time_decoding() {
+ let bytes = [0xb0, 0x52, 0x39, 0x69];
+ let expected = BlockTime::from(1_765_364_400); // 10 Dec 2025 11:00 UTC
+
+ let mut decoder = BlockTime::decoder();
+ assert_eq!(decoder.read_limit(), 4);
+ assert!(!decoder.push_bytes(&mut bytes.as_slice()).unwrap());
+ assert_eq!(decoder.read_limit(), 0);
+
+ let decoded = decoder.end().unwrap();
+ assert_eq!(decoded, expected);
+ }
+
+ #[test]
+ #[cfg(all(feature = "encoding", feature = "alloc"))]
+ fn block_time_decoding_error() {
+ let bytes = [0xb0, 0x52, 0x39]; // 3 bytes is an EOF error
+
+ let mut decoder = BlockTimeDecoder::default();
+ assert!(decoder.push_bytes(&mut bytes.as_slice()).unwrap());
+
+ let error = decoder.end().unwrap_err();
+ assert!(matches!(error, BlockTimeDecoderError(UnexpectedEofError { .. })));
+ }
}
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.