OSDN Git Service

new repo
[bytom/vapor.git] / vendor / golang.org / x / net / ipv4 / icmp_test.go
1 // 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
5 package ipv4_test
6
7 import (
8         "net"
9         "reflect"
10         "runtime"
11         "testing"
12
13         "golang.org/x/net/internal/nettest"
14         "golang.org/x/net/ipv4"
15 )
16
17 var icmpStringTests = []struct {
18         in  ipv4.ICMPType
19         out string
20 }{
21         {ipv4.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 "linux":
38         default:
39                 t.Skipf("not supported on %s", runtime.GOOS)
40         }
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
63 func TestSetICMPFilter(t *testing.T) {
64         switch runtime.GOOS {
65         case "linux":
66         default:
67                 t.Skipf("not supported on %s", runtime.GOOS)
68         }
69         if m, ok := nettest.SupportsRawIPSocket(); !ok {
70                 t.Skip(m)
71         }
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 }