github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/net/lif/address_test.go (about)

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