message_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. func TestMarshalAndParseMessage(t *testing.T) {
  15. fn := func(t *testing.T, proto int, tms []icmp.Message) {
  16. var pshs [][]byte
  17. switch proto {
  18. case iana.ProtocolICMP:
  19. pshs = [][]byte{nil}
  20. case iana.ProtocolIPv6ICMP:
  21. pshs = [][]byte{
  22. icmp.IPv6PseudoHeader(net.ParseIP("fe80::1"), net.ParseIP("ff02::1")),
  23. nil,
  24. }
  25. }
  26. for i, tm := range tms {
  27. for _, psh := range pshs {
  28. b, err := tm.Marshal(psh)
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. m, err := icmp.ParseMessage(proto, b)
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. if m.Type != tm.Type || m.Code != tm.Code {
  37. t.Errorf("#%d: got %#v; want %#v", i, m, &tm)
  38. }
  39. if !reflect.DeepEqual(m.Body, tm.Body) {
  40. t.Errorf("#%d: got %#v; want %#v", i, m.Body, tm.Body)
  41. }
  42. }
  43. }
  44. }
  45. t.Run("IPv4", func(t *testing.T) {
  46. fn(t, iana.ProtocolICMP,
  47. []icmp.Message{
  48. {
  49. Type: ipv4.ICMPTypeDestinationUnreachable, Code: 15,
  50. Body: &icmp.DstUnreach{
  51. Data: []byte("ERROR-INVOKING-PACKET"),
  52. },
  53. },
  54. {
  55. Type: ipv4.ICMPTypeTimeExceeded, Code: 1,
  56. Body: &icmp.TimeExceeded{
  57. Data: []byte("ERROR-INVOKING-PACKET"),
  58. },
  59. },
  60. {
  61. Type: ipv4.ICMPTypeParameterProblem, Code: 2,
  62. Body: &icmp.ParamProb{
  63. Pointer: 8,
  64. Data: []byte("ERROR-INVOKING-PACKET"),
  65. },
  66. },
  67. {
  68. Type: ipv4.ICMPTypeEcho, Code: 0,
  69. Body: &icmp.Echo{
  70. ID: 1, Seq: 2,
  71. Data: []byte("HELLO-R-U-THERE"),
  72. },
  73. },
  74. {
  75. Type: ipv4.ICMPTypeExtendedEchoRequest, Code: 0,
  76. Body: &icmp.ExtendedEchoRequest{
  77. ID: 1, Seq: 2,
  78. },
  79. },
  80. {
  81. Type: ipv4.ICMPTypeExtendedEchoReply, Code: 0,
  82. Body: &icmp.ExtendedEchoReply{
  83. State: 4 /* Delay */, Active: true, IPv4: true,
  84. },
  85. },
  86. {
  87. Type: ipv4.ICMPTypePhoturis,
  88. Body: &icmp.DefaultMessageBody{
  89. Data: []byte{0x80, 0x40, 0x20, 0x10},
  90. },
  91. },
  92. })
  93. })
  94. t.Run("IPv6", func(t *testing.T) {
  95. fn(t, iana.ProtocolIPv6ICMP,
  96. []icmp.Message{
  97. {
  98. Type: ipv6.ICMPTypeDestinationUnreachable, Code: 6,
  99. Body: &icmp.DstUnreach{
  100. Data: []byte("ERROR-INVOKING-PACKET"),
  101. },
  102. },
  103. {
  104. Type: ipv6.ICMPTypePacketTooBig, Code: 0,
  105. Body: &icmp.PacketTooBig{
  106. MTU: 1<<16 - 1,
  107. Data: []byte("ERROR-INVOKING-PACKET"),
  108. },
  109. },
  110. {
  111. Type: ipv6.ICMPTypeTimeExceeded, Code: 1,
  112. Body: &icmp.TimeExceeded{
  113. Data: []byte("ERROR-INVOKING-PACKET"),
  114. },
  115. },
  116. {
  117. Type: ipv6.ICMPTypeParameterProblem, Code: 2,
  118. Body: &icmp.ParamProb{
  119. Pointer: 8,
  120. Data: []byte("ERROR-INVOKING-PACKET"),
  121. },
  122. },
  123. {
  124. Type: ipv6.ICMPTypeEchoRequest, Code: 0,
  125. Body: &icmp.Echo{
  126. ID: 1, Seq: 2,
  127. Data: []byte("HELLO-R-U-THERE"),
  128. },
  129. },
  130. {
  131. Type: ipv6.ICMPTypeExtendedEchoRequest, Code: 0,
  132. Body: &icmp.ExtendedEchoRequest{
  133. ID: 1, Seq: 2,
  134. },
  135. },
  136. {
  137. Type: ipv6.ICMPTypeExtendedEchoReply, Code: 0,
  138. Body: &icmp.ExtendedEchoReply{
  139. State: 5 /* Probe */, Active: true, IPv6: true,
  140. },
  141. },
  142. {
  143. Type: ipv6.ICMPTypeDuplicateAddressConfirmation,
  144. Body: &icmp.DefaultMessageBody{
  145. Data: []byte{0x80, 0x40, 0x20, 0x10},
  146. },
  147. },
  148. })
  149. })
  150. }