github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/tcpip/link/muxed/injectable_test.go (about) 1 // Copyright 2019 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 muxed 16 17 import ( 18 "bytes" 19 "net" 20 "os" 21 "testing" 22 23 "golang.org/x/sys/unix" 24 "github.com/SagerNet/gvisor/pkg/tcpip" 25 "github.com/SagerNet/gvisor/pkg/tcpip/buffer" 26 "github.com/SagerNet/gvisor/pkg/tcpip/link/fdbased" 27 "github.com/SagerNet/gvisor/pkg/tcpip/network/ipv4" 28 "github.com/SagerNet/gvisor/pkg/tcpip/stack" 29 ) 30 31 func TestInjectableEndpointRawDispatch(t *testing.T) { 32 endpoint, sock, dstIP := makeTestInjectableEndpoint(t) 33 34 endpoint.InjectOutbound(dstIP, []byte{0xFA}) 35 36 buf := make([]byte, ipv4.MaxTotalSize) 37 bytesRead, err := sock.Read(buf) 38 if err != nil { 39 t.Fatalf("Unable to read from socketpair: %v", err) 40 } 41 if got, want := buf[:bytesRead], []byte{0xFA}; !bytes.Equal(got, want) { 42 t.Fatalf("Read %v from the socketpair, wanted %v", got, want) 43 } 44 } 45 46 func TestInjectableEndpointDispatch(t *testing.T) { 47 endpoint, sock, dstIP := makeTestInjectableEndpoint(t) 48 49 pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ 50 ReserveHeaderBytes: 1, 51 Data: buffer.NewViewFromBytes([]byte{0xFB}).ToVectorisedView(), 52 }) 53 pkt.TransportHeader().Push(1)[0] = 0xFA 54 var packetRoute stack.RouteInfo 55 packetRoute.RemoteAddress = dstIP 56 57 endpoint.WritePacket(packetRoute, ipv4.ProtocolNumber, pkt) 58 59 buf := make([]byte, 6500) 60 bytesRead, err := sock.Read(buf) 61 if err != nil { 62 t.Fatalf("Unable to read from socketpair: %v", err) 63 } 64 if got, want := buf[:bytesRead], []byte{0xFA, 0xFB}; !bytes.Equal(got, want) { 65 t.Fatalf("Read %v from the socketpair, wanted %v", got, want) 66 } 67 } 68 69 func TestInjectableEndpointDispatchHdrOnly(t *testing.T) { 70 endpoint, sock, dstIP := makeTestInjectableEndpoint(t) 71 72 pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ 73 ReserveHeaderBytes: 1, 74 Data: buffer.NewView(0).ToVectorisedView(), 75 }) 76 pkt.TransportHeader().Push(1)[0] = 0xFA 77 var packetRoute stack.RouteInfo 78 packetRoute.RemoteAddress = dstIP 79 endpoint.WritePacket(packetRoute, ipv4.ProtocolNumber, pkt) 80 buf := make([]byte, 6500) 81 bytesRead, err := sock.Read(buf) 82 if err != nil { 83 t.Fatalf("Unable to read from socketpair: %v", err) 84 } 85 if got, want := buf[:bytesRead], []byte{0xFA}; !bytes.Equal(got, want) { 86 t.Fatalf("Read %v from the socketpair, wanted %v", got, want) 87 } 88 } 89 90 func makeTestInjectableEndpoint(t *testing.T) (*InjectableEndpoint, *os.File, tcpip.Address) { 91 dstIP := tcpip.Address(net.ParseIP("1.2.3.4").To4()) 92 pair, err := unix.Socketpair(unix.AF_UNIX, 93 unix.SOCK_SEQPACKET|unix.SOCK_CLOEXEC|unix.SOCK_NONBLOCK, 0) 94 if err != nil { 95 t.Fatal("Failed to create socket pair:", err) 96 } 97 underlyingEndpoint := fdbased.NewInjectable(pair[1], 6500, stack.CapabilityNone) 98 routes := map[tcpip.Address]stack.InjectableLinkEndpoint{dstIP: underlyingEndpoint} 99 endpoint := NewInjectableEndpoint(routes) 100 return endpoint, os.NewFile(uintptr(pair[0]), "test route end"), dstIP 101 }