ipv4_test.go 1.5 KB

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