From f06b25601006ba42c427349837dca6d5634bc718 Mon Sep 17 00:00:00 2001 From: trinity-1686a Date: Sat, 26 Feb 2022 00:33:44 +0100 Subject: [PATCH] use wallclock where possible in tests --- crates/tor-circmgr/src/path/exitpath.rs | 3 +++ crates/tor-circmgr/src/usage.rs | 2 ++ crates/tor-dirclient/src/lib.rs | 1 + crates/tor-dirclient/src/request.rs | 1 + crates/tor-dirmgr/src/bootstrap.rs | 12 +++++------ crates/tor-dirmgr/src/event.rs | 1 + crates/tor-dirmgr/src/lib.rs | 18 +++++++++------- crates/tor-guardmgr/src/guard.rs | 11 ++++++++-- crates/tor-guardmgr/src/sample.rs | 22 ++++++++++++++++---- crates/tor-guardmgr/src/util.rs | 1 + crates/tor-netdir/src/testnet.rs | 1 + crates/tor-netdir/src/weight.rs | 1 + crates/tor-netdoc/src/doc/authcert/build.rs | 2 ++ crates/tor-netdoc/src/doc/netstatus/build.rs | 1 + crates/tor-rtcompat/src/lib.rs | 6 +++--- crates/tor-rtcompat/src/timer.rs | 1 + crates/tor-rtmock/src/time.rs | 2 ++ crates/tor-rtmock/tests/rtcompat_timing.rs | 1 + 18 files changed, 64 insertions(+), 23 deletions(-) diff --git a/crates/tor-circmgr/src/path/exitpath.rs b/crates/tor-circmgr/src/path/exitpath.rs index 17c2d50d7..67150fc4a 100644 --- a/crates/tor-circmgr/src/path/exitpath.rs +++ b/crates/tor-circmgr/src/path/exitpath.rs @@ -265,6 +265,7 @@ mod test { let dirinfo = (&netdir).into(); let config = PathConfig::default(); let guards: OptDummyGuardMgr<'_> = None; + #[allow(clippy::disallowed_methods)] let now = SystemTime::now(); for _ in 0..1000 { @@ -310,6 +311,7 @@ mod test { .unwrap(); let dirinfo = (&netdir).into(); let guards: OptDummyGuardMgr<'_> = None; + #[allow(clippy::disallowed_methods)] let now = SystemTime::now(); let config = PathConfig::default(); @@ -357,6 +359,7 @@ mod test { let dirinfo = (&netdir).into(); let guards: OptDummyGuardMgr<'_> = None; let config = PathConfig::default(); + #[allow(clippy::disallowed_methods)] let now = SystemTime::now(); // With target ports diff --git a/crates/tor-circmgr/src/usage.rs b/crates/tor-circmgr/src/usage.rs index 8a1071baf..a9541b0db 100644 --- a/crates/tor-circmgr/src/usage.rs +++ b/crates/tor-circmgr/src/usage.rs @@ -728,6 +728,7 @@ mod test { let di = (&netdir).into(); let config = crate::PathConfig::default(); let guards: OptDummyGuardMgr<'_> = None; + #[allow(clippy::disallowed_methods)] let now = SystemTime::now(); // Only doing basic tests for now. We'll test the path @@ -805,6 +806,7 @@ mod test { let di = (&netdir).into(); let config = crate::PathConfig::default(); let guards: OptDummyGuardMgr<'_> = None; + #[allow(clippy::disallowed_methods)] let now = SystemTime::now(); let (path, usage, _, _) = TargetCircUsage::TimeoutTesting diff --git a/crates/tor-dirclient/src/lib.rs b/crates/tor-dirclient/src/lib.rs index 61af8ae31..2434d3487 100644 --- a/crates/tor-dirclient/src/lib.rs +++ b/crates/tor-dirclient/src/lib.rs @@ -478,6 +478,7 @@ mod test { ) -> (Result<()>, Vec) { // We don't need to do anything fancy here, since we aren't simulating // a timeout. + #[allow(clippy::disallowed_methods)] let mock_time = MockSleepProvider::new(std::time::SystemTime::now()); let mut output = Vec::new(); diff --git a/crates/tor-dirclient/src/request.rs b/crates/tor-dirclient/src/request.rs index ffa58faa8..4d474e65c 100644 --- a/crates/tor-dirclient/src/request.rs +++ b/crates/tor-dirclient/src/request.rs @@ -492,6 +492,7 @@ mod test { .unwrap(); let d2 = b"blah blah blah 12 blah blah blah"; + #[allow(clippy::disallowed_methods)] let d3 = SystemTime::now(); let mut req = ConsensusRequest::default(); diff --git a/crates/tor-dirmgr/src/bootstrap.rs b/crates/tor-dirmgr/src/bootstrap.rs index a24fe0264..6a0c7ac13 100644 --- a/crates/tor-dirmgr/src/bootstrap.rs +++ b/crates/tor-dirmgr/src/bootstrap.rs @@ -353,9 +353,11 @@ mod test { use std::convert::TryInto; use std::sync::Mutex; use tor_netdoc::doc::microdesc::MdDigest; + use tor_rtcompat::SleepProvider; #[test] fn week() { + #[allow(clippy::disallowed_methods)] let now = SystemTime::now(); let one_day = Duration::new(86400, 0); @@ -500,14 +502,13 @@ mod test { fn all_in_cache() { // Let's try bootstrapping when everything is in the cache. tor_rtcompat::test_with_one_runtime!(|rt| async { + let now = rt.wallclock(); let (_tempdir, mgr) = new_mgr(rt); { let mut store = mgr.store_if_rw().unwrap().lock().unwrap(); for h in [H1, H2, H3, H4, H5] { - store - .store_microdescs(&[("ignore", &h)], SystemTime::now()) - .unwrap(); + store.store_microdescs(&[("ignore", &h)], now).unwrap(); } } let mgr = Arc::new(mgr); @@ -533,14 +534,13 @@ mod test { // Let's try bootstrapping with all of phase1 and part of // phase 2 in cache. tor_rtcompat::test_with_one_runtime!(|rt| async { + let now = rt.wallclock(); let (_tempdir, mgr) = new_mgr(rt); { let mut store = mgr.store_if_rw().unwrap().lock().unwrap(); for h in [H1, H2, H3] { - store - .store_microdescs(&[("ignore", &h)], SystemTime::now()) - .unwrap(); + store.store_microdescs(&[("ignore", &h)], now).unwrap(); } } { diff --git a/crates/tor-dirmgr/src/event.rs b/crates/tor-dirmgr/src/event.rs index 48935012f..5614db6da 100644 --- a/crates/tor-dirmgr/src/event.rs +++ b/crates/tor-dirmgr/src/event.rs @@ -657,6 +657,7 @@ mod test { #[test] fn dir_status_basics() { + #[allow(clippy::disallowed_methods)] let now = SystemTime::now(); let hour = Duration::new(3600, 0); diff --git a/crates/tor-dirmgr/src/lib.rs b/crates/tor-dirmgr/src/lib.rs index 9c785435a..2d2dc87c1 100644 --- a/crates/tor-dirmgr/src/lib.rs +++ b/crates/tor-dirmgr/src/lib.rs @@ -959,6 +959,7 @@ mod test { use std::time::Duration; use tempfile::TempDir; use tor_netdoc::doc::{authcert::AuthCertKeyIds, netstatus::Lifetime}; + use tor_rtcompat::SleepProvider; pub(crate) fn new_mgr(runtime: R) -> (TempDir, DirMgr) { let dir = TempDir::new().unwrap(); @@ -984,12 +985,12 @@ mod test { #[test] fn load_and_store_internals() { tor_rtcompat::test_with_one_runtime!(|rt| async { - let (_tempdir, mgr) = new_mgr(rt); - - let now = SystemTime::now(); + let now = rt.wallclock(); let tomorrow = now + Duration::from_secs(86400); let later = tomorrow + Duration::from_secs(86400); + let (_tempdir, mgr) = new_mgr(rt); + // Seed the storage with a bunch of junk. let d1 = [5_u8; 32]; let d2 = [7; 32]; @@ -1099,12 +1100,12 @@ mod test { #[test] fn make_consensus_request() { tor_rtcompat::test_with_one_runtime!(|rt| async { - let (_tempdir, mgr) = new_mgr(rt); - - let now = SystemTime::now(); + let now = rt.wallclock(); let tomorrow = now + Duration::from_secs(86400); let later = tomorrow + Duration::from_secs(86400); + let (_tempdir, mgr) = new_mgr(rt); + // Try with an empty store. let req = mgr .make_consensus_request(ConsensusFlavor::Microdesc) @@ -1194,6 +1195,9 @@ mod test { #[test] fn expand_response() { tor_rtcompat::test_with_one_runtime!(|rt| async { + let now = rt.wallclock(); + let day = Duration::from_secs(86400); + let (_tempdir, mgr) = new_mgr(rt); // Try a simple request: nothing should happen. @@ -1217,8 +1221,6 @@ mod test { // we can ask for a diff. { let mut store = mgr.store.lock().unwrap(); - let now = SystemTime::now(); - let day = Duration::from_secs(86400); let d_in = [0x99; 32]; // This one, we can fake. let cmeta = ConsensusMeta::new( Lifetime::new(now, now + day, now + 2 * day).unwrap(), diff --git a/crates/tor-guardmgr/src/guard.rs b/crates/tor-guardmgr/src/guard.rs index bfafd8382..2b1c1f477 100644 --- a/crates/tor-guardmgr/src/guard.rs +++ b/crates/tor-guardmgr/src/guard.rs @@ -709,6 +709,7 @@ mod test { fn basic_guard() -> Guard { let id = basic_id(); let ports = vec!["127.0.0.7:7777".parse().unwrap()]; + #[allow(clippy::disallowed_methods)] let added = SystemTime::now(); Guard::new(id, ports, added) } @@ -848,9 +849,11 @@ mod test { fn record_success() { let t1 = Instant::now() - Duration::from_secs(10); // has to be in the future, since the guard's "added_at" time is based on now. - let t2 = SystemTime::now() + Duration::from_secs(300 * 86400); + #[allow(clippy::disallowed_methods)] + let now = SystemTime::now(); + let t2 = now + Duration::from_secs(300 * 86400); let t3 = Instant::now() + Duration::from_secs(310 * 86400); - let t4 = SystemTime::now() + Duration::from_secs(320 * 86400); + let t4 = now + Duration::from_secs(320 * 86400); let mut g = basic_guard(); g.record_failure(t1, true); @@ -906,6 +909,7 @@ mod test { fn expiration() { const DAY: Duration = Duration::from_secs(24 * 60 * 60); let params = GuardParams::default(); + #[allow(clippy::disallowed_methods)] let now = SystemTime::now(); let g = basic_guard(); @@ -937,6 +941,7 @@ mod test { .unwrap_if_sufficient() .unwrap(); let params = GuardParams::default(); + #[allow(clippy::disallowed_methods)] let now = SystemTime::now(); // Construct a guard from a relay from the netdir. @@ -993,6 +998,7 @@ mod test { .unwrap(); //let params = GuardParams::default(); + #[allow(clippy::disallowed_methods)] let now = SystemTime::now(); // Try a guard that isn't in the netdir at all. @@ -1075,6 +1081,7 @@ mod test { let mut g = basic_guard(); let params = GuardParams::default(); + #[allow(clippy::disallowed_methods)] let now = SystemTime::now(); let _ignore = g.record_success(now, ¶ms); diff --git a/crates/tor-guardmgr/src/sample.rs b/crates/tor-guardmgr/src/sample.rs index 27f5dc2b4..7b33f8d46 100644 --- a/crates/tor-guardmgr/src/sample.rs +++ b/crates/tor-guardmgr/src/sample.rs @@ -826,6 +826,7 @@ mod test { let mut samples: Vec> = Vec::new(); for _ in 0..3 { let mut guards = GuardSet::default(); + #[allow(clippy::disallowed_methods)] guards.extend_sample_as_needed(SystemTime::now(), ¶ms, &netdir); assert_eq!(guards.guards.len(), params.min_filtered_sample_size); assert_eq!(guards.confirmed.len(), 0); @@ -838,10 +839,14 @@ mod test { assert!(relay.is_flagged_guard()); assert!(relay.is_dir_cache()); assert!(guards.contains_relay(&relay)); - assert!(!guard.is_expired(¶ms, SystemTime::now())); + #[allow(clippy::disallowed_methods)] + { + assert!(!guard.is_expired(¶ms, SystemTime::now())); + } } // Make sure that the sample doesn't expand any further. + #[allow(clippy::disallowed_methods)] guards.extend_sample_as_needed(SystemTime::now(), ¶ms, &netdir); assert_eq!(guards.guards.len(), params.min_filtered_sample_size); guards.assert_consistency(); @@ -861,8 +866,10 @@ mod test { min_filtered_sample_size: 5, ..GuardParams::default() }; + + #[allow(clippy::disallowed_methods)] let t1 = SystemTime::now(); - let t2 = SystemTime::now() + Duration::from_secs(20); + let t2 = t1 + Duration::from_secs(20); let mut guards = GuardSet::default(); guards.extend_sample_as_needed(t1, ¶ms, &netdir); @@ -897,9 +904,10 @@ mod test { n_primary: 4, ..GuardParams::default() }; + #[allow(clippy::disallowed_methods)] let t1 = SystemTime::now(); - let t2 = SystemTime::now() + Duration::from_secs(20); - let t3 = SystemTime::now() + Duration::from_secs(30); + let t2 = t1 + Duration::from_secs(20); + let t3 = t2 + Duration::from_secs(30); let mut guards = GuardSet::default(); guards.extend_sample_as_needed(t1, ¶ms, &netdir); @@ -943,6 +951,7 @@ mod test { fn expiration() { let netdir = netdir(); let params = GuardParams::default(); + #[allow(clippy::disallowed_methods)] let t1 = SystemTime::now(); let mut guards = GuardSet::default(); @@ -977,6 +986,7 @@ mod test { n_primary: 2, ..GuardParams::default() }; + #[allow(clippy::disallowed_methods)] let st1 = SystemTime::now(); let i1 = Instant::now(); let sec = Duration::from_secs(1); @@ -1100,6 +1110,7 @@ mod test { max_sample_bw_fraction: 1.0, ..GuardParams::default() }; + #[allow(clippy::disallowed_methods)] let mut st = SystemTime::now(); let mut inst = Instant::now(); let sec = Duration::from_secs(1); @@ -1142,6 +1153,7 @@ mod test { let mut guards = GuardSet::default(); + #[allow(clippy::disallowed_methods)] guards.extend_sample_as_needed(SystemTime::now(), ¶ms, &netdir); guards.select_primary_guards(¶ms); @@ -1179,11 +1191,13 @@ mod test { }; let usage = crate::GuardUsageBuilder::default().build().unwrap(); let mut guards = GuardSet::default(); + #[allow(clippy::disallowed_methods)] guards.extend_sample_as_needed(SystemTime::now(), ¶ms, &netdir); guards.select_primary_guards(¶ms); assert_eq!(guards.primary.len(), 2); let (_kind, p_id1) = guards.pick_guard(&usage, ¶ms).unwrap(); + #[allow(clippy::disallowed_methods)] guards.record_success(&p_id1, ¶ms, SystemTime::now()); assert_eq!(guards.missing_primary_microdescriptors(&netdir), 0); diff --git a/crates/tor-guardmgr/src/util.rs b/crates/tor-guardmgr/src/util.rs index 48aaab106..5e89e1a67 100644 --- a/crates/tor-guardmgr/src/util.rs +++ b/crates/tor-guardmgr/src/util.rs @@ -66,6 +66,7 @@ mod test { #[test] fn test_randomize_time() { + #[allow(clippy::disallowed_methods)] let now = SystemTime::now(); let one_hour = Duration::from_secs(3600); let ten_sec = Duration::from_secs(10); diff --git a/crates/tor-netdir/src/testnet.rs b/crates/tor-netdir/src/testnet.rs index a8ca364f4..d08a0ef17 100644 --- a/crates/tor-netdir/src/testnet.rs +++ b/crates/tor-netdir/src/testnet.rs @@ -138,6 +138,7 @@ where f | RelayFlags::EXIT | RelayFlags::GUARD, ]; + #[allow(clippy::disallowed_methods)] let now = SystemTime::now(); let one_day = Duration::new(86400, 0); let mut bld = MdConsensus::builder(); diff --git a/crates/tor-netdir/src/weight.rs b/crates/tor-netdir/src/weight.rs index a1170bbe5..ecf804885 100644 --- a/crates/tor-netdir/src/weight.rs +++ b/crates/tor-netdir/src/weight.rs @@ -619,6 +619,7 @@ mod test { #[test] fn weightset_from_consensus() { use rand::Rng; + #[allow(clippy::disallowed_methods)] let now = SystemTime::now(); let one_hour = Duration::new(3600, 0); let mut rng = rand::thread_rng(); diff --git a/crates/tor-netdoc/src/doc/authcert/build.rs b/crates/tor-netdoc/src/doc/authcert/build.rs index 7c29e171c..eb09e251d 100644 --- a/crates/tor-netdoc/src/doc/authcert/build.rs +++ b/crates/tor-netdoc/src/doc/authcert/build.rs @@ -146,6 +146,7 @@ mod test { #[test] fn simple_cert() { + #[allow(clippy::disallowed_methods)] let now = SystemTime::now(); let one_hour = Duration::new(3600, 0); let later = now + one_hour * 2; @@ -166,6 +167,7 @@ mod test { #[test] fn failing_cert() { + #[allow(clippy::disallowed_methods)] let now = SystemTime::now(); let one_hour = Duration::new(3600, 0); let later = now + one_hour * 2; diff --git a/crates/tor-netdoc/src/doc/netstatus/build.rs b/crates/tor-netdoc/src/doc/netstatus/build.rs index 0863b8eb2..e1eb350c7 100644 --- a/crates/tor-netdoc/src/doc/netstatus/build.rs +++ b/crates/tor-netdoc/src/doc/netstatus/build.rs @@ -377,6 +377,7 @@ mod test { #[test] fn consensus() { + #[allow(clippy::disallowed_methods)] let now = SystemTime::now(); let one_hour = Duration::new(3600, 0); diff --git a/crates/tor-rtcompat/src/lib.rs b/crates/tor-rtcompat/src/lib.rs index f41fbe9bb..70e885078 100644 --- a/crates/tor-rtcompat/src/lib.rs +++ b/crates/tor-rtcompat/src/lib.rs @@ -385,7 +385,7 @@ mod test { use native_tls_crate as native_tls; use std::io::Result as IoResult; use std::net::{Ipv4Addr, SocketAddrV4}; - use std::time::{Duration, Instant, SystemTime}; + use std::time::{Duration, Instant}; // Test "sleep" with a tiny delay, and make sure that at least that // much delay happens. @@ -436,14 +436,14 @@ mod test { let rt = runtime.clone(); runtime.block_on(async { let i1 = Instant::now(); - let now = SystemTime::now(); + let now = runtime.wallclock(); let one_millis = Duration::from_millis(1); let one_millis_later = now + one_millis; rt.sleep_until_wallclock(one_millis_later).await; let i2 = Instant::now(); - let newtime = SystemTime::now(); + let newtime = runtime.wallclock(); assert!(newtime >= one_millis_later); assert!(i2 - i1 >= one_millis); }); diff --git a/crates/tor-rtcompat/src/timer.rs b/crates/tor-rtcompat/src/timer.rs index f2e0dc7e5..d06765186 100644 --- a/crates/tor-rtcompat/src/timer.rs +++ b/crates/tor-rtcompat/src/timer.rs @@ -203,6 +203,7 @@ mod test { } let minute = Duration::from_secs(60); let second = Duration::from_secs(1); + #[allow(clippy::disallowed_methods)] let start = SystemTime::now(); let target = start + 30 * minute; diff --git a/crates/tor-rtmock/src/time.rs b/crates/tor-rtmock/src/time.rs index c4d703e7b..e0e3c0cf1 100644 --- a/crates/tor-rtmock/src/time.rs +++ b/crates/tor-rtmock/src/time.rs @@ -439,6 +439,7 @@ mod test { #[test] fn basics_of_time_travel() { + #[allow(clippy::disallowed_methods)] let w1 = SystemTime::now(); let sp = MockSleepProvider::new(w1); let i1 = sp.now(); @@ -461,6 +462,7 @@ mod test { use std::sync::atomic::AtomicBool; use std::sync::atomic::Ordering; + #[allow(clippy::disallowed_methods)] let sp = MockSleepProvider::new(SystemTime::now()); let one_hour = Duration::new(3600, 0); diff --git a/crates/tor-rtmock/tests/rtcompat_timing.rs b/crates/tor-rtmock/tests/rtcompat_timing.rs index 6c4daf68b..21d6622cb 100644 --- a/crates/tor-rtmock/tests/rtcompat_timing.rs +++ b/crates/tor-rtmock/tests/rtcompat_timing.rs @@ -17,6 +17,7 @@ fn timeouts() { oneshot::Sender<()>, Timeout, tor_rtmock::time::Sleeping>, ) { + #[allow(clippy::disallowed_methods)] let start = SystemTime::now(); let (send, recv) = oneshot::channel::<()>(); let mock_sp = MockSleepProvider::new(start);