github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/net/ipv6/unicastsockopt_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  	"net"
     9  	"runtime"
    10  	"testing"
    11  
    12  	"golang.org/x/net/internal/iana"
    13  	"golang.org/x/net/internal/nettest"
    14  	"golang.org/x/net/ipv6"
    15  )
    16  
    17  func TestConnUnicastSocketOptions(t *testing.T) {
    18  	switch runtime.GOOS {
    19  	case "nacl", "plan9", "solaris", "windows":
    20  		t.Skipf("not supported on %s", runtime.GOOS)
    21  	}
    22  	if !supportsIPv6 {
    23  		t.Skip("ipv6 is not supported")
    24  	}
    25  
    26  	ln, err := net.Listen("tcp6", "[::1]:0")
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  	defer ln.Close()
    31  
    32  	done := make(chan bool)
    33  	go acceptor(t, ln, done)
    34  
    35  	c, err := net.Dial("tcp6", ln.Addr().String())
    36  	if err != nil {
    37  		t.Fatal(err)
    38  	}
    39  	defer c.Close()
    40  
    41  	testUnicastSocketOptions(t, ipv6.NewConn(c))
    42  
    43  	<-done
    44  }
    45  
    46  var packetConnUnicastSocketOptionTests = []struct {
    47  	net, proto, addr string
    48  }{
    49  	{"udp6", "", "[::1]:0"},
    50  	{"ip6", ":ipv6-icmp", "::1"},
    51  }
    52  
    53  func TestPacketConnUnicastSocketOptions(t *testing.T) {
    54  	switch runtime.GOOS {
    55  	case "nacl", "plan9", "solaris", "windows":
    56  		t.Skipf("not supported on %s", runtime.GOOS)
    57  	}
    58  	if !supportsIPv6 {
    59  		t.Skip("ipv6 is not supported")
    60  	}
    61  
    62  	m, ok := nettest.SupportsRawIPSocket()
    63  	for _, tt := range packetConnUnicastSocketOptionTests {
    64  		if tt.net == "ip6" && !ok {
    65  			t.Log(m)
    66  			continue
    67  		}
    68  		c, err := net.ListenPacket(tt.net+tt.proto, tt.addr)
    69  		if err != nil {
    70  			t.Fatal(err)
    71  		}
    72  		defer c.Close()
    73  
    74  		testUnicastSocketOptions(t, ipv6.NewPacketConn(c))
    75  	}
    76  }
    77  
    78  type testIPv6UnicastConn interface {
    79  	TrafficClass() (int, error)
    80  	SetTrafficClass(int) error
    81  	HopLimit() (int, error)
    82  	SetHopLimit(int) error
    83  }
    84  
    85  func testUnicastSocketOptions(t *testing.T, c testIPv6UnicastConn) {
    86  	tclass := iana.DiffServCS0 | iana.NotECNTransport
    87  	if err := c.SetTrafficClass(tclass); err != nil {
    88  		switch runtime.GOOS {
    89  		case "darwin": // older darwin kernels don't support IPV6_TCLASS option
    90  			t.Logf("not supported on %s", runtime.GOOS)
    91  			goto next
    92  		}
    93  		t.Fatal(err)
    94  	}
    95  	if v, err := c.TrafficClass(); err != nil {
    96  		t.Fatal(err)
    97  	} else if v != tclass {
    98  		t.Fatalf("got %v; want %v", v, tclass)
    99  	}
   100  
   101  next:
   102  	hoplim := 255
   103  	if err := c.SetHopLimit(hoplim); err != nil {
   104  		t.Fatal(err)
   105  	}
   106  	if v, err := c.HopLimit(); err != nil {
   107  		t.Fatal(err)
   108  	} else if v != hoplim {
   109  		t.Fatalf("got %v; want %v", v, hoplim)
   110  	}
   111  }