mirror of
https://github.com/KusakabeShi/EtherGuard-VPN.git
synced 2024-11-29 10:43:09 +01:00
f60b3919be
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>
41 lines
1.2 KiB
Go
41 lines
1.2 KiB
Go
/* SPDX-License-Identifier: MIT
|
|
*
|
|
* Copyright (C) 2017-2020 WireGuard LLC. All Rights Reserved.
|
|
*/
|
|
|
|
package tai64n
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// 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) {
|
|
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)
|
|
}
|
|
})
|
|
}
|
|
}
|