This repository has been archived by the owner on Jan 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathparse_test.go
82 lines (76 loc) · 1.47 KB
/
parse_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
package wireguardexporter_test
import (
"strings"
"testing"
"github.com/google/go-cmp/cmp"
wireguardexporter "github.com/mdlayher/wireguard_exporter"
)
func TestParsePeers(t *testing.T) {
tests := []struct {
name string
s string
peers map[string]string
ok bool
}{
{
name: "bad TOML",
s: "xxx",
},
{
name: "bad keys",
s: `
[bad]
[[bad.bad]]
`,
},
{
name: "bad public key",
s: `
[[peer]]
public_key = "x"
`,
},
{
name: "empty name",
s: `
[[peer]]
public_key = "AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE="
name = ""
`,
},
{
name: "ok",
s: `
[[peer]]
public_key = "AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE="
name = "foo"
[[peer]]
public_key = "AgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgI="
name = "bar"
`,
peers: map[string]string{
"AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE=": "foo",
"AgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgI=": "bar",
},
ok: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
peers, err := wireguardexporter.ParsePeers(strings.NewReader(tt.s))
if tt.ok && err != nil {
t.Fatalf("failed to parse peer mappings: %v", err)
}
if !tt.ok && err == nil {
t.Fatal("expected an error, but none occurred")
}
if err != nil {
t.Logf("err: %v", err)
return
}
if diff := cmp.Diff(tt.peers, peers); diff != "" {
t.Fatalf("unexpected peers (-want +got):\n%s", diff)
}
})
}
}