github.com/google/netstack@v0.0.0-20191123085552-55fcc16cd0eb/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  	"syscall"
    22  	"testing"
    23  
    24  	"github.com/google/netstack/tcpip"
    25  	"github.com/google/netstack/tcpip/buffer"
    26  	"github.com/google/netstack/tcpip/link/fdbased"
    27  	"github.com/google/netstack/tcpip/network/ipv4"
    28  	"github.com/google/netstack/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  	hdr := buffer.NewPrependable(1)
    50  	hdr.Prepend(1)[0] = 0xFA
    51  	packetRoute := stack.Route{RemoteAddress: dstIP}
    52  
    53  	endpoint.WritePacket(&packetRoute, nil /* gso */, ipv4.ProtocolNumber, tcpip.PacketBuffer{
    54  		Header: hdr,
    55  		Data:   buffer.NewViewFromBytes([]byte{0xFB}).ToVectorisedView(),
    56  	})
    57  
    58  	buf := make([]byte, 6500)
    59  	bytesRead, err := sock.Read(buf)
    60  	if err != nil {
    61  		t.Fatalf("Unable to read from socketpair: %v", err)
    62  	}
    63  	if got, want := buf[:bytesRead], []byte{0xFA, 0xFB}; !bytes.Equal(got, want) {
    64  		t.Fatalf("Read %v from the socketpair, wanted %v", got, want)
    65  	}
    66  }
    67  
    68  func TestInjectableEndpointDispatchHdrOnly(t *testing.T) {
    69  	endpoint, sock, dstIP := makeTestInjectableEndpoint(t)
    70  	hdr := buffer.NewPrependable(1)
    71  	hdr.Prepend(1)[0] = 0xFA
    72  	packetRoute := stack.Route{RemoteAddress: dstIP}
    73  	endpoint.WritePacket(&packetRoute, nil /* gso */, ipv4.ProtocolNumber, tcpip.PacketBuffer{
    74  		Header: hdr,
    75  		Data:   buffer.NewView(0).ToVectorisedView(),
    76  	})
    77  	buf := make([]byte, 6500)
    78  	bytesRead, err := sock.Read(buf)
    79  	if err != nil {
    80  		t.Fatalf("Unable to read from socketpair: %v", err)
    81  	}
    82  	if got, want := buf[:bytesRead], []byte{0xFA}; !bytes.Equal(got, want) {
    83  		t.Fatalf("Read %v from the socketpair, wanted %v", got, want)
    84  	}
    85  }
    86  
    87  func makeTestInjectableEndpoint(t *testing.T) (*InjectableEndpoint, *os.File, tcpip.Address) {
    88  	dstIP := tcpip.Address(net.ParseIP("1.2.3.4").To4())
    89  	pair, err := syscall.Socketpair(syscall.AF_UNIX,
    90  		syscall.SOCK_SEQPACKET|syscall.SOCK_CLOEXEC|syscall.SOCK_NONBLOCK, 0)
    91  	if err != nil {
    92  		t.Fatal("Failed to create socket pair:", err)
    93  	}
    94  	underlyingEndpoint := fdbased.NewInjectable(pair[1], 6500, stack.CapabilityNone)
    95  	routes := map[tcpip.Address]stack.InjectableLinkEndpoint{dstIP: underlyingEndpoint}
    96  	endpoint := NewInjectableEndpoint(routes)
    97  	return endpoint, os.NewFile(uintptr(pair[0]), "test route end"), dstIP
    98  }