github.com/roboticscm/goman@v0.0.0-20210203095141-87c07b4a0a55/src/net/interface_linux_test.go (about)

     1  // Copyright 2012 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  package net
     6  
     7  import (
     8  	"fmt"
     9  	"os/exec"
    10  	"testing"
    11  )
    12  
    13  func (ti *testInterface) setBroadcast(suffix int) error {
    14  	ti.name = fmt.Sprintf("gotest%d", suffix)
    15  	xname, err := exec.LookPath("ip")
    16  	if err != nil {
    17  		return err
    18  	}
    19  	ti.setupCmds = append(ti.setupCmds, &exec.Cmd{
    20  		Path: xname,
    21  		Args: []string{"ip", "link", "add", ti.name, "type", "dummy"},
    22  	})
    23  	ti.teardownCmds = append(ti.teardownCmds, &exec.Cmd{
    24  		Path: xname,
    25  		Args: []string{"ip", "link", "delete", ti.name, "type", "dummy"},
    26  	})
    27  	return nil
    28  }
    29  
    30  func (ti *testInterface) setPointToPoint(suffix int, local, remote string) error {
    31  	ti.name = fmt.Sprintf("gotest%d", suffix)
    32  	ti.local = local
    33  	ti.remote = remote
    34  	xname, err := exec.LookPath("ip")
    35  	if err != nil {
    36  		return err
    37  	}
    38  	ti.setupCmds = append(ti.setupCmds, &exec.Cmd{
    39  		Path: xname,
    40  		Args: []string{"ip", "tunnel", "add", ti.name, "mode", "gre", "local", local, "remote", remote},
    41  	})
    42  	ti.teardownCmds = append(ti.teardownCmds, &exec.Cmd{
    43  		Path: xname,
    44  		Args: []string{"ip", "tunnel", "del", ti.name, "mode", "gre", "local", local, "remote", remote},
    45  	})
    46  	xname, err = exec.LookPath("ifconfig")
    47  	if err != nil {
    48  		return err
    49  	}
    50  	ti.setupCmds = append(ti.setupCmds, &exec.Cmd{
    51  		Path: xname,
    52  		Args: []string{"ifconfig", ti.name, "inet", local, "dstaddr", remote},
    53  	})
    54  	return nil
    55  }
    56  
    57  const (
    58  	numOfTestIPv4MCAddrs = 14
    59  	numOfTestIPv6MCAddrs = 18
    60  )
    61  
    62  var (
    63  	igmpInterfaceTable = []Interface{
    64  		{Name: "lo"},
    65  		{Name: "eth0"}, {Name: "eth1"}, {Name: "eth2"},
    66  		{Name: "eth0.100"}, {Name: "eth0.101"}, {Name: "eth0.102"}, {Name: "eth0.103"},
    67  		{Name: "device1tap2"},
    68  	}
    69  	igmp6InterfaceTable = []Interface{
    70  		{Name: "lo"},
    71  		{Name: "eth0"}, {Name: "eth1"}, {Name: "eth2"},
    72  		{Name: "eth0.100"}, {Name: "eth0.101"}, {Name: "eth0.102"}, {Name: "eth0.103"},
    73  		{Name: "device1tap2"},
    74  		{Name: "pan0"},
    75  	}
    76  )
    77  
    78  func TestParseProcNet(t *testing.T) {
    79  	defer func() {
    80  		if p := recover(); p != nil {
    81  			t.Fatalf("parseProcNetIGMP or parseProtNetIGMP6 panicked: %v", p)
    82  		}
    83  	}()
    84  
    85  	var ifmat4 []Addr
    86  	for _, ifi := range igmpInterfaceTable {
    87  		ifmat := parseProcNetIGMP("testdata/igmp", &ifi)
    88  		ifmat4 = append(ifmat4, ifmat...)
    89  	}
    90  	if len(ifmat4) != numOfTestIPv4MCAddrs {
    91  		t.Fatalf("parseProcNetIGMP returns %v addresses, expected %v", len(ifmat4), numOfTestIPv4MCAddrs)
    92  	}
    93  
    94  	var ifmat6 []Addr
    95  	for _, ifi := range igmp6InterfaceTable {
    96  		ifmat := parseProcNetIGMP6("testdata/igmp6", &ifi)
    97  		ifmat6 = append(ifmat6, ifmat...)
    98  	}
    99  	if len(ifmat6) != numOfTestIPv6MCAddrs {
   100  		t.Fatalf("parseProcNetIGMP6 returns %v addresses, expected %v", len(ifmat6), numOfTestIPv6MCAddrs)
   101  	}
   102  }