github.com/labulakalia/water@v0.0.5-0.20231118024244-f351ca6784b6/ipv4_darwin_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, self net.IP, remote net.IP, dev string) {
    20  	if err := exec.Command("ifconfig", dev, "inet", self.String(), remote.String(), "up").Run(); err != nil {
    21  		t.Fatal(err)
    22  	}
    23  }
    24  
    25  func teardownIfce(t *testing.T, ifce *Interface) {
    26  	if err := ifce.Close(); err != nil {
    27  		t.Fatal(err)
    28  	}
    29  	if err := exec.Command("ifconfig", ifce.Name(), "down").Run(); err != nil {
    30  		t.Fatal(err)
    31  	}
    32  }
    33  
    34  func TestP2PTUN(t *testing.T) {
    35  	var (
    36  		self   = net.IPv4(10, 0, 42, 1)
    37  		remote = net.IPv4(10, 0, 42, 2)
    38  	)
    39  
    40  	ifce, err := New(Config{DeviceType: TUN})
    41  	if err != nil {
    42  		t.Fatalf("creating TUN error: %v\n", err)
    43  	}
    44  	defer teardownIfce(t, ifce)
    45  
    46  	dataCh, errCh := startRead(t, ifce)
    47  
    48  	setupIfce(t, self, remote, ifce.Name())
    49  	startPing(t, remote, false)
    50  
    51  	waitForPingOrBust(t, false, false, self, remote, dataCh, errCh)
    52  }