github.com/labulakalia/water@v0.0.5-0.20231118024244-f351ca6784b6/ipv4_windows_test.go (about)

     1  package water
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  	"os/exec"
     7  	"strings"
     8  	"testing"
     9  )
    10  
    11  func startPing(t *testing.T, dst net.IP, _ bool) {
    12  	if err := exec.Command("ping", "-n", "4", dst.String()).Start(); err != nil {
    13  		t.Fatal(err)
    14  	}
    15  }
    16  
    17  func setupIfce(t *testing.T, ipNet net.IPNet, dev string) {
    18  	sargs := fmt.Sprintf("interface ip set address name=REPLACE_ME source=static addr=REPLACE_ME mask=REPLACE_ME gateway=none")
    19  	args := strings.Split(sargs, " ")
    20  	args[4] = fmt.Sprintf("name=%s", dev)
    21  	args[6] = fmt.Sprintf("addr=%s", ipNet.IP)
    22  	args[7] = fmt.Sprintf("mask=%d.%d.%d.%d", ipNet.Mask[0], ipNet.Mask[1], ipNet.Mask[2], ipNet.Mask[3])
    23  	cmd := exec.Command("netsh", args...)
    24  	if err := cmd.Run(); err != nil {
    25  		t.Fatal(err)
    26  	}
    27  }
    28  
    29  func teardownIfce(t *testing.T, ifce *Interface) {
    30  	if err := ifce.Close(); err != nil {
    31  		t.Fatal(err)
    32  	}
    33  }
    34  
    35  func TestBroadcastTAP(t *testing.T) {
    36  	var (
    37  		self = net.IPv4(10, 0, 42, 1)
    38  		mask = net.IPv4Mask(255, 255, 255, 0)
    39  		brd  = net.IPv4(10, 0, 42, 255)
    40  	)
    41  
    42  	ifce, err := New(Config{DeviceType: TAP})
    43  	if err != nil {
    44  		t.Fatalf("creating TAP error: %v\n", err)
    45  	}
    46  	defer teardownIfce(t, ifce)
    47  
    48  	dataCh, errCh := startRead(t, ifce)
    49  
    50  	setupIfce(t, net.IPNet{IP: self, Mask: mask}, ifce.Name())
    51  	startPing(t, brd, true)
    52  
    53  	waitForPingOrBust(t, true, true, self, brd, dataCh, errCh)
    54  }