1
2
3
4
5 package ipv6_test
6
7 import (
8 "net"
9 "runtime"
10 "testing"
11
12 "golang.org/x/net/internal/iana"
13 "golang.org/x/net/ipv6"
14 "golang.org/x/net/nettest"
15 )
16
17 func TestConnUnicastSocketOptions(t *testing.T) {
18 switch runtime.GOOS {
19 case "fuchsia", "hurd", "js", "nacl", "plan9", "wasip1", "windows":
20 t.Skipf("not supported on %s", runtime.GOOS)
21 }
22 if _, err := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback); err != nil {
23 t.Skip("ipv6 is not enabled for loopback interface")
24 }
25
26 ln, err := net.Listen("tcp6", "[::1]:0")
27 if err != nil {
28 t.Fatal(err)
29 }
30 defer ln.Close()
31
32 errc := make(chan error, 1)
33 go func() {
34 c, err := ln.Accept()
35 if err != nil {
36 errc <- err
37 return
38 }
39 errc <- c.Close()
40 }()
41
42 c, err := net.Dial("tcp6", ln.Addr().String())
43 if err != nil {
44 t.Fatal(err)
45 }
46 defer c.Close()
47
48 testUnicastSocketOptions(t, ipv6.NewConn(c))
49
50 if err := <-errc; err != nil {
51 t.Errorf("server: %v", err)
52 }
53 }
54
55 var packetConnUnicastSocketOptionTests = []struct {
56 net, proto, addr string
57 }{
58 {"udp6", "", "[::1]:0"},
59 {"ip6", ":ipv6-icmp", "::1"},
60 }
61
62 func TestPacketConnUnicastSocketOptions(t *testing.T) {
63 switch runtime.GOOS {
64 case "fuchsia", "hurd", "js", "nacl", "plan9", "wasip1", "windows":
65 t.Skipf("not supported on %s", runtime.GOOS)
66 }
67 if _, err := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback); err != nil {
68 t.Skip("ipv6 is not enabled for loopback interface")
69 }
70
71 ok := nettest.SupportsRawSocket()
72 for _, tt := range packetConnUnicastSocketOptionTests {
73 if tt.net == "ip6" && !ok {
74 t.Logf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
75 continue
76 }
77 c, err := net.ListenPacket(tt.net+tt.proto, tt.addr)
78 if err != nil {
79 t.Fatal(err)
80 }
81 defer c.Close()
82
83 testUnicastSocketOptions(t, ipv6.NewPacketConn(c))
84 }
85 }
86
87 type testIPv6UnicastConn interface {
88 TrafficClass() (int, error)
89 SetTrafficClass(int) error
90 HopLimit() (int, error)
91 SetHopLimit(int) error
92 }
93
94 func testUnicastSocketOptions(t *testing.T, c testIPv6UnicastConn) {
95 t.Helper()
96
97 tclass := iana.DiffServCS0 | iana.NotECNTransport
98 if err := c.SetTrafficClass(tclass); err != nil {
99 t.Fatal(err)
100 }
101 if v, err := c.TrafficClass(); err != nil {
102 t.Fatal(err)
103 } else if v != tclass {
104 t.Fatalf("got %v; want %v", v, tclass)
105 }
106
107 hoplim := 255
108 if err := c.SetHopLimit(hoplim); err != nil {
109 t.Fatal(err)
110 }
111 if v, err := c.HopLimit(); err != nil {
112 t.Fatal(err)
113 } else if v != hoplim {
114 t.Fatalf("got %v; want %v", v, hoplim)
115 }
116 }
117
View as plain text