-
Notifications
You must be signed in to change notification settings - Fork 7
/
conn_test.go
58 lines (44 loc) · 1.25 KB
/
conn_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package fastafpacket
import (
"encoding/binary"
"testing"
"time"
"github.com/stretchr/testify/assert"
"golang.org/x/sys/unix"
)
func TestParseTimestamps(t *testing.T) {
t.Run("success", func(t *testing.T) {
t.Parallel()
hardware := time.Date(1982, 1, 13, 1, 1, 1, 1, time.UTC)
software := hardware.Add(10 * time.Millisecond)
msg := unix.SocketControlMessage{
Header: unix.Cmsghdr{
Level: unix.SOL_SOCKET,
Type: SocketOptTimestamping,
},
Data: make([]byte, 48),
}
hardwarenano := hardware.UnixNano()
softwarenano := software.UnixNano()
binary.LittleEndian.PutUint64(msg.Data[0:], 0)
binary.LittleEndian.PutUint64(msg.Data[8:], uint64(softwarenano))
binary.LittleEndian.PutUint64(msg.Data[32:], 0)
binary.LittleEndian.PutUint64(msg.Data[40:], uint64(hardwarenano))
ts, err := ParseSocketTimestamps(msg)
assert.NoError(t, err)
assert.True(t, ts.Hardware.Equal(hardware))
assert.True(t, ts.Software.Equal(software))
})
t.Run("failure", func(t *testing.T) {
t.Parallel()
msg := unix.SocketControlMessage{
Header: unix.Cmsghdr{
Level: unix.SOL_XDP,
},
}
ts, err := ParseSocketTimestamps(msg)
assert.Error(t, err)
assert.True(t, ts.Hardware.IsZero())
assert.True(t, ts.Software.IsZero())
})
}