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.

442 lines
10 KiB

  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. &icmp.InterfaceInfo{
  34. Class: 2,
  35. Type: 0x0f,
  36. Interface: &net.Interface{
  37. Index: 15,
  38. Name: "en101",
  39. MTU: 8192,
  40. },
  41. Addr: &net.IPAddr{
  42. IP: net.IPv4(192, 168, 0, 1).To4(),
  43. },
  44. },
  45. },
  46. },
  47. },
  48. {
  49. Type: ipv4.ICMPTypeTimeExceeded, Code: 1,
  50. Body: &icmp.TimeExceeded{
  51. Data: []byte("ERROR-INVOKING-PACKET"),
  52. Extensions: []icmp.Extension{
  53. &icmp.InterfaceInfo{
  54. Class: 2,
  55. Type: 0x0f,
  56. Interface: &net.Interface{
  57. Index: 15,
  58. Name: "en101",
  59. MTU: 8192,
  60. },
  61. Addr: &net.IPAddr{
  62. IP: net.IPv4(192, 168, 0, 1).To4(),
  63. },
  64. },
  65. &icmp.MPLSLabelStack{
  66. Class: 1,
  67. Type: 1,
  68. Labels: []icmp.MPLSLabel{
  69. {
  70. Label: 16014,
  71. TC: 0x4,
  72. S: true,
  73. TTL: 255,
  74. },
  75. },
  76. },
  77. },
  78. },
  79. },
  80. {
  81. Type: ipv4.ICMPTypeParameterProblem, Code: 2,
  82. Body: &icmp.ParamProb{
  83. Pointer: 8,
  84. Data: []byte("ERROR-INVOKING-PACKET"),
  85. Extensions: []icmp.Extension{
  86. &icmp.MPLSLabelStack{
  87. Class: 1,
  88. Type: 1,
  89. Labels: []icmp.MPLSLabel{
  90. {
  91. Label: 16014,
  92. TC: 0x4,
  93. S: true,
  94. TTL: 255,
  95. },
  96. },
  97. },
  98. &icmp.InterfaceInfo{
  99. Class: 2,
  100. Type: 0x0f,
  101. Interface: &net.Interface{
  102. Index: 15,
  103. Name: "en101",
  104. MTU: 8192,
  105. },
  106. Addr: &net.IPAddr{
  107. IP: net.IPv4(192, 168, 0, 1).To4(),
  108. },
  109. },
  110. &icmp.InterfaceInfo{
  111. Class: 2,
  112. Type: 0x2f,
  113. Interface: &net.Interface{
  114. Index: 16,
  115. Name: "en102",
  116. MTU: 8192,
  117. },
  118. Addr: &net.IPAddr{
  119. IP: net.IPv4(192, 168, 0, 2).To4(),
  120. },
  121. },
  122. },
  123. },
  124. },
  125. }
  126. func TestMarshalAndParseMultipartMessageForIPv4(t *testing.T) {
  127. for i, tt := range marshalAndParseMultipartMessageForIPv4Tests {
  128. b, err := tt.Marshal(nil)
  129. if err != nil {
  130. t.Fatal(err)
  131. }
  132. if b[5] != 32 {
  133. t.Errorf("#%v: got %v; want 32", i, b[5])
  134. }
  135. m, err := icmp.ParseMessage(iana.ProtocolICMP, b)
  136. if err != nil {
  137. t.Fatal(err)
  138. }
  139. if m.Type != tt.Type || m.Code != tt.Code {
  140. t.Errorf("#%v: got %v; want %v", i, m, &tt)
  141. }
  142. switch m.Type {
  143. case ipv4.ICMPTypeDestinationUnreachable:
  144. got, want := m.Body.(*icmp.DstUnreach), tt.Body.(*icmp.DstUnreach)
  145. if !reflect.DeepEqual(got.Extensions, want.Extensions) {
  146. t.Error(dumpExtensions(i, got.Extensions, want.Extensions))
  147. }
  148. if len(got.Data) != 128 {
  149. t.Errorf("#%v: got %v; want 128", i, len(got.Data))
  150. }
  151. case ipv4.ICMPTypeTimeExceeded:
  152. got, want := m.Body.(*icmp.TimeExceeded), tt.Body.(*icmp.TimeExceeded)
  153. if !reflect.DeepEqual(got.Extensions, want.Extensions) {
  154. t.Error(dumpExtensions(i, got.Extensions, want.Extensions))
  155. }
  156. if len(got.Data) != 128 {
  157. t.Errorf("#%v: got %v; want 128", i, len(got.Data))
  158. }
  159. case ipv4.ICMPTypeParameterProblem:
  160. got, want := m.Body.(*icmp.ParamProb), tt.Body.(*icmp.ParamProb)
  161. if !reflect.DeepEqual(got.Extensions, want.Extensions) {
  162. t.Error(dumpExtensions(i, got.Extensions, want.Extensions))
  163. }
  164. if len(got.Data) != 128 {
  165. t.Errorf("#%v: got %v; want 128", i, len(got.Data))
  166. }
  167. }
  168. }
  169. }
  170. var marshalAndParseMultipartMessageForIPv6Tests = []icmp.Message{
  171. {
  172. Type: ipv6.ICMPTypeDestinationUnreachable, Code: 6,
  173. Body: &icmp.DstUnreach{
  174. Data: []byte("ERROR-INVOKING-PACKET"),
  175. Extensions: []icmp.Extension{
  176. &icmp.MPLSLabelStack{
  177. Class: 1,
  178. Type: 1,
  179. Labels: []icmp.MPLSLabel{
  180. {
  181. Label: 16014,
  182. TC: 0x4,
  183. S: true,
  184. TTL: 255,
  185. },
  186. },
  187. },
  188. &icmp.InterfaceInfo{
  189. Class: 2,
  190. Type: 0x0f,
  191. Interface: &net.Interface{
  192. Index: 15,
  193. Name: "en101",
  194. MTU: 8192,
  195. },
  196. Addr: &net.IPAddr{
  197. IP: net.ParseIP("fe80::1"),
  198. Zone: "en101",
  199. },
  200. },
  201. },
  202. },
  203. },
  204. {
  205. Type: ipv6.ICMPTypeTimeExceeded, Code: 1,
  206. Body: &icmp.TimeExceeded{
  207. Data: []byte("ERROR-INVOKING-PACKET"),
  208. Extensions: []icmp.Extension{
  209. &icmp.InterfaceInfo{
  210. Class: 2,
  211. Type: 0x0f,
  212. Interface: &net.Interface{
  213. Index: 15,
  214. Name: "en101",
  215. MTU: 8192,
  216. },
  217. Addr: &net.IPAddr{
  218. IP: net.ParseIP("fe80::1"),
  219. Zone: "en101",
  220. },
  221. },
  222. &icmp.MPLSLabelStack{
  223. Class: 1,
  224. Type: 1,
  225. Labels: []icmp.MPLSLabel{
  226. {
  227. Label: 16014,
  228. TC: 0x4,
  229. S: true,
  230. TTL: 255,
  231. },
  232. },
  233. },
  234. &icmp.InterfaceInfo{
  235. Class: 2,
  236. Type: 0x2f,
  237. Interface: &net.Interface{
  238. Index: 16,
  239. Name: "en102",
  240. MTU: 8192,
  241. },
  242. Addr: &net.IPAddr{
  243. IP: net.ParseIP("fe80::1"),
  244. Zone: "en102",
  245. },
  246. },
  247. },
  248. },
  249. },
  250. }
  251. func TestMarshalAndParseMultipartMessageForIPv6(t *testing.T) {
  252. pshicmp := icmp.IPv6PseudoHeader(net.ParseIP("fe80::1"), net.ParseIP("ff02::1"))
  253. for i, tt := range marshalAndParseMultipartMessageForIPv6Tests {
  254. for _, psh := range [][]byte{pshicmp, nil} {
  255. b, err := tt.Marshal(psh)
  256. if err != nil {
  257. t.Fatal(err)
  258. }
  259. if b[4] != 16 {
  260. t.Errorf("#%v: got %v; want 16", i, b[4])
  261. }
  262. m, err := icmp.ParseMessage(iana.ProtocolIPv6ICMP, b)
  263. if err != nil {
  264. t.Fatal(err)
  265. }
  266. if m.Type != tt.Type || m.Code != tt.Code {
  267. t.Errorf("#%v: got %v; want %v", i, m, &tt)
  268. }
  269. switch m.Type {
  270. case ipv6.ICMPTypeDestinationUnreachable:
  271. got, want := m.Body.(*icmp.DstUnreach), tt.Body.(*icmp.DstUnreach)
  272. if !reflect.DeepEqual(got.Extensions, want.Extensions) {
  273. t.Error(dumpExtensions(i, got.Extensions, want.Extensions))
  274. }
  275. if len(got.Data) != 128 {
  276. t.Errorf("#%v: got %v; want 128", i, len(got.Data))
  277. }
  278. case ipv6.ICMPTypeTimeExceeded:
  279. got, want := m.Body.(*icmp.TimeExceeded), tt.Body.(*icmp.TimeExceeded)
  280. if !reflect.DeepEqual(got.Extensions, want.Extensions) {
  281. t.Error(dumpExtensions(i, got.Extensions, want.Extensions))
  282. }
  283. if len(got.Data) != 128 {
  284. t.Errorf("#%v: got %v; want 128", i, len(got.Data))
  285. }
  286. }
  287. }
  288. }
  289. }
  290. func dumpExtensions(i int, gotExts, wantExts []icmp.Extension) string {
  291. var s string
  292. for j, got := range gotExts {
  293. switch got := got.(type) {
  294. case *icmp.MPLSLabelStack:
  295. want := wantExts[j].(*icmp.MPLSLabelStack)
  296. if !reflect.DeepEqual(got, want) {
  297. s += fmt.Sprintf("#%v/%v: got %#v; want %#v\n", i, j, got, want)
  298. }
  299. case *icmp.InterfaceInfo:
  300. want := wantExts[j].(*icmp.InterfaceInfo)
  301. if !reflect.DeepEqual(got, want) {
  302. s += fmt.Sprintf("#%v/%v: got %#v, %#v, %#v; want %#v, %#v, %#v\n", i, j, got, got.Interface, got.Addr, want, want.Interface, want.Addr)
  303. }
  304. }
  305. }
  306. return s[:len(s)-1]
  307. }
  308. var multipartMessageBodyLenTests = []struct {
  309. proto int
  310. in icmp.MessageBody
  311. out int
  312. }{
  313. {
  314. iana.ProtocolICMP,
  315. &icmp.DstUnreach{
  316. Data: make([]byte, ipv4.HeaderLen),
  317. },
  318. 4 + ipv4.HeaderLen, // unused and original datagram
  319. },
  320. {
  321. iana.ProtocolICMP,
  322. &icmp.TimeExceeded{
  323. Data: make([]byte, ipv4.HeaderLen),
  324. },
  325. 4 + ipv4.HeaderLen, // unused and original datagram
  326. },
  327. {
  328. iana.ProtocolICMP,
  329. &icmp.ParamProb{
  330. Data: make([]byte, ipv4.HeaderLen),
  331. },
  332. 4 + ipv4.HeaderLen, // [pointer, unused] and original datagram
  333. },
  334. {
  335. iana.ProtocolICMP,
  336. &icmp.ParamProb{
  337. Data: make([]byte, ipv4.HeaderLen),
  338. Extensions: []icmp.Extension{
  339. &icmp.MPLSLabelStack{},
  340. },
  341. },
  342. 4 + 4 + 4 + 0 + 128, // [pointer, length, unused], extension header, object header, object payload, original datagram
  343. },
  344. {
  345. iana.ProtocolICMP,
  346. &icmp.ParamProb{
  347. Data: make([]byte, 128),
  348. Extensions: []icmp.Extension{
  349. &icmp.MPLSLabelStack{},
  350. },
  351. },
  352. 4 + 4 + 4 + 0 + 128, // [pointer, length, unused], extension header, object header, object payload and original datagram
  353. },
  354. {
  355. iana.ProtocolICMP,
  356. &icmp.ParamProb{
  357. Data: make([]byte, 129),
  358. Extensions: []icmp.Extension{
  359. &icmp.MPLSLabelStack{},
  360. },
  361. },
  362. 4 + 4 + 4 + 0 + 132, // [pointer, length, unused], extension header, object header, object payload and original datagram
  363. },
  364. {
  365. iana.ProtocolIPv6ICMP,
  366. &icmp.DstUnreach{
  367. Data: make([]byte, ipv6.HeaderLen),
  368. },
  369. 4 + ipv6.HeaderLen, // unused and original datagram
  370. },
  371. {
  372. iana.ProtocolIPv6ICMP,
  373. &icmp.PacketTooBig{
  374. Data: make([]byte, ipv6.HeaderLen),
  375. },
  376. 4 + ipv6.HeaderLen, // mtu and original datagram
  377. },
  378. {
  379. iana.ProtocolIPv6ICMP,
  380. &icmp.TimeExceeded{
  381. Data: make([]byte, ipv6.HeaderLen),
  382. },
  383. 4 + ipv6.HeaderLen, // unused and original datagram
  384. },
  385. {
  386. iana.ProtocolIPv6ICMP,
  387. &icmp.ParamProb{
  388. Data: make([]byte, ipv6.HeaderLen),
  389. },
  390. 4 + ipv6.HeaderLen, // pointer and original datagram
  391. },
  392. {
  393. iana.ProtocolIPv6ICMP,
  394. &icmp.DstUnreach{
  395. Data: make([]byte, 127),
  396. Extensions: []icmp.Extension{
  397. &icmp.MPLSLabelStack{},
  398. },
  399. },
  400. 4 + 4 + 4 + 0 + 128, // [length, unused], extension header, object header, object payload and original datagram
  401. },
  402. {
  403. iana.ProtocolIPv6ICMP,
  404. &icmp.DstUnreach{
  405. Data: make([]byte, 128),
  406. Extensions: []icmp.Extension{
  407. &icmp.MPLSLabelStack{},
  408. },
  409. },
  410. 4 + 4 + 4 + 0 + 128, // [length, unused], extension header, object header, object payload and original datagram
  411. },
  412. {
  413. iana.ProtocolIPv6ICMP,
  414. &icmp.DstUnreach{
  415. Data: make([]byte, 129),
  416. Extensions: []icmp.Extension{
  417. &icmp.MPLSLabelStack{},
  418. },
  419. },
  420. 4 + 4 + 4 + 0 + 136, // [length, unused], extension header, object header, object payload and original datagram
  421. },
  422. }
  423. func TestMultipartMessageBodyLen(t *testing.T) {
  424. for i, tt := range multipartMessageBodyLenTests {
  425. if out := tt.in.Len(tt.proto); out != tt.out {
  426. t.Errorf("#%d: got %d; want %d", i, out, tt.out)
  427. }
  428. }
  429. }