2012-01-25 21:56:26 +01:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2012-01-25 22:54:22 +01:00
|
|
|
"io"
|
2012-01-25 21:56:26 +01:00
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var unicastTests = []struct {
|
|
|
|
net string
|
|
|
|
laddr string
|
|
|
|
ipv6 bool
|
|
|
|
packet bool
|
|
|
|
}{
|
2012-01-25 22:54:22 +01:00
|
|
|
{net: "tcp4", laddr: "127.0.0.1:0"},
|
|
|
|
{net: "tcp4", laddr: "previous"},
|
|
|
|
{net: "tcp6", laddr: "[::1]:0", ipv6: true},
|
|
|
|
{net: "tcp6", laddr: "previous", ipv6: true},
|
|
|
|
{net: "udp4", laddr: "127.0.0.1:0", packet: true},
|
|
|
|
{net: "udp6", laddr: "[::1]:0", ipv6: true, packet: true},
|
2012-01-25 21:56:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnicastTCPAndUDP(t *testing.T) {
|
|
|
|
if runtime.GOOS == "plan9" || runtime.GOOS == "windows" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-01-25 22:54:22 +01:00
|
|
|
prevladdr := ""
|
2012-01-25 21:56:26 +01:00
|
|
|
for _, tt := range unicastTests {
|
|
|
|
if tt.ipv6 && !supportsIPv6 {
|
|
|
|
continue
|
|
|
|
}
|
2012-01-25 22:54:22 +01:00
|
|
|
var (
|
|
|
|
fd *netFD
|
|
|
|
closer io.Closer
|
|
|
|
)
|
2012-01-25 21:56:26 +01:00
|
|
|
if !tt.packet {
|
2012-01-25 22:54:22 +01:00
|
|
|
if tt.laddr == "previous" {
|
|
|
|
tt.laddr = prevladdr
|
|
|
|
}
|
|
|
|
l, err := Listen(tt.net, tt.laddr)
|
2012-01-25 21:56:26 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Listen failed: %v", err)
|
|
|
|
}
|
2012-01-25 22:54:22 +01:00
|
|
|
prevladdr = l.Addr().String()
|
|
|
|
closer = l
|
|
|
|
fd = l.(*TCPListener).fd
|
2012-01-25 21:56:26 +01:00
|
|
|
} else {
|
|
|
|
c, err := ListenPacket(tt.net, tt.laddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ListenPacket failed: %v", err)
|
|
|
|
}
|
2012-01-25 22:54:22 +01:00
|
|
|
closer = c
|
2012-01-25 21:56:26 +01:00
|
|
|
fd = c.(*UDPConn).fd
|
|
|
|
}
|
|
|
|
if !tt.ipv6 {
|
|
|
|
testIPv4UnicastSocketOptions(t, fd)
|
|
|
|
} else {
|
|
|
|
testIPv6UnicastSocketOptions(t, fd)
|
|
|
|
}
|
2012-01-25 22:54:22 +01:00
|
|
|
closer.Close()
|
2012-01-25 21:56:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testIPv4UnicastSocketOptions(t *testing.T, fd *netFD) {
|
|
|
|
tos, err := ipv4TOS(fd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ipv4TOS failed: %v", err)
|
|
|
|
}
|
|
|
|
t.Logf("IPv4 TOS: %v", tos)
|
|
|
|
err = setIPv4TOS(fd, 1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("setIPv4TOS failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ttl, err := ipv4TTL(fd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ipv4TTL failed: %v", err)
|
|
|
|
}
|
|
|
|
t.Logf("IPv4 TTL: %v", ttl)
|
|
|
|
err = setIPv4TTL(fd, 1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("setIPv4TTL failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testIPv6UnicastSocketOptions(t *testing.T, fd *netFD) {
|
|
|
|
tos, err := ipv6TrafficClass(fd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ipv6TrafficClass failed: %v", err)
|
|
|
|
}
|
|
|
|
t.Logf("IPv6 TrafficClass: %v", tos)
|
|
|
|
err = setIPv6TrafficClass(fd, 1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("setIPv6TrafficClass failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
hoplim, err := ipv6HopLimit(fd)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ipv6HopLimit failed: %v", err)
|
|
|
|
}
|
|
|
|
t.Logf("IPv6 HopLimit: %v", hoplim)
|
|
|
|
err = setIPv6HopLimit(fd, 1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("setIPv6HopLimit failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|