OSDN Git Service

new repo
[bytom/vapor.git] / vendor / golang.org / x / net / ipv6 / unicastsockopt_test.go
1 // Copyright 2013 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.
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/internal/nettest"
14         "golang.org/x/net/ipv6"
15 )
16
17 func TestConnUnicastSocketOptions(t *testing.T) {
18         switch runtime.GOOS {
19         case "nacl", "plan9", "windows":
20                 t.Skipf("not supported on %s", runtime.GOOS)
21         }
22         if !supportsIPv6 {
23                 t.Skip("ipv6 is not supported")
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 "nacl", "plan9", "windows":
65                 t.Skipf("not supported on %s", runtime.GOOS)
66         }
67         if !supportsIPv6 {
68                 t.Skip("ipv6 is not supported")
69         }
70
71         m, ok := nettest.SupportsRawIPSocket()
72         for _, tt := range packetConnUnicastSocketOptionTests {
73                 if tt.net == "ip6" && !ok {
74                         t.Log(m)
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         tclass := iana.DiffServCS0 | iana.NotECNTransport
96         if err := c.SetTrafficClass(tclass); err != nil {
97                 switch runtime.GOOS {
98                 case "darwin": // older darwin kernels don't support IPV6_TCLASS option
99                         t.Logf("not supported on %s", runtime.GOOS)
100                         goto next
101                 }
102                 t.Fatal(err)
103         }
104         if v, err := c.TrafficClass(); err != nil {
105                 t.Fatal(err)
106         } else if v != tclass {
107                 t.Fatalf("got %v; want %v", v, tclass)
108         }
109
110 next:
111         hoplim := 255
112         if err := c.SetHopLimit(hoplim); err != nil {
113                 t.Fatal(err)
114         }
115         if v, err := c.HopLimit(); err != nil {
116                 t.Fatal(err)
117         } else if v != hoplim {
118                 t.Fatalf("got %v; want %v", v, hoplim)
119         }
120 }