blob: 0914513418812788549e0da0b4d53e6fa5865acf [file] [log] [blame]
Mikio Haracdfc4ce2013-06-04 17:42:58 +09001// 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
5package ipv6_test
6
7import (
8 "code.google.com/p/go.net/ipv6"
9 "net"
10 "os"
11 "reflect"
12 "runtime"
13 "sync"
14 "testing"
15)
16
Mikio Hara1a760202013-12-31 23:24:59 +090017var icmpStringTests = []struct {
18 in ipv6.ICMPType
19 out string
20}{
21 {ipv6.ICMPTypeDestinationUnreachable, "destination unreachable"},
22
23 {256, "<nil>"},
24}
25
26func TestICMPString(t *testing.T) {
27 for _, tt := range icmpStringTests {
28 s := tt.in.String()
29 if s != tt.out {
30 t.Errorf("got %s; expected %s", s, tt.out)
31 }
32 }
33}
34
Mikio Haracdfc4ce2013-06-04 17:42:58 +090035func TestICMPFilter(t *testing.T) {
36 switch runtime.GOOS {
Mikio Hara0cda7282014-01-23 06:36:22 +010037 case "dragonfly", "plan9", "solaris", "windows":
Mikio Haracdfc4ce2013-06-04 17:42:58 +090038 t.Skipf("not supported on %q", runtime.GOOS)
39 }
40
41 var f ipv6.ICMPFilter
42 for _, toggle := range []bool{false, true} {
43 f.SetAll(toggle)
44 var wg sync.WaitGroup
45 for _, typ := range []ipv6.ICMPType{
46 ipv6.ICMPTypeDestinationUnreachable,
47 ipv6.ICMPTypeEchoReply,
48 ipv6.ICMPTypeNeighborSolicitation,
49 ipv6.ICMPTypeDuplicateAddressConfirmation,
50 } {
51 wg.Add(1)
52 go func(typ ipv6.ICMPType) {
53 defer wg.Done()
54 f.Set(typ, false)
55 if f.WillBlock(typ) {
56 t.Errorf("ipv6.ICMPFilter.Set(%v, false) failed", typ)
57 }
58 f.Set(typ, true)
59 if !f.WillBlock(typ) {
60 t.Errorf("ipv6.ICMPFilter.Set(%v, true) failed", typ)
61 }
62 }(typ)
63 }
64 wg.Wait()
65 }
66}
67
68func TestSetICMPFilter(t *testing.T) {
69 switch runtime.GOOS {
Mikio Hara0cda7282014-01-23 06:36:22 +010070 case "dragonfly", "plan9", "solaris", "windows":
Mikio Haracdfc4ce2013-06-04 17:42:58 +090071 t.Skipf("not supported on %q", runtime.GOOS)
72 }
Mikio Haraa46af892013-06-06 17:12:18 +090073 if !supportsIPv6 {
74 t.Skip("ipv6 is not supported")
75 }
Mikio Haracdfc4ce2013-06-04 17:42:58 +090076 if os.Getuid() != 0 {
77 t.Skip("must be root")
78 }
79
80 c, err := net.ListenPacket("ip6:ipv6-icmp", "::1")
81 if err != nil {
82 t.Fatalf("net.ListenPacket failed: %v", err)
83 }
84 defer c.Close()
85
86 p := ipv6.NewPacketConn(c)
87
88 var f ipv6.ICMPFilter
89 f.SetAll(true)
90 f.Set(ipv6.ICMPTypeEchoRequest, false)
91 f.Set(ipv6.ICMPTypeEchoReply, false)
92 if err := p.SetICMPFilter(&f); err != nil {
93 t.Fatalf("ipv6.PacketConn.SetICMPFilter failed: %v", err)
94 }
95 kf, err := p.ICMPFilter()
96 if err != nil {
97 t.Fatalf("ipv6.PacketConn.ICMPFilter failed: %v", err)
98 }
99 if !reflect.DeepEqual(kf, &f) {
100 t.Fatalf("got unexpected filter %#v; expected %#v", kf, f)
101 }
102}