nettest

package
v0.0.0-...-0bbbf19 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dial

func Dial(t testing.TB, network, address string) net.Conn

Dial connects to the address on the named network.

Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only), "udp", "udp4" (IPv4-only), "udp6" (IPv6-only), "ip", "ip4" (IPv4-only), "ip6" (IPv6-only), "unix", "unixgram" and "unixpacket".

For TCP and UDP networks, the address has the form "host:port". The host must be a literal IP address, or a host name that can be resolved to IP addresses. The port must be a literal port number or a service name. If the host is a literal IPv6 address it must be enclosed in square brackets, as in "[2001:db8::1]:80" or "[fe80::1%zone]:80". The zone specifies the scope of the literal IPv6 address as defined in RFC 4007. The functions [JoinHostPort] and SplitHostPort manipulate a pair of host and port in this form. When using TCP, and the host resolves to multiple IP addresses, Dial will try each IP address in order until one succeeds.

Examples:

Dial("tcp", "golang.org:http")
Dial("tcp", "192.0.2.1:http")
Dial("tcp", "198.51.100.1:80")
Dial("udp", "[2001:db8::1]:domain")
Dial("udp", "[fe80::1%lo0]:53")
Dial("tcp", ":80")

For IP networks, the network must be "ip", "ip4" or "ip6" followed by a colon and a literal protocol number or a protocol name, and the address has the form "host". The host must be a literal IP address or a literal IPv6 address with zone. It depends on each operating system how the operating system behaves with a non-well known protocol number such as "0" or "255".

Examples:

Dial("ip4:1", "192.0.2.1")
Dial("ip6:ipv6-icmp", "2001:db8::1")
Dial("ip6:58", "fe80::1%lo0")

For TCP, UDP and IP networks, if the host is empty or a literal unspecified IP address, as in ":80", "0.0.0.0:80" or "[::]:80" for TCP and UDP, "", "0.0.0.0" or "::" for IP, the local system is assumed.

For Unix networks, the address must be a file system path.

func DialIP

func DialIP(t testing.TB, network string, laddr, raddr *net.IPAddr) *net.IPConn

DialIP acts like Dial for IP networks.

The network must be an IP network name; see func Dial for details.

If laddr is nil, a local address is automatically chosen. If the IP field of raddr is nil or an unspecified IP address, the local system is assumed.

func DialTCP

func DialTCP(t testing.TB, network string, laddr, raddr *net.TCPAddr) *net.TCPConn

DialTCP acts like Dial for TCP networks.

The network must be a TCP network name; see func Dial for details.

If laddr is nil, a local address is automatically chosen. If the IP field of raddr is nil or an unspecified IP address, the local system is assumed.

func DialTimeout

func DialTimeout(t testing.TB, network, address string, timeout time.Duration) net.Conn

DialTimeout acts like Dial but takes a timeout.

The timeout includes name resolution, if required. When using TCP, and the host in the address parameter resolves to multiple IP addresses, the timeout is spread over each consecutive dial, such that each is given an appropriate fraction of the time to connect.

See func Dial for a description of the network and address parameters.

func DialUDP

func DialUDP(t testing.TB, network string, laddr, raddr *net.UDPAddr) *net.UDPConn

DialUDP acts like Dial for UDP networks.

The network must be a UDP network name; see func Dial for details.

If laddr is nil, a local address is automatically chosen. If the IP field of raddr is nil or an unspecified IP address, the local system is assumed.

func DialUnix

func DialUnix(t testing.TB, network string, laddr, raddr *net.UnixAddr) *net.UnixConn

DialUnix acts like Dial for Unix networks.

The network must be a Unix network name; see func Dial for details.

If laddr is non-nil, it is used as the local address for the connection.

func FileConn

func FileConn(t testing.TB, f *os.File) (c net.Conn)

FileConn returns a copy of the network connection corresponding to the open file f. It is the caller's responsibility to close f when finished. Closing c does not affect f, and closing f does not affect c.

func FileListener

func FileListener(t testing.TB, f *os.File) (ln net.Listener)

FileListener returns a copy of the network listener corresponding to the open file f. It is the caller's responsibility to close ln when finished. Closing ln does not affect f, and closing f does not affect ln.

