-
Notifications
You must be signed in to change notification settings - Fork 0
/
wireguard_test.go
69 lines (66 loc) · 1.59 KB
/
wireguard_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package plexus
import (
"net"
"os/user"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/vishvananda/netlink"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
func TestNew(t *testing.T) {
user, err := user.Current()
assert.Nil(t, err)
if user.Uid != "0" {
t.Log("this test must be run as root")
t.FailNow()
}
key, err := wgtypes.GeneratePrivateKey()
assert.Nil(t, err)
peerKey, err := wgtypes.ParseKey("uREcerxMksoD3K0dy1ciJDRGzGCJ8jvIzJ5r9jWApXY=")
assert.Nil(t, err)
port := 51820
keepalive := time.Second * 25
config := wgtypes.Config{
PrivateKey: &key,
ListenPort: &port,
ReplacePeers: true,
Peers: []wgtypes.PeerConfig{
{
PublicKey: peerKey,
ReplaceAllowedIPs: true,
AllowedIPs: []net.IPNet{
{
IP: net.ParseIP("10.10.10.2"),
Mask: net.CIDRMask(32, 32),
},
},
Endpoint: &net.UDPAddr{
IP: net.ParseIP("8.8.8.8"),
Port: 51820,
},
PersistentKeepaliveInterval: &keepalive,
},
},
}
address := netlink.Addr{
IPNet: &net.IPNet{
IP: net.ParseIP("10.10.10.1"),
Mask: net.CIDRMask(24, 32),
},
}
wg := New("wgtest", 1420, address, config)
assert.Equal(t, "wgtest", wg.Attrs().Name)
assert.Equal(t, 1420, wg.Attrs().MTU)
err = wg.Up()
assert.Nil(t, err)
assert.Equal(t, "wireguard", wg.Type())
link, err := netlink.LinkByName(wg.Name)
assert.Nil(t, err)
assert.Equal(t, link.Attrs().Index, wg.Attrs().Index)
routes, err := netlink.RouteGet(net.ParseIP("10.10.10.10"))
assert.Nil(t, err)
assert.Equal(t, wg.Attrs().Index, routes[0].LinkIndex)
err = wg.Down()
assert.Nil(t, err)
}