golang.org/x/net@v0.25.1-0.20240516223405-c87a5b62e243/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/ipv4" 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", "zos": 20 t.Skipf("not supported on %s", runtime.GOOS) 21 } 22 if _, err := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback); err != nil { 23 t.Skipf("not available on %s", runtime.GOOS) 24 } 25 26 ln, err := net.Listen("tcp4", "127.0.0.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("tcp4", ln.Addr().String()) 43 if err != nil { 44 t.Fatal(err) 45 } 46 defer c.Close() 47 48 testUnicastSocketOptions(t, ipv4.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 {"udp4", "", "127.0.0.1:0"}, 59 {"ip4", ":icmp", "127.0.0.1"}, 60 } 61 62 func TestPacketConnUnicastSocketOptions(t *testing.T) { 63 switch runtime.GOOS { 64 case "fuchsia", "hurd", "js", "nacl", "plan9", "wasip1", "windows", "zos": 65 t.Skipf("not supported on %s", runtime.GOOS) 66 } 67 if _, err := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback); err != nil { 68 t.Skipf("not available on %s", runtime.GOOS) 69 } 70 71 ok := nettest.SupportsRawSocket() 72 for _, tt := range packetConnUnicastSocketOptionTests { 73 if tt.net == "ip4" && !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, ipv4.NewPacketConn(c)) 84 } 85 } 86 87 func TestRawConnUnicastSocketOptions(t *testing.T) { 88 if !nettest.SupportsRawSocket() { 89 t.Skipf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH) 90 } 91 if _, err := nettest.RoutedInterface("ip4", net.FlagUp|net.FlagLoopback); err != nil { 92 t.Skipf("not available on %s", runtime.GOOS) 93 } 94 95 c, err := net.ListenPacket("ip4:icmp", "127.0.0.1") 96 if err != nil { 97 t.Fatal(err) 98 } 99 defer c.Close() 100 101 r, err := ipv4.NewRawConn(c) 102 if err != nil { 103 t.Fatal(err) 104 } 105 106 testUnicastSocketOptions(t, r) 107 } 108 109 type testIPv4UnicastConn interface { 110 TOS() (int, error) 111 SetTOS(int) error 112 TTL() (int, error) 113 SetTTL(int) error 114 } 115 116 func testUnicastSocketOptions(t *testing.T, c testIPv4UnicastConn) { 117 t.Helper() 118 119 tos := iana.DiffServCS0 | iana.NotECNTransport 120 switch runtime.GOOS { 121 case "windows": 122 // IP_TOS option is supported on Windows 8 and beyond. 123 t.Skipf("not supported on %s", runtime.GOOS) 124 } 125 126 if err := c.SetTOS(tos); err != nil { 127 t.Fatal(err) 128 } 129 if v, err := c.TOS(); err != nil { 130 t.Fatal(err) 131 } else if v != tos { 132 t.Fatalf("got %v; want %v", v, tos) 133 } 134 const ttl = 255 135 if err := c.SetTTL(ttl); err != nil { 136 t.Fatal(err) 137 } 138 if v, err := c.TTL(); err != nil { 139 t.Fatal(err) 140 } else if v != ttl { 141 t.Fatalf("got %v; want %v", v, ttl) 142 } 143 }