ipv4_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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
  5. import (
  6. "encoding/binary"
  7. "net"
  8. "reflect"
  9. "runtime"
  10. "testing"
  11. "golang.org/x/net/ipv4"
  12. )
  13. type ipv4HeaderTest struct {
  14. wireHeaderFromKernel [ipv4.HeaderLen]byte
  15. wireHeaderFromTradBSDKernel [ipv4.HeaderLen]byte
  16. Header *ipv4.Header
  17. }
  18. var ipv4HeaderLittleEndianTest = ipv4HeaderTest{
  19. // TODO(mikio): Add platform dependent wire header formats when
  20. // we support new platforms.
  21. wireHeaderFromKernel: [ipv4.HeaderLen]byte{
  22. 0x45, 0x01, 0xbe, 0xef,
  23. 0xca, 0xfe, 0x45, 0xdc,
  24. 0xff, 0x01, 0xde, 0xad,
  25. 172, 16, 254, 254,
  26. 192, 168, 0, 1,
  27. },
  28. wireHeaderFromTradBSDKernel: [ipv4.HeaderLen]byte{
  29. 0x45, 0x01, 0xef, 0xbe,
  30. 0xca, 0xfe, 0x45, 0xdc,
  31. 0xff, 0x01, 0xde, 0xad,
  32. 172, 16, 254, 254,
  33. 192, 168, 0, 1,
  34. },
  35. Header: &ipv4.Header{
  36. Version: ipv4.Version,
  37. Len: ipv4.HeaderLen,
  38. TOS: 1,
  39. TotalLen: 0xbeef,
  40. ID: 0xcafe,
  41. Flags: ipv4.DontFragment,
  42. FragOff: 1500,
  43. TTL: 255,
  44. Protocol: 1,
  45. Checksum: 0xdead,
  46. Src: net.IPv4(172, 16, 254, 254),
  47. Dst: net.IPv4(192, 168, 0, 1),
  48. },
  49. }
  50. func TestParseIPv4Header(t *testing.T) {
  51. tt := &ipv4HeaderLittleEndianTest
  52. if nativeEndian != binary.LittleEndian {
  53. t.Skip("no test for non-little endian machine yet")
  54. }
  55. var wh []byte
  56. switch runtime.GOOS {
  57. case "darwin":
  58. wh = tt.wireHeaderFromTradBSDKernel[:]
  59. case "freebsd":
  60. if freebsdVersion >= 1000000 {
  61. wh = tt.wireHeaderFromKernel[:]
  62. } else {
  63. wh = tt.wireHeaderFromTradBSDKernel[:]
  64. }
  65. default:
  66. wh = tt.wireHeaderFromKernel[:]
  67. }
  68. h, err := ParseIPv4Header(wh)
  69. if err != nil {
  70. t.Fatal(err)
  71. }
  72. if !reflect.DeepEqual(h, tt.Header) {
  73. t.Fatalf("got %#v; want %#v", h, tt.Header)
  74. }
  75. }