github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/net/lif/link_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  func (ll *Link) String() string {
    16  	return fmt.Sprintf("name=%s index=%d type=%d flags=%#x mtu=%d addr=%v", ll.Name, ll.Index, ll.Type, ll.Flags, ll.MTU, llAddr(ll.Addr))
    17  }
    18  
    19  type linkPack struct {
    20  	af  int
    21  	lls []Link
    22  }
    23  
    24  func linkPacks() ([]linkPack, error) {
    25  	var lastErr error
    26  	var lps []linkPack
    27  	for _, af := range [...]int{sysAF_UNSPEC, sysAF_INET, sysAF_INET6} {
    28  		lls, err := Links(af, "")
    29  		if err != nil {
    30  			lastErr = err
    31  			continue
    32  		}
    33  		lps = append(lps, linkPack{af: af, lls: lls})
    34  	}
    35  	return lps, lastErr
    36  }
    37  
    38  func TestLinks(t *testing.T) {
    39  	lps, err := linkPacks()
    40  	if len(lps) == 0 && err != nil {
    41  		t.Fatal(err)
    42  	}
    43  	for _, lp := range lps {
    44  		n := 0
    45  		for _, sll := range lp.lls {
    46  			lls, err := Links(lp.af, sll.Name)
    47  			if err != nil {
    48  				t.Fatal(lp.af, sll.Name, err)
    49  			}
    50  			for _, ll := range lls {
    51  				if ll.Name != sll.Name || ll.Index != sll.Index {
    52  					t.Errorf("af=%s got %v; want %v", addrFamily(lp.af), &ll, &sll)
    53  					continue
    54  				}
    55  				t.Logf("af=%s name=%s %v", addrFamily(lp.af), sll.Name, &ll)
    56  				n++
    57  			}
    58  		}
    59  		if n != len(lp.lls) {
    60  			t.Errorf("af=%s got %d; want %d", addrFamily(lp.af), n, len(lp.lls))
    61  			continue
    62  		}
    63  	}
    64  }