message_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.ICMPTypePhoturis,
  76. Body: &icmp.DefaultMessageBody{
  77. Data: []byte{0x80, 0x40, 0x20, 0x10},
  78. },
  79. },
  80. })
  81. })
  82. t.Run("IPv6", func(t *testing.T) {
  83. fn(t, iana.ProtocolIPv6ICMP,
  84. []icmp.Message{
  85. {
  86. Type: ipv6.ICMPTypeDestinationUnreachable, Code: 6,
  87. Body: &icmp.DstUnreach{
  88. Data: []byte("ERROR-INVOKING-PACKET"),
  89. },
  90. },
  91. {
  92. Type: ipv6.ICMPTypePacketTooBig, Code: 0,
  93. Body: &icmp.PacketTooBig{
  94. MTU: 1<<16 - 1,
  95. Data: []byte("ERROR-INVOKING-PACKET"),
  96. },
  97. },
  98. {
  99. Type: ipv6.ICMPTypeTimeExceeded, Code: 1,
  100. Body: &icmp.TimeExceeded{
  101. Data: []byte("ERROR-INVOKING-PACKET"),
  102. },
  103. },
  104. {
  105. Type: ipv6.ICMPTypeParameterProblem, Code: 2,
  106. Body: &icmp.ParamProb{
  107. Pointer: 8,
  108. Data: []byte("ERROR-INVOKING-PACKET"),
  109. },
  110. },
  111. {
  112. Type: ipv6.ICMPTypeEchoRequest, Code: 0,
  113. Body: &icmp.Echo{
  114. ID: 1, Seq: 2,
  115. Data: []byte("HELLO-R-U-THERE"),
  116. },
  117. },
  118. {
  119. Type: ipv6.ICMPTypeDuplicateAddressConfirmation,
  120. Body: &icmp.DefaultMessageBody{
  121. Data: []byte{0x80, 0x40, 0x20, 0x10},
  122. },
  123. },
  124. })
  125. })
  126. }