extension_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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
  5. import (
  6. "reflect"
  7. "testing"
  8. "golang.org/x/net/internal/iana"
  9. )
  10. var marshalAndParseExtensionTests = []struct {
  11. proto int
  12. hdr []byte
  13. obj []byte
  14. exts []Extension
  15. }{
  16. // MPLS label stack with no label
  17. {
  18. proto: iana.ProtocolICMP,
  19. hdr: []byte{
  20. 0x20, 0x00, 0x00, 0x00,
  21. },
  22. obj: []byte{
  23. 0x00, 0x04, 0x01, 0x01,
  24. },
  25. exts: []Extension{
  26. &MPLSLabelStack{
  27. Class: classMPLSLabelStack,
  28. Type: typeIncomingMPLSLabelStack,
  29. },
  30. },
  31. },
  32. // MPLS label stack with a single label
  33. {
  34. proto: iana.ProtocolIPv6ICMP,
  35. hdr: []byte{
  36. 0x20, 0x00, 0x00, 0x00,
  37. },
  38. obj: []byte{
  39. 0x00, 0x08, 0x01, 0x01,
  40. 0x03, 0xe8, 0xe9, 0xff,
  41. },
  42. exts: []Extension{
  43. &MPLSLabelStack{
  44. Class: classMPLSLabelStack,
  45. Type: typeIncomingMPLSLabelStack,
  46. Labels: []MPLSLabel{
  47. {
  48. Label: 16014,
  49. TC: 0x4,
  50. S: true,
  51. TTL: 255,
  52. },
  53. },
  54. },
  55. },
  56. },
  57. // MPLS label stack with multiple labels
  58. {
  59. proto: iana.ProtocolICMP,
  60. hdr: []byte{
  61. 0x20, 0x00, 0x00, 0x00,
  62. },
  63. obj: []byte{
  64. 0x00, 0x0c, 0x01, 0x01,
  65. 0x03, 0xe8, 0xde, 0xfe,
  66. 0x03, 0xe8, 0xe1, 0xff,
  67. },
  68. exts: []Extension{
  69. &MPLSLabelStack{
  70. Class: classMPLSLabelStack,
  71. Type: typeIncomingMPLSLabelStack,
  72. Labels: []MPLSLabel{
  73. {
  74. Label: 16013,
  75. TC: 0x7,
  76. S: false,
  77. TTL: 254,
  78. },
  79. {
  80. Label: 16014,
  81. TC: 0,
  82. S: true,
  83. TTL: 255,
  84. },
  85. },
  86. },
  87. },
  88. },
  89. }
  90. func TestMarshalAndParseExtension(t *testing.T) {
  91. for i, tt := range marshalAndParseExtensionTests {
  92. for j, ext := range tt.exts {
  93. var err error
  94. var b []byte
  95. switch ext := ext.(type) {
  96. case *MPLSLabelStack:
  97. b, err = ext.Marshal(tt.proto)
  98. if err != nil {
  99. t.Errorf("#%v/%v: %v", i, j, err)
  100. continue
  101. }
  102. }
  103. if !reflect.DeepEqual(b, tt.obj) {
  104. t.Errorf("#%v/%v: got %#v; want %#v", i, j, b, tt.obj)
  105. continue
  106. }
  107. }
  108. for j, wire := range []struct {
  109. data []byte // original datagram
  110. inlattr int // length of padded original datagram, a hint
  111. outlattr int // length of padded original datagram, a want
  112. err error
  113. }{
  114. {nil, 0, -1, errNoExtension},
  115. {make([]byte, 127), 128, -1, errNoExtension},
  116. {make([]byte, 128), 127, -1, errNoExtension},
  117. {make([]byte, 128), 128, -1, errNoExtension},
  118. {make([]byte, 128), 129, -1, errNoExtension},
  119. {append(make([]byte, 128), append(tt.hdr, tt.obj...)...), 127, 128, nil},
  120. {append(make([]byte, 128), append(tt.hdr, tt.obj...)...), 128, 128, nil},
  121. {append(make([]byte, 128), append(tt.hdr, tt.obj...)...), 129, 128, nil},
  122. {append(make([]byte, 512), append(tt.hdr, tt.obj...)...), 511, -1, errNoExtension},
  123. {append(make([]byte, 512), append(tt.hdr, tt.obj...)...), 512, 512, nil},
  124. {append(make([]byte, 512), append(tt.hdr, tt.obj...)...), 513, -1, errNoExtension},
  125. } {
  126. exts, l, err := parseExtensions(wire.data, wire.inlattr)
  127. if err != wire.err {
  128. t.Errorf("#%v/%v: got %v; want %v", i, j, err, wire.err)
  129. continue
  130. }
  131. if wire.err != nil {
  132. continue
  133. }
  134. if l != wire.outlattr {
  135. t.Errorf("#%v/%v: got %v; want %v", i, j, l, wire.outlattr)
  136. }
  137. if !reflect.DeepEqual(exts, tt.exts) {
  138. for j, ext := range exts {
  139. switch ext := ext.(type) {
  140. case *MPLSLabelStack:
  141. want := tt.exts[j].(*MPLSLabelStack)
  142. t.Errorf("#%v/%v: got %#v; want %#v", i, j, ext, want)
  143. }
  144. }
  145. continue
  146. }
  147. }
  148. }
  149. }