blob: e342bf1d901fe1bf26c1878923dd13d446219a28 [file] [log] [blame]
Mikio Harad2e5a122012-09-26 21:03:09 +09001// 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 Harad2e5a122012-09-26 21:03:09 +09005package ipv4_test
6
7import (
Mikio Harad2e5a122012-09-26 21:03:09 +09008 "net"
Mikio Hara6fb72752013-11-03 20:59:24 +09009 "runtime"
Mikio Harad2e5a122012-09-26 21:03:09 +090010 "testing"
Mikio Harae5dac172014-10-07 06:35:16 +090011
Andrew Gerrandfbe893d2014-11-10 09:04:43 +110012 "golang.org/x/net/internal/nettest"
13 "golang.org/x/net/ipv4"
Mikio Harad2e5a122012-09-26 21:03:09 +090014)
15
Mikio Hara6fb72752013-11-03 20:59:24 +090016var 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 Harad2e5a122012-09-26 21:03:09 +090020}
21
Mikio Hara6fb72752013-11-03 20:59:24 +090022func TestUDPSinglePacketConnWithMultipleGroupListeners(t *testing.T) {
23 switch runtime.GOOS {
Mikio Harad3b1e662014-10-30 14:15:42 +090024 case "nacl", "plan9", "solaris", "windows":
Mikio Hara200fa7c2015-01-30 09:49:48 +090025 t.Skipf("not supported on %s", runtime.GOOS)
Mikio Hara6fb72752013-11-03 20:59:24 +090026 }
Mikio Harae5dac172014-10-07 06:35:16 +090027 if testing.Short() {
Mikio Hara413497e2013-04-04 14:37:01 +090028 t.Skip("to avoid external network")
Mikio Harad2e5a122012-09-26 21:03:09 +090029 }
30
Mikio Hara6fb72752013-11-03 20:59:24 +090031 for _, gaddr := range udpMultipleGroupListenerTests {
32 c, err := net.ListenPacket("udp4", "0.0.0.0:0") // wildcard address with no reusable port
Mikio Harad2e5a122012-09-26 21:03:09 +090033 if err != nil {
Mikio Harac25a4f32014-11-16 11:30:27 +090034 t.Fatal(err)
Mikio Harad2e5a122012-09-26 21:03:09 +090035 }
36 defer c.Close()
37
38 p := ipv4.NewPacketConn(c)
Mikio Harad2e5a122012-09-26 21:03:09 +090039 var mift []*net.Interface
Mikio Hara6fb72752013-11-03 20:59:24 +090040
Mikio Harad2e5a122012-09-26 21:03:09 +090041 ift, err := net.Interfaces()
42 if err != nil {
Mikio Harac25a4f32014-11-16 11:30:27 +090043 t.Fatal(err)
Mikio Harad2e5a122012-09-26 21:03:09 +090044 }
45 for i, ifi := range ift {
Mikio Hara2c60c922014-10-08 06:54:30 +090046 if _, ok := nettest.IsMulticastCapable("ip4", &ifi); !ok {
Mikio Harad2e5a122012-09-26 21:03:09 +090047 continue
48 }
Mikio Hara6fb72752013-11-03 20:59:24 +090049 if err := p.JoinGroup(&ifi, gaddr); err != nil {
Mikio Harac25a4f32014-11-16 11:30:27 +090050 t.Fatal(err)
Mikio Harad2e5a122012-09-26 21:03:09 +090051 }
52 mift = append(mift, &ift[i])
53 }
54 for _, ifi := range mift {
Mikio Hara6fb72752013-11-03 20:59:24 +090055 if err := p.LeaveGroup(ifi, gaddr); err != nil {
Mikio Harac25a4f32014-11-16 11:30:27 +090056 t.Fatal(err)
Mikio Harad2e5a122012-09-26 21:03:09 +090057 }
58 }
59 }
60}
61
Mikio Hara6fb72752013-11-03 20:59:24 +090062func TestUDPMultiplePacketConnWithMultipleGroupListeners(t *testing.T) {
63 switch runtime.GOOS {
Mikio Harad3b1e662014-10-30 14:15:42 +090064 case "nacl", "plan9", "solaris", "windows":
Mikio Hara200fa7c2015-01-30 09:49:48 +090065 t.Skipf("not supported on %s", runtime.GOOS)
Mikio Hara6fb72752013-11-03 20:59:24 +090066 }
Mikio Harae5dac172014-10-07 06:35:16 +090067 if testing.Short() {
Mikio Hara413497e2013-04-04 14:37:01 +090068 t.Skip("to avoid external network")
Mikio Harad2e5a122012-09-26 21:03:09 +090069 }
70
Mikio Hara6fb72752013-11-03 20:59:24 +090071 for _, gaddr := range udpMultipleGroupListenerTests {
72 c1, err := net.ListenPacket("udp4", "224.0.0.0:1024") // wildcard address with reusable port
Mikio Harad2e5a122012-09-26 21:03:09 +090073 if err != nil {
Mikio Harac25a4f32014-11-16 11:30:27 +090074 t.Fatal(err)
Mikio Harad2e5a122012-09-26 21:03:09 +090075 }
76 defer c1.Close()
77
Mikio Hara6fb72752013-11-03 20:59:24 +090078 c2, err := net.ListenPacket("udp4", "224.0.0.0:1024") // wildcard address with reusable port
Mikio Harad2e5a122012-09-26 21:03:09 +090079 if err != nil {
Mikio Harac25a4f32014-11-16 11:30:27 +090080 t.Fatal(err)
Mikio Harad2e5a122012-09-26 21:03:09 +090081 }
82 defer c2.Close()
83
84 var ps [2]*ipv4.PacketConn
85 ps[0] = ipv4.NewPacketConn(c1)
86 ps[1] = ipv4.NewPacketConn(c2)
Mikio Harad2e5a122012-09-26 21:03:09 +090087 var mift []*net.Interface
Mikio Hara6fb72752013-11-03 20:59:24 +090088
Mikio Harad2e5a122012-09-26 21:03:09 +090089 ift, err := net.Interfaces()
90 if err != nil {
Mikio Harac25a4f32014-11-16 11:30:27 +090091 t.Fatal(err)
Mikio Harad2e5a122012-09-26 21:03:09 +090092 }
93 for i, ifi := range ift {
Mikio Hara2c60c922014-10-08 06:54:30 +090094 if _, ok := nettest.IsMulticastCapable("ip4", &ifi); !ok {
Mikio Harad2e5a122012-09-26 21:03:09 +090095 continue
96 }
97 for _, p := range ps {
Mikio Hara6fb72752013-11-03 20:59:24 +090098 if err := p.JoinGroup(&ifi, gaddr); err != nil {
Mikio Harac25a4f32014-11-16 11:30:27 +090099 t.Fatal(err)
Mikio Harad2e5a122012-09-26 21:03:09 +0900100 }
101 }
102 mift = append(mift, &ift[i])
103 }
104 for _, ifi := range mift {
105 for _, p := range ps {
Mikio Hara6fb72752013-11-03 20:59:24 +0900106 if err := p.LeaveGroup(ifi, gaddr); err != nil {
Mikio Harac25a4f32014-11-16 11:30:27 +0900107 t.Fatal(err)
Mikio Harad2e5a122012-09-26 21:03:09 +0900108 }
109 }
110 }
111 }
112}
113
Mikio Hara6fb72752013-11-03 20:59:24 +0900114func TestUDPPerInterfaceSinglePacketConnWithSingleGroupListener(t *testing.T) {
115 switch runtime.GOOS {
Mikio Harad3b1e662014-10-30 14:15:42 +0900116 case "nacl", "plan9", "solaris", "windows":
Mikio Hara200fa7c2015-01-30 09:49:48 +0900117 t.Skipf("not supported on %s", runtime.GOOS)
Mikio Harad2e5a122012-09-26 21:03:09 +0900118 }
Mikio Harae5dac172014-10-07 06:35:16 +0900119 if testing.Short() {
Mikio Hara413497e2013-04-04 14:37:01 +0900120 t.Skip("to avoid external network")
Mikio Harad2e5a122012-09-26 21:03:09 +0900121 }
122
Mikio Hara6fb72752013-11-03 20:59:24 +0900123 gaddr := net.IPAddr{IP: net.IPv4(224, 0, 0, 254)} // see RFC 4727
Mikio Harad2e5a122012-09-26 21:03:09 +0900124 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 Harac25a4f32014-11-16 11:30:27 +0900132 t.Fatal(err)
Mikio Harad2e5a122012-09-26 21:03:09 +0900133 }
134 for i, ifi := range ift {
Mikio Hara2c60c922014-10-08 06:54:30 +0900135 ip, ok := nettest.IsMulticastCapable("ip4", &ifi)
Mikio Harad2e5a122012-09-26 21:03:09 +0900136 if !ok {
137 continue
138 }
Mikio Hara6fb72752013-11-03 20:59:24 +0900139 c, err := net.ListenPacket("udp4", ip.String()+":"+"1024") // unicast address with non-reusable port
Mikio Harad2e5a122012-09-26 21:03:09 +0900140 if err != nil {
Mikio Harac25a4f32014-11-16 11:30:27 +0900141 t.Fatal(err)
Mikio Harad2e5a122012-09-26 21:03:09 +0900142 }
143 defer c.Close()
144 p := ipv4.NewPacketConn(c)
Mikio Hara6fb72752013-11-03 20:59:24 +0900145 if err := p.JoinGroup(&ifi, &gaddr); err != nil {
Mikio Harac25a4f32014-11-16 11:30:27 +0900146 t.Fatal(err)
Mikio Harad2e5a122012-09-26 21:03:09 +0900147 }
148 mlt = append(mlt, &ml{p, &ift[i]})
149 }
150 for _, m := range mlt {
Mikio Hara6fb72752013-11-03 20:59:24 +0900151 if err := m.c.LeaveGroup(m.ifi, &gaddr); err != nil {
Mikio Harac25a4f32014-11-16 11:30:27 +0900152 t.Fatal(err)
Mikio Harad2e5a122012-09-26 21:03:09 +0900153 }
154 }
155}
156
Mikio Hara6fb72752013-11-03 20:59:24 +0900157func TestIPSingleRawConnWithSingleGroupListener(t *testing.T) {
158 switch runtime.GOOS {
Mikio Harad3b1e662014-10-30 14:15:42 +0900159 case "nacl", "plan9", "solaris", "windows":
Mikio Hara200fa7c2015-01-30 09:49:48 +0900160 t.Skipf("not supported on %s", runtime.GOOS)
Mikio Hara6fb72752013-11-03 20:59:24 +0900161 }
Mikio Harae5dac172014-10-07 06:35:16 +0900162 if testing.Short() {
Mikio Hara413497e2013-04-04 14:37:01 +0900163 t.Skip("to avoid external network")
Mikio Harad2e5a122012-09-26 21:03:09 +0900164 }
Mikio Hara99928be2015-01-28 12:12:10 +0900165 if m, ok := nettest.SupportsRawIPSocket(); !ok {
166 t.Skip(m)
Mikio Harad2e5a122012-09-26 21:03:09 +0900167 }
168
Mikio Hara6fb72752013-11-03 20:59:24 +0900169 c, err := net.ListenPacket("ip4:icmp", "0.0.0.0") // wildcard address
170 if err != nil {
Mikio Harac25a4f32014-11-16 11:30:27 +0900171 t.Fatal(err)
Mikio Hara6fb72752013-11-03 20:59:24 +0900172 }
173 defer c.Close()
174
175 r, err := ipv4.NewRawConn(c)
176 if err != nil {
Mikio Harac25a4f32014-11-16 11:30:27 +0900177 t.Fatal(err)
Mikio Hara6fb72752013-11-03 20:59:24 +0900178 }
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 Harac25a4f32014-11-16 11:30:27 +0900184 t.Fatal(err)
Mikio Hara6fb72752013-11-03 20:59:24 +0900185 }
186 for i, ifi := range ift {
Mikio Hara2c60c922014-10-08 06:54:30 +0900187 if _, ok := nettest.IsMulticastCapable("ip4", &ifi); !ok {
Mikio Hara6fb72752013-11-03 20:59:24 +0900188 continue
189 }
190 if err := r.JoinGroup(&ifi, &gaddr); err != nil {
Mikio Harac25a4f32014-11-16 11:30:27 +0900191 t.Fatal(err)
Mikio Hara6fb72752013-11-03 20:59:24 +0900192 }
193 mift = append(mift, &ift[i])
194 }
195 for _, ifi := range mift {
196 if err := r.LeaveGroup(ifi, &gaddr); err != nil {
Mikio Harac25a4f32014-11-16 11:30:27 +0900197 t.Fatal(err)
Mikio Hara6fb72752013-11-03 20:59:24 +0900198 }
199 }
200}
201
202func TestIPPerInterfaceSingleRawConnWithSingleGroupListener(t *testing.T) {
203 switch runtime.GOOS {
Mikio Harad3b1e662014-10-30 14:15:42 +0900204 case "nacl", "plan9", "solaris", "windows":
Mikio Hara200fa7c2015-01-30 09:49:48 +0900205 t.Skipf("not supported on %s", runtime.GOOS)
Mikio Hara6fb72752013-11-03 20:59:24 +0900206 }
Mikio Harae5dac172014-10-07 06:35:16 +0900207 if testing.Short() {
Mikio Hara6fb72752013-11-03 20:59:24 +0900208 t.Skip("to avoid external network")
209 }
Mikio Hara99928be2015-01-28 12:12:10 +0900210 if m, ok := nettest.SupportsRawIPSocket(); !ok {
211 t.Skip(m)
Mikio Hara6fb72752013-11-03 20:59:24 +0900212 }
213
214 gaddr := net.IPAddr{IP: net.IPv4(224, 0, 0, 254)} // see RFC 4727
Mikio Harad2e5a122012-09-26 21:03:09 +0900215 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 Harac25a4f32014-11-16 11:30:27 +0900223 t.Fatal(err)
Mikio Harad2e5a122012-09-26 21:03:09 +0900224 }
225 for i, ifi := range ift {
Mikio Hara2c60c922014-10-08 06:54:30 +0900226 ip, ok := nettest.IsMulticastCapable("ip4", &ifi)
Mikio Harad2e5a122012-09-26 21:03:09 +0900227 if !ok {
228 continue
229 }
Mikio Hara6fb72752013-11-03 20:59:24 +0900230 c, err := net.ListenPacket("ip4:253", ip.String()) // unicast address
Mikio Harad2e5a122012-09-26 21:03:09 +0900231 if err != nil {
Mikio Harac25a4f32014-11-16 11:30:27 +0900232 t.Fatal(err)
Mikio Harad2e5a122012-09-26 21:03:09 +0900233 }
234 defer c.Close()
235 r, err := ipv4.NewRawConn(c)
236 if err != nil {
Mikio Harac25a4f32014-11-16 11:30:27 +0900237 t.Fatal(err)
Mikio Harad2e5a122012-09-26 21:03:09 +0900238 }
Mikio Hara6fb72752013-11-03 20:59:24 +0900239 if err := r.JoinGroup(&ifi, &gaddr); err != nil {
Mikio Harac25a4f32014-11-16 11:30:27 +0900240 t.Fatal(err)
Mikio Harad2e5a122012-09-26 21:03:09 +0900241 }
242 mlt = append(mlt, &ml{r, &ift[i]})
243 }
244 for _, m := range mlt {
Mikio Hara6fb72752013-11-03 20:59:24 +0900245 if err := m.c.LeaveGroup(m.ifi, &gaddr); err != nil {
Mikio Harac25a4f32014-11-16 11:30:27 +0900246 t.Fatal(err)
Mikio Harad2e5a122012-09-26 21:03:09 +0900247 }
248 }
249}