func FilePacketConn

func FilePacketConn(t testing.TB, f *os.File) (c net.PacketConn)

FilePacketConn returns a copy of the packet network connection corresponding to the open file f. It is the caller's responsibility to close f when finished. Closing c does not affect f, and closing f does not affect c.

func InterfaceAddrs

func InterfaceAddrs(t testing.TB) []net.Addr

InterfaceAddrs returns a list of the system's unicast interface addresses.

The returned list does not identify the associated interface; use Interfaces and [Interface.Addrs] for more detail.

func InterfaceByIndex

func InterfaceByIndex(t testing.TB, index int) *net.Interface

InterfaceByIndex returns the interface specified by index.

On Solaris, it returns one of the logical network interfaces sharing the logical data link; for more precision use InterfaceByName.

func InterfaceByName

func InterfaceByName(t testing.TB, name string) *net.Interface

InterfaceByName returns the interface specified by name.

func Interfaces

func Interfaces(t testing.TB) []net.Interface

Interfaces returns a list of the system's network interfaces.

func Listen

func Listen(t testing.TB, network, address string) net.Listener

Listen announces on the local network address.

The network must be "tcp", "tcp4", "tcp6", "unix" or "unixpacket".

For TCP networks, if the host in the address parameter is empty or a literal unspecified IP address, Listen listens on all available unicast and anycast IP addresses of the local system. To only use IPv4, use network "tcp4". The address can use a host name, but this is not recommended, because it will create a listener for at most one of the host's IP addresses. If the port in the address parameter is empty or "0", as in "127.0.0.1:" or "[::1]:0", a port number is automatically chosen. The [Addr] method of [Listener] can be used to discover the chosen port.

See func Dial for a description of the network and address parameters.

Listen uses context.Background internally; to specify the context, use [ListenConfig.Listen].

func ListenIP

func ListenIP(t testing.TB, network string, laddr *net.IPAddr) *net.IPConn

ListenIP acts like ListenPacket for IP networks.

The network must be an IP network name; see func Dial for details.

If the IP field of laddr is nil or an unspecified IP address, ListenIP listens on all available IP addresses of the local system except multicast IP addresses.

func ListenMulticastUDP

func ListenMulticastUDP(t testing.TB, network string, ifi *net.Interface, gaddr *net.UDPAddr) *net.UDPConn

ListenMulticastUDP acts like ListenPacket for UDP networks but takes a group address on a specific network interface.

The network must be a UDP network name; see func Dial for details.

ListenMulticastUDP listens on all available IP addresses of the local system including the group, multicast IP address. If ifi is nil, ListenMulticastUDP uses the system-assigned multicast interface, although this is not recommended because the assignment depends on platforms and sometimes it might require routing configuration. If the Port field of gaddr is 0, a port number is automatically chosen.

ListenMulticastUDP is just for convenience of simple, small applications. There are golang.org/x/net/ipv4 and golang.org/x/net/ipv6 packages for general purpose uses.

Note that ListenMulticastUDP will set the IP_MULTICAST_LOOP socket option to 0 under IPPROTO_IP, to disable loopback of multicast packets.

func ListenPacket

func ListenPacket(t testing.TB, network, address string) net.PacketConn

ListenPacket announces on the local network address.

The network must be "udp", "udp4", "udp6", "unixgram", or an IP transport. The IP transports are "ip", "ip4", or "ip6" followed by a colon and a literal protocol number or a protocol name, as in "ip:1" or "ip:icmp".

For UDP and IP networks, if the host in the address parameter is empty or a literal unspecified IP address, ListenPacket listens on all available IP addresses of the local system except multicast IP addresses. To only use IPv4, use network "udp4" or "ip4:proto". The address can use a host name, but this is not recommended, because it will create a listener for at most one of the host's IP addresses. If the port in the address parameter is empty or "0", as in "127.0.0.1:" or "[::1]:0", a port number is automatically chosen. The LocalAddr method of [PacketConn] can be used to discover the chosen port.

See func Dial for a description of the network and address parameters.

ListenPacket uses context.Background internally; to specify the context, use [ListenConfig.ListenPacket].

func ListenTCP

func ListenTCP(t testing.TB, network string, laddr *net.TCPAddr) *net.TCPListener

ListenTCP acts like Listen for TCP networks.

The network must be a TCP network name; see func Dial for details.

If the IP field of laddr is nil or an unspecified IP address, ListenTCP listens on all available unicast and anycast IP addresses of the local system. If the Port field of laddr is 0, a port number is automatically chosen.

func ListenUDP

func ListenUDP(t testing.TB, network string, laddr *net.UDPAddr) *net.UDPConn

ListenUDP acts like ListenPacket for UDP networks.

The network must be a UDP network name; see func Dial for details.

If the IP field of laddr is nil or an unspecified IP address, ListenUDP listens on all available IP addresses of the local system except multicast IP addresses. If the Port field of laddr is 0, a port number is automatically chosen.

func ListenUnix

func ListenUnix(t testing.TB, network string, laddr *net.UnixAddr) *net.UnixListener

ListenUnix acts like Listen for Unix networks.

The network must be "unix" or "unixpacket".

func ListenUnixgram

func ListenUnixgram(t testing.TB, network string, laddr *net.UnixAddr) *net.UnixConn

ListenUnixgram acts like ListenPacket for Unix networks.

The network must be "unixgram".

func LookupAddr

func LookupAddr(t testing.TB, addr string) (names []string)

LookupAddr performs a reverse lookup for the given address, returning a list of names mapping to that address.

The returned names are validated to be properly formatted presentation-format domain names. If the response contains invalid names, those records are filtered out and an error will be returned alongside the remaining results, if any.

When using the host C library resolver, at most one result will be returned. To bypass the host resolver, use a custom [Resolver].

LookupAddr uses context.Background internally; to specify the context, use [Resolver.LookupAddr].

func LookupCNAME

func LookupCNAME(t testing.TB, host string) (cname string)

LookupCNAME returns the canonical name for the given host. Callers that do not care about the canonical name can call LookupHost or LookupIP directly; both take care of resolving the canonical name as part of the lookup.

A canonical name is the final name after following zero or more CNAME records. LookupCNAME does not return an error if host does not contain DNS "CNAME" records, as long as host resolves to address records.

The returned canonical name is validated to be a properly formatted presentation-format domain name.

LookupCNAME uses context.Background internally; to specify the context, use [Resolver.LookupCNAME].

func LookupHost

func LookupHost(t testing.TB, host string) (addrs []string)

LookupHost looks up the given host using the local resolver. It returns a slice of that host's addresses.

LookupHost uses context.Background internally; to specify the context, use [Resolver.LookupHost].

func LookupIP

func LookupIP(t testing.TB, host string) []net.IP

LookupIP looks up host using the local resolver. It returns a slice of that host's IPv4 and IPv6 addresses.

func LookupMX

func LookupMX(t testing.TB, name string) []*net.MX

LookupMX returns the DNS MX records for the given domain name sorted by preference.

The returned mail server names are validated to be properly formatted presentation-format domain names. If the response contains invalid names, those records are filtered out and an error will be returned alongside the remaining results, if any.

LookupMX uses context.Background internally; to specify the context, use [Resolver.LookupMX].

func LookupNS

func LookupNS(t testing.TB, name string) []*net.NS

LookupNS returns the DNS NS records for the given domain name.

The returned name server names are validated to be properly formatted presentation-format domain names. If the response contains invalid names, those records are filtered out and an error will be returned alongside the remaining results, if any.

LookupNS uses context.Background internally; to specify the context, use [Resolver.LookupNS].

func LookupPort

func LookupPort(t testing.TB, network, service string) (port int)

LookupPort looks up the port for the given network and service.

LookupPort uses context.Background internally; to specify the context, use [Resolver.LookupPort].

func LookupSRV

func LookupSRV(t testing.TB, service, proto, name string) (cname string, addrs []*net.SRV)

LookupSRV tries to resolve an [SRV] query of the given service, protocol, and domain name. The proto is "tcp" or "udp". The returned records are sorted by priority and randomized by weight within a priority.

