github.com/borderzero/water@v0.0.1/ipv4_linux_test.go (about)

     1  package water
     2  
     3  import (
     4  	"net"
     5  	"os/exec"
     6  	"testing"
     7  )
     8  
     9  func startPing(t *testing.T, dst net.IP, dashB bool) {
    10  	params := []string{"-c", "4", dst.String()}
    11  	if dashB {
    12  		params = append([]string{"-b"}, params...)
    13  	}
    14  	if err := exec.Command("ping", params...).Start(); err != nil {
    15  		t.Fatal(err)
    16  	}
    17  }
    18  
    19  func setupIfce(t *testing.T, ipNet net.IPNet, dev string) {
    20  	if err := exec.Command("ip", "link", "set", dev, "up").Run(); err != nil {
    21  		t.Fatal(err)
    22  	}
    23  	if err := exec.Command("ip", "addr", "add", ipNet.String(), "dev", dev).Run(); err != nil {
    24  		t.Fatal(err)
    25  	}
    26  }
    27  
    28  func teardownIfce(t *testing.T, ifce *Interface) {
    29  	if err := ifce.Close(); err != nil {
    30  		t.Fatal(err)
    31  	}
    32  	if err := exec.Command("ip", "link", "set", ifce.Name(), "down").Run(); err != nil {
    33  		t.Fatal(err)
    34  	}
    35  }
    36  
    37  func TestBroadcastTAP(t *testing.T) {
    38  	var (
    39  		self = net.IPv4(10, 0, 42, 1)
    40  		mask = net.IPv4Mask(255, 255, 255, 0)
    41  		brd  = net.IPv4(10, 0, 42, 255)
    42  	)
    43  
    44  	ifce, err := New(Config{DeviceType: TAP})
    45  	if err != nil {
    46  		t.Fatalf("creating TAP error: %v\n", err)
    47  	}
    48  	defer teardownIfce(t, ifce)
    49  
    50  	dataCh, errCh := startRead(t, ifce)
    51  
    52  	setupIfce(t, net.IPNet{IP: self, Mask: mask}, ifce.Name())
    53  	startPing(t, brd, true)
    54  
    55  	waitForPingOrBust(t, true, true, self, brd, dataCh, errCh)
    56  }
    57  
    58  func TestBroadcastTUN(t *testing.T) {
    59  	var (
    60  		self = net.IPv4(10, 0, 42, 1)
    61  		mask = net.IPv4Mask(255, 255, 255, 0)
    62  		brd  = net.IPv4(10, 0, 42, 255)
    63  	)
    64  
    65  	ifce, err := New(Config{DeviceType: TUN})
    66  	if err != nil {
    67  		t.Fatalf("creating TUN error: %v\n", err)
    68  	}
    69  	defer teardownIfce(t, ifce)
    70  
    71  	dataCh, errCh := startRead(t, ifce)
    72  
    73  	setupIfce(t, net.IPNet{IP: self, Mask: mask}, ifce.Name())
    74  	startPing(t, brd, true)
    75  
    76  	waitForPingOrBust(t, false, true, self, brd, dataCh, errCh)
    77  }
    78  
    79  func TestUnicastTUN(t *testing.T) {
    80  	var (
    81  		self   = net.IPv4(10, 0, 42, 1)
    82  		mask   = net.IPv4Mask(255, 255, 255, 0)
    83  		remote = net.IPv4(10, 0, 42, 2)
    84  	)
    85  
    86  	ifce, err := New(Config{DeviceType: TUN})
    87  	if err != nil {
    88  		t.Fatalf("creating TUN error: %v\n", err)
    89  	}
    90  	defer teardownIfce(t, ifce)
    91  
    92  	dataCh, errCh := startRead(t, ifce)
    93  
    94  	setupIfce(t, net.IPNet{IP: self, Mask: mask}, ifce.Name())
    95  	startPing(t, remote, false)
    96  
    97  	waitForPingOrBust(t, false, false, self, remote, dataCh, errCh)
    98  }