2020-07-02 21:19:43 +00:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package natlab
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"inet.af/netaddr"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAllocIPs(t *testing.T) {
|
|
|
|
n := NewInternet()
|
|
|
|
saw := map[netaddr.IP]bool{}
|
|
|
|
for i := 0; i < 255; i++ {
|
|
|
|
for _, f := range []func(*Machine) netaddr.IP{n.allocIPv4, n.allocIPv6} {
|
|
|
|
ip := f(nil)
|
|
|
|
if saw[ip] {
|
|
|
|
t.Fatalf("got duplicate %v", ip)
|
|
|
|
}
|
|
|
|
saw[ip] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should work:
|
|
|
|
n.allocIPv6(nil)
|
|
|
|
|
|
|
|
// But allocating another IPv4 should panic, exhausting the
|
|
|
|
// limited /24 range:
|
|
|
|
defer func() {
|
|
|
|
if e := recover(); fmt.Sprint(e) != "pool exhausted" {
|
|
|
|
t.Errorf("unexpected panic: %v", e)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
n.allocIPv4(nil)
|
|
|
|
t.Fatalf("expected panic from IPv4")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSendPacket(t *testing.T) {
|
|
|
|
internet := NewInternet()
|
|
|
|
|
|
|
|
foo := NewMachine("foo")
|
|
|
|
bar := NewMachine("bar")
|
|
|
|
ifFoo := foo.Attach("eth0", internet)
|
|
|
|
ifBar := bar.Attach("enp0s1", internet)
|
|
|
|
|
|
|
|
fooAddr := netaddr.IPPort{IP: ifFoo.V4(), Port: 123}
|
|
|
|
barAddr := netaddr.IPPort{IP: ifBar.V4(), Port: 456}
|
|
|
|
|
|
|
|
fooPC, err := foo.ListenPacket("udp4", fooAddr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
barPC, err := bar.ListenPacket("udp4", barAddr.String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
const msg = "some message"
|
|
|
|
if _, err := fooPC.WriteTo([]byte(msg), barAddr.UDPAddr()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, 1500) // TODO: care about MTUs in the natlab package somewhere
|
|
|
|
n, addr, err := barPC.ReadFrom(buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
buf = buf[:n]
|
|
|
|
if string(buf) != msg {
|
|
|
|
t.Errorf("read %q; want %q", buf, msg)
|
|
|
|
}
|
|
|
|
if addr.String() != fooAddr.String() {
|
|
|
|
t.Errorf("addr = %q; want %q", addr, fooAddr)
|
|
|
|
}
|
|
|
|
}
|
2020-07-03 00:52:58 +00:00
|
|
|
|
2020-07-03 01:47:06 +00:00
|
|
|
func TestMultiNetwork(t *testing.T) {
|
2020-07-03 02:21:51 +00:00
|
|
|
lan := &Network{
|
2020-07-03 01:47:06 +00:00
|
|
|
Name: "lan",
|
2020-07-03 00:52:58 +00:00
|
|
|
Prefix4: mustPrefix("192.168.0.0/24"),
|
|
|
|
}
|
2020-07-03 01:47:06 +00:00
|
|
|
internet := NewInternet()
|
2020-07-03 00:52:58 +00:00
|
|
|
|
2020-07-03 01:47:06 +00:00
|
|
|
client := NewMachine("client")
|
|
|
|
nat := NewMachine("nat")
|
|
|
|
server := NewMachine("server")
|
|
|
|
|
2020-07-03 02:21:51 +00:00
|
|
|
ifClient := client.Attach("eth0", lan)
|
2020-07-03 01:47:06 +00:00
|
|
|
ifNATWAN := nat.Attach("ethwan", internet)
|
2020-07-03 02:21:51 +00:00
|
|
|
ifNATLAN := nat.Attach("ethlan", lan)
|
2020-07-03 01:47:06 +00:00
|
|
|
ifServer := server.Attach("eth0", internet)
|
2020-07-03 00:52:58 +00:00
|
|
|
|
2020-07-03 01:47:06 +00:00
|
|
|
clientPC, err := client.ListenPacket("udp", ":123")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
natPC, err := nat.ListenPacket("udp", ":456")
|
2020-07-03 00:52:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-07-03 01:47:06 +00:00
|
|
|
serverPC, err := server.ListenPacket("udp", ":789")
|
2020-07-03 00:52:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-07-03 01:47:06 +00:00
|
|
|
clientAddr := netaddr.IPPort{IP: ifClient.V4(), Port: 123}
|
|
|
|
natLANAddr := netaddr.IPPort{IP: ifNATLAN.V4(), Port: 456}
|
|
|
|
natWANAddr := netaddr.IPPort{IP: ifNATWAN.V4(), Port: 456}
|
|
|
|
serverAddr := netaddr.IPPort{IP: ifServer.V4(), Port: 789}
|
|
|
|
|
|
|
|
const msg1, msg2 = "hello", "world"
|
|
|
|
if _, err := natPC.WriteTo([]byte(msg1), clientAddr.UDPAddr()); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, err := natPC.WriteTo([]byte(msg2), serverAddr.UDPAddr()); err != nil {
|
2020-07-03 00:52:58 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, 1500)
|
2020-07-03 01:47:06 +00:00
|
|
|
n, addr, err := clientPC.ReadFrom(buf)
|
2020-07-03 00:52:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-07-03 01:47:06 +00:00
|
|
|
if string(buf[:n]) != msg1 {
|
|
|
|
t.Errorf("read %q; want %q", buf[:n], msg1)
|
2020-07-03 00:52:58 +00:00
|
|
|
}
|
2020-07-03 01:47:06 +00:00
|
|
|
if addr.String() != natLANAddr.String() {
|
|
|
|
t.Errorf("addr = %q; want %q", addr, natLANAddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
n, addr, err = serverPC.ReadFrom(buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if string(buf[:n]) != msg2 {
|
|
|
|
t.Errorf("read %q; want %q", buf[:n], msg2)
|
|
|
|
}
|
|
|
|
if addr.String() != natWANAddr.String() {
|
|
|
|
t.Errorf("addr = %q; want %q", addr, natLANAddr)
|
2020-07-03 00:52:58 +00:00
|
|
|
}
|
|
|
|
}
|