icmp_test.go 2.2 KB

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