address_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 solaris
  5. package lif
  6. import (
  7. "fmt"
  8. "testing"
  9. )
  10. type addrFamily int
  11. func (af addrFamily) String() string {
  12. switch af {
  13. case sysAF_UNSPEC:
  14. return "unspec"
  15. case sysAF_INET:
  16. return "inet4"
  17. case sysAF_INET6:
  18. return "inet6"
  19. default:
  20. return fmt.Sprintf("%d", af)
  21. }
  22. }
  23. const hexDigit = "0123456789abcdef"
  24. type llAddr []byte
  25. func (a llAddr) String() string {
  26. if len(a) == 0 {
  27. return ""
  28. }
  29. buf := make([]byte, 0, len(a)*3-1)
  30. for i, b := range a {
  31. if i > 0 {
  32. buf = append(buf, ':')
  33. }
  34. buf = append(buf, hexDigit[b>>4])
  35. buf = append(buf, hexDigit[b&0xF])
  36. }
  37. return string(buf)
  38. }
  39. type ipAddr []byte
  40. func (a ipAddr) String() string {
  41. if len(a) == 0 {
  42. return "<nil>"
  43. }
  44. if len(a) == 4 {
  45. return fmt.Sprintf("%d.%d.%d.%d", a[0], a[1], a[2], a[3])
  46. }
  47. if len(a) == 16 {
  48. return fmt.Sprintf("%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x", a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10], a[11], a[12], a[13], a[14], a[15])
  49. }
  50. s := make([]byte, len(a)*2)
  51. for i, tn := range a {
  52. s[i*2], s[i*2+1] = hexDigit[tn>>4], hexDigit[tn&0xf]
  53. }
  54. return string(s)
  55. }
  56. func (a *Inet4Addr) String() string {
  57. return fmt.Sprintf("(%s %s %d)", addrFamily(a.Family()), ipAddr(a.IP[:]), a.PrefixLen)
  58. }
  59. func (a *Inet6Addr) String() string {
  60. return fmt.Sprintf("(%s %s %d %d)", addrFamily(a.Family()), ipAddr(a.IP[:]), a.PrefixLen, a.ZoneID)
  61. }
  62. type addrPack struct {
  63. af int
  64. as []Addr
  65. }
  66. func addrPacks() ([]addrPack, error) {
  67. var lastErr error
  68. var aps []addrPack
  69. for _, af := range [...]int{sysAF_UNSPEC, sysAF_INET, sysAF_INET6} {
  70. as, err := Addrs(af, "")
  71. if err != nil {
  72. lastErr = err
  73. continue
  74. }
  75. aps = append(aps, addrPack{af: af, as: as})
  76. }
  77. return aps, lastErr
  78. }
  79. func TestAddrs(t *testing.T) {
  80. aps, err := addrPacks()
  81. if len(aps) == 0 && err != nil {
  82. t.Fatal(err)
  83. }
  84. lps, err := linkPacks()
  85. if len(lps) == 0 && err != nil {
  86. t.Fatal(err)
  87. }
  88. for _, lp := range lps {
  89. n := 0
  90. for _, ll := range lp.lls {
  91. as, err := Addrs(lp.af, ll.Name)
  92. if err != nil {
  93. t.Fatal(lp.af, ll.Name, err)
  94. }
  95. t.Logf("af=%s name=%s %v", addrFamily(lp.af), ll.Name, as)
  96. n += len(as)
  97. }
  98. for _, ap := range aps {
  99. if ap.af != lp.af {
  100. continue
  101. }
  102. if n != len(ap.as) {
  103. t.Errorf("af=%s got %d; want %d", addrFamily(lp.af), n, len(ap.as))
  104. continue
  105. }
  106. }
  107. }
  108. }