~ubuntu-branches/ubuntu/vivid/juju-core/vivid-proposed

« back to all changes in this revision

Viewing changes to src/code.google.com/p/go.net/ipv6/icmp_test.go

  • Committer: Package Import Robot
  • Author(s): Curtis C. Hovey
  • Date: 2015-09-29 19:43:29 UTC
  • mfrom: (47.1.4 wily-proposed)
  • Revision ID: package-import@ubuntu.com-20150929194329-9y496tbic30hc7vp
Tags: 1.24.6-0ubuntu1~15.04.1
Backport of 1.24.6 from wily. (LP: #1500916, #1497087)

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
        "code.google.com/p/go.net/ipv6"
9
 
        "net"
10
 
        "os"
11
 
        "reflect"
12
 
        "runtime"
13
 
        "sync"
14
 
        "testing"
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; expected %s", s, tt.out)
31
 
                }
32
 
        }
33
 
}
34
 
 
35
 
func TestICMPFilter(t *testing.T) {
36
 
        switch runtime.GOOS {
37
 
        case "dragonfly", "plan9", "solaris", "windows":
38
 
                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
 
 
68
 
func TestSetICMPFilter(t *testing.T) {
69
 
        switch runtime.GOOS {
70
 
        case "dragonfly", "plan9", "solaris", "windows":
71
 
                t.Skipf("not supported on %q", runtime.GOOS)
72
 
        }
73
 
        if !supportsIPv6 {
74
 
                t.Skip("ipv6 is not supported")
75
 
        }
76
 
        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
 
}