2019-01-02 00:55:51 +00:00
|
|
|
/* SPDX-License-Identifier: MIT
|
2018-05-03 13:04:00 +00:00
|
|
|
*
|
2020-05-02 08:08:26 +00:00
|
|
|
* Copyright (C) 2017-2020 WireGuard LLC. All Rights Reserved.
|
2018-05-03 13:04:00 +00:00
|
|
|
*/
|
|
|
|
|
2019-03-03 03:04:41 +00:00
|
|
|
package device
|
2018-03-08 15:44:27 +00:00
|
|
|
|
2019-05-29 16:18:20 +00:00
|
|
|
import (
|
2019-10-12 16:44:05 +00:00
|
|
|
"bufio"
|
2019-05-29 16:18:20 +00:00
|
|
|
"bytes"
|
2020-07-30 16:20:49 +00:00
|
|
|
"fmt"
|
2019-10-12 16:44:05 +00:00
|
|
|
"net"
|
|
|
|
"strings"
|
2019-05-29 16:18:20 +00:00
|
|
|
"testing"
|
2019-10-12 16:44:05 +00:00
|
|
|
"time"
|
|
|
|
|
2020-01-07 15:43:17 +00:00
|
|
|
"golang.zx2c4.com/wireguard/tun/tuntest"
|
2019-05-29 16:18:20 +00:00
|
|
|
)
|
2018-03-08 15:44:27 +00:00
|
|
|
|
2020-07-30 16:20:49 +00:00
|
|
|
func getFreePort(t *testing.T) string {
|
|
|
|
l, err := net.ListenPacket("udp", "localhost:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
return fmt.Sprintf("%d", l.LocalAddr().(*net.UDPAddr).Port)
|
|
|
|
}
|
|
|
|
|
2019-10-12 16:44:05 +00:00
|
|
|
func TestTwoDevicePing(t *testing.T) {
|
2020-07-30 16:20:49 +00:00
|
|
|
port1 := getFreePort(t)
|
|
|
|
port2 := getFreePort(t)
|
|
|
|
|
2019-10-12 16:44:05 +00:00
|
|
|
cfg1 := `private_key=481eb0d8113a4a5da532d2c3e9c14b53c8454b34ab109676f6b58c2245e37b58
|
2020-07-30 16:20:49 +00:00
|
|
|
listen_port={{PORT1}}
|
2019-10-12 16:44:05 +00:00
|
|
|
replace_peers=true
|
|
|
|
public_key=f70dbb6b1b92a1dde1c783b297016af3f572fef13b0abb16a2623d89a58e9725
|
|
|
|
protocol_version=1
|
|
|
|
replace_allowed_ips=true
|
|
|
|
allowed_ip=1.0.0.2/32
|
2020-07-30 16:20:49 +00:00
|
|
|
endpoint=127.0.0.1:{{PORT2}}`
|
|
|
|
cfg1 = strings.ReplaceAll(cfg1, "{{PORT1}}", port1)
|
|
|
|
cfg1 = strings.ReplaceAll(cfg1, "{{PORT2}}", port2)
|
|
|
|
|
2020-01-07 15:43:17 +00:00
|
|
|
tun1 := tuntest.NewChannelTUN()
|
2019-10-12 16:44:05 +00:00
|
|
|
dev1 := NewDevice(tun1.TUN(), NewLogger(LogLevelDebug, "dev1: "))
|
|
|
|
dev1.Up()
|
|
|
|
defer dev1.Close()
|
|
|
|
if err := dev1.IpcSetOperation(bufio.NewReader(strings.NewReader(cfg1))); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-03-08 15:44:27 +00:00
|
|
|
|
2019-10-12 16:44:05 +00:00
|
|
|
cfg2 := `private_key=98c7989b1661a0d64fd6af3502000f87716b7c4bbcf00d04fc6073aa7b539768
|
2020-07-30 16:20:49 +00:00
|
|
|
listen_port={{PORT2}}
|
2019-10-12 16:44:05 +00:00
|
|
|
replace_peers=true
|
|
|
|
public_key=49e80929259cebdda4f322d6d2b1a6fad819d603acd26fd5d845e7a123036427
|
|
|
|
protocol_version=1
|
|
|
|
replace_allowed_ips=true
|
|
|
|
allowed_ip=1.0.0.1/32
|
2020-07-30 16:20:49 +00:00
|
|
|
endpoint=127.0.0.1:{{PORT1}}`
|
|
|
|
cfg2 = strings.ReplaceAll(cfg2, "{{PORT1}}", port1)
|
|
|
|
cfg2 = strings.ReplaceAll(cfg2, "{{PORT2}}", port2)
|
|
|
|
|
2020-01-07 15:43:17 +00:00
|
|
|
tun2 := tuntest.NewChannelTUN()
|
2019-10-12 16:44:05 +00:00
|
|
|
dev2 := NewDevice(tun2.TUN(), NewLogger(LogLevelDebug, "dev2: "))
|
|
|
|
dev2.Up()
|
|
|
|
defer dev2.Close()
|
|
|
|
if err := dev2.IpcSetOperation(bufio.NewReader(strings.NewReader(cfg2))); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-03-08 15:44:27 +00:00
|
|
|
|
2019-10-12 16:44:05 +00:00
|
|
|
t.Run("ping 1.0.0.1", func(t *testing.T) {
|
2020-01-07 15:43:17 +00:00
|
|
|
msg2to1 := tuntest.Ping(net.ParseIP("1.0.0.1"), net.ParseIP("1.0.0.2"))
|
2019-10-12 16:44:05 +00:00
|
|
|
tun2.Outbound <- msg2to1
|
|
|
|
select {
|
|
|
|
case msgRecv := <-tun1.Inbound:
|
|
|
|
if !bytes.Equal(msg2to1, msgRecv) {
|
|
|
|
t.Error("ping did not transit correctly")
|
|
|
|
}
|
|
|
|
case <-time.After(300 * time.Millisecond):
|
|
|
|
t.Error("ping did not transit")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("ping 1.0.0.2", func(t *testing.T) {
|
2020-01-07 15:43:17 +00:00
|
|
|
msg1to2 := tuntest.Ping(net.ParseIP("1.0.0.2"), net.ParseIP("1.0.0.1"))
|
2019-10-12 16:44:05 +00:00
|
|
|
tun1.Outbound <- msg1to2
|
|
|
|
select {
|
|
|
|
case msgRecv := <-tun2.Inbound:
|
|
|
|
if !bytes.Equal(msg1to2, msgRecv) {
|
|
|
|
t.Error("return ping did not transit correctly")
|
|
|
|
}
|
|
|
|
case <-time.After(300 * time.Millisecond):
|
|
|
|
t.Error("return ping did not transit")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-03-08 15:44:27 +00:00
|
|
|
|
2019-05-29 16:18:20 +00:00
|
|
|
func assertNil(t *testing.T, err error) {
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-03 20:44:06 +00:00
|
|
|
func assertEqual(t *testing.T, a, b []byte) {
|
|
|
|
if !bytes.Equal(a, b) {
|
2019-05-29 16:18:20 +00:00
|
|
|
t.Fatal(a, "!=", b)
|
|
|
|
}
|
|
|
|
}
|
2019-10-12 16:44:05 +00:00
|
|
|
|
|
|
|
func randDevice(t *testing.T) *Device {
|
|
|
|
sk, err := newPrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
tun := newDummyTUN("dummy")
|
|
|
|
logger := NewLogger(LogLevelError, "")
|
|
|
|
device := NewDevice(tun, logger)
|
|
|
|
device.SetPrivateKey(sk)
|
|
|
|
return device
|
|
|
|
}
|