vpntest

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: GPL-3.0 Imports: 15 Imported by: 0

Documentation

Overview

Package vpntest provides utitities to facilitate testing different minivpn packages.

Package vpntest provides utilities for minivpn testing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertPanic

func AssertPanic(t *testing.T, f func())

Types

type Addr

type Addr struct {
	MockString  func() string
	MockNetwork func() string
}

Addr allows mocking net.Addr.

func (*Addr) Network

func (a *Addr) Network() string

Network calls MockNetwork.

func (*Addr) String

func (a *Addr) String() string

String calls MockString.

type Conn

type Conn struct {
	MockRead             func(b []byte) (int, error)
	MockWrite            func(b []byte) (int, error)
	MockClose            func() error
	MockLocalAddr        func() net.Addr
	MockRemoteAddr       func() net.Addr
	MockSetDeadline      func(t time.Time) error
	MockSetReadDeadline  func(t time.Time) error
	MockSetWriteDeadline func(t time.Time) error
}

Conn is a mockable net.Conn.

func (*Conn) Close

func (c *Conn) Close() error

Close calls MockClose.

func (*Conn) LocalAddr

func (c *Conn) LocalAddr() net.Addr

LocalAddr calls MockLocalAddr.

func (*Conn) Read

func (c *Conn) Read(b []byte) (int, error)

Read calls MockRead.

func (*Conn) RemoteAddr

func (c *Conn) RemoteAddr() net.Addr

RemoteAddr calls MockRemoteAddr.

func (*Conn) SetDeadline

func (c *Conn) SetDeadline(t time.Time) error

SetDeadline calls MockSetDeadline.

func (*Conn) SetReadDeadline

func (c *Conn) SetReadDeadline(t time.Time) error

SetReadDeadline calls MockSetReadDeadline.

func (*Conn) SetWriteDeadline

func (c *Conn) SetWriteDeadline(t time.Time) error

SetWriteDeadline calls MockSetWriteDeadline.

func (*Conn) Write

func (c *Conn) Write(b []byte) (int, error)

Write calls MockWrite.

type Dialer

type Dialer struct {
	MockDialContext func(ctx context.Context, network, address string) (net.Conn, error)
}

Dialer is a mockable Dialer.

func (*Dialer) DialContext

func (d *Dialer) DialContext(ctx context.Context, network, address string) (net.Conn, error)

DialContext calls MockDialContext.

type EchoServer

type EchoServer struct {

	// LocalSessionID is needed to produce incoming packets that pass sanity checks.
	LocalSessionID model.SessionID

	// RemoteSessionID is needed to produce ACKs.
	RemoteSessionID model.SessionID
	// contains filtered or unexported fields
}

EchoServer is a dummy server intended for testing. It will: - send sequential packets back to a client implementation, containing each the same payload and the same packet ID than incoming. - write every seen packet on the ACK array for the echo response.

func NewEchoServer

func NewEchoServer(dataIn, dataOut chan *model.Packet) *EchoServer

NewEchoServer creates an EchoServer given two channels of [model.Packet]s.

func (*EchoServer) Start

func (e *EchoServer) Start()

Start starts the EchoServer.

func (*EchoServer) Stop

func (e *EchoServer) Stop()

Stop stops the EchoServer.

type LoggedPacket

type LoggedPacket struct {
	ID     int
	Opcode model.Opcode
	ACKs   []model.PacketID
	At     time.Duration
}

LoggedPacket is a trace of a received packet.

type PacketLog

type PacketLog []*LoggedPacket

PacketLog is a sequence of LoggedPacket.

func (PacketLog) ACKs

func (l PacketLog) ACKs() []int

ACKs filters the log and returns an array of unique ids that have been acked either as ack packets or as part of the ack array of an outgoing packet.

func (PacketLog) IDSequence

func (l PacketLog) IDSequence() []int

IDSequence returns a sequence of int from the logged packets.

type PacketReader

type PacketReader struct {
	// contains filtered or unexported fields
}

PacketReader reads packets from a channel.

func NewPacketReader

func NewPacketReader(ch <-chan *model.Packet) *PacketReader

NewPacketReader creates a new PacketReader.

func (*PacketReader) Log

func (pr *PacketReader) Log() PacketLog

Log returns the log of the received packets.

func (*PacketReader) Payload

func (pr *PacketReader) Payload() string

Payload returns the string payload constructed from the payloads in the received packets.

func (*PacketReader) WaitForNumberOfACKs

func (pr *PacketReader) WaitForNumberOfACKs(total int, start time.Time)

WaitForNumberOfACKs will read from the channel until the given number of acks have been received.

func (*PacketReader) WaitForOrderedPayloadLen

