diff options
author | Dmytro Shynkevych <dmytro@tailscale.com> | 2020-05-05 18:37:54 -0400 |
---|---|---|
committer | David Crawshaw <crawshaw@tailscale.com> | 2020-05-06 16:01:48 +1000 |
commit | f60b3919bec891d37652edc25c48d83345d9885c (patch) | |
tree | eb4156d4891e71dc8f244e059062bc6527057396 /tai64n/tai64n_test.go | |
parent | da9d300cf8b50d1cf629b7fa4423d122a74a6871 (diff) |
tai64n: make the test deterministic
In the presence of preemption, the current test may fail transiently.
This uses static test data instead to ensure consistent behavior.
Signed-off-by: Dmytro Shynkevych <dmytro@tailscale.com>
Diffstat (limited to 'tai64n/tai64n_test.go')
-rw-r--r-- | tai64n/tai64n_test.go | 40 |
1 files changed, 25 insertions, 15 deletions
diff --git a/tai64n/tai64n_test.go b/tai64n/tai64n_test.go index 05a9d8f..6df7367 100644 --- a/tai64n/tai64n_test.go +++ b/tai64n/tai64n_test.go @@ -10,21 +10,31 @@ import ( "time" ) -/* Testing the essential property of the timestamp - * as used by WireGuard. - */ +// Test that timestamps are monotonic as required by Wireguard and that +// nanosecond-level information is whitened to prevent side channel attacks. func TestMonotonic(t *testing.T) { - old := Now() - for i := 0; i < 50; i++ { - next := Now() - if next.After(old) { - t.Error("Whitening insufficient") - } - time.Sleep(time.Duration(whitenerMask)/time.Nanosecond + 1) - next = Now() - if !next.After(old) { - t.Error("Not monotonically increasing on whitened nano-second scale") - } - old = next + startTime := time.Unix(0, 123456789) // a nontrivial bit pattern + // Whitening should reduce timestamp granularity + // to more than 10 but fewer than 20 milliseconds. + tests := []struct { + name string + t1, t2 time.Time + wantAfter bool + }{ + {"after_10_ns", startTime, startTime.Add(10 * time.Nanosecond), false}, + {"after_10_us", startTime, startTime.Add(10 * time.Microsecond), false}, + {"after_1_ms", startTime, startTime.Add(time.Millisecond), false}, + {"after_10_ms", startTime, startTime.Add(10 * time.Millisecond), false}, + {"after_20_ms", startTime, startTime.Add(20 * time.Millisecond), true}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ts1, ts2 := stamp(tt.t1), stamp(tt.t2) + got := ts2.After(ts1) + if got != tt.wantAfter { + t.Errorf("after = %v; want %v", got, tt.wantAfter) + } + }) } } |