or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

bpf.mdcontext-ctxhttp.mdcontext.mddict.mddns-dnsmessage.mdhtml-atom.mdhtml-charset.mdhtml.mdhttp-httpguts.mdhttp-httpproxy.mdhttp2-h2c.mdhttp2-hpack.mdhttp2.mdicmp.mdidna.mdindex.mdipv4.mdipv6.mdnettest.mdnetutil.mdproxy.mdpublicsuffix.mdquic-qlog.mdquic.mdtrace.mdwebdav.mdwebsocket.mdxsrftoken.md
tile.json

nettest.mddocs/

Network Testing Utilities

Package nettest provides utilities for network testing.

Import

import "golang.org/x/net/nettest"

Types

// MakePipe creates a connection between two endpoints
type MakePipe func() (c1, c2 net.Conn, stop func(), err error)

MakePipe returns a pair of connections where anything written to c1 is read by c2 and vice-versa. The stop function closes all resources.

Functions

// LocalPath returns a local path for Unix-domain protocol testing
func LocalPath() (string, error)

// LoopbackInterface returns an available logical network interface for loopback test
func LoopbackInterface() (*net.Interface, error)

// MulticastSource returns a unicast IP address on ifi for multicast testing
func MulticastSource(network string, ifi *net.Interface) (net.IP, error)

// NewLocalListener returns a listener on a loopback IP address or local file system path
func NewLocalListener(network string) (net.Listener, error)

// NewLocalPacketListener returns a packet listener on loopback IP or local file system path
func NewLocalPacketListener(network string) (net.PacketConn, error)

// RoutedInterface returns a network interface that can route IP traffic
func RoutedInterface(network string, flags net.Flags) (*net.Interface, error)

// SupportsIPv4 reports whether the platform supports IPv4 networking
func SupportsIPv4() bool

// SupportsIPv6 reports whether the platform supports IPv6 networking
func SupportsIPv6() bool

// SupportsRawSocket reports whether raw sockets are available in the current session
func SupportsRawSocket() bool

// TestConn tests that a net.Conn implementation properly satisfies the interface
func TestConn(t *testing.T, mp MakePipe)

// TestableAddress reports whether address is testable on the current platform
func TestableAddress(network, address string) bool

// TestableNetwork reports whether network is testable on the current platform
func TestableNetwork(network string) bool

Usage Examples

Checking Platform Capabilities

import (
    "golang.org/x/net/nettest"
    "testing"
)

func TestNetworkFeatures(t *testing.T) {
    if !nettest.SupportsIPv4() {
        t.Skip("IPv4 not supported")
    }

    if !nettest.SupportsIPv6() {
        t.Skip("IPv6 not supported")
    }

    if !nettest.SupportsRawSocket() {
        t.Skip("Raw sockets not available")
    }

    // Run tests...
}

Creating Test Listeners

func TestServer(t *testing.T) {
    // Create a local TCP listener for testing
    ln, err := nettest.NewLocalListener("tcp")
    if err != nil {
        t.Fatal(err)
    }
    defer ln.Close()

    // Use listener for testing
    go func() {
        conn, err := ln.Accept()
        if err != nil {
            return
        }
        defer conn.Close()
        // Handle connection...
    }()

    // Connect to listener
    conn, err := net.Dial("tcp", ln.Addr().String())
    if err != nil {
        t.Fatal(err)
    }
    defer conn.Close()

    // Test communication...
}

Testing UDP Services

func TestUDPService(t *testing.T) {
    // Create local UDP packet listener
    pc, err := nettest.NewLocalPacketListener("udp")
    if err != nil {
        t.Fatal(err)
    }
    defer pc.Close()

    // Send and receive packets
    addr := pc.LocalAddr()

    conn, err := net.Dial("udp", addr.String())
    if err != nil {
        t.Fatal(err)
    }
    defer conn.Close()

    // Test packet exchange...
}

Testing Custom net.Conn Implementation

func TestCustomConn(t *testing.T) {
    nettest.TestConn(t, func() (c1, c2 net.Conn, stop func(), err error) {
        // Create a pair of connected custom connections
        server, client := net.Pipe()

        stop = func() {
            server.Close()
            client.Close()
        }

        return server, client, stop, nil
    })
}

Platform-Specific Tests

func TestIPv6Multicast(t *testing.T) {
    if !nettest.SupportsIPv6() {
        t.Skip("IPv6 not supported")
    }

    if !nettest.TestableNetwork("ip6") {
        t.Skip("IPv6 not testable")
    }

    ifi, err := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagMulticast)
    if err != nil {
        t.Fatal(err)
    }

    mcastAddr, err := nettest.MulticastSource("ip6", ifi)
    if err != nil {
        t.Fatal(err)
    }

    t.Logf("Multicast source: %v", mcastAddr)

    // Test multicast functionality...
}

Unix Domain Socket Testing

func TestUnixSocket(t *testing.T) {
    if !nettest.TestableNetwork("unix") {
        t.Skip("Unix domain sockets not testable")
    }

    path, err := nettest.LocalPath()
    if err != nil {
        t.Fatal(err)
    }

    ln, err := net.Listen("unix", path)
    if err != nil {
        t.Fatal(err)
    }
    defer func() {
        ln.Close()
        os.Remove(path)
    }()

    // Test Unix socket...
}

Interface Discovery

func TestLoopbackInterface(t *testing.T) {
    ifi, err := nettest.LoopbackInterface()
    if err != nil {
        t.Fatal(err)
    }

    t.Logf("Loopback interface: %s", ifi.Name)
    t.Logf("MTU: %d", ifi.MTU)
    t.Logf("Hardware address: %s", ifi.HardwareAddr)
}

func TestRoutedInterface(t *testing.T) {
    ifi, err := nettest.RoutedInterface("ip4", net.FlagUp)
    if err != nil {
        t.Fatal(err)
    }

    t.Logf("Routed interface: %s", ifi.Name)

    addrs, err := ifi.Addrs()
    if err != nil {
        t.Fatal(err)
    }

    for _, addr := range addrs {
        t.Logf("Address: %s", addr)
    }
}