summaryrefslogtreecommitdiffhomepage
path: root/pkg/sentry/socket/control/control_test.go
blob: 7e28a0cefc3ca92a3b5bba5fb3dc36abe54021aa (plain)
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
59
// Copyright 2020 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package control provides internal representations of socket control
// messages.
package control

import (
	"testing"

	"github.com/google/go-cmp/cmp"
	"gvisor.dev/gvisor/pkg/abi/linux"
	"gvisor.dev/gvisor/pkg/binary"
	"gvisor.dev/gvisor/pkg/hostarch"
	"gvisor.dev/gvisor/pkg/sentry/socket"
)

func TestParse(t *testing.T) {
	// Craft the control message to parse.
	length := linux.SizeOfControlMessageHeader + linux.SizeOfTimeval
	hdr := linux.ControlMessageHeader{
		Length: uint64(length),
		Level:  linux.SOL_SOCKET,
		Type:   linux.SO_TIMESTAMP,
	}
	buf := make([]byte, 0, length)
	buf = binary.Marshal(buf, hostarch.ByteOrder, &hdr)
	ts := linux.Timeval{
		Sec:  2401,
		Usec: 343,
	}
	buf = binary.Marshal(buf, hostarch.ByteOrder, &ts)

	cmsg, err := Parse(nil, nil, buf, 8 /* width */)
	if err != nil {
		t.Fatalf("Parse(_, _, %+v, _): %v", cmsg, err)
	}

	want := socket.ControlMessages{
		IP: socket.IPControlMessages{
			HasTimestamp: true,
			Timestamp:    ts.ToNsecCapped(),
		},
	}
	if diff := cmp.Diff(want, cmsg); diff != "" {
		t.Errorf("unexpected message parsed, (-want, +got):\n%s", diff)
	}
}