header_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2012 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 ipv4
  5. import (
  6. "bytes"
  7. "encoding/binary"
  8. "net"
  9. "reflect"
  10. "runtime"
  11. "strings"
  12. "testing"
  13. )
  14. type headerTest struct {
  15. wireHeaderFromKernel [HeaderLen]byte
  16. wireHeaderToKernel [HeaderLen]byte
  17. wireHeaderFromTradBSDKernel [HeaderLen]byte
  18. wireHeaderToTradBSDKernel [HeaderLen]byte
  19. wireHeaderFromFreeBSD10Kernel [HeaderLen]byte
  20. wireHeaderToFreeBSD10Kernel [HeaderLen]byte
  21. *Header
  22. }
  23. var headerLittleEndianTest = headerTest{
  24. // TODO(mikio): Add platform dependent wire header formats when
  25. // we support new platforms.
  26. wireHeaderFromKernel: [HeaderLen]byte{
  27. 0x45, 0x01, 0xbe, 0xef,
  28. 0xca, 0xfe, 0x45, 0xdc,
  29. 0xff, 0x01, 0xde, 0xad,
  30. 172, 16, 254, 254,
  31. 192, 168, 0, 1,
  32. },
  33. wireHeaderToKernel: [HeaderLen]byte{
  34. 0x45, 0x01, 0xbe, 0xef,
  35. 0xca, 0xfe, 0x45, 0xdc,
  36. 0xff, 0x01, 0xde, 0xad,
  37. 172, 16, 254, 254,
  38. 192, 168, 0, 1,
  39. },
  40. wireHeaderFromTradBSDKernel: [HeaderLen]byte{
  41. 0x45, 0x01, 0xdb, 0xbe,
  42. 0xca, 0xfe, 0xdc, 0x45,
  43. 0xff, 0x01, 0xde, 0xad,
  44. 172, 16, 254, 254,
  45. 192, 168, 0, 1,
  46. },
  47. wireHeaderToTradBSDKernel: [HeaderLen]byte{
  48. 0x45, 0x01, 0xef, 0xbe,
  49. 0xca, 0xfe, 0xdc, 0x45,
  50. 0xff, 0x01, 0xde, 0xad,
  51. 172, 16, 254, 254,
  52. 192, 168, 0, 1,
  53. },
  54. wireHeaderFromFreeBSD10Kernel: [HeaderLen]byte{
  55. 0x45, 0x01, 0xef, 0xbe,
  56. 0xca, 0xfe, 0xdc, 0x45,
  57. 0xff, 0x01, 0xde, 0xad,
  58. 172, 16, 254, 254,
  59. 192, 168, 0, 1,
  60. },
  61. wireHeaderToFreeBSD10Kernel: [HeaderLen]byte{
  62. 0x45, 0x01, 0xef, 0xbe,
  63. 0xca, 0xfe, 0xdc, 0x45,
  64. 0xff, 0x01, 0xde, 0xad,
  65. 172, 16, 254, 254,
  66. 192, 168, 0, 1,
  67. },
  68. Header: &Header{
  69. Version: Version,
  70. Len: HeaderLen,
  71. TOS: 1,
  72. TotalLen: 0xbeef,
  73. ID: 0xcafe,
  74. Flags: DontFragment,
  75. FragOff: 1500,
  76. TTL: 255,
  77. Protocol: 1,
  78. Checksum: 0xdead,
  79. Src: net.IPv4(172, 16, 254, 254),
  80. Dst: net.IPv4(192, 168, 0, 1),
  81. },
  82. }
  83. func TestMarshalHeader(t *testing.T) {
  84. tt := &headerLittleEndianTest
  85. if nativeEndian != binary.LittleEndian {
  86. t.Skip("no test for non-little endian machine yet")
  87. }
  88. b, err := tt.Header.Marshal()
  89. if err != nil {
  90. t.Fatal(err)
  91. }
  92. var wh []byte
  93. switch runtime.GOOS {
  94. case "darwin", "dragonfly", "netbsd":
  95. wh = tt.wireHeaderToTradBSDKernel[:]
  96. case "freebsd":
  97. switch {
  98. case freebsdVersion < 1000000:
  99. wh = tt.wireHeaderToTradBSDKernel[:]
  100. case 1000000 <= freebsdVersion && freebsdVersion < 1100000:
  101. wh = tt.wireHeaderToFreeBSD10Kernel[:]
  102. default:
  103. wh = tt.wireHeaderToKernel[:]
  104. }
  105. default:
  106. wh = tt.wireHeaderToKernel[:]
  107. }
  108. if !bytes.Equal(b, wh) {
  109. t.Fatalf("got %#v; want %#v", b, wh)
  110. }
  111. }
  112. func TestParseHeader(t *testing.T) {
  113. tt := &headerLittleEndianTest
  114. if nativeEndian != binary.LittleEndian {
  115. t.Skip("no test for big endian machine yet")
  116. }
  117. var wh []byte
  118. switch runtime.GOOS {
  119. case "darwin", "dragonfly", "netbsd":
  120. wh = tt.wireHeaderFromTradBSDKernel[:]
  121. case "freebsd":
  122. switch {
  123. case freebsdVersion < 1000000:
  124. wh = tt.wireHeaderFromTradBSDKernel[:]
  125. case 1000000 <= freebsdVersion && freebsdVersion < 1100000:
  126. wh = tt.wireHeaderFromFreeBSD10Kernel[:]
  127. default:
  128. wh = tt.wireHeaderFromKernel[:]
  129. }
  130. default:
  131. wh = tt.wireHeaderFromKernel[:]
  132. }
  133. h, err := ParseHeader(wh)
  134. if err != nil {
  135. t.Fatal(err)
  136. }
  137. if !reflect.DeepEqual(h, tt.Header) {
  138. t.Fatalf("got %#v; want %#v", h, tt.Header)
  139. }
  140. s := h.String()
  141. if strings.Contains(s, ",") {
  142. t.Fatalf("should be space-separated values: %s", s)
  143. }
  144. }