1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
12 "golang.org/x/net/internal/iana"
13 "golang.org/x/net/internal/nettest"
14 "golang.org/x/net/ipv4"
17 func TestConnUnicastSocketOptions(t *testing.T) {
19 case "nacl", "plan9", "windows":
20 t.Skipf("not supported on %s", runtime.GOOS)
22 ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
24 t.Skipf("not available on %s", runtime.GOOS)
27 ln, err := net.Listen("tcp4", "127.0.0.1:0")
33 errc := make(chan error, 1)
43 c, err := net.Dial("tcp4", ln.Addr().String())
49 testUnicastSocketOptions(t, ipv4.NewConn(c))
51 if err := <-errc; err != nil {
52 t.Errorf("server: %v", err)
56 var packetConnUnicastSocketOptionTests = []struct {
57 net, proto, addr string
59 {"udp4", "", "127.0.0.1:0"},
60 {"ip4", ":icmp", "127.0.0.1"},
63 func TestPacketConnUnicastSocketOptions(t *testing.T) {
65 case "nacl", "plan9", "windows":
66 t.Skipf("not supported on %s", runtime.GOOS)
68 ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
70 t.Skipf("not available on %s", runtime.GOOS)
73 m, ok := nettest.SupportsRawIPSocket()
74 for _, tt := range packetConnUnicastSocketOptionTests {
75 if tt.net == "ip4" && !ok {
79 c, err := net.ListenPacket(tt.net+tt.proto, tt.addr)
85 testUnicastSocketOptions(t, ipv4.NewPacketConn(c))
89 func TestRawConnUnicastSocketOptions(t *testing.T) {
91 case "nacl", "plan9", "windows":
92 t.Skipf("not supported on %s", runtime.GOOS)
94 if m, ok := nettest.SupportsRawIPSocket(); !ok {
97 ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
99 t.Skipf("not available on %s", runtime.GOOS)
102 c, err := net.ListenPacket("ip4:icmp", "127.0.0.1")
108 r, err := ipv4.NewRawConn(c)
113 testUnicastSocketOptions(t, r)
116 type testIPv4UnicastConn interface {
123 func testUnicastSocketOptions(t *testing.T, c testIPv4UnicastConn) {
124 tos := iana.DiffServCS0 | iana.NotECNTransport
125 switch runtime.GOOS {
127 // IP_TOS option is supported on Windows 8 and beyond.
128 t.Skipf("not supported on %s", runtime.GOOS)
131 if err := c.SetTOS(tos); err != nil {
134 if v, err := c.TOS(); err != nil {
137 t.Fatalf("got %v; want %v", v, tos)
140 if err := c.SetTTL(ttl); err != nil {
143 if v, err := c.TTL(); err != nil {
146 t.Fatalf("got %v; want %v", v, ttl)