func (pr *PacketReader) WaitForOrderedPayloadLen(total int, start time.Time)

WaitForOrderedPayloadLen will read from the channel until the given number of characters have been read.

func (*PacketReader) WaitForSequence

func (pr *PacketReader) WaitForSequence(seq []int, start time.Time) bool

WaitForSequence loops reading from the internal channel until the logged sequence matches the len of the expected sequence; it returns true if the obtained packet ID sequence matches the expected one.

type PacketRelay

type PacketRelay struct {
	// contains filtered or unexported fields
}

PacketRelay sends any received packet, without modifications.

func NewPacketRelay

func NewPacketRelay(dataIn <-chan *model.Packet, dataOut chan<- *model.Packet) *PacketRelay

NewPacketRelay reads packets from one channel and writes them to another.

func (*PacketRelay) RelayWithLosses

func (pr *PacketRelay) RelayWithLosses(losses []int)

RelayWithLosses will relay incoming packets according to a vector of packetID that must be dropped. To specify repeated losses for a packet ID, the vector of losses must repeat the id several times.

func (*PacketRelay) Stop

func (pr *PacketRelay) Stop()

Stop will stop the relay loop.

type PacketWriter

type PacketWriter struct {

	// LocalSessionID is needed to produce incoming packets that pass sanity checks.
	LocalSessionID model.SessionID

	// RemoteSessionID is needed to produce ACKs.
	RemoteSessionID model.SessionID
	// contains filtered or unexported fields
}

PacketWriter writes packets into a channel.

func NewPacketWriter

func NewPacketWriter(ch chan<- *model.Packet) *PacketWriter

NewPacketWriter creates a new PacketWriter.

func (*PacketWriter) WritePacketWithID

func (pw *PacketWriter) WritePacketWithID(i int)

WritePacketWithID writes a dummy control packet with the passed ID.

func (*PacketWriter) WriteSequence

func (pw *PacketWriter) WriteSequence(seq []string)

WriteSequence writes the passed packet sequence (in their string representation) to the configured channel. It will wait the specified interval between one packet and the next. The input sequence strings will be expanded for range notation, as in [1..10]

func (*PacketWriter) WriteSequenceWithFixedPayload

func (pw *PacketWriter) WriteSequenceWithFixedPayload(seq []string, payload string, size int)

WriteSequenceWithFixedPayload will write packets according to the sequence specified in seq, but will sequentially pick the payload from the passed payload string, in increments defined by size.

type TestPacket

type TestPacket struct {
	// Opcode is the OpenVPN packet opcode.
	Opcode model.Opcode

	// ID is the packet sequence
	ID int

	// ACKs is the ack array in this packet
	ACKs []int

	// IAT is the inter-arrival time until the next packet is received.
	IAT time.Duration
}

TestPacket is used to simulate incoming packets over the network. The goal is to be able to have a compact representation of a sequence of packets, their type, and extra properties like inter-arrival time.

func NewTestPacketFromString

func NewTestPacketFromString(s string) (*TestPacket, error)

NewTestPacketFromString constructs a new TestPacket. The input representation for the test packet string is in the form: "[ID] OPCODE (ack:) +42ms" where the ack array is optional.

type TestingCert

type TestingCert struct {
	Cert string
	Key  string
	CA   string
}

TestingCert holds key, cert and ca to pass to tests needing to mock certificates.

func WriteTestingCerts

func WriteTestingCerts(dir string) (TestingCert, error)

WriteTEestingCerts will write valid certificates in the passed dir, and return a TestingCert and any error.

type Witness

type Witness struct {
	// contains filtered or unexported fields
}

A Witness checks for different conditions over a reader

func NewWitness

func NewWitness(r *PacketReader) *Witness

NewWitness constructs a Witness from a PacketReader.

func NewWitnessFromChannel

func NewWitnessFromChannel(ch <-chan *model.Packet) *Witness

NewWitnessFromChannel constructs a Witness from a channel of packets.

func (*Witness) Log

func (w *Witness) Log() PacketLog

Log returns the packet log from the internal reader this witness uses.

func (*Witness) Payload

func (w *Witness) Payload() string

Payload returns the string payload reconstructed from the received packets.

func (*Witness) VerifyNumberOfACKs

func (w *Witness) VerifyNumberOfACKs(total int, start time.Time) bool

VerifyNumberOfACKs tells the underlying reader to wait for a given number of acks, returns true if we have the same number of acks.

func (*Witness) VerifyOrderedPayload

func (w *Witness) VerifyOrderedPayload(payload string, t time.Time) bool

VerifyOrderedPayload checks that the received payload matches the one we expect.

Jump to

Keyboard shortcuts

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