1
0

message_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. }