mirror of
https://github.com/fumiama/water.git
synced 2026-06-23 12:40:40 +08:00
test cleanup
This commit is contained in:
@@ -6,13 +6,10 @@ import (
|
||||
"os/exec"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/songgao/water/waterutil"
|
||||
)
|
||||
|
||||
func startBroadcast(t *testing.T, dst net.IP) {
|
||||
if err := exec.Command("ping", "-n", "2", dst.String()).Start(); err != nil {
|
||||
func startPing(t *testing.T, dst net.IP, _ bool) {
|
||||
if err := exec.Command("ping", "-n", "4", dst.String()).Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
@@ -29,6 +26,12 @@ func setupIfce(t *testing.T, ipNet net.IPNet, dev string) {
|
||||
}
|
||||
}
|
||||
|
||||
func teardownIfce(t *testing.T, ifce *Interface) {
|
||||
if err := ifce.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBroadcastTAP(t *testing.T) {
|
||||
var (
|
||||
self = net.IPv4(10, 0, 42, 1)
|
||||
@@ -40,46 +43,12 @@ func TestBroadcastTAP(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("creating TAP error: %v\n", err)
|
||||
}
|
||||
defer teardownIfce(t, ifce)
|
||||
|
||||
dataCh, errCh := startRead(t, ifce)
|
||||
|
||||
setupIfce(t, net.IPNet{IP: self, Mask: mask}, ifce.Name())
|
||||
startBroadcast(t, brd)
|
||||
startPing(t, brd, true)
|
||||
|
||||
dataCh := make(chan []byte, 8)
|
||||
errCh := make(chan error)
|
||||
startRead(t, ifce, dataCh, errCh)
|
||||
|
||||
timeout := time.NewTimer(8 * time.Second).C
|
||||
|
||||
readFrame:
|
||||
for {
|
||||
select {
|
||||
case buffer := <-dataCh:
|
||||
ethertype := waterutil.MACEthertype(buffer)
|
||||
if ethertype != waterutil.IPv4 {
|
||||
continue readFrame
|
||||
}
|
||||
if !waterutil.IsBroadcast(waterutil.MACDestination(buffer)) {
|
||||
continue readFrame
|
||||
}
|
||||
packet := waterutil.MACPayload(buffer)
|
||||
if !waterutil.IsIPv4(packet) {
|
||||
continue readFrame
|
||||
}
|
||||
if !waterutil.IPv4Source(packet).Equal(self) {
|
||||
continue readFrame
|
||||
}
|
||||
if !waterutil.IPv4Destination(packet).Equal(brd) {
|
||||
continue readFrame
|
||||
}
|
||||
if waterutil.IPv4Protocol(packet) != waterutil.ICMP {
|
||||
continue readFrame
|
||||
}
|
||||
t.Logf("received broadcast frame: %#v\n", buffer)
|
||||
break readFrame
|
||||
case err := <-errCh:
|
||||
t.Fatalf("read error: %v", err)
|
||||
case <-timeout:
|
||||
t.Fatal("Waiting for broadcast packet timeout")
|
||||
}
|
||||
}
|
||||
waitForPingOrBust(t, true, true, self, brd, dataCh, errCh)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user