context

package
v0.0.0-...-973e103 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2024 License: Apache-2.0, MIT Imports: 21 Imported by: 0

Documentation

Overview

Package context provides a context used by datagram-based network endpoints tests. It also defines the TestFlow type to facilitate IP configurations.

Index

Constants

View Source
const (
	// NICID is the id of the nic created by the Context.
	NICID = 1

	// DefaultMTU is the MTU used by the Context, except where another value is
	// explicitly specified during initialization. It is chosen to match the MTU
	// of loopback interfaces on linux systems.
	DefaultMTU = 65536
)
View Source
const (

	// StackPort is the port TestFlow uses with StackAddr.
	StackPort = 1234

	// TestPort is the port TestFlow uses with TestAddr.
	TestPort = 4096
)

Variables

View Source
var (
	// BroadcastAddr is the IPv4 broadcast address.
	BroadcastAddr = header.IPv4Broadcast

	// StackAddr is the IPv4 address assigned to the stack's NIC and is used by
	// TestFlow as the local address.
	StackAddr = tcpip.AddrFromSlice([]byte("\x0a\x00\x00\x01"))

	// StackV4MappedAddr is the IPv4-mapped IPv6 StackAddr.
	StackV4MappedAddr = tcpip.AddrFromSlice(append([]byte(v4MappedAddrPrefix), StackAddr.AsSlice()...))

	// TestAddr is the IPv4 address used by TestFlow as the remote address.
	TestAddr = tcpip.AddrFromSlice([]byte("\x0a\x00\x00\x02"))

	// TestV4MappedAddr is the IPv4-mapped IPv6 TestAddr.
	TestV4MappedAddr = tcpip.AddrFromSlice(append([]byte(v4MappedAddrPrefix), TestAddr.AsSlice()...))

	// MulticastAddr is the IPv4 multicast address used by IPv4 multicast
	// TestFlow.
	MulticastAddr = tcpip.AddrFromSlice([]byte("\xe8\x2b\xd3\xea"))

	// MulticastV4MappedAddr is the IPv4-mapped IPv6 MulticastAddr.
	MulticastV4MappedAddr = tcpip.AddrFromSlice(append([]byte(v4MappedAddrPrefix), MulticastAddr.AsSlice()...))

	// BroadcastV4MappedAddr is the IPv4-mapped IPv6 BroadcastAddr.
	BroadcastV4MappedAddr = tcpip.AddrFromSlice(append([]byte(v4MappedAddrPrefix), BroadcastAddr.AsSlice()...))

	// V4MappedWildcardAddr is the IPv4-mapped IPv6 wildcard (any) address.
	V4MappedWildcardAddr = tcpip.AddrFromSlice([]byte(v4MappedAddrPrefix + "\x00\x00\x00\x00"))

	// StackV6Addr is the IPv6 address assigned to the stack's NIC and is used by
	// TestFlow as the local address.
	StackV6Addr = tcpip.AddrFromSlice([]byte("\x0a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01"))

	// TestV6Addr is the IPv6 address used by TestFlow as the remote address.
	TestV6Addr = tcpip.AddrFromSlice([]byte("\x0a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02"))

	// MulticastV6Addr is the IPv6 multicast address used by IPv6 multicast
	// TestFlow.
	MulticastV6Addr = tcpip.AddrFromSlice([]byte("\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"))
)

Functions

func BuildUDPPacket

func BuildUDPPacket(payload []byte, flow TestFlow, direction PacketDirection, tosOrTclass, ttlOrHopLimit uint8, badChecksum bool) []byte

BuildUDPPacket builds an IPv4 or IPv6 UDP packet, depending on the specified TestFlow.

func BuildV4UDPPacket

func BuildV4UDPPacket(payload []byte, h Header4Tuple, tos, ttl uint8, badChecksum bool) []byte

BuildV4UDPPacket builds an IPv4 UDP packet.

func BuildV6UDPPacket

func BuildV6UDPPacket(payload []byte, h Header4Tuple, tclass, hoplimit uint8, badChecksum bool) []byte

