You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
1.9 KiB

  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/internal/nettest"
  11. "golang.org/x/net/ipv6"
  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 "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 "nacl", "plan9", "windows":
  56. t.Skipf("not supported on %s", runtime.GOOS)
  57. }
  58. if !supportsIPv6 {
  59. t.Skip("ipv6 is not supported")
  60. }
  61. if m, ok := nettest.SupportsRawIPSocket(); !ok {
  62. t.Skip(m)
  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. }