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