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