blob: 3324b54df63c09bfe07342ad37717a9e0df18ef6 [file] [log] [blame]
Mikio Harae92559f2014-12-04 17:31:40 +09001// Copyright 2014 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 ipv4_test
6
7import (
8 "net"
Mikio Harae92559f2014-12-04 17:31:40 +09009 "reflect"
10 "runtime"
11 "testing"
12
Mikio Hara99928be2015-01-28 12:12:10 +090013 "golang.org/x/net/internal/nettest"
Mikio Harae92559f2014-12-04 17:31:40 +090014 "golang.org/x/net/ipv4"
15)
16
17var icmpStringTests = []struct {
18 in ipv4.ICMPType
19 out string
20}{
21 {ipv4.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; want %s", s, tt.out)
31 }
32 }
33}
34
35func TestICMPFilter(t *testing.T) {
36 switch runtime.GOOS {
37 case "linux":
38 default:
Mikio Hara200fa7c2015-01-30 09:49:48 +090039 t.Skipf("not supported on %s", runtime.GOOS)
Mikio Harae92559f2014-12-04 17:31:40 +090040 }
41
42 var f ipv4.ICMPFilter
43 for _, toggle := range []bool{false, true} {
44 f.SetAll(toggle)
45 for _, typ := range []ipv4.ICMPType{
46 ipv4.ICMPTypeDestinationUnreachable,
47 ipv4.ICMPTypeEchoReply,
48 ipv4.ICMPTypeTimeExceeded,
49 ipv4.ICMPTypeParameterProblem,
50 } {
51 f.Accept(typ)
52 if f.WillBlock(typ) {
53 t.Errorf("ipv4.ICMPFilter.Set(%v, false) failed", typ)
54 }
55 f.Block(typ)
56 if !f.WillBlock(typ) {
57 t.Errorf("ipv4.ICMPFilter.Set(%v, true) failed", typ)
58 }
59 }
60 }
61}
62
63func TestSetICMPFilter(t *testing.T) {
64 switch runtime.GOOS {
65 case "linux":
66 default:
Mikio Hara200fa7c2015-01-30 09:49:48 +090067 t.Skipf("not supported on %s", runtime.GOOS)
Mikio Harae92559f2014-12-04 17:31:40 +090068 }
Mikio Hara99928be2015-01-28 12:12:10 +090069 if m, ok := nettest.SupportsRawIPSocket(); !ok {
70 t.Skip(m)
Mikio Harae92559f2014-12-04 17:31:40 +090071 }
72
73 c, err := net.ListenPacket("ip4:icmp", "127.0.0.1")
74 if err != nil {
75 t.Fatal(err)
76 }
77 defer c.Close()
78
79 p := ipv4.NewPacketConn(c)
80
81 var f ipv4.ICMPFilter
82 f.SetAll(true)
83 f.Accept(ipv4.ICMPTypeEcho)
84 f.Accept(ipv4.ICMPTypeEchoReply)
85 if err := p.SetICMPFilter(&f); err != nil {
86 t.Fatal(err)
87 }
88 kf, err := p.ICMPFilter()
89 if err != nil {
90 t.Fatal(err)
91 }
92 if !reflect.DeepEqual(kf, &f) {
93 t.Fatalf("got %#v; want %#v", kf, f)
94 }
95}