From 6cecaf31575d77d8f30be65a0a5d34055ee220a4 Mon Sep 17 00:00:00 2001 From: Mathias Hall-Andersen Date: Thu, 8 Mar 2018 16:44:27 +0100 Subject: [PATCH] Begin work on full device<->device unit-test To simulate a full interaction between two WireGuard instances without networking, using dummy instances of the interfaces --- bind_test.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ device_test.go | 43 +++++++++++++++++++++++++++++++++++++++++ endpoint_test.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++ receive.go | 2 +- 4 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 bind_test.go create mode 100644 device_test.go create mode 100644 endpoint_test.go diff --git a/bind_test.go b/bind_test.go new file mode 100644 index 0000000..41c4225 --- /dev/null +++ b/bind_test.go @@ -0,0 +1,50 @@ +package main + +import "errors" + +type DummyDatagram struct { + msg []byte + endpoint Endpoint + world bool // better type +} + +type DummyBind struct { + in6 chan DummyDatagram + ou6 chan DummyDatagram + in4 chan DummyDatagram + ou4 chan DummyDatagram + closed bool +} + +func (b *DummyBind) SetMark(v uint32) error { + return nil +} + +func (b *DummyBind) ReceiveIPv6(buff []byte) (int, Endpoint, error) { + datagram, ok := <-b.in6 + if !ok { + return 0, nil, errors.New("closed") + } + copy(buff, datagram.msg) + return len(datagram.msg), datagram.endpoint, nil +} + +func (b *DummyBind) ReceiveIPv4(buff []byte) (int, Endpoint, error) { + datagram, ok := <-b.in4 + if !ok { + return 0, nil, errors.New("closed") + } + copy(buff, datagram.msg) + return len(datagram.msg), datagram.endpoint, nil +} + +func (b *DummyBind) Close() error { + close(b.in6) + close(b.in4) + b.closed = true + return nil +} + +func (b *DummyBind) Send(buff []byte, end Endpoint) error { + return nil +} diff --git a/device_test.go b/device_test.go new file mode 100644 index 0000000..abd0208 --- /dev/null +++ b/device_test.go @@ -0,0 +1,43 @@ +package main + +/* Create two device instances and simulate full WireGuard interaction + * without network dependencies + */ + +import "testing" + +func TestDevice(t *testing.T) { + + // prepare tun devices for generating traffic + + tun1, err := CreateDummyTUN("tun1") + if err != nil { + t.Error("failed to create tun:", err.Error()) + } + + tun2, err := CreateDummyTUN("tun2") + if err != nil { + t.Error("failed to create tun:", err.Error()) + } + + println(tun1) + println(tun2) + + // prepare endpoints + + end1, err := CreateDummyEndpoint() + if err != nil { + t.Error("failed to create endpoint:", err.Error()) + } + + end2, err := CreateDummyEndpoint() + if err != nil { + t.Error("failed to create endpoint:", err.Error()) + } + + println(end1) + println(end2) + + // create binds + +} diff --git a/endpoint_test.go b/endpoint_test.go new file mode 100644 index 0000000..7021e48 --- /dev/null +++ b/endpoint_test.go @@ -0,0 +1,48 @@ +package main + +import ( + "math/rand" + "net" +) + +type DummyEndpoint struct { + src [16]byte + dst [16]byte +} + +func CreateDummyEndpoint() (*DummyEndpoint, error) { + var end DummyEndpoint + if _, err := rand.Read(end.src[:]); err != nil { + return nil, err + } + _, err := rand.Read(end.dst[:]) + return &end, err +} + +func (e *DummyEndpoint) ClearSrc() {} + +func (e *DummyEndpoint) SrcToString() string { + var addr net.UDPAddr + addr.IP = e.SrcIP() + addr.Port = 1000 + return addr.String() +} + +func (e *DummyEndpoint) DstToString() string { + var addr net.UDPAddr + addr.IP = e.DstIP() + addr.Port = 1000 + return addr.String() +} + +func (e *DummyEndpoint) SrcToBytes() []byte { + return e.src[:] +} + +func (e *DummyEndpoint) DstIP() net.IP { + return e.dst[:] +} + +func (e *DummyEndpoint) SrcIP() net.IP { + return e.src[:] +} diff --git a/receive.go b/receive.go index fbaf77d..96ba08d 100644 --- a/receive.go +++ b/receive.go @@ -475,7 +475,7 @@ func (device *Device) RoutineHandshake() { peer.endpoint = elem.endpoint peer.mutex.Unlock() - logDebug.Println("Received handshake initiation from", peer) + logDebug.Println(peer, ": Received handshake initiation") peer.TimerEphemeralKeyCreated()