github.com/liloew/wireguard-go@v0.0.0-20220224014633-9cd745e6f114/tai64n/tai64n_test.go (about) 1 /* SPDX-License-Identifier: MIT 2 * 3 * Copyright (C) 2017-2021 WireGuard LLC. All Rights Reserved. 4 */ 5 6 package tai64n 7 8 import ( 9 "testing" 10 "time" 11 ) 12 13 // Test that timestamps are monotonic as required by Wireguard and that 14 // nanosecond-level information is whitened to prevent side channel attacks. 15 func TestMonotonic(t *testing.T) { 16 startTime := time.Unix(0, 123456789) // a nontrivial bit pattern 17 // Whitening should reduce timestamp granularity 18 // to more than 10 but fewer than 20 milliseconds. 19 tests := []struct { 20 name string 21 t1, t2 time.Time 22 wantAfter bool 23 }{ 24 {"after_10_ns", startTime, startTime.Add(10 * time.Nanosecond), false}, 25 {"after_10_us", startTime, startTime.Add(10 * time.Microsecond), false}, 26 {"after_1_ms", startTime, startTime.Add(time.Millisecond), false}, 27 {"after_10_ms", startTime, startTime.Add(10 * time.Millisecond), false}, 28 {"after_20_ms", startTime, startTime.Add(20 * time.Millisecond), true}, 29 } 30 31 for _, tt := range tests { 32 t.Run(tt.name, func(t *testing.T) { 33 ts1, ts2 := stamp(tt.t1), stamp(tt.t2) 34 got := ts2.After(ts1) 35 if got != tt.wantAfter { 36 t.Errorf("after = %v; want %v", got, tt.wantAfter) 37 } 38 }) 39 } 40 }