BuildV6UDPPacket builds an IPv6 UDP packet.

Types

type Context

type Context struct {
	// T is the testing context.
	T *testing.T

	// LinkEP is the link endpoint that is attached to the stack's NIC.
	LinkEP *channel.Endpoint

	// Stack is the networking stack owned by the context.
	Stack *stack.Stack

	// EP is the transport endpoint owned by the context.
	EP tcpip.Endpoint

	// WQ is the wait queue associated with EP and is used to block for events on
	// EP.
	WQ waiter.Queue
}

Context is a testing context for datagram-based network endpoints.

func New

func New(t *testing.T, transportProtocols []stack.TransportProtocolFactory) *Context

New allocates and initializes a test context containing a configured stack.

func NewWithOptions

func NewWithOptions(t *testing.T, transportProtocols []stack.TransportProtocolFactory, options Options) *Context

NewWithOptions allocates and initializes a test context containing a configured stack with the provided options.

func (*Context) CheckEndpointReadStats

func (c *Context) CheckEndpointReadStats(incr uint64, want *tcpip.TransportEndpointStats, err tcpip.Error)

CheckEndpointReadStats checks that the read statistic related to the given error has been incremented as expected.

func (*Context) CheckEndpointWriteStats

func (c *Context) CheckEndpointWriteStats(incr uint64, want *tcpip.TransportEndpointStats, err tcpip.Error)

CheckEndpointWriteStats checks that the write statistic related to the given error has been incremented as expected.

func (*Context) Cleanup

func (c *Context) Cleanup()

Cleanup closes the context endpoint if required.

func (*Context) CreateEndpoint

func (c *Context) CreateEndpoint(network tcpip.NetworkProtocolNumber, transport tcpip.TransportProtocolNumber)

CreateEndpoint creates the Context's Endpoint.

func (*Context) CreateEndpointForFlow

func (c *Context) CreateEndpointForFlow(flow TestFlow, transport tcpip.TransportProtocolNumber)

CreateEndpointForFlow creates the Context's Endpoint and configured it according to the given TestFlow.

func (*Context) CreateRawEndpoint

func (c *Context) CreateRawEndpoint(network tcpip.NetworkProtocolNumber, transport tcpip.TransportProtocolNumber)

CreateRawEndpoint creates the Context's Endpoint.

func (*Context) CreateRawEndpointForFlow

func (c *Context) CreateRawEndpointForFlow(flow TestFlow, transport tcpip.TransportProtocolNumber)

CreateRawEndpointForFlow creates the Context's Endpoint and configured it according to the given TestFlow.

func (*Context) InjectPacket

func (c *Context) InjectPacket(netProto tcpip.NetworkProtocolNumber, buf []byte)

InjectPacket injects a packet into the context's link endpoint.

func (*Context) ReadFromEndpointExpectError

func (c *Context) ReadFromEndpointExpectError()

ReadFromEndpointExpectError reads from the endpoint and checks that an error was returned.

func (*Context) ReadFromEndpointExpectNoPacket

func (c *Context) ReadFromEndpointExpectNoPacket()

ReadFromEndpointExpectNoPacket reads from the endpoint and checks that no packets was received.

func (*Context) ReadFromEndpointExpectSuccess

func (c *Context) ReadFromEndpointExpectSuccess(payload []byte, flow TestFlow, checkers ...checker.ControlMessagesChecker)

ReadFromEndpointExpectSuccess attempts to reads from the endpoint and performs checks on the received packet, according to the given flow and checkers.

type Header4Tuple

type Header4Tuple struct {
	Src tcpip.FullAddress
	Dst tcpip.FullAddress
}

Header4Tuple stores the 4-tuple {src-IP, src-port, dst-IP, dst-port} used in a packet header. These values are used to populate a header or verify one. Note that because they are used in packet headers, the addresses are never in a V4-mapped format.

type Options

