ipv4_test.go 1.9 KB

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