summaryrefslogtreecommitdiff
path: root/internal/wireguard/wireguard_test.go
blob: 812ed8f13dc2e596758ec9c819e8b5441b9165b8 (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
package wireguard

import (
	"fmt"
	"testing"

	"herkulessi.de/git/eduvpn-common/internal/test"
	"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)

func TestConfig(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
		want   string
		wantep string
		werr   string
	}{
		{
			config: `
`,
			want: "",
			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()),
			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
ProxyEndpoint = https://vpn.example.org/example
`,
			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
ProxyEndpoint = https://vpn.example.org/example
`, k.String()),
			werr: "",
		},
	}

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