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