multipart_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // Copyright 2015 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. "fmt"
  7. "net"
  8. "reflect"
  9. "testing"
  10. "golang.org/x/net/icmp"
  11. "golang.org/x/net/internal/iana"
  12. "golang.org/x/net/ipv4"
  13. "golang.org/x/net/ipv6"
  14. )
  15. var marshalAndParseMultipartMessageForIPv4Tests = []icmp.Message{
  16. {
  17. Type: ipv4.ICMPTypeDestinationUnreachable, Code: 15,
  18. Body: &icmp.DstUnreach{
  19. Data: []byte("ERROR-INVOKING-PACKET"),
  20. Extensions: []icmp.Extension{
  21. &icmp.MPLSLabelStack{
  22. Class: 1,
  23. Type: 1,
  24. Labels: []icmp.MPLSLabel{
  25. {
  26. Label: 16014,
  27. TC: 0x4,
  28. S: true,
  29. TTL: 255,
  30. },
  31. },
  32. },
  33. },
  34. },
  35. },
  36. {
  37. Type: ipv4.ICMPTypeTimeExceeded, Code: 1,
  38. Body: &icmp.TimeExceeded{
  39. Data: []byte("ERROR-INVOKING-PACKET"),
  40. Extensions: []icmp.Extension{
  41. &icmp.MPLSLabelStack{
  42. Class: 1,
  43. Type: 1,
  44. Labels: []icmp.MPLSLabel{
  45. {
  46. Label: 16014,
  47. TC: 0x4,
  48. S: true,
  49. TTL: 255,
  50. },
  51. },
  52. },
  53. },
  54. },
  55. },
  56. {
  57. Type: ipv4.ICMPTypeParameterProblem, Code: 2,
  58. Body: &icmp.ParamProb{
  59. Pointer: 8,
  60. Data: []byte("ERROR-INVOKING-PACKET"),
  61. Extensions: []icmp.Extension{
  62. &icmp.MPLSLabelStack{
  63. Class: 1,
  64. Type: 1,
  65. Labels: []icmp.MPLSLabel{
  66. {
  67. Label: 16014,
  68. TC: 0x4,
  69. S: true,
  70. TTL: 255,
  71. },
  72. },
  73. },
  74. },
  75. },
  76. },
  77. }
  78. func TestMarshalAndParseMultipartMessageForIPv4(t *testing.T) {
  79. for i, tt := range marshalAndParseMultipartMessageForIPv4Tests {
  80. b, err := tt.Marshal(nil)
  81. if err != nil {
  82. t.Fatal(err)
  83. }
  84. if b[5] != 32 {
  85. t.Errorf("#%v: got %v; want 32", i, b[5])
  86. }
  87. m, err := icmp.ParseMessage(iana.ProtocolICMP, b)
  88. if err != nil {
  89. t.Fatal(err)
  90. }
  91. if m.Type != tt.Type || m.Code != tt.Code {
  92. t.Errorf("#%v: got %v; want %v", i, m, &tt)
  93. }
  94. switch m.Type {
  95. case ipv4.ICMPTypeDestinationUnreachable:
  96. got, want := m.Body.(*icmp.DstUnreach), tt.Body.(*icmp.DstUnreach)
  97. if !reflect.DeepEqual(got.Extensions, want.Extensions) {
  98. t.Errorf("#%v: got %#v; want %#v", i, got.Extensions, want.Extensions)
  99. }
  100. if len(got.Data) != 128 {
  101. t.Errorf("#%v: got %v; want 128", i, len(got.Data))
  102. }
  103. case ipv4.ICMPTypeTimeExceeded:
  104. got, want := m.Body.(*icmp.TimeExceeded), tt.Body.(*icmp.TimeExceeded)
  105. if !reflect.DeepEqual(got.Extensions, want.Extensions) {
  106. t.Error(dumpExtensions(i, got.Extensions, want.Extensions))
  107. }
  108. if len(got.Data) != 128 {
  109. t.Errorf("#%v: got %v; want 128", i, len(got.Data))
  110. }
  111. case ipv4.ICMPTypeParameterProblem:
  112. got, want := m.Body.(*icmp.ParamProb), tt.Body.(*icmp.ParamProb)
  113. if !reflect.DeepEqual(got.Extensions, want.Extensions) {
  114. t.Error(dumpExtensions(i, got.Extensions, want.Extensions))
  115. }
  116. if len(got.Data) != 128 {
  117. t.Errorf("#%v: got %v; want 128", i, len(got.Data))
  118. }
  119. }
  120. }
  121. }
  122. var marshalAndParseMultipartMessageForIPv6Tests = []icmp.Message{
  123. {
  124. Type: ipv6.ICMPTypeDestinationUnreachable, Code: 6,
  125. Body: &icmp.DstUnreach{
  126. Data: []byte("ERROR-INVOKING-PACKET"),
  127. Extensions: []icmp.Extension{
  128. &icmp.MPLSLabelStack{
  129. Class: 1,
  130. Type: 1,
  131. Labels: []icmp.MPLSLabel{
  132. {
  133. Label: 16014,
  134. TC: 0x4,
  135. S: true,
  136. TTL: 255,
  137. },
  138. },
  139. },
  140. },
  141. },
  142. },
  143. {
  144. Type: ipv6.ICMPTypeTimeExceeded, Code: 1,
  145. Body: &icmp.TimeExceeded{
  146. Data: []byte("ERROR-INVOKING-PACKET"),
  147. Extensions: []icmp.Extension{
  148. &icmp.MPLSLabelStack{
  149. Class: 1,
  150. Type: 1,
  151. Labels: []icmp.MPLSLabel{
  152. {
  153. Label: 16014,
  154. TC: 0x4,
  155. S: true,
  156. TTL: 255,
  157. },
  158. },
  159. },
  160. },
  161. },
  162. },
  163. }
  164. func TestMarshalAndParseMultipartMessageForIPv6(t *testing.T) {
  165. pshicmp := icmp.IPv6PseudoHeader(net.ParseIP("fe80::1"), net.ParseIP("ff02::1"))
  166. for i, tt := range marshalAndParseMultipartMessageForIPv6Tests {
  167. for _, psh := range [][]byte{pshicmp, nil} {
  168. b, err := tt.Marshal(psh)
  169. if err != nil {
  170. t.Fatal(err)
  171. }
  172. if b[4] != 16 {
  173. t.Errorf("#%v: got %v; want 16", i, b[4])
  174. }
  175. m, err := icmp.ParseMessage(iana.ProtocolIPv6ICMP, b)
  176. if err != nil {
  177. t.Fatal(err)
  178. }
  179. if m.Type != tt.Type || m.Code != tt.Code {
  180. t.Errorf("#%v: got %v; want %v", i, m, &tt)
  181. }
  182. switch m.Type {
  183. case ipv6.ICMPTypeDestinationUnreachable:
  184. got, want := m.Body.(*icmp.DstUnreach), tt.Body.(*icmp.DstUnreach)
  185. if !reflect.DeepEqual(got.Extensions, want.Extensions) {
  186. t.Error(dumpExtensions(i, got.Extensions, want.Extensions))
  187. }
  188. if len(got.Data) != 128 {
  189. t.Errorf("#%v: got %v; want 128", i, len(got.Data))
  190. }
  191. case ipv6.ICMPTypeTimeExceeded:
  192. got, want := m.Body.(*icmp.TimeExceeded), tt.Body.(*icmp.TimeExceeded)
  193. if !reflect.DeepEqual(got.Extensions, want.Extensions) {
  194. t.Error(dumpExtensions(i, got.Extensions, want.Extensions))
  195. }
  196. if len(got.Data) != 128 {
  197. t.Errorf("#%v: got %v; want 128", i, len(got.Data))
  198. }
  199. }
  200. }
  201. }
  202. }
  203. func dumpExtensions(i int, gotExts, wantExts []icmp.Extension) string {
  204. var s string
  205. for j, got := range gotExts {
  206. switch got := got.(type) {
  207. case *icmp.MPLSLabelStack:
  208. want := wantExts[j].(*icmp.MPLSLabelStack)
  209. if !reflect.DeepEqual(got, want) {
  210. s += fmt.Sprintf("#%v/%v: got %#v; want %#v\n", i, j, got, want)
  211. }
  212. }
  213. }
  214. return s[:len(s)-1]
  215. }