OSDN Git Service

new repo
[bytom/vapor.git] / vendor / golang.org / x / net / ipv4 / unicastsockopt_test.go
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.
4
5 package ipv4_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/ipv4"
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         ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
23         if ifi == nil {
24                 t.Skipf("not available on %s", runtime.GOOS)
25         }
26
27         ln, err := net.Listen("tcp4", "127.0.0.1:0")
28         if err != nil {
29                 t.Fatal(err)
30         }
31         defer ln.Close()
32
33         errc := make(chan error, 1)
34         go func() {
35                 c, err := ln.Accept()
36                 if err != nil {
37                         errc <- err
38                         return
39                 }
40                 errc <- c.Close()
41         }()
42
43         c, err := net.Dial("tcp4", ln.Addr().String())
44         if err != nil {
45                 t.Fatal(err)
46         }
47         defer c.Close()
48
49         testUnicastSocketOptions(t, ipv4.NewConn(c))
50
51         if err := <-errc; err != nil {
52                 t.Errorf("server: %v", err)
53         }
54 }
55
56 var packetConnUnicastSocketOptionTests = []struct {
57         net, proto, addr string
58 }{
59         {"udp4", "", "127.0.0.1:0"},
60         {"ip4", ":icmp", "127.0.0.1"},
61 }
62
63 func TestPacketConnUnicastSocketOptions(t *testing.T) {
64         switch runtime.GOOS {
65         case "nacl", "plan9", "windows":
66                 t.Skipf("not supported on %s", runtime.GOOS)
67         }
68         ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
69         if ifi == nil {
70                 t.Skipf("not available on %s", runtime.GOOS)
71         }
72
73         m, ok := nettest.SupportsRawIPSocket()
74         for _, tt := range packetConnUnicastSocketOptionTests {
75                 if tt.net == "ip4" && !ok {
76                         t.Log(m)
77                         continue
78                 }
79                 c, err := net.ListenPacket(tt.net+tt.proto, tt.addr)
80                 if err != nil {
81                         t.Fatal(err)
82                 }
83                 defer c.Close()
84
85                 testUnicastSocketOptions(t, ipv4.NewPacketConn(c))
86         }
87 }
88
89 func TestRawConnUnicastSocketOptions(t *testing.T) {
90         switch runtime.GOOS {
91         case "nacl", "plan9", "windows":
92                 t.Skipf("not supported on %s", runtime.GOOS)
93         }
94         if m, ok := nettest.SupportsRawIPSocket(); !ok {
95                 t.Skip(m)
96         }
97         ifi := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback)
98         if ifi == nil {
99                 t.Skipf("not available on %s", runtime.GOOS)
100         }
101
102         c, err := net.ListenPacket("ip4:icmp", "127.0.0.1")
103         if err != nil {
104                 t.Fatal(err)
105         }
106         defer c.Close()
107
108         r, err := ipv4.NewRawConn(c)
109         if err != nil {
110                 t.Fatal(err)
111         }
112
113         testUnicastSocketOptions(t, r)
114 }
115
116 type testIPv4UnicastConn interface {
117         TOS() (int, error)
118         SetTOS(int) error
119         TTL() (int, error)
120         SetTTL(int) error
121 }
122
123 func testUnicastSocketOptions(t *testing.T, c testIPv4UnicastConn) {
124         tos := iana.DiffServCS0 | iana.NotECNTransport
125         switch runtime.GOOS {
126         case "windows":
127                 // IP_TOS option is supported on Windows 8 and beyond.
128                 t.Skipf("not supported on %s", runtime.GOOS)
129         }
130
131         if err := c.SetTOS(tos); err != nil {
132                 t.Fatal(err)
133         }
134         if v, err := c.TOS(); err != nil {
135                 t.Fatal(err)
136         } else if v != tos {
137                 t.Fatalf("got %v; want %v", v, tos)
138         }
139         const ttl = 255
140         if err := c.SetTTL(ttl); err != nil {
141                 t.Fatal(err)
142         }
143         if v, err := c.TTL(); err != nil {
144                 t.Fatal(err)
145         } else if v != ttl {
146                 t.Fatalf("got %v; want %v", v, ttl)
147         }
148 }