github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/sentry/socket/control/control_test.go (about) 1 // Copyright 2020 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package control provides internal representations of socket control 16 // messages. 17 package control 18 19 import ( 20 "testing" 21 22 "github.com/google/go-cmp/cmp" 23 "github.com/SagerNet/gvisor/pkg/abi/linux" 24 "github.com/SagerNet/gvisor/pkg/binary" 25 "github.com/SagerNet/gvisor/pkg/hostarch" 26 "github.com/SagerNet/gvisor/pkg/sentry/socket" 27 ) 28 29 func TestParse(t *testing.T) { 30 // Craft the control message to parse. 31 length := linux.SizeOfControlMessageHeader + linux.SizeOfTimeval 32 hdr := linux.ControlMessageHeader{ 33 Length: uint64(length), 34 Level: linux.SOL_SOCKET, 35 Type: linux.SO_TIMESTAMP, 36 } 37 buf := make([]byte, 0, length) 38 buf = binary.Marshal(buf, hostarch.ByteOrder, &hdr) 39 ts := linux.Timeval{ 40 Sec: 2401, 41 Usec: 343, 42 } 43 buf = binary.Marshal(buf, hostarch.ByteOrder, &ts) 44 45 cmsg, err := Parse(nil, nil, buf, 8 /* width */) 46 if err != nil { 47 t.Fatalf("Parse(_, _, %+v, _): %v", cmsg, err) 48 } 49 50 want := socket.ControlMessages{ 51 IP: socket.IPControlMessages{ 52 HasTimestamp: true, 53 Timestamp: ts.ToNsecCapped(), 54 }, 55 } 56 if diff := cmp.Diff(want, cmsg); diff != "" { 57 t.Errorf("unexpected message parsed, (-want, +got):\n%s", diff) 58 } 59 }