summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNora Widdecke <nora@sequoia-pgp.org>2021-03-25 17:19:15 +0100
committerNora Widdecke <nora@sequoia-pgp.org>2021-04-06 13:26:10 +0200
commitdc6a0273a5ef4ad2ae66ed620e25672f258bf426 (patch)
tree2d7b09285cad28cd20371e2cb7f690aa0f7a7523
parenteb371b84d900295e8d9c31fe91226f5cb41a2fbf (diff)
openpgp: Fix SystemTime test.
- The size of SystemTime is an implementation detail of the standard library and does not give a direct indication whether values larger that i32::MAX will fit in it. - Adjust the test to observe if we are on a system can represent a large value as a SystemTime or not, and assert that the values are clamped correctly.
-rw-r--r--openpgp/src/types/timestamp.rs51
1 files changed, 38 insertions, 13 deletions
diff --git a/openpgp/src/types/timestamp.rs b/openpgp/src/types/timestamp.rs
index af318c29..7fe80a42 100644
--- a/openpgp/src/types/timestamp.rs
+++ b/openpgp/src/types/timestamp.rs
@@ -669,21 +669,46 @@ mod tests {
}
// #668
- // Ensure that, on 32-bit platforms, Timestamps between i32::MAX + 1 and u32::MAX are
- // clamped down to i32::MAX, and values below are not altered.
- #[cfg(any(target_arch = "x86", target_arch = "arm", target_arch = "mips"))]
+ // Ensure that, on systems where the SystemTime can only represent values
+ // up to i32::MAX (generally, 32-bit systems), Timestamps between
+ // i32::MAX + 1 and u32::MAX are clamped down to i32::MAX, and values below
+ // are not altered.
#[test]
fn system_time_32_bit() -> Result<()> {
- let t1 = Timestamp::from(u32::MAX);
- let t2 = Timestamp::from(i32::MAX as u32 + 1);
- assert_eq!(SystemTime::from(t1),
- UNIX_EPOCH + SystemDuration::new(i32::MAX as u64, 0));
- assert_eq!(SystemTime::from(t2),
- UNIX_EPOCH + SystemDuration::new(i32::MAX as u64, 0));
-
- let t3 = Timestamp::from(i32::MAX as u32 - 1);
- assert_eq!(SystemTime::from(t3),
- UNIX_EPOCH + SystemDuration::new(i32::MAX as u64 - 1, 0));
+ let is_system_time_too_small = UNIX_EPOCH
+ .checked_add(SystemDuration::new(i32::MAX as u64 + 1, 0))
+ .is_none();
+
+ let t1 = Timestamp::from(i32::MAX as u32 - 1);
+ let t2 = Timestamp::from(i32::MAX as u32);
+ let t3 = Timestamp::from(i32::MAX as u32 + 1);
+ let t4 = Timestamp::from(u32::MAX);
+
+ if is_system_time_too_small {
+ assert_eq!(SystemTime::from(t1),
+ UNIX_EPOCH + SystemDuration::new(i32::MAX as u64 - 1, 0));
+
+ assert_eq!(SystemTime::from(t2),
+ UNIX_EPOCH + SystemDuration::new(i32::MAX as u64, 0));
+
+ assert_eq!(SystemTime::from(t3),
+ UNIX_EPOCH + SystemDuration::new(i32::MAX as u64, 0));
+
+ assert_eq!(SystemTime::from(t4),
+ UNIX_EPOCH + SystemDuration::new(i32::MAX as u64, 0));
+ } else {
+ assert_eq!(SystemTime::from(t1),
+ UNIX_EPOCH + SystemDuration::new(i32::MAX as u64 - 1, 0));
+
+ assert_eq!(SystemTime::from(t2),
+ UNIX_EPOCH + SystemDuration::new(i32::MAX as u64, 0));
+
+ assert_eq!(SystemTime::from(t3),
+ UNIX_EPOCH + SystemDuration::new(i32::MAX as u64 + 1, 0));
+
+ assert_eq!(SystemTime::from(t4),
+ UNIX_EPOCH + SystemDuration::new(u32::MAX as u64, 0));
+ }
Ok(())
}
}