summaryrefslogtreecommitdiff
path: root/internal/wireguard/wireguard_test.go
blob: ddb9d56c9ceb17d604e9ba7fc0a9b39df97a60b1 (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package wireguard

import (
	"fmt"
	"testing"

	"github.com/eduvpn/eduvpn-common/internal/test"
	"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)

func TestConfigReplace(t *testing.T) {
	k, err := wgtypes.GeneratePrivateKey()
	if err != nil {
		t.Fatalf("Failed to generate key for wg config replace: %v", err)
	}

	cases := []struct {
		config string
		proxy  string
		want   string
		wantep string
		werr   string
	}{
		{
			config: `
`,
			want:   "",
			wantep: "",
			proxy:  "",
			werr:   "parsed ini is empty",
		},
		{
			config: `
[Interface]
PrivateKey = bla
[interface]

[interface2]

interface

 [Interface]

[Interface]test
`,
			want: fmt.Sprintf(`[Interface]
PrivateKey = %s
[interface]
[interface2]
`, k.String()),
			wantep: "",
			proxy:  "",
			werr:   "",
		},
		{
			config: `
[Interface]
MTU = 1392
PrivateKey =
Address = 10.146.176.5/24,fdee:1ead:29e8:22a2::5/64
DNS = 9.9.9.9,2620:fe::fe

[Peer]
PublicKey =
AllowedIPs = 0.0.0.0/0,::/0
# TCPEndpoint is a proprietary eduVPN / Let's Connect! extension
# See https://docs.eduvpn.org/server/v3/proxyguard.html#client on how to use the TCP proxy
TCPEndpoint = vpn.example.org:51820
`,
			want: fmt.Sprintf(`[Interface]
MTU = 1392
PrivateKey = %s
Address = 10.146.176.5/24,fdee:1ead:29e8:22a2::5/64
DNS = 9.9.9.9,2620:fe::fe
[Peer]
PublicKey =
AllowedIPs = 0.0.0.0/0,::/0
Endpoint = 127.0.0.1:1337
`, k.String()),
			wantep: "vpn.example.org:51820",
			proxy:  "127.0.0.1:1337",
			werr:   "",
		},
	}

	for _, c := range cases {
		gcfg, gep, err := configReplace(c.config, k, c.proxy)
		test.AssertError(t, err, c.werr)
		if gcfg != c.want {
			t.Fatalf("Got config: %s, not equal to config: %s", gcfg, c.want)
		}

		if gep != c.wantep {
			t.Fatalf("Got endpoint: %s, not equal to endpoint: %s", gep, c.wantep)
		}
	}
}