-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplate_test.go
124 lines (114 loc) · 2.69 KB
/
template_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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"bytes"
"fmt"
"net"
"testing"
"text/template"
)
var peer = Peer{
Active: true,
Name: "Louie",
ID: 3,
PublicKey: "abcdefg0==",
PrivateKey: "shh==secret",
VirtualIP: "10.11.32.87",
}
func TestServerConfigTemplates(t *testing.T) {
baseOutput := `[Interface]
Address = 10.22.65.87/16
ListenPort = 4566
PrivateKey = topsecret==
PostUp = iptables -A FORWARD -i %i -o %i -j ACCEPT
PostDown = iptables -D FORWARD -i %i -o %i -j ACCEPT
SaveConfig = false
`
peerOutput := `
# Louie
[Peer]
PublicKey = abcdefg0==
AllowedIPs = 10.11.32.87/32
`
var serverTemplates = []struct {
numPeers int
out string
}{
{0, baseOutput},
{1, baseOutput + peerOutput},
{2, baseOutput + peerOutput + peerOutput},
}
for _, tt := range serverTemplates {
t.Run(fmt.Sprintf("%d peers in template", tt.numPeers), func(t *testing.T) {
tmpl := template.Must(template.ParseFiles("templates/server.conf.tmpl"))
var b bytes.Buffer
s := MakeTestServerConfig()
for i := 0; i < tt.numPeers; i++ {
s.Peers = append(s.Peers, peer)
}
err := tmpl.Execute(&b, *s)
if err != nil {
t.Fatalf("error opening template: %s", err)
}
if b.String() != tt.out {
t.Errorf("got: %s, want: %s\n", b.String(), tt.out)
}
})
}
}
func TestPeerConfigTemplate(t *testing.T) {
var peerTemplates = []struct {
qr bool
output string
}{
{false, `[Interface]
Address = 10.11.32.87/32
PrivateKey = shh==secret
[Peer]
PublicKey = helloworld==
Endpoint = 188.272.271.04:4566
AllowedIPs = 10.22.0.0/16
`},
{true, `[Interface]
Address = 10.11.32.87/32
PrivateKey = shh==secret
[Peer]
PublicKey = helloworld==
Endpoint = 188.272.271.04:4566
AllowedIPs = 10.22.0.0/16
`},
}
for _, tt := range peerTemplates {
t.Run(fmt.Sprintf("QR code requested: %v", tt.qr), func(t *testing.T) {
tmpl := template.Must(template.ParseFiles("templates/peer.conf.tmpl"))
var b bytes.Buffer
s := MakeTestServerConfig()
_, ipNet, err := net.ParseCIDR(s.VirtualIP + "/" + s.CIDR)
if err != nil {
t.Errorf("error parsing CIDR: %s", err)
}
err = tmpl.Execute(&b, struct {
VirtualIP string
PrivateKey string
ServerPublicKey string
PublicIP string
Port string
AllowedIPs string
QRCode bool
}{
VirtualIP: peer.VirtualIP,
PrivateKey: peer.PrivateKey,
ServerPublicKey: s.PublicKey,
PublicIP: s.PublicIP,
Port: s.Port,
AllowedIPs: ipNet.String(),
QRCode: tt.qr,
})
if err != nil {
t.Fatalf("error opening template: %s", err)
}
if b.String() != tt.output {
t.Errorf("got: %s, want: %s\n", b.String(), tt.output)
}
})
}
}