...
Run Format

Source file src/net/interface_linux_test.go

Documentation: net

     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.setupCmds = append(ti.setupCmds, &exec.Cmd{
    24  		Path: xname,
    25  		Args: []string{"ip", "address", "add", ti.local, "peer", ti.remote, "dev", ti.name},
    26  	})
    27  	ti.teardownCmds = append(ti.teardownCmds, &exec.Cmd{
    28  		Path: xname,
    29  		Args: []string{"ip", "address", "del", ti.local, "peer", ti.remote, "dev", ti.name},
    30  	})
    31  	ti.teardownCmds = append(ti.teardownCmds, &exec.Cmd{
    32  		Path: xname,
    33  		Args: []string{"ip", "link", "delete", ti.name, "type", "dummy"},
    34  	})
    35  	return nil
    36  }
    37  
    38  func (ti *testInterface) setPointToPoint(suffix int) error {
    39  	ti.name = fmt.Sprintf("gotest%d", suffix)
    40  	xname, err := exec.LookPath("ip")
    41  	if err != nil {
    42  		return err
    43  	}
    44  	ti.setupCmds = append(ti.setupCmds, &exec.Cmd{
    45  		Path: xname,
    46  		Args: []string{"ip", "tunnel", "add", ti.name, "mode", "gre", "local", ti.local, "remote", ti.remote},
    47  	})
    48  	ti.setupCmds = append(ti.setupCmds, &exec.Cmd{
    49  		Path: xname,
    50  		Args: []string{"ip", "address", "add", ti.local, "peer", ti.remote, "dev", ti.name},
    51  	})
    52  	ti.teardownCmds = append(ti.teardownCmds, &exec.Cmd{
    53  		Path: xname,
    54  		Args: []string{"ip", "address", "del", ti.local, "peer", ti.remote, "dev", ti.name},
    55  	})
    56  	ti.teardownCmds = append(ti.teardownCmds, &exec.Cmd{
    57  		Path: xname,
    58  		Args: []string{"ip", "tunnel", "del", ti.name, "mode", "gre", "local", ti.local, "remote", ti.remote},
    59  	})
    60  	return nil
    61  }
    62  
    63  const (
    64  	numOfTestIPv4MCAddrs = 14
    65  	numOfTestIPv6MCAddrs = 18
    66  )
    67  
    68  var (
    69  	igmpInterfaceTable = []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  	}
    75  	igmp6InterfaceTable = []Interface{
    76  		{Name: "lo"},
    77  		{Name: "eth0"}, {Name: "eth1"}, {Name: "eth2"},
    78  		{Name: "eth0.100"}, {Name: "eth0.101"}, {Name: "eth0.102"}, {Name: "eth0.103"},
    79  		{Name: "device1tap2"},
    80  		{Name: "pan0"},
    81  	}
    82  )
    83  
    84  func TestParseProcNet(t *testing.T) {
    85  	defer func() {
    86  		if p := recover(); p != nil {
    87  			t.Fatalf("panicked: %v", p)
    88  		}
    89  	}()
    90  
    91  	var ifmat4 []Addr
    92  	for _, ifi := range igmpInterfaceTable {
    93  		ifmat := parseProcNetIGMP("testdata/igmp", &ifi)
    94  		ifmat4 = append(ifmat4, ifmat...)
    95  	}
    96  	if len(ifmat4) != numOfTestIPv4MCAddrs {
    97  		t.Fatalf("got %d; want %d", len(ifmat4), numOfTestIPv4MCAddrs)
    98  	}
    99  
   100  	var ifmat6 []Addr
   101  	for _, ifi := range igmp6InterfaceTable {
   102  		ifmat := parseProcNetIGMP6("testdata/igmp6", &ifi)
   103  		ifmat6 = append(ifmat6, ifmat...)
   104  	}
   105  	if len(ifmat6) != numOfTestIPv6MCAddrs {
   106  		t.Fatalf("got %d; want %d", len(ifmat6), numOfTestIPv6MCAddrs)
   107  	}
   108  }
   109  

View as plain text