gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/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 "gvisor.dev/gvisor/pkg/buffer" 25 "gvisor.dev/gvisor/pkg/refs" 26 "gvisor.dev/gvisor/pkg/tcpip" 27 "gvisor.dev/gvisor/pkg/tcpip/link/fdbased" 28 "gvisor.dev/gvisor/pkg/tcpip/network/ipv4" 29 "gvisor.dev/gvisor/pkg/tcpip/stack" 30 ) 31 32 func TestInjectableEndpointRawDispatch(t *testing.T) { 33 endpoint, sock, dstIP := makeTestInjectableEndpoint(t) 34 35 v := buffer.NewViewWithData([]byte{0xFA}) 36 defer v.Release() 37 endpoint.InjectOutbound(dstIP, v) 38 39 buf := make([]byte, ipv4.MaxTotalSize) 40 bytesRead, err := sock.Read(buf) 41 if err != nil { 42 t.Fatalf("Unable to read from socketpair: %v", err) 43 } 44 if got, want := buf[:bytesRead], []byte{0xFA}; !bytes.Equal(got, want) { 45 t.Fatalf("Read %v from the socketpair, wanted %v", got, want) 46 } 47 } 48 49 func TestInjectableEndpointDispatch(t *testing.T) { 50 endpoint, sock, dstIP := makeTestInjectableEndpoint(t) 51 52 pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ 53 ReserveHeaderBytes: 1, 54 Payload: buffer.MakeWithData([]byte{0xFB}), 55 }) 56 defer pkt.DecRef() 57 pkt.TransportHeader().Push(1)[0] = 0xFA 58 pkt.EgressRoute.RemoteAddress = dstIP 59 pkt.NetworkProtocolNumber = ipv4.ProtocolNumber 60 61 var pkts stack.PacketBufferList 62 pkts.PushBack(pkt) 63 if _, err := endpoint.WritePackets(pkts); err != nil { 64 t.Fatalf("Unable to write packets: %s", err) 65 } 66 67 buf := make([]byte, 6500) 68 bytesRead, err := sock.Read(buf) 69 if err != nil { 70 t.Fatalf("Unable to read from socketpair: %v", err) 71 } 72 if got, want := buf[:bytesRead], []byte{0xFA, 0xFB}; !bytes.Equal(got, want) { 73 t.Fatalf("Read %v from the socketpair, wanted %v", got, want) 74 } 75 } 76 77 func TestInjectableEndpointDispatchHdrOnly(t *testing.T) { 78 endpoint, sock, dstIP := makeTestInjectableEndpoint(t) 79 80 pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{ 81 ReserveHeaderBytes: 1, 82 }) 83 defer pkt.DecRef() 84 pkt.TransportHeader().Push(1)[0] = 0xFA 85 pkt.EgressRoute.RemoteAddress = dstIP 86 pkt.NetworkProtocolNumber = ipv4.ProtocolNumber 87 88 var pkts stack.PacketBufferList 89 pkts.PushBack(pkt) 90 if _, err := endpoint.WritePackets(pkts); err != nil { 91 t.Fatalf("Unable to write packets: %s", err) 92 } 93 buf := make([]byte, 6500) 94 bytesRead, err := sock.Read(buf) 95 if err != nil { 96 t.Fatalf("Unable to read from socketpair: %v", err) 97 } 98 if got, want := buf[:bytesRead], []byte{0xFA}; !bytes.Equal(got, want) { 99 t.Fatalf("Read %v from the socketpair, wanted %v", got, want) 100 } 101 } 102 103 func makeTestInjectableEndpoint(t *testing.T) (*InjectableEndpoint, *os.File, tcpip.Address) { 104 dstIP := tcpip.AddrFromSlice(net.ParseIP("1.2.3.4").To4()) 105 pair, err := unix.Socketpair(unix.AF_UNIX, 106 unix.SOCK_SEQPACKET|unix.SOCK_CLOEXEC|unix.SOCK_NONBLOCK, 0) 107 if err != nil { 108 t.Fatal("Failed to create socket pair:", err) 109 } 110 underlyingEndpoint, err := fdbased.NewInjectable(pair[1], 6500, stack.CapabilityNone) 111 if err != nil { 112 t.Fatalf("fdbased.NewInjectable(%d, 6500, stack.CapabilityNone) failed: %s", pair[1], err) 113 } 114 routes := map[tcpip.Address]stack.InjectableLinkEndpoint{dstIP: underlyingEndpoint} 115 endpoint := NewInjectableEndpoint(routes) 116 return endpoint, os.NewFile(uintptr(pair[0]), "test route end"), dstIP 117 } 118 119 func TestMain(m *testing.M) { 120 refs.SetLeakMode(refs.LeaksPanic) 121 code := m.Run() 122 refs.DoLeakCheck() 123 os.Exit(code) 124 }