2019-01-02 00:55:51 +00:00
|
|
|
/* SPDX-License-Identifier: MIT
|
2018-05-03 13:04:00 +00:00
|
|
|
*
|
2021-01-28 16:52:15 +00:00
|
|
|
* Copyright (C) 2017-2021 WireGuard LLC. All Rights Reserved.
|
2018-05-03 13:04:00 +00:00
|
|
|
*/
|
|
|
|
|
2018-02-12 21:29:11 +00:00
|
|
|
package tai64n
|
2018-02-11 21:53:39 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-05-05 22:37:54 +00:00
|
|
|
// Test that timestamps are monotonic as required by Wireguard and that
|
|
|
|
// nanosecond-level information is whitened to prevent side channel attacks.
|
2018-02-11 21:53:39 +00:00
|
|
|
func TestMonotonic(t *testing.T) {
|
2020-05-05 22:37:54 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
2018-02-11 21:53:39 +00:00
|
|
|
}
|
|
|
|
}
|