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