icmp_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. func TestICMPFilter(t *testing.T) {
  15. switch runtime.GOOS {
  16. case "plan9", "windows":
  17. t.Skipf("not supported on %q", runtime.GOOS)
  18. }
  19. var f ipv6.ICMPFilter
  20. for _, toggle := range []bool{false, true} {
  21. f.SetAll(toggle)
  22. var wg sync.WaitGroup
  23. for _, typ := range []ipv6.ICMPType{
  24. ipv6.ICMPTypeDestinationUnreachable,
  25. ipv6.ICMPTypeEchoReply,
  26. ipv6.ICMPTypeNeighborSolicitation,
  27. ipv6.ICMPTypeDuplicateAddressConfirmation,
  28. } {
  29. wg.Add(1)
  30. go func(typ ipv6.ICMPType) {
  31. defer wg.Done()
  32. f.Set(typ, false)
  33. if f.WillBlock(typ) {
  34. t.Errorf("ipv6.ICMPFilter.Set(%v, false) failed", typ)
  35. }
  36. f.Set(typ, true)
  37. if !f.WillBlock(typ) {
  38. t.Errorf("ipv6.ICMPFilter.Set(%v, true) failed", typ)
  39. }
  40. }(typ)
  41. }
  42. wg.Wait()
  43. }
  44. }
  45. func TestSetICMPFilter(t *testing.T) {
  46. switch runtime.GOOS {
  47. case "plan9", "windows":
  48. t.Skipf("not supported on %q", runtime.GOOS)
  49. }
  50. if !supportsIPv6 {
  51. t.Skip("ipv6 is not supported")
  52. }
  53. if os.Getuid() != 0 {
  54. t.Skip("must be root")
  55. }
  56. c, err := net.ListenPacket("ip6:ipv6-icmp", "::1")
  57. if err != nil {
  58. t.Fatalf("net.ListenPacket failed: %v", err)
  59. }
  60. defer c.Close()
  61. p := ipv6.NewPacketConn(c)
  62. var f ipv6.ICMPFilter
  63. f.SetAll(true)
  64. f.Set(ipv6.ICMPTypeEchoRequest, false)
  65. f.Set(ipv6.ICMPTypeEchoReply, false)
  66. if err := p.SetICMPFilter(&f); err != nil {
  67. t.Fatalf("ipv6.PacketConn.SetICMPFilter failed: %v", err)
  68. }
  69. kf, err := p.ICMPFilter()
  70. if err != nil {
  71. t.Fatalf("ipv6.PacketConn.ICMPFilter failed: %v", err)
  72. }
  73. if !reflect.DeepEqual(kf, &f) {
  74. t.Fatalf("got unexpected filter %#v; expected %#v", kf, f)
  75. }
  76. }