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.
 
 
 

135 lines
2.9 KiB

  1. // Copyright 2014 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 icmp_test
  5. import (
  6. "net"
  7. "reflect"
  8. "testing"
  9. "golang.org/x/net/icmp"
  10. "golang.org/x/net/internal/iana"
  11. "golang.org/x/net/ipv4"
  12. "golang.org/x/net/ipv6"
  13. )
  14. var marshalAndParseMessageForIPv4Tests = []icmp.Message{
  15. {
  16. Type: ipv4.ICMPTypeDestinationUnreachable, Code: 15,
  17. Body: &icmp.DstUnreach{
  18. Data: []byte("ERROR-INVOKING-PACKET"),
  19. },
  20. },
  21. {
  22. Type: ipv4.ICMPTypeTimeExceeded, Code: 1,
  23. Body: &icmp.TimeExceeded{
  24. Data: []byte("ERROR-INVOKING-PACKET"),
  25. },
  26. },
  27. {
  28. Type: ipv4.ICMPTypeParameterProblem, Code: 2,
  29. Body: &icmp.ParamProb{
  30. Pointer: 8,
  31. Data: []byte("ERROR-INVOKING-PACKET"),
  32. },
  33. },
  34. {
  35. Type: ipv4.ICMPTypeEcho, Code: 0,
  36. Body: &icmp.Echo{
  37. ID: 1, Seq: 2,
  38. Data: []byte("HELLO-R-U-THERE"),
  39. },
  40. },
  41. {
  42. Type: ipv4.ICMPTypePhoturis,
  43. Body: &icmp.DefaultMessageBody{
  44. Data: []byte{0x80, 0x40, 0x20, 0x10},
  45. },
  46. },
  47. }
  48. func TestMarshalAndParseMessageForIPv4(t *testing.T) {
  49. for i, tt := range marshalAndParseMessageForIPv4Tests {
  50. b, err := tt.Marshal(nil)
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. m, err := icmp.ParseMessage(iana.ProtocolICMP, b)
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. if m.Type != tt.Type || m.Code != tt.Code {
  59. t.Errorf("#%v: got %v; want %v", i, m, &tt)
  60. }
  61. if !reflect.DeepEqual(m.Body, tt.Body) {
  62. t.Errorf("#%v: got %v; want %v", i, m.Body, tt.Body)
  63. }
  64. }
  65. }
  66. var marshalAndParseMessageForIPv6Tests = []icmp.Message{
  67. {
  68. Type: ipv6.ICMPTypeDestinationUnreachable, Code: 6,
  69. Body: &icmp.DstUnreach{
  70. Data: []byte("ERROR-INVOKING-PACKET"),
  71. },
  72. },
  73. {
  74. Type: ipv6.ICMPTypePacketTooBig, Code: 0,
  75. Body: &icmp.PacketTooBig{
  76. MTU: 1<<16 - 1,
  77. Data: []byte("ERROR-INVOKING-PACKET"),
  78. },
  79. },
  80. {
  81. Type: ipv6.ICMPTypeTimeExceeded, Code: 1,
  82. Body: &icmp.TimeExceeded{
  83. Data: []byte("ERROR-INVOKING-PACKET"),
  84. },
  85. },
  86. {
  87. Type: ipv6.ICMPTypeParameterProblem, Code: 2,
  88. Body: &icmp.ParamProb{
  89. Pointer: 8,
  90. Data: []byte("ERROR-INVOKING-PACKET"),
  91. },
  92. },
  93. {
  94. Type: ipv6.ICMPTypeEchoRequest, Code: 0,
  95. Body: &icmp.Echo{
  96. ID: 1, Seq: 2,
  97. Data: []byte("HELLO-R-U-THERE"),
  98. },
  99. },
  100. {
  101. Type: ipv6.ICMPTypeDuplicateAddressConfirmation,
  102. Body: &icmp.DefaultMessageBody{
  103. Data: []byte{0x80, 0x40, 0x20, 0x10},
  104. },
  105. },
  106. }
  107. func TestMarshalAndParseMessageForIPv6(t *testing.T) {
  108. pshicmp := icmp.IPv6PseudoHeader(net.ParseIP("fe80::1"), net.ParseIP("ff02::1"))
  109. for i, tt := range marshalAndParseMessageForIPv6Tests {
  110. for _, psh := range [][]byte{pshicmp, nil} {
  111. b, err := tt.Marshal(psh)
  112. if err != nil {
  113. t.Fatal(err)
  114. }
  115. m, err := icmp.ParseMessage(iana.ProtocolIPv6ICMP, b)
  116. if err != nil {
  117. t.Fatal(err)
  118. }
  119. if m.Type != tt.Type || m.Code != tt.Code {
  120. t.Errorf("#%v: got %v; want %v", i, m, &tt)
  121. }
  122. if !reflect.DeepEqual(m.Body, tt.Body) {
  123. t.Errorf("#%v: got %v; want %v", i, m.Body, tt.Body)
  124. }
  125. }
  126. }
  127. }