github.com/Andyfoo/golang/x/net@v0.0.0-20190901054642-57c1bf301704/ipv6/sockopt_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  	"fmt"
     9  	"net"
    10  	"runtime"
    11  	"testing"
    12  
    13  	"github.com/Andyfoo/golang/x/net/internal/iana"
    14  	"github.com/Andyfoo/golang/x/net/ipv6"
    15  	"github.com/Andyfoo/golang/x/net/nettest"
    16  )
    17  
    18  func TestConnInitiatorPathMTU(t *testing.T) {
    19  	switch runtime.GOOS {
    20  	case "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
    21  		t.Skipf("not supported on %s", runtime.GOOS)
    22  	}
    23  	if !nettest.SupportsIPv6() {
    24  		t.Skip("ipv6 is not supported")
    25  	}
    26  
    27  	ln, err := net.Listen("tcp6", "[::1]:0")
    28  	if err != nil {
    29  		t.Fatal(err)
    30  	}
    31  	defer ln.Close()
    32  
    33  	done := make(chan bool)
    34  	go acceptor(t, ln, done)
    35  
    36  	c, err := net.Dial("tcp6", ln.Addr().String())
    37  	if err != nil {
    38  		t.Fatal(err)
    39  	}
    40  	defer c.Close()
    41  
    42  	if pmtu, err := ipv6.NewConn(c).PathMTU(); err != nil {
    43  		switch runtime.GOOS {
    44  		case "darwin": // older darwin kernels don't support IPV6_PATHMTU option
    45  			t.Logf("not supported on %s", runtime.GOOS)
    46  		default:
    47  			t.Fatal(err)
    48  		}
    49  	} else {
    50  		t.Logf("path mtu for %v: %v", c.RemoteAddr(), pmtu)
    51  	}
    52  
    53  	<-done
    54  }
    55  
    56  func TestConnResponderPathMTU(t *testing.T) {
    57  	switch runtime.GOOS {
    58  	case "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
    59  		t.Skipf("not supported on %s", runtime.GOOS)
    60  	}
    61  	if !nettest.SupportsIPv6() {
    62  		t.Skip("ipv6 is not supported")
    63  	}
    64  
    65  	ln, err := net.Listen("tcp6", "[::1]:0")
    66  	if err != nil {
    67  		t.Fatal(err)
    68  	}
    69  	defer ln.Close()
    70  
    71  	done := make(chan bool)
    72  	go connector(t, "tcp6", ln.Addr().String(), done)
    73  
    74  	c, err := ln.Accept()
    75  	if err != nil {
    76  		t.Fatal(err)
    77  	}
    78  	defer c.Close()
    79  
    80  	if pmtu, err := ipv6.NewConn(c).PathMTU(); err != nil {
    81  		switch runtime.GOOS {
    82  		case "darwin": // older darwin kernels don't support IPV6_PATHMTU option
    83  			t.Logf("not supported on %s", runtime.GOOS)
    84  		default:
    85  			t.Fatal(err)
    86  		}
    87  	} else {
    88  		t.Logf("path mtu for %v: %v", c.RemoteAddr(), pmtu)
    89  	}
    90  
    91  	<-done
    92  }
    93  
    94  func TestPacketConnChecksum(t *testing.T) {
    95  	switch runtime.GOOS {
    96  	case "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
    97  		t.Skipf("not supported on %s", runtime.GOOS)
    98  	}
    99  	if !nettest.SupportsIPv6() {
   100  		t.Skip("ipv6 is not supported")
   101  	}
   102  	if !nettest.SupportsRawSocket() {
   103  		t.Skipf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
   104  	}
   105  
   106  	c, err := net.ListenPacket(fmt.Sprintf("ip6:%d", iana.ProtocolOSPFIGP), "::") // OSPF for IPv6
   107  	if err != nil {
   108  		t.Fatal(err)
   109  	}
   110  	defer c.Close()
   111  
   112  	p := ipv6.NewPacketConn(c)
   113  	offset := 12 // see RFC 5340
   114  
   115  	for _, toggle := range []bool{false, true} {
   116  		if err := p.SetChecksum(toggle, offset); err != nil {
   117  			if toggle {
   118  				t.Fatalf("ipv6.PacketConn.SetChecksum(%v, %v) failed: %v", toggle, offset, err)
   119  			} else {
   120  				// Some platforms never allow to disable the kernel
   121  				// checksum processing.
   122  				t.Logf("ipv6.PacketConn.SetChecksum(%v, %v) failed: %v", toggle, offset, err)
   123  			}
   124  		}
   125  		if on, offset, err := p.Checksum(); err != nil {
   126  			t.Fatal(err)
   127  		} else {
   128  			t.Logf("kernel checksum processing enabled=%v, offset=%v", on, offset)
   129  		}
   130  	}
   131  }