github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/system/net/net_test.go (about)

     1  package net
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/isyscore/isc-gobase/system/common"
     6  	"math"
     7  	"os"
     8  	"testing"
     9  )
    10  
    11  func skipIfNotImplementedErr(t *testing.T, err error) {
    12  	if err == common.ErrNotImplementedError {
    13  		t.Skip("not implemented")
    14  	}
    15  }
    16  
    17  func TestAddrString(t *testing.T) {
    18  	v := Addr{IP: "192.168.0.1", Port: 8000}
    19  
    20  	s := fmt.Sprintf("%v", v)
    21  	if s != "{\"ip\":\"192.168.0.1\",\"port\":8000}" {
    22  		t.Errorf("Addr string is invalid: %v", v)
    23  	}
    24  }
    25  
    26  func TestNetIOCountersStatString(t *testing.T) {
    27  	v := IOCountersStat{
    28  		Name:      "test",
    29  		BytesSent: 100,
    30  	}
    31  	e := `{"name":"test","bytesSent":100,"bytesRecv":0,"packetsSent":0,"packetsRecv":0,"errin":0,"errout":0,"dropin":0,"dropout":0,"fifoin":0,"fifoout":0}`
    32  	if e != fmt.Sprintf("%v", v) {
    33  		t.Errorf("NetIOCountersStat string is invalid: %v", v)
    34  	}
    35  }
    36  
    37  func TestNetProtoCountersStatString(t *testing.T) {
    38  	v := ProtoCountersStat{
    39  		Protocol: "tcp",
    40  		Stats: map[string]int64{
    41  			"MaxConn":      -1,
    42  			"ActiveOpens":  4000,
    43  			"PassiveOpens": 3000,
    44  		},
    45  	}
    46  	e := `{"protocol":"tcp","stats":{"ActiveOpens":4000,"MaxConn":-1,"PassiveOpens":3000}}`
    47  	if e != fmt.Sprintf("%v", v) {
    48  		t.Errorf("NetProtoCountersStat string is invalid: %v", v)
    49  	}
    50  
    51  }
    52  
    53  func TestNetConnectionStatString(t *testing.T) {
    54  	v := ConnectionStat{
    55  		Fd:     10,
    56  		Family: 10,
    57  		Type:   10,
    58  		Uids:   []int32{10, 10},
    59  	}
    60  	e := `{"fd":10,"family":10,"type":10,"localaddr":{"ip":"","port":0},"remoteaddr":{"ip":"","port":0},"status":"","uids":[10,10],"pid":0}`
    61  	if e != fmt.Sprintf("%v", v) {
    62  		t.Errorf("NetConnectionStat string is invalid: %v", v)
    63  	}
    64  
    65  }
    66  
    67  func TestNetIOCountersAll(t *testing.T) {
    68  	v, err := IOCounters(false)
    69  	skipIfNotImplementedErr(t, err)
    70  	if err != nil {
    71  		t.Errorf("Could not get NetIOCounters: %v", err)
    72  	}
    73  	per, err := IOCounters(true)
    74  	skipIfNotImplementedErr(t, err)
    75  	if err != nil {
    76  		t.Errorf("Could not get NetIOCounters: %v", err)
    77  	}
    78  	if len(v) != 1 {
    79  		t.Errorf("Could not get NetIOCounters: %v", v)
    80  	}
    81  	if v[0].Name != "all" {
    82  		t.Errorf("Invalid NetIOCounters: %v", v)
    83  	}
    84  	var pr uint64
    85  	for _, p := range per {
    86  		pr += p.PacketsRecv
    87  	}
    88  	// small diff is ok
    89  	if math.Abs(float64(v[0].PacketsRecv-pr)) > 5 {
    90  		if ci := os.Getenv("CI"); ci != "" {
    91  			// This test often fails in CI. so just print even if failed.
    92  			fmt.Printf("invalid sum value: %v, %v", v[0].PacketsRecv, pr)
    93  		} else {
    94  			t.Errorf("invalid sum value: %v, %v", v[0].PacketsRecv, pr)
    95  		}
    96  	}
    97  }
    98  
    99  func TestNetIOCountersPerNic(t *testing.T) {
   100  	v, err := IOCounters(true)
   101  	skipIfNotImplementedErr(t, err)
   102  	if err != nil {
   103  		t.Errorf("Could not get NetIOCounters: %v", err)
   104  	}
   105  	if len(v) == 0 {
   106  		t.Errorf("Could not get NetIOCounters: %v", v)
   107  	}
   108  	for _, vv := range v {
   109  		if vv.Name == "" {
   110  			t.Errorf("Invalid NetIOCounters: %v", vv)
   111  		}
   112  	}
   113  }
   114  
   115  func TestGetNetIOCountersAll(t *testing.T) {
   116  	n := []IOCountersStat{
   117  		{
   118  			Name:        "a",
   119  			BytesRecv:   10,
   120  			PacketsRecv: 10,
   121  		},
   122  		{
   123  			Name:        "b",
   124  			BytesRecv:   10,
   125  			PacketsRecv: 10,
   126  			Errin:       10,
   127  		},
   128  	}
   129  	ret, err := getIOCountersAll(n)
   130  	skipIfNotImplementedErr(t, err)
   131  	if err != nil {
   132  		t.Error(err)
   133  	}
   134  	if len(ret) != 1 {
   135  		t.Errorf("invalid return count")
   136  	}
   137  	if ret[0].Name != "all" {
   138  		t.Errorf("invalid return name")
   139  	}
   140  	if ret[0].BytesRecv != 20 {
   141  		t.Errorf("invalid count bytesrecv")
   142  	}
   143  	if ret[0].Errin != 10 {
   144  		t.Errorf("invalid count errin")
   145  	}
   146  }
   147  
   148  func TestNetInterfaces(t *testing.T) {
   149  	v, err := Interfaces()
   150  	skipIfNotImplementedErr(t, err)
   151  	if err != nil {
   152  		t.Errorf("Could not get NetInterfaceStat: %v", err)
   153  	}
   154  	if len(v) == 0 {
   155  		t.Errorf("Could not get NetInterfaceStat: %v", err)
   156  	}
   157  	for _, vv := range v {
   158  		if vv.Name == "" {
   159  			t.Errorf("Invalid NetInterface: %v", vv)
   160  		}
   161  	}
   162  }
   163  
   164  func TestNetConnections(t *testing.T) {
   165  	if ci := os.Getenv("CI"); ci != "" { // skip if test on drone.io
   166  		return
   167  	}
   168  
   169  	v, err := Connections("inet")
   170  	skipIfNotImplementedErr(t, err)
   171  	if err != nil {
   172  		t.Errorf("could not get NetConnections: %v", err)
   173  	}
   174  	if len(v) == 0 {
   175  		t.Errorf("could not get NetConnections: %v", v)
   176  	}
   177  	for _, vv := range v {
   178  		if vv.Family == 0 {
   179  			t.Errorf("invalid NetConnections: %v", vv)
   180  		}
   181  	}
   182  
   183  }
   184  
   185  func TestIpPortAvailable(t *testing.T) {
   186  	// 测试
   187  }