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