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
|
package main
import (
"bytes"
"testing"
)
func TestConfig(t *testing.T) {
cases := []struct {
Config string
Tests map[string]string
}{
{
Config: `
# Comment
go.universe.tf 1.2.3.4
*.universe.tf 2.3.4.5
# Comment
google.* 3.4.5.6
/gooo+gle\.com/ 4.5.6.7
`,
Tests: map[string]string{
"go.universe.tf": "1.2.3.4",
"foo.universe.tf": "2.3.4.5",
"bar.universe.tf": "2.3.4.5",
"google.com": "3.4.5.6",
"google.fr": "3.4.5.6",
"goooooooooogle.com": "4.5.6.7",
"blah.com": "",
"google.com.br": "",
"foo.bar.universe.tf": "",
"goooooglexcom": "",
},
},
}
for _, test := range cases {
var cfg Config
if err := cfg.Read(bytes.NewBufferString(test.Config)); err != nil {
t.Fatalf("Failed to read config (%s):\n%q", err, test.Config)
}
for hostname, expected := range test.Tests {
actual := cfg.Match(hostname)
if expected != actual {
t.Errorf("cfg.Match(%q) is %q, want %q", hostname, actual, expected)
}
}
}
}
|