mirror of
https://github.com/yggdrasil-network/water.git
synced 2025-05-19 08:25:09 +03:00
test cleanup
This commit is contained in:
parent
3fe638a7bf
commit
6ad6edefb1
5 changed files with 129 additions and 124 deletions
|
@ -4,13 +4,14 @@ import (
|
|||
"net"
|
||||
"os/exec"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/songgao/water/waterutil"
|
||||
)
|
||||
|
||||
func startBroadcast(t *testing.T, dst net.IP) {
|
||||
if err := exec.Command("ping", "-b", "-c", "4", dst.String()).Start(); err != nil {
|
||||
func startPing(t *testing.T, dst net.IP, dashB bool) {
|
||||
params := []string{"-c", "4", dst.String()}
|
||||
if dashB {
|
||||
params = append([]string{"-b"}, params...)
|
||||
}
|
||||
if err := exec.Command("ping", params...).Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
@ -46,45 +47,52 @@ func TestBroadcastTAP(t *testing.T) {
|
|||
}
|
||||
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)
|
||||
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)
|
||||
}
|
||||
|
||||
func TestBroadcastTUN(t *testing.T) {
|
||||
var (
|
||||
self = net.IPv4(10, 0, 42, 1)
|
||||
mask = net.IPv4Mask(255, 255, 255, 0)
|
||||
brd = net.IPv4(10, 0, 42, 255)
|
||||
)
|
||||
|
||||
ifce, err := New(Config{DeviceType: TUN})
|
||||
if err != nil {
|
||||
t.Fatalf("creating TUN error: %v\n", err)
|
||||
}
|
||||
defer teardownIfce(t, ifce)
|
||||
|
||||
dataCh, errCh := startRead(t, ifce)
|
||||
|
||||
setupIfce(t, net.IPNet{IP: self, Mask: mask}, ifce.Name())
|
||||
startPing(t, brd, true)
|
||||
|
||||
waitForPingOrBust(t, false, true, self, brd, dataCh, errCh)
|
||||
}
|
||||
|
||||
func TestUnicastTUN(t *testing.T) {
|
||||
var (
|
||||
self = net.IPv4(10, 0, 42, 1)
|
||||
mask = net.IPv4Mask(255, 255, 255, 0)
|
||||
remote = net.IPv4(10, 0, 42, 2)
|
||||
)
|
||||
|
||||
ifce, err := New(Config{DeviceType: TUN})
|
||||
if err != nil {
|
||||
t.Fatalf("creating TUN error: %v\n", err)
|
||||
}
|
||||
defer teardownIfce(t, ifce)
|
||||
|
||||
dataCh, errCh := startRead(t, ifce)
|
||||
|
||||
setupIfce(t, net.IPNet{IP: self, Mask: mask}, ifce.Name())
|
||||
startPing(t, remote, false)
|
||||
|
||||
waitForPingOrBust(t, false, false, self, remote, dataCh, errCh)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue