github.com/Andyfoo/golang/x/net@v0.0.0-20190901054642-57c1bf301704/ipv6/unicast_test.go (about) 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 "bytes" 9 "net" 10 "os" 11 "runtime" 12 "testing" 13 "time" 14 15 "github.com/Andyfoo/golang/x/net/icmp" 16 "github.com/Andyfoo/golang/x/net/internal/iana" 17 "github.com/Andyfoo/golang/x/net/ipv6" 18 "github.com/Andyfoo/golang/x/net/nettest" 19 ) 20 21 func TestPacketConnReadWriteUnicastUDP(t *testing.T) { 22 switch runtime.GOOS { 23 case "fuchsia", "hurd", "js", "nacl", "plan9", "windows": 24 t.Skipf("not supported on %s", runtime.GOOS) 25 } 26 if !nettest.SupportsIPv6() { 27 t.Skip("ipv6 is not supported") 28 } 29 30 c, err := nettest.NewLocalPacketListener("udp6") 31 if err != nil { 32 t.Fatal(err) 33 } 34 defer c.Close() 35 p := ipv6.NewPacketConn(c) 36 defer p.Close() 37 38 dst := c.LocalAddr() 39 cm := ipv6.ControlMessage{ 40 TrafficClass: iana.DiffServAF11 | iana.CongestionExperienced, 41 Src: net.IPv6loopback, 42 } 43 cf := ipv6.FlagTrafficClass | ipv6.FlagHopLimit | ipv6.FlagSrc | ipv6.FlagDst | ipv6.FlagInterface | ipv6.FlagPathMTU 44 ifi, _ := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback) 45 if ifi != nil { 46 cm.IfIndex = ifi.Index 47 } 48 wb := []byte("HELLO-R-U-THERE") 49 50 for i, toggle := range []bool{true, false, true} { 51 if err := p.SetControlMessage(cf, toggle); err != nil { 52 if protocolNotSupported(err) { 53 t.Logf("not supported on %s", runtime.GOOS) 54 continue 55 } 56 t.Fatal(err) 57 } 58 cm.HopLimit = i + 1 59 if err := p.SetWriteDeadline(time.Now().Add(100 * time.Millisecond)); err != nil { 60 t.Fatal(err) 61 } 62 if n, err := p.WriteTo(wb, &cm, dst); err != nil { 63 t.Fatal(err) 64 } else if n != len(wb) { 65 t.Fatalf("got %v; want %v", n, len(wb)) 66 } 67 rb := make([]byte, 128) 68 if err := p.SetReadDeadline(time.Now().Add(100 * time.Millisecond)); err != nil { 69 t.Fatal(err) 70 } 71 if n, _, _, err := p.ReadFrom(rb); err != nil { 72 t.Fatal(err) 73 } else if !bytes.Equal(rb[:n], wb) { 74 t.Fatalf("got %v; want %v", rb[:n], wb) 75 } 76 } 77 } 78 79 func TestPacketConnReadWriteUnicastICMP(t *testing.T) { 80 switch runtime.GOOS { 81 case "fuchsia", "hurd", "js", "nacl", "plan9", "windows": 82 t.Skipf("not supported on %s", runtime.GOOS) 83 } 84 if !nettest.SupportsIPv6() { 85 t.Skip("ipv6 is not supported") 86 } 87 if !nettest.SupportsRawSocket() { 88 t.Skipf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH) 89 } 90 91 c, err := net.ListenPacket("ip6:ipv6-icmp", "::1") 92 if err != nil { 93 t.Fatal(err) 94 } 95 defer c.Close() 96 p := ipv6.NewPacketConn(c) 97 defer p.Close() 98 99 dst, err := net.ResolveIPAddr("ip6", "::1") 100 if err != nil { 101 t.Fatal(err) 102 } 103 104 pshicmp := icmp.IPv6PseudoHeader(c.LocalAddr().(*net.IPAddr).IP, dst.IP) 105 cm := ipv6.ControlMessage{ 106 TrafficClass: iana.DiffServAF11 | iana.CongestionExperienced, 107 Src: net.IPv6loopback, 108 } 109 cf := ipv6.FlagTrafficClass | ipv6.FlagHopLimit | ipv6.FlagSrc | ipv6.FlagDst | ipv6.FlagInterface | ipv6.FlagPathMTU 110 ifi, _ := nettest.RoutedInterface("ip6", net.FlagUp|net.FlagLoopback) 111 if ifi != nil { 112 cm.IfIndex = ifi.Index 113 } 114 115 var f ipv6.ICMPFilter 116 f.SetAll(true) 117 f.Accept(ipv6.ICMPTypeEchoReply) 118 if err := p.SetICMPFilter(&f); err != nil { 119 t.Fatal(err) 120 } 121 122 var psh []byte 123 for i, toggle := range []bool{true, false, true} { 124 if toggle { 125 psh = nil 126 if err := p.SetChecksum(true, 2); err != nil { 127 // AIX, Illumos and Solaris never allow 128 // modification of ICMP properties. 129 switch runtime.GOOS { 130 case "aix", "illumos", "solaris": 131 default: 132 t.Fatal(err) 133 } 134 } 135 } else { 136 psh = pshicmp 137 // Some platforms never allow to disable the 138 // kernel checksum processing. 139 p.SetChecksum(false, -1) 140 } 141 wb, err := (&icmp.Message{ 142 Type: ipv6.ICMPTypeEchoRequest, Code: 0, 143 Body: &icmp.Echo{ 144 ID: os.Getpid() & 0xffff, Seq: i + 1, 145 Data: []byte("HELLO-R-U-THERE"), 146 }, 147 }).Marshal(psh) 148 if err != nil { 149 t.Fatal(err) 150 } 151 if err := p.SetControlMessage(cf, toggle); err != nil { 152 if protocolNotSupported(err) { 153 t.Logf("not supported on %s", runtime.GOOS) 154 continue 155 } 156 t.Fatal(err) 157 } 158 cm.HopLimit = i + 1 159 if err := p.SetWriteDeadline(time.Now().Add(100 * time.Millisecond)); err != nil { 160 t.Fatal(err) 161 } 162 if n, err := p.WriteTo(wb, &cm, dst); err != nil { 163 t.Fatal(err) 164 } else if n != len(wb) { 165 t.Fatalf("got %v; want %v", n, len(wb)) 166 } 167 rb := make([]byte, 128) 168 if err := p.SetReadDeadline(time.Now().Add(100 * time.Millisecond)); err != nil { 169 t.Fatal(err) 170 } 171 if n, _, _, err := p.ReadFrom(rb); err != nil { 172 switch runtime.GOOS { 173 case "darwin": // older darwin kernels have some limitation on receiving icmp packet through raw socket 174 t.Logf("not supported on %s", runtime.GOOS) 175 continue 176 } 177 t.Fatal(err) 178 } else { 179 if m, err := icmp.ParseMessage(iana.ProtocolIPv6ICMP, rb[:n]); err != nil { 180 t.Fatal(err) 181 } else if m.Type != ipv6.ICMPTypeEchoReply || m.Code != 0 { 182 t.Fatalf("got type=%v, code=%v; want type=%v, code=%v", m.Type, m.Code, ipv6.ICMPTypeEchoReply, 0) 183 } 184 } 185 } 186 }