Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 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 | |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 5 | package ipv4_test |
| 6 | |
| 7 | import ( |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 8 | "net" |
Mikio Hara | 6fb7275 | 2013-11-03 20:59:24 +0900 | [diff] [blame] | 9 | "runtime" |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 10 | "testing" |
Mikio Hara | e5dac17 | 2014-10-07 06:35:16 +0900 | [diff] [blame] | 11 | |
Andrew Gerrand | fbe893d | 2014-11-10 09:04:43 +1100 | [diff] [blame] | 12 | "golang.org/x/net/internal/nettest" |
| 13 | "golang.org/x/net/ipv4" |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 14 | ) |
| 15 | |
Mikio Hara | 6fb7275 | 2013-11-03 20:59:24 +0900 | [diff] [blame] | 16 | var udpMultipleGroupListenerTests = []net.Addr{ |
| 17 | &net.UDPAddr{IP: net.IPv4(224, 0, 0, 249)}, // see RFC 4727 |
| 18 | &net.UDPAddr{IP: net.IPv4(224, 0, 0, 250)}, |
| 19 | &net.UDPAddr{IP: net.IPv4(224, 0, 0, 254)}, |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 20 | } |
| 21 | |
Mikio Hara | 6fb7275 | 2013-11-03 20:59:24 +0900 | [diff] [blame] | 22 | func TestUDPSinglePacketConnWithMultipleGroupListeners(t *testing.T) { |
| 23 | switch runtime.GOOS { |
Mikio Hara | d3b1e66 | 2014-10-30 14:15:42 +0900 | [diff] [blame] | 24 | case "nacl", "plan9", "solaris", "windows": |
Mikio Hara | 200fa7c | 2015-01-30 09:49:48 +0900 | [diff] [blame] | 25 | t.Skipf("not supported on %s", runtime.GOOS) |
Mikio Hara | 6fb7275 | 2013-11-03 20:59:24 +0900 | [diff] [blame] | 26 | } |
Mikio Hara | e5dac17 | 2014-10-07 06:35:16 +0900 | [diff] [blame] | 27 | if testing.Short() { |
Mikio Hara | 413497e | 2013-04-04 14:37:01 +0900 | [diff] [blame] | 28 | t.Skip("to avoid external network") |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 29 | } |
| 30 | |
Mikio Hara | 6fb7275 | 2013-11-03 20:59:24 +0900 | [diff] [blame] | 31 | for _, gaddr := range udpMultipleGroupListenerTests { |
| 32 | c, err := net.ListenPacket("udp4", "0.0.0.0:0") // wildcard address with no reusable port |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 33 | if err != nil { |
Mikio Hara | c25a4f3 | 2014-11-16 11:30:27 +0900 | [diff] [blame] | 34 | t.Fatal(err) |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 35 | } |
| 36 | defer c.Close() |
| 37 | |
| 38 | p := ipv4.NewPacketConn(c) |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 39 | var mift []*net.Interface |
Mikio Hara | 6fb7275 | 2013-11-03 20:59:24 +0900 | [diff] [blame] | 40 | |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 41 | ift, err := net.Interfaces() |
| 42 | if err != nil { |
Mikio Hara | c25a4f3 | 2014-11-16 11:30:27 +0900 | [diff] [blame] | 43 | t.Fatal(err) |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 44 | } |
| 45 | for i, ifi := range ift { |
Mikio Hara | 2c60c92 | 2014-10-08 06:54:30 +0900 | [diff] [blame] | 46 | if _, ok := nettest.IsMulticastCapable("ip4", &ifi); !ok { |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 47 | continue |
| 48 | } |
Mikio Hara | 6fb7275 | 2013-11-03 20:59:24 +0900 | [diff] [blame] | 49 | if err := p.JoinGroup(&ifi, gaddr); err != nil { |
Mikio Hara | c25a4f3 | 2014-11-16 11:30:27 +0900 | [diff] [blame] | 50 | t.Fatal(err) |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 51 | } |
| 52 | mift = append(mift, &ift[i]) |
| 53 | } |
| 54 | for _, ifi := range mift { |
Mikio Hara | 6fb7275 | 2013-11-03 20:59:24 +0900 | [diff] [blame] | 55 | if err := p.LeaveGroup(ifi, gaddr); err != nil { |
Mikio Hara | c25a4f3 | 2014-11-16 11:30:27 +0900 | [diff] [blame] | 56 | t.Fatal(err) |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
Mikio Hara | 6fb7275 | 2013-11-03 20:59:24 +0900 | [diff] [blame] | 62 | func TestUDPMultiplePacketConnWithMultipleGroupListeners(t *testing.T) { |
| 63 | switch runtime.GOOS { |
Mikio Hara | d3b1e66 | 2014-10-30 14:15:42 +0900 | [diff] [blame] | 64 | case "nacl", "plan9", "solaris", "windows": |
Mikio Hara | 200fa7c | 2015-01-30 09:49:48 +0900 | [diff] [blame] | 65 | t.Skipf("not supported on %s", runtime.GOOS) |
Mikio Hara | 6fb7275 | 2013-11-03 20:59:24 +0900 | [diff] [blame] | 66 | } |
Mikio Hara | e5dac17 | 2014-10-07 06:35:16 +0900 | [diff] [blame] | 67 | if testing.Short() { |
Mikio Hara | 413497e | 2013-04-04 14:37:01 +0900 | [diff] [blame] | 68 | t.Skip("to avoid external network") |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 69 | } |
| 70 | |
Mikio Hara | 6fb7275 | 2013-11-03 20:59:24 +0900 | [diff] [blame] | 71 | for _, gaddr := range udpMultipleGroupListenerTests { |
| 72 | c1, err := net.ListenPacket("udp4", "224.0.0.0:1024") // wildcard address with reusable port |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 73 | if err != nil { |
Mikio Hara | c25a4f3 | 2014-11-16 11:30:27 +0900 | [diff] [blame] | 74 | t.Fatal(err) |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 75 | } |
| 76 | defer c1.Close() |
| 77 | |
Mikio Hara | 6fb7275 | 2013-11-03 20:59:24 +0900 | [diff] [blame] | 78 | c2, err := net.ListenPacket("udp4", "224.0.0.0:1024") // wildcard address with reusable port |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 79 | if err != nil { |
Mikio Hara | c25a4f3 | 2014-11-16 11:30:27 +0900 | [diff] [blame] | 80 | t.Fatal(err) |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 81 | } |
| 82 | defer c2.Close() |
| 83 | |
| 84 | var ps [2]*ipv4.PacketConn |
| 85 | ps[0] = ipv4.NewPacketConn(c1) |
| 86 | ps[1] = ipv4.NewPacketConn(c2) |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 87 | var mift []*net.Interface |
Mikio Hara | 6fb7275 | 2013-11-03 20:59:24 +0900 | [diff] [blame] | 88 | |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 89 | ift, err := net.Interfaces() |
| 90 | if err != nil { |
Mikio Hara | c25a4f3 | 2014-11-16 11:30:27 +0900 | [diff] [blame] | 91 | t.Fatal(err) |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 92 | } |
| 93 | for i, ifi := range ift { |
Mikio Hara | 2c60c92 | 2014-10-08 06:54:30 +0900 | [diff] [blame] | 94 | if _, ok := nettest.IsMulticastCapable("ip4", &ifi); !ok { |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 95 | continue |
| 96 | } |
| 97 | for _, p := range ps { |
Mikio Hara | 6fb7275 | 2013-11-03 20:59:24 +0900 | [diff] [blame] | 98 | if err := p.JoinGroup(&ifi, gaddr); err != nil { |
Mikio Hara | c25a4f3 | 2014-11-16 11:30:27 +0900 | [diff] [blame] | 99 | t.Fatal(err) |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | mift = append(mift, &ift[i]) |
| 103 | } |
| 104 | for _, ifi := range mift { |
| 105 | for _, p := range ps { |
Mikio Hara | 6fb7275 | 2013-11-03 20:59:24 +0900 | [diff] [blame] | 106 | if err := p.LeaveGroup(ifi, gaddr); err != nil { |
Mikio Hara | c25a4f3 | 2014-11-16 11:30:27 +0900 | [diff] [blame] | 107 | t.Fatal(err) |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
Mikio Hara | 6fb7275 | 2013-11-03 20:59:24 +0900 | [diff] [blame] | 114 | func TestUDPPerInterfaceSinglePacketConnWithSingleGroupListener(t *testing.T) { |
| 115 | switch runtime.GOOS { |
Mikio Hara | d3b1e66 | 2014-10-30 14:15:42 +0900 | [diff] [blame] | 116 | case "nacl", "plan9", "solaris", "windows": |
Mikio Hara | 200fa7c | 2015-01-30 09:49:48 +0900 | [diff] [blame] | 117 | t.Skipf("not supported on %s", runtime.GOOS) |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 118 | } |
Mikio Hara | e5dac17 | 2014-10-07 06:35:16 +0900 | [diff] [blame] | 119 | if testing.Short() { |
Mikio Hara | 413497e | 2013-04-04 14:37:01 +0900 | [diff] [blame] | 120 | t.Skip("to avoid external network") |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 121 | } |
| 122 | |
Mikio Hara | 6fb7275 | 2013-11-03 20:59:24 +0900 | [diff] [blame] | 123 | gaddr := net.IPAddr{IP: net.IPv4(224, 0, 0, 254)} // see RFC 4727 |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 124 | type ml struct { |
| 125 | c *ipv4.PacketConn |
| 126 | ifi *net.Interface |
| 127 | } |
| 128 | var mlt []*ml |
| 129 | |
| 130 | ift, err := net.Interfaces() |
| 131 | if err != nil { |
Mikio Hara | c25a4f3 | 2014-11-16 11:30:27 +0900 | [diff] [blame] | 132 | t.Fatal(err) |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 133 | } |
| 134 | for i, ifi := range ift { |
Mikio Hara | 2c60c92 | 2014-10-08 06:54:30 +0900 | [diff] [blame] | 135 | ip, ok := nettest.IsMulticastCapable("ip4", &ifi) |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 136 | if !ok { |
| 137 | continue |
| 138 | } |
Mikio Hara | 6fb7275 | 2013-11-03 20:59:24 +0900 | [diff] [blame] | 139 | c, err := net.ListenPacket("udp4", ip.String()+":"+"1024") // unicast address with non-reusable port |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 140 | if err != nil { |
Mikio Hara | c25a4f3 | 2014-11-16 11:30:27 +0900 | [diff] [blame] | 141 | t.Fatal(err) |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 142 | } |
| 143 | defer c.Close() |
| 144 | p := ipv4.NewPacketConn(c) |
Mikio Hara | 6fb7275 | 2013-11-03 20:59:24 +0900 | [diff] [blame] | 145 | if err := p.JoinGroup(&ifi, &gaddr); err != nil { |
Mikio Hara | c25a4f3 | 2014-11-16 11:30:27 +0900 | [diff] [blame] | 146 | t.Fatal(err) |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 147 | } |
| 148 | mlt = append(mlt, &ml{p, &ift[i]}) |
| 149 | } |
| 150 | for _, m := range mlt { |
Mikio Hara | 6fb7275 | 2013-11-03 20:59:24 +0900 | [diff] [blame] | 151 | if err := m.c.LeaveGroup(m.ifi, &gaddr); err != nil { |
Mikio Hara | c25a4f3 | 2014-11-16 11:30:27 +0900 | [diff] [blame] | 152 | t.Fatal(err) |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
Mikio Hara | 6fb7275 | 2013-11-03 20:59:24 +0900 | [diff] [blame] | 157 | func TestIPSingleRawConnWithSingleGroupListener(t *testing.T) { |
| 158 | switch runtime.GOOS { |
Mikio Hara | d3b1e66 | 2014-10-30 14:15:42 +0900 | [diff] [blame] | 159 | case "nacl", "plan9", "solaris", "windows": |
Mikio Hara | 200fa7c | 2015-01-30 09:49:48 +0900 | [diff] [blame] | 160 | t.Skipf("not supported on %s", runtime.GOOS) |
Mikio Hara | 6fb7275 | 2013-11-03 20:59:24 +0900 | [diff] [blame] | 161 | } |
Mikio Hara | e5dac17 | 2014-10-07 06:35:16 +0900 | [diff] [blame] | 162 | if testing.Short() { |
Mikio Hara | 413497e | 2013-04-04 14:37:01 +0900 | [diff] [blame] | 163 | t.Skip("to avoid external network") |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 164 | } |
Mikio Hara | 99928be | 2015-01-28 12:12:10 +0900 | [diff] [blame] | 165 | if m, ok := nettest.SupportsRawIPSocket(); !ok { |
| 166 | t.Skip(m) |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 167 | } |
| 168 | |
Mikio Hara | 6fb7275 | 2013-11-03 20:59:24 +0900 | [diff] [blame] | 169 | c, err := net.ListenPacket("ip4:icmp", "0.0.0.0") // wildcard address |
| 170 | if err != nil { |
Mikio Hara | c25a4f3 | 2014-11-16 11:30:27 +0900 | [diff] [blame] | 171 | t.Fatal(err) |
Mikio Hara | 6fb7275 | 2013-11-03 20:59:24 +0900 | [diff] [blame] | 172 | } |
| 173 | defer c.Close() |
| 174 | |
| 175 | r, err := ipv4.NewRawConn(c) |
| 176 | if err != nil { |
Mikio Hara | c25a4f3 | 2014-11-16 11:30:27 +0900 | [diff] [blame] | 177 | t.Fatal(err) |
Mikio Hara | 6fb7275 | 2013-11-03 20:59:24 +0900 | [diff] [blame] | 178 | } |
| 179 | gaddr := net.IPAddr{IP: net.IPv4(224, 0, 0, 254)} // see RFC 4727 |
| 180 | var mift []*net.Interface |
| 181 | |
| 182 | ift, err := net.Interfaces() |
| 183 | if err != nil { |
Mikio Hara | c25a4f3 | 2014-11-16 11:30:27 +0900 | [diff] [blame] | 184 | t.Fatal(err) |
Mikio Hara | 6fb7275 | 2013-11-03 20:59:24 +0900 | [diff] [blame] | 185 | } |
| 186 | for i, ifi := range ift { |
Mikio Hara | 2c60c92 | 2014-10-08 06:54:30 +0900 | [diff] [blame] | 187 | if _, ok := nettest.IsMulticastCapable("ip4", &ifi); !ok { |
Mikio Hara | 6fb7275 | 2013-11-03 20:59:24 +0900 | [diff] [blame] | 188 | continue |
| 189 | } |
| 190 | if err := r.JoinGroup(&ifi, &gaddr); err != nil { |
Mikio Hara | c25a4f3 | 2014-11-16 11:30:27 +0900 | [diff] [blame] | 191 | t.Fatal(err) |
Mikio Hara | 6fb7275 | 2013-11-03 20:59:24 +0900 | [diff] [blame] | 192 | } |
| 193 | mift = append(mift, &ift[i]) |
| 194 | } |
| 195 | for _, ifi := range mift { |
| 196 | if err := r.LeaveGroup(ifi, &gaddr); err != nil { |
Mikio Hara | c25a4f3 | 2014-11-16 11:30:27 +0900 | [diff] [blame] | 197 | t.Fatal(err) |
Mikio Hara | 6fb7275 | 2013-11-03 20:59:24 +0900 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | func TestIPPerInterfaceSingleRawConnWithSingleGroupListener(t *testing.T) { |
| 203 | switch runtime.GOOS { |
Mikio Hara | d3b1e66 | 2014-10-30 14:15:42 +0900 | [diff] [blame] | 204 | case "nacl", "plan9", "solaris", "windows": |
Mikio Hara | 200fa7c | 2015-01-30 09:49:48 +0900 | [diff] [blame] | 205 | t.Skipf("not supported on %s", runtime.GOOS) |
Mikio Hara | 6fb7275 | 2013-11-03 20:59:24 +0900 | [diff] [blame] | 206 | } |
Mikio Hara | e5dac17 | 2014-10-07 06:35:16 +0900 | [diff] [blame] | 207 | if testing.Short() { |
Mikio Hara | 6fb7275 | 2013-11-03 20:59:24 +0900 | [diff] [blame] | 208 | t.Skip("to avoid external network") |
| 209 | } |
Mikio Hara | 99928be | 2015-01-28 12:12:10 +0900 | [diff] [blame] | 210 | if m, ok := nettest.SupportsRawIPSocket(); !ok { |
| 211 | t.Skip(m) |
Mikio Hara | 6fb7275 | 2013-11-03 20:59:24 +0900 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | gaddr := net.IPAddr{IP: net.IPv4(224, 0, 0, 254)} // see RFC 4727 |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 215 | type ml struct { |
| 216 | c *ipv4.RawConn |
| 217 | ifi *net.Interface |
| 218 | } |
| 219 | var mlt []*ml |
| 220 | |
| 221 | ift, err := net.Interfaces() |
| 222 | if err != nil { |
Mikio Hara | c25a4f3 | 2014-11-16 11:30:27 +0900 | [diff] [blame] | 223 | t.Fatal(err) |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 224 | } |
| 225 | for i, ifi := range ift { |
Mikio Hara | 2c60c92 | 2014-10-08 06:54:30 +0900 | [diff] [blame] | 226 | ip, ok := nettest.IsMulticastCapable("ip4", &ifi) |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 227 | if !ok { |
| 228 | continue |
| 229 | } |
Mikio Hara | 6fb7275 | 2013-11-03 20:59:24 +0900 | [diff] [blame] | 230 | c, err := net.ListenPacket("ip4:253", ip.String()) // unicast address |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 231 | if err != nil { |
Mikio Hara | c25a4f3 | 2014-11-16 11:30:27 +0900 | [diff] [blame] | 232 | t.Fatal(err) |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 233 | } |
| 234 | defer c.Close() |
| 235 | r, err := ipv4.NewRawConn(c) |
| 236 | if err != nil { |
Mikio Hara | c25a4f3 | 2014-11-16 11:30:27 +0900 | [diff] [blame] | 237 | t.Fatal(err) |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 238 | } |
Mikio Hara | 6fb7275 | 2013-11-03 20:59:24 +0900 | [diff] [blame] | 239 | if err := r.JoinGroup(&ifi, &gaddr); err != nil { |
Mikio Hara | c25a4f3 | 2014-11-16 11:30:27 +0900 | [diff] [blame] | 240 | t.Fatal(err) |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 241 | } |
| 242 | mlt = append(mlt, &ml{r, &ift[i]}) |
| 243 | } |
| 244 | for _, m := range mlt { |
Mikio Hara | 6fb7275 | 2013-11-03 20:59:24 +0900 | [diff] [blame] | 245 | if err := m.c.LeaveGroup(m.ifi, &gaddr); err != nil { |
Mikio Hara | c25a4f3 | 2014-11-16 11:30:27 +0900 | [diff] [blame] | 246 | t.Fatal(err) |
Mikio Hara | d2e5a12 | 2012-09-26 21:03:09 +0900 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | } |