address_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2016 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. // +build darwin dragonfly freebsd netbsd openbsd
  5. package route
  6. import (
  7. "reflect"
  8. "testing"
  9. )
  10. type parseAddrsTest struct {
  11. attrs uint
  12. fn func(int, []byte) (int, Addr, error)
  13. b []byte
  14. as []Addr
  15. }
  16. var parseAddrsLittleEndianTests = []parseAddrsTest{
  17. {
  18. sysRTA_DST | sysRTA_GATEWAY | sysRTA_NETMASK | sysRTA_BRD,
  19. parseKernelInetAddr,
  20. []byte{
  21. 0x38, 0x12, 0x0, 0x0, 0xff, 0xff, 0xff, 0x0,
  22. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  23. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  24. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  25. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  26. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  27. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  28. 0x38, 0x12, 0x2, 0x0, 0x6, 0x3, 0x6, 0x0,
  29. 0x65, 0x6d, 0x31, 0x0, 0xc, 0x29, 0x66, 0x2c,
  30. 0xdc, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  31. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  32. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  33. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  34. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  35. 0x10, 0x2, 0x0, 0x0, 0xac, 0x10, 0xdc, 0xb4,
  36. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  37. 0x10, 0x2, 0x0, 0x0, 0xac, 0x10, 0xdc, 0xff,
  38. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  39. },
  40. []Addr{
  41. &LinkAddr{Index: 0},
  42. &LinkAddr{Index: 2, Name: "em1", Addr: []byte{0x00, 0x0c, 0x29, 0x66, 0x2c, 0xdc}},
  43. &Inet4Addr{IP: [4]byte{172, 16, 220, 180}},
  44. nil,
  45. nil,
  46. nil,
  47. nil,
  48. &Inet4Addr{IP: [4]byte{172, 16, 220, 255}},
  49. },
  50. },
  51. {
  52. sysRTA_NETMASK | sysRTA_IFP | sysRTA_IFA,
  53. parseKernelInetAddr,
  54. []byte{
  55. 0x7, 0x0, 0x0, 0x0, 0xff, 0xff, 0xff, 0x0,
  56. 0x18, 0x12, 0xa, 0x0, 0x87, 0x8, 0x0, 0x0,
  57. 0x76, 0x6c, 0x61, 0x6e, 0x35, 0x36, 0x38, 0x32,
  58. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  59. 0x10, 0x2, 0x0, 0x0, 0xa9, 0xfe, 0x0, 0x1,
  60. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  61. },
  62. []Addr{
  63. nil,
  64. nil,
  65. &Inet4Addr{IP: [4]byte{255, 255, 255, 0}},
  66. nil,
  67. &LinkAddr{Index: 10, Name: "vlan5682"},
  68. &Inet4Addr{IP: [4]byte{169, 254, 0, 1}},
  69. nil,
  70. nil,
  71. },
  72. },
  73. }
  74. func TestParseAddrs(t *testing.T) {
  75. tests := parseAddrsLittleEndianTests
  76. if nativeEndian != littleEndian {
  77. t.Skip("no test for non-little endian machine yet")
  78. }
  79. for i, tt := range tests {
  80. as, err := parseAddrs(tt.attrs, tt.fn, tt.b)
  81. if err != nil {
  82. t.Error(i, err)
  83. continue
  84. }
  85. as = as[:8] // the list varies between operating systems
  86. if !reflect.DeepEqual(as, tt.as) {
  87. t.Errorf("#%d: got %+v; want %+v", i, as, tt.as)
  88. continue
  89. }
  90. }
  91. }