Files

170 lines
4.7 KiB
Go
Raw Permalink Normal View History

2022-01-28 13:41:00 -08:00
// Copyright 2022 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package packetsocket_test
import (
"math"
"os"
"testing"
"gvisor.dev/gvisor/pkg/refs"
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/header"
"gvisor.dev/gvisor/pkg/tcpip/link/packetsocket"
"gvisor.dev/gvisor/pkg/tcpip/stack"
)
var _ stack.LinkEndpoint = (*nullEndpoint)(nil)
type nullEndpoint struct {
disp stack.NetworkDispatcher
}
func (*nullEndpoint) MTU() uint32 {
return math.MaxUint32
}
2024-06-24 16:54:47 -07:00
func (*nullEndpoint) SetMTU(uint32) {}
2022-01-28 13:41:00 -08:00
func (*nullEndpoint) Capabilities() stack.LinkEndpointCapabilities {
return 0
}
func (*nullEndpoint) MaxHeaderLength() uint16 {
return 0
}
func (*nullEndpoint) LinkAddress() tcpip.LinkAddress {
return ""
}
func (*nullEndpoint) SetLinkAddress(tcpip.LinkAddress) {
2022-01-28 13:41:00 -08:00
}
func (*nullEndpoint) WritePackets(pkts stack.PacketBufferList) (int, tcpip.Error) {
return pkts.Len(), nil
}
func (e *nullEndpoint) Attach(d stack.NetworkDispatcher) { e.disp = d }
func (e *nullEndpoint) IsAttached() bool { return e.disp != nil }
func (*nullEndpoint) Wait() {}
func (*nullEndpoint) ARPHardwareType() header.ARPHardwareType { return header.ARPHardwareNone }
2024-02-29 11:07:59 -08:00
func (*nullEndpoint) AddHeader(*stack.PacketBuffer) {}
func (*nullEndpoint) ParseHeader(*stack.PacketBuffer) bool { return true }
func (*nullEndpoint) Close() {}
func (*nullEndpoint) SetOnCloseAction(func()) {}
2022-01-28 13:41:00 -08:00
var _ stack.NetworkDispatcher = (*testNetworkDispatcher)(nil)
type linkPacketInfo struct {
2024-02-29 11:07:59 -08:00
pkt *stack.PacketBuffer
2022-01-28 13:41:00 -08:00
protocol tcpip.NetworkProtocolNumber
}
type networkPacketInfo struct {
2024-02-29 11:07:59 -08:00
pkt *stack.PacketBuffer
2022-01-28 13:41:00 -08:00
protocol tcpip.NetworkProtocolNumber
}
type testNetworkDispatcher struct {
t *testing.T
linkPacket linkPacketInfo
networkPacket networkPacketInfo
}
func (t *testNetworkDispatcher) reset() {
2024-03-20 05:28:16 -07:00
if pkt := t.linkPacket.pkt; pkt != nil {
2022-01-28 13:41:00 -08:00
pkt.DecRef()
}
2024-03-20 05:28:16 -07:00
if pkt := t.networkPacket.pkt; pkt != nil {
2022-01-28 13:41:00 -08:00
pkt.DecRef()
}
*t = testNetworkDispatcher{}
}
2024-02-29 11:07:59 -08:00
func (t *testNetworkDispatcher) DeliverNetworkPacket(protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) {
2022-01-28 13:41:00 -08:00
networkPacket := networkPacketInfo{
pkt: pkt.IncRef(),
2022-01-28 13:41:00 -08:00
protocol: protocol,
}
if t.networkPacket != (networkPacketInfo{}) {
t.t.Fatalf("already delivered network packet = %#v; new = %#v", t.networkPacket, networkPacket)
}
t.networkPacket = networkPacket
}
2024-02-29 11:07:59 -08:00
func (t *testNetworkDispatcher) DeliverLinkPacket(protocol tcpip.NetworkProtocolNumber, pkt *stack.PacketBuffer) {
2022-01-28 13:41:00 -08:00
linkPacket := linkPacketInfo{
pkt: pkt.IncRef(),
2022-01-28 13:41:00 -08:00
protocol: protocol,
}
if t.linkPacket != (linkPacketInfo{}) {
t.t.Fatalf("already delivered link packet = %#v; new = %#v", t.linkPacket, linkPacket)
}
t.linkPacket = linkPacket
}
func TestPacketDispatch(t *testing.T) {
const protocol = 5
var nullEP nullEndpoint
ep := packetsocket.New(&nullEP)
var d testNetworkDispatcher
defer d.reset()
ep.Attach(&d)
pkt := stack.NewPacketBuffer(stack.PacketBufferOptions{})
defer pkt.DecRef()
pkt.NetworkProtocolNumber = protocol
{
2023-01-12 07:40:05 -08:00
pkt.PktType = tcpip.PacketOutgoing
2022-01-28 13:41:00 -08:00
var pkts stack.PacketBufferList
pkts.PushBack(pkt)
if n, err := ep.WritePackets(pkts); err != nil {
t.Fatalf("ep.WritePackets(_): %s", err)
} else if n != 1 {
t.Fatalf("got ep.WritePackets(_) = %d, want = 1", n)
}
if want := (networkPacketInfo{}); d.networkPacket != want {
t.Errorf("got d.networkPacket = %#v, want = %#v", d.networkPacket, want)
}
2023-01-12 07:40:05 -08:00
if want := (linkPacketInfo{pkt: pkt, protocol: protocol}); d.linkPacket != want {
2022-01-28 13:41:00 -08:00
t.Errorf("got d.linkPacket = %#v, want = %#v", d.linkPacket, want)
}
}
d.reset()
{
2023-01-12 07:40:05 -08:00
pkt.PktType = tcpip.PacketHost
2022-01-28 13:41:00 -08:00
nullEP.disp.DeliverNetworkPacket(protocol, pkt)
if want := (networkPacketInfo{pkt: pkt, protocol: protocol}); d.networkPacket != want {
t.Errorf("got d.networkPacket = %#v, want = %#v", d.networkPacket, want)
}
2023-01-12 07:40:05 -08:00
if want := (linkPacketInfo{pkt: pkt, protocol: protocol}); d.linkPacket != want {
2022-01-28 13:41:00 -08:00
t.Errorf("got d.linkPacket = %#v, want = %#v", d.linkPacket, want)
}
}
}
func TestMain(m *testing.M) {
refs.SetLeakMode(refs.LeaksPanic)
code := m.Run()
2022-12-01 12:43:06 -08:00
refs.DoLeakCheck()
2022-01-28 13:41:00 -08:00
os.Exit(code)
}