icmp_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. "net"
  7. "reflect"
  8. "runtime"
  9. "testing"
  10. "golang.org/x/net/ipv6"
  11. "golang.org/x/net/nettest"
  12. )
  13. var icmpStringTests = []struct {
  14. in ipv6.ICMPType
  15. out string
  16. }{
  17. {ipv6.ICMPTypeDestinationUnreachable, "destination unreachable"},
  18. {256, "<nil>"},
  19. }
  20. func TestICMPString(t *testing.T) {
  21. for _, tt := range icmpStringTests {
  22. s := tt.in.String()
  23. if s != tt.out {
  24. t.Errorf("got %s; want %s", s, tt.out)
  25. }
  26. }
  27. }
  28. func TestICMPFilter(t *testing.T) {
  29. switch runtime.GOOS {
  30. case "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
  31. t.Skipf("not supported on %s", runtime.GOOS)
  32. }
  33. var f ipv6.ICMPFilter
  34. for _, toggle := range []bool{false, true} {
  35. f.SetAll(toggle)
  36. for _, typ := range []ipv6.ICMPType{
  37. ipv6.ICMPTypeDestinationUnreachable,
  38. ipv6.ICMPTypeEchoReply,
  39. ipv6.ICMPTypeNeighborSolicitation,
  40. ipv6.ICMPTypeDuplicateAddressConfirmation,
  41. } {
  42. f.Accept(typ)
  43. if f.WillBlock(typ) {
  44. t.Errorf("ipv6.ICMPFilter.Set(%v, false) failed", typ)
  45. }
  46. f.Block(typ)
  47. if !f.WillBlock(typ) {
  48. t.Errorf("ipv6.ICMPFilter.Set(%v, true) failed", typ)
  49. }
  50. }
  51. }
  52. }
  53. func TestSetICMPFilter(t *testing.T) {
  54. switch runtime.GOOS {
  55. case "fuchsia", "hurd", "js", "nacl", "plan9", "windows":
  56. t.Skipf("not supported on %s", runtime.GOOS)
  57. }
  58. if !nettest.SupportsIPv6() {
  59. t.Skip("ipv6 is not supported")
  60. }
  61. if !nettest.SupportsRawSocket() {
  62. t.Skipf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
  63. }
  64. c, err := net.ListenPacket("ip6:ipv6-icmp", "::1")
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. defer c.Close()
  69. p := ipv6.NewPacketConn(c)
  70. var f ipv6.ICMPFilter
  71. f.SetAll(true)
  72. f.Accept(ipv6.ICMPTypeEchoRequest)
  73. f.Accept(ipv6.ICMPTypeEchoReply)
  74. if err := p.SetICMPFilter(&f); err != nil {
  75. t.Fatal(err)
  76. }
  77. kf, err := p.ICMPFilter()
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. if !reflect.DeepEqual(kf, &f) {
  82. t.Fatalf("got %#v; want %#v", kf, f)
  83. }
  84. }