github.com/Andyfoo/golang/x/net@v0.0.0-20190901054642-57c1bf301704/ipv6/icmp_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  	"reflect"
    10  	"runtime"
    11  	"testing"
    12  
    13  	"github.com/Andyfoo/golang/x/net/ipv6"
    14  	"github.com/Andyfoo/golang/x/net/nettest"
    15  )
    16  
    17  var icmpStringTests = []struct {
    18  	in  ipv6.ICMPType
    19  	out string
    20  }{
    21  	{ipv6.ICMPTypeDestinationUnreachable, "destination unreachable"},
    22  
    23  	{256, "<nil>"},
    24  }
    25  
    26  func TestICMPString(t *testing.T) {
    27  	for _, tt := range icmpStringTests {
    28  		s := tt.in.String()
    29  		if s != tt.out {
    30  			t.Errorf("got %s; want %s", s, tt.out)
    31  		}
    32  	}
    33  }
    34  
    35  func TestICMPFilter(t *testing.T) {
    36  	switch runtime.GOOS {
    37  	case "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
    38  		t.Skipf("not supported on %s", runtime.GOOS)
    39  	}
    40  
    41  	var f ipv6.ICMPFilter
    42  	for _, toggle := range []bool{false, true} {
    43  		f.SetAll(toggle)
    44  		for _, typ := range []ipv6.ICMPType{
    45  			ipv6.ICMPTypeDestinationUnreachable,
    46  			ipv6.ICMPTypeEchoReply,
    47  			ipv6.ICMPTypeNeighborSolicitation,
    48  			ipv6.ICMPTypeDuplicateAddressConfirmation,
    49  		} {
    50  			f.Accept(typ)
    51  			if f.WillBlock(typ) {
    52  				t.Errorf("ipv6.ICMPFilter.Set(%v, false) failed", typ)
    53  			}
    54  			f.Block(typ)
    55  			if !f.WillBlock(typ) {
    56  				t.Errorf("ipv6.ICMPFilter.Set(%v, true) failed", typ)
    57  			}
    58  		}
    59  	}
    60  }
    61  
    62  func TestSetICMPFilter(t *testing.T) {
    63  	switch runtime.GOOS {
    64  	case "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
    65  		t.Skipf("not supported on %s", runtime.GOOS)
    66  	}
    67  	if !nettest.SupportsIPv6() {
    68  		t.Skip("ipv6 is not supported")
    69  	}
    70  	if !nettest.SupportsRawSocket() {
    71  		t.Skipf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
    72  	}
    73  
    74  	c, err := net.ListenPacket("ip6:ipv6-icmp", "::1")
    75  	if err != nil {
    76  		t.Fatal(err)
    77  	}
    78  	defer c.Close()
    79  
    80  	p := ipv6.NewPacketConn(c)
    81  
    82  	var f ipv6.ICMPFilter
    83  	f.SetAll(true)
    84  	f.Accept(ipv6.ICMPTypeEchoRequest)
    85  	f.Accept(ipv6.ICMPTypeEchoReply)
    86  	if err := p.SetICMPFilter(&f); err != nil {
    87  		t.Fatal(err)
    88  	}
    89  	kf, err := p.ICMPFilter()
    90  	if err != nil {
    91  		t.Fatal(err)
    92  	}
    93  	if !reflect.DeepEqual(kf, &f) {
    94  		t.Fatalf("got %#v; want %#v", kf, f)
    95  	}
    96  }