type Options struct {
	// MTU is the mtu that the link endpoint will be initialized with.
	MTU uint32

	// HandleLocal specifies if non-loopback interfaces are allowed to loop
	// packets.
	HandleLocal bool
}

Options contains options for creating a new test context.

type PacketDirection

type PacketDirection int

PacketDirection specifies the direction of a TestFlow.

const (

	// Incoming indicates the direction from Test*Addr to Stack*Addr.
	Incoming PacketDirection

	// Outgoing indicates the direction from Test*Addr to Stack*Addr.
	Outgoing
)

type TestFlow

type TestFlow int

TestFlow implements a helper type used for sending and receiving test packets. A given test TestFlow value defines 1) the socket endpoint used for the test and 2) the type of packet send or received on the endpoint. E.g., a MulticastV6Only TestFlow is a IPv6 multicast packet passing through a V6-only endpoint. The type provides helper methods to characterize the TestFlow (e.g., IsV4) as well as return a proper Header4Tuple for it.

const (

	// UnicastV4 is IPv4 unicast on an IPv4 socket
	UnicastV4 TestFlow

	// UnicastV4in6 is IPv4-mapped IPv6 unicast on an IPv6 dual socket
	UnicastV4in6

	// UnicastV6 is IPv6 unicast on an IPv6 socket
	UnicastV6

	// UnicastV6Only is IPv6 unicast on an IPv6-only socket
	UnicastV6Only

	// MulticastV4 is IPv4 multicast on an IPv4 socket
	MulticastV4

	// MulticastV4in6 is IPv4-mapped IPv6 multicast on an IPv6 dual socket
	MulticastV4in6

	// MulticastV6 is IPv6 multicast on an IPv6 socket
	MulticastV6

	// MulticastV6Only IPv6 multicast on an IPv6-only socket
	MulticastV6Only

	// Broadcast is IPv4 broadcast on an IPv4 socket
	Broadcast

	// BroadcastIn6 is IPv4-mapped IPv6 broadcast on an IPv6 dual socket
	BroadcastIn6

	// ReverseMulticastV4 is IPv4 multicast src. Must fail.
	ReverseMulticastV4

	// ReverseMulticastV6 is IPv6 multicast src. Must fail.
	ReverseMulticastV6
)

func (TestFlow) CheckerFn

func (flow TestFlow) CheckerFn() func(*testing.T, *buffer.View, ...checker.NetworkChecker)

CheckerFn returns the correct network checker for the current TestFlow.

func (TestFlow) GetMulticastAddr

func (flow TestFlow) GetMulticastAddr() tcpip.Address

GetMulticastAddr returns the multicast address of a TestFlow.

func (TestFlow) IsMulticast

func (flow TestFlow) IsMulticast() bool

IsMulticast returns true if the TestFlow is multicast.

func (TestFlow) IsV4

func (flow TestFlow) IsV4() bool

IsV4 returns true for IPv4 TestFlow's.

func (TestFlow) IsV6

func (flow TestFlow) IsV6() bool

IsV6 returns true for IPv6 TestFlow's.

func (TestFlow) MakeHeader4Tuple

func (flow TestFlow) MakeHeader4Tuple(direction PacketDirection) Header4Tuple

MakeHeader4Tuple returns the Header4Tuple for the given TestFlow and direction. Note that the tuple contains no mapped addresses as those only exist at the socket level but not at the packet header level.

func (TestFlow) MapAddrIfApplicable

func (flow TestFlow) MapAddrIfApplicable(v4Addr tcpip.Address) tcpip.Address

MapAddrIfApplicable converts the given IPv4 address into its V4-mapped version if it is applicable to the TestFlow.

func (TestFlow) NetProto

func (flow TestFlow) NetProto() tcpip.NetworkProtocolNumber

NetProto returns the network protocol of a TestFlow.

func (TestFlow) SockProto

func (flow TestFlow) SockProto() tcpip.NetworkProtocolNumber

SockProto returns the network protocol number a socket must be configured with to support a given TestFlow.

func (TestFlow) String

func (flow TestFlow) String() string

String implements fmt.Stringer interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL