Source file src/net/interface_linux_test.go

     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) setLinkLocal(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", "link", "add", ti.name, "type", "dummy"},
    47  	})
    48  	ti.setupCmds = append(ti.setupCmds, &exec.Cmd{
    49  		Path: xname,
    50  		Args: []string{"ip", "address", "add", ti.local, "dev", ti.name},
    51  	})
    52  	ti.teardownCmds = append(ti.teardownCmds, &exec.Cmd{
    53  		Path: xname,
    54  		Args: []string{"ip", "address", "del", ti.local, "dev", ti.name},
    55  	})
    56  	ti.teardownCmds = append(ti.teardownCmds, &exec.Cmd{
    57  		Path: xname,
    58  		Args: []string{"ip", "link", "delete", ti.name, "type", "dummy"},
    59  	})
    60  	return nil
    61  }
    62  
    63  func (ti *testInterface) setPointToPoint(suffix int) error {
    64  	ti.name = fmt.Sprintf("gotest%d", suffix)
    65  	xname, err := exec.LookPath("ip")
    66  	if err != nil {
    67  		return err
    68  	}
    69  	ti.setupCmds = append(ti.setupCmds, &exec.Cmd{
    70  		Path: xname,
    71  		Args: []string{"ip", "tunnel", "add", ti.name, "mode", "gre", "local", ti.local, "remote", ti.remote},
    72  	})
    73  	ti.setupCmds = append(ti.setupCmds, &exec.Cmd{
    74  		Path: xname,
    75  		Args: []string{"ip", "address", "add", ti.local, "peer", ti.remote, "dev", ti.name},
    76  	})
    77  	ti.teardownCmds = append(ti.teardownCmds, &exec.Cmd{
    78  		Path: xname,
    79  		Args: []string{"ip", "address", "del", ti.local, "peer", ti.remote, "dev", ti.name},
    80  	})
    81  	ti.teardownCmds = append(ti.teardownCmds, &exec.Cmd{
    82  		Path: xname,
    83  		Args: []string{"ip", "tunnel", "del", ti.name, "mode", "gre", "local", ti.local, "remote", ti.remote},
    84  	})
    85  	return nil
    86  }
    87  
    88  const (
    89  	numOfTestIPv4MCAddrs = 14
    90  	numOfTestIPv6MCAddrs = 18
    91  )
    92  
    93  var (
    94  	igmpInterfaceTable = []Interface{
    95  		{Name: "lo"},
    96  		{Name: "eth0"}, {Name: "eth1"}, {Name: "eth2"},
    97  		{Name: "eth0.100"}, {Name: "eth0.101"}, {Name: "eth0.102"}, {Name: "eth0.103"},
    98  		{Name: "device1tap2"},
    99  	}
   100  	igmp6InterfaceTable = []Interface{
   101  		{Name: "lo"},
   102  		{Name: "eth0"}, {Name: "eth1"}, {Name: "eth2"},
   103  		{Name: "eth0.100"}, {Name: "eth0.101"}, {Name: "eth0.102"}, {Name: "eth0.103"},
   104  		{Name: "device1tap2"},
   105  		{Name: "pan0"},
   106  	}
   107  )
   108  
   109  func TestParseProcNet(t *testing.T) {
   110  	defer func() {
   111  		if p := recover(); p != nil {
   112  			t.Fatalf("panicked: %v", p)
   113  		}
   114  	}()
   115  
   116  	var ifmat4 []Addr
   117  	for _, ifi := range igmpInterfaceTable {
   118  		ifmat := parseProcNetIGMP("testdata/igmp", &ifi)
   119  		ifmat4 = append(ifmat4, ifmat...)
   120  	}
   121  	if len(ifmat4) != numOfTestIPv4MCAddrs {
   122  		t.Fatalf("got %d; want %d", len(ifmat4), numOfTestIPv4MCAddrs)
   123  	}
   124  
   125  	var ifmat6 []Addr
   126  	for _, ifi := range igmp6InterfaceTable {
   127  		ifmat := parseProcNetIGMP6("testdata/igmp6", &ifi)
   128  		ifmat6 = append(ifmat6, ifmat...)
   129  	}
   130  	if len(ifmat6) != numOfTestIPv6MCAddrs {
   131  		t.Fatalf("got %d; want %d", len(ifmat6), numOfTestIPv6MCAddrs)
   132  	}
   133  }
   134  

View as plain text