ipv4_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. func TestParseIPv4Header(t *testing.T) {
  15. switch socket.NativeEndian {
  16. case binary.LittleEndian:
  17. t.Run("LittleEndian", func(t *testing.T) {
  18. // TODO(mikio): Add platform dependent wire
  19. // header formats when we support new
  20. // 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. th := &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. var wh []byte
  50. switch runtime.GOOS {
  51. case "darwin":
  52. wh = wireHeaderFromTradBSDKernel[:]
  53. case "freebsd":
  54. if freebsdVersion >= 1000000 {
  55. wh = wireHeaderFromKernel[:]
  56. } else {
  57. wh = wireHeaderFromTradBSDKernel[:]
  58. }
  59. default:
  60. wh = wireHeaderFromKernel[:]
  61. }
  62. h, err := ParseIPv4Header(wh)
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. if !reflect.DeepEqual(h, th) {
  67. t.Fatalf("got %#v; want %#v", h, th)
  68. }
  69. })
  70. }
  71. }