mirror of
https://github.com/yggdrasil-network/water.git
synced 2025-05-19 16:35:10 +03:00
Merge pull request #2 from songgao/songgao/add_tests
Add TAP test (ipv4 broadcast)
This commit is contained in:
commit
e7338c341f
4 changed files with 113 additions and 2 deletions
81
ipv4_test.go
Normal file
81
ipv4_test.go
Normal file
|
@ -0,0 +1,81 @@
|
|||
package water
|
||||
|
||||
import (
|
||||
"net"
|
||||
"os/exec"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/songgao/water/waterutil"
|
||||
)
|
||||
|
||||
const BUFFERSIZE = 1522
|
||||
|
||||
func startRead(ch chan<- []byte, ifce *Interface) {
|
||||
go func() {
|
||||
for {
|
||||
buffer := make([]byte, BUFFERSIZE)
|
||||
_, err := ifce.Read(buffer)
|
||||
if err == nil {
|
||||
ch <- buffer
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func startBroadcast(t *testing.T, dst net.IP) {
|
||||
if err := exec.Command("ping", "-b", "-c", "2", dst.String()).Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBroadcast(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 := NewTAP("test")
|
||||
if err != nil {
|
||||
t.Fatalf("creating TAP error: %v\n", err)
|
||||
}
|
||||
|
||||
setupIfce(t, net.IPNet{IP: self, Mask: mask}, ifce.Name())
|
||||
startBroadcast(t, brd)
|
||||
|
||||
dataCh := make(chan []byte, 8)
|
||||
startRead(dataCh, ifce)
|
||||
|
||||
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
|
||||
}
|
||||
break readFrame
|
||||
case <-timeout:
|
||||
t.Fatal("Waiting for broadcast packet timeout")
|
||||
}
|
||||
}
|
||||
}
|
16
ipv4_test_linux.go
Normal file
16
ipv4_test_linux.go
Normal file
|
@ -0,0 +1,16 @@
|
|||
package water
|
||||
|
||||
import (
|
||||
"net"
|
||||
"os/exec"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func setupIfce(t *testing.T, ipNet net.IPNet, dev string) {
|
||||
if err := exec.Command("ip", "link", "set", dev, "up").Run(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := exec.Command("ip", "addr", "add", ipNet.String(), "dev", dev).Run(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
12
ipv4_test_other.go
Normal file
12
ipv4_test_other.go
Normal file
|
@ -0,0 +1,12 @@
|
|||
// +build !linux
|
||||
|
||||
package water
|
||||
|
||||
import (
|
||||
"net"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func setupIfce(t *testing.T, ipNet net.IPNet, dev string) {
|
||||
t.Fatal("unsupported platform")
|
||||
}
|
|
@ -2,10 +2,12 @@
|
|||
|
||||
package water
|
||||
|
||||
import "errors"
|
||||
|
||||
func newTAP(ifName string) (ifce *Interface, err error) {
|
||||
panic("water: tap interface not implemented on this platform")
|
||||
return nil, errors.New("tap interface not implemented on this platform")
|
||||
}
|
||||
|
||||
func newTUN(ifName string) (ifce *Interface, err error) {
|
||||
panic("water: tap interface not implemented on this platform")
|
||||
return nil, errors.New("tap interface not implemented on this platform")
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue