github.com/ader1990/go@v0.0.0-20140630135419-8c24447fa791/src/pkg/net/multicast_test.go (about)

     1  // Copyright 2011 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 net
     6  
     7  import (
     8  	"fmt"
     9  	"os"
    10  	"runtime"
    11  	"testing"
    12  )
    13  
    14  var ipv4MulticastListenerTests = []struct {
    15  	net   string
    16  	gaddr *UDPAddr // see RFC 4727
    17  }{
    18  	{"udp", &UDPAddr{IP: IPv4(224, 0, 0, 254), Port: 12345}},
    19  
    20  	{"udp4", &UDPAddr{IP: IPv4(224, 0, 0, 254), Port: 12345}},
    21  }
    22  
    23  // TestIPv4MulticastListener tests both single and double listen to a
    24  // test listener with same address family, same group address and same
    25  // port.
    26  func TestIPv4MulticastListener(t *testing.T) {
    27  	switch runtime.GOOS {
    28  	case "nacl", "plan9":
    29  		t.Skipf("skipping test on %q", runtime.GOOS)
    30  	case "solaris":
    31  		t.Skipf("skipping test on solaris, see issue 7399")
    32  	}
    33  
    34  	closer := func(cs []*UDPConn) {
    35  		for _, c := range cs {
    36  			if c != nil {
    37  				c.Close()
    38  			}
    39  		}
    40  	}
    41  
    42  	for _, ifi := range []*Interface{loopbackInterface(), nil} {
    43  		// Note that multicast interface assignment by system
    44  		// is not recommended because it usually relies on
    45  		// routing stuff for finding out an appropriate
    46  		// nexthop containing both network and link layer
    47  		// adjacencies.
    48  		if ifi == nil && !*testExternal {
    49  			continue
    50  		}
    51  		for _, tt := range ipv4MulticastListenerTests {
    52  			var err error
    53  			cs := make([]*UDPConn, 2)
    54  			if cs[0], err = ListenMulticastUDP(tt.net, ifi, tt.gaddr); err != nil {
    55  				t.Fatalf("First ListenMulticastUDP on %v failed: %v", ifi, err)
    56  			}
    57  			if err := checkMulticastListener(cs[0], tt.gaddr.IP); err != nil {
    58  				closer(cs)
    59  				t.Fatal(err)
    60  			}
    61  			if cs[1], err = ListenMulticastUDP(tt.net, ifi, tt.gaddr); err != nil {
    62  				closer(cs)
    63  				t.Fatalf("Second ListenMulticastUDP on %v failed: %v", ifi, err)
    64  			}
    65  			if err := checkMulticastListener(cs[1], tt.gaddr.IP); err != nil {
    66  				closer(cs)
    67  				t.Fatal(err)
    68  			}
    69  			closer(cs)
    70  		}
    71  	}
    72  }
    73  
    74  var ipv6MulticastListenerTests = []struct {
    75  	net   string
    76  	gaddr *UDPAddr // see RFC 4727
    77  }{
    78  	{"udp", &UDPAddr{IP: ParseIP("ff01::114"), Port: 12345}},
    79  	{"udp", &UDPAddr{IP: ParseIP("ff02::114"), Port: 12345}},
    80  	{"udp", &UDPAddr{IP: ParseIP("ff04::114"), Port: 12345}},
    81  	{"udp", &UDPAddr{IP: ParseIP("ff05::114"), Port: 12345}},
    82  	{"udp", &UDPAddr{IP: ParseIP("ff08::114"), Port: 12345}},
    83  	{"udp", &UDPAddr{IP: ParseIP("ff0e::114"), Port: 12345}},
    84  
    85  	{"udp6", &UDPAddr{IP: ParseIP("ff01::114"), Port: 12345}},
    86  	{"udp6", &UDPAddr{IP: ParseIP("ff02::114"), Port: 12345}},
    87  	{"udp6", &UDPAddr{IP: ParseIP("ff04::114"), Port: 12345}},
    88  	{"udp6", &UDPAddr{IP: ParseIP("ff05::114"), Port: 12345}},
    89  	{"udp6", &UDPAddr{IP: ParseIP("ff08::114"), Port: 12345}},
    90  	{"udp6", &UDPAddr{IP: ParseIP("ff0e::114"), Port: 12345}},
    91  }
    92  
    93  // TestIPv6MulticastListener tests both single and double listen to a
    94  // test listener with same address family, same group address and same
    95  // port.
    96  func TestIPv6MulticastListener(t *testing.T) {
    97  	switch runtime.GOOS {
    98  	case "plan9":
    99  		t.Skipf("skipping test on %q", runtime.GOOS)
   100  	case "solaris":
   101  		t.Skipf("skipping test on solaris, see issue 7399")
   102  	}
   103  	if !supportsIPv6 {
   104  		t.Skip("ipv6 is not supported")
   105  	}
   106  	if os.Getuid() != 0 {
   107  		t.Skip("skipping test; must be root")
   108  	}
   109  
   110  	closer := func(cs []*UDPConn) {
   111  		for _, c := range cs {
   112  			if c != nil {
   113  				c.Close()
   114  			}
   115  		}
   116  	}
   117  
   118  	for _, ifi := range []*Interface{loopbackInterface(), nil} {
   119  		// Note that multicast interface assignment by system
   120  		// is not recommended because it usually relies on
   121  		// routing stuff for finding out an appropriate
   122  		// nexthop containing both network and link layer
   123  		// adjacencies.
   124  		if ifi == nil && (!*testExternal || !*testIPv6) {
   125  			continue
   126  		}
   127  		for _, tt := range ipv6MulticastListenerTests {
   128  			var err error
   129  			cs := make([]*UDPConn, 2)
   130  			if cs[0], err = ListenMulticastUDP(tt.net, ifi, tt.gaddr); err != nil {
   131  				t.Fatalf("First ListenMulticastUDP on %v failed: %v", ifi, err)
   132  			}
   133  			if err := checkMulticastListener(cs[0], tt.gaddr.IP); err != nil {
   134  				closer(cs)
   135  				t.Fatal(err)
   136  			}
   137  			if cs[1], err = ListenMulticastUDP(tt.net, ifi, tt.gaddr); err != nil {
   138  				closer(cs)
   139  				t.Fatalf("Second ListenMulticastUDP on %v failed: %v", ifi, err)
   140  			}
   141  			if err := checkMulticastListener(cs[1], tt.gaddr.IP); err != nil {
   142  				closer(cs)
   143  				t.Fatal(err)
   144  			}
   145  			closer(cs)
   146  		}
   147  	}
   148  }
   149  
   150  func checkMulticastListener(c *UDPConn, ip IP) error {
   151  	if ok, err := multicastRIBContains(ip); err != nil {
   152  		return err
   153  	} else if !ok {
   154  		return fmt.Errorf("%q not found in multicast RIB", ip.String())
   155  	}
   156  	la := c.LocalAddr()
   157  	if la, ok := la.(*UDPAddr); !ok || la.Port == 0 {
   158  		return fmt.Errorf("got %v; expected a proper address with non-zero port number", la)
   159  	}
   160  	return nil
   161  }
   162  
   163  func multicastRIBContains(ip IP) (bool, error) {
   164  	switch runtime.GOOS {
   165  	case "dragonfly", "netbsd", "openbsd", "plan9", "solaris", "windows":
   166  		return true, nil // not implemented yet
   167  	case "linux":
   168  		if runtime.GOARCH == "arm" || runtime.GOARCH == "alpha" {
   169  			return true, nil // not implemented yet
   170  		}
   171  	}
   172  	ift, err := Interfaces()
   173  	if err != nil {
   174  		return false, err
   175  	}
   176  	for _, ifi := range ift {
   177  		ifmat, err := ifi.MulticastAddrs()
   178  		if err != nil {
   179  			return false, err
   180  		}
   181  		for _, ifma := range ifmat {
   182  			if ifma.(*IPAddr).IP.Equal(ip) {
   183  				return true, nil
   184  			}
   185  		}
   186  	}
   187  	return false, nil
   188  }