LookupSRV constructs the DNS name to look up following RFC 2782. That is, it looks up _service._proto.name. To accommodate services publishing SRV records under non-standard names, if both service and proto are empty strings, LookupSRV looks up name directly.

The returned service names are validated to be properly formatted presentation-format domain names. If the response contains invalid names, those records are filtered out and an error will be returned alongside the remaining results, if any.

func LookupTXT

func LookupTXT(t testing.TB, name string) []string

LookupTXT returns the DNS TXT records for the given domain name.

LookupTXT uses context.Background internally; to specify the context, use [Resolver.LookupTXT].

func ParseCIDR

func ParseCIDR(t testing.TB, s string) (net.IP, *net.IPNet)

ParseCIDR parses s as a CIDR notation IP address and prefix length, like "192.0.2.0/24" or "2001:db8::/32", as defined in RFC 4632 and RFC 4291.

It returns the IP address and the network implied by the IP and prefix length. For example, ParseCIDR("192.0.2.1/24") returns the IP address 192.0.2.1 and the network 192.0.2.0/24.

func ParseMAC

func ParseMAC(t testing.TB, s string) (hw net.HardwareAddr)

ParseMAC parses s as an IEEE 802 MAC-48, EUI-48, EUI-64, or a 20-octet IP over InfiniBand link-layer address using one of the following formats:

00:00:5e:00:53:01
02:00:5e:10:00:00:00:01
00:00:00:00:fe:80:00:00:00:00:00:00:02:00:5e:10:00:00:00:01
00-00-5e-00-53-01
02-00-5e-10-00-00-00-01
00-00-00-00-fe-80-00-00-00-00-00-00-02-00-5e-10-00-00-00-01
0000.5e00.5301
0200.5e10.0000.0001
0000.0000.fe80.0000.0000.0000.0200.5e10.0000.0001

func ResolveIPAddr

func ResolveIPAddr(t testing.TB, network, address string) *net.IPAddr

ResolveIPAddr returns an address of IP end point.

The network must be an IP network name.

If the host in the address parameter is not a literal IP address, ResolveIPAddr resolves the address to an address of IP end point. Otherwise, it parses the address as a literal IP address. The address parameter can use a host name, but this is not recommended, because it will return at most one of the host name's IP addresses.

See func Dial for a description of the network and address parameters.

func ResolveTCPAddr

func ResolveTCPAddr(t testing.TB, network, address string) *net.TCPAddr

ResolveTCPAddr returns an address of TCP end point.

The network must be a TCP network name.

If the host in the address parameter is not a literal IP address or the port is not a literal port number, ResolveTCPAddr resolves the address to an address of TCP end point. Otherwise, it parses the address as a pair of literal IP address and port number. The address parameter can use a host name, but this is not recommended, because it will return at most one of the host name's IP addresses.

See func Dial for a description of the network and address parameters.

func ResolveUDPAddr

func ResolveUDPAddr(t testing.TB, network, address string) *net.UDPAddr

ResolveUDPAddr returns an address of UDP end point.

The network must be a UDP network name.

If the host in the address parameter is not a literal IP address or the port is not a literal port number, ResolveUDPAddr resolves the address to an address of UDP end point. Otherwise, it parses the address as a pair of literal IP address and port number. The address parameter can use a host name, but this is not recommended, because it will return at most one of the host name's IP addresses.

See func Dial for a description of the network and address parameters.

func ResolveUnixAddr

func ResolveUnixAddr(t testing.TB, network, address string) *net.UnixAddr

ResolveUnixAddr returns an address of Unix domain socket end point.

The network must be a Unix network name.

See func Dial for a description of the network and address parameters.

func SplitHostPort

func SplitHostPort(t testing.TB, hostport string) (host, port string)

SplitHostPort splits a network address of the form "host:port", "host%zone:port", "[host]:port" or "[host%zone]:port" into host or host%zone and port.

A literal IPv6 address in hostport must be enclosed in square brackets, as in "[::1]:80", "[::1%lo0]:80".

See func Dial for a description of the hostport parameter, and host and port results.

Types

This section is empty.

Jump to

Keyboard shortcuts

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