-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmxs_test.go
73 lines (69 loc) · 1.76 KB
/
mxs_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
package sts
import (
"fmt"
"net"
"testing"
)
func mxsAreEqual(a, b []*net.MX) bool {
if len(a) != len(b) {
return false
}
for _, x := range a {
found := false
for _, y := range b {
if y != nil && x != nil && *y == *x {
found = true
break
}
}
if !found {
return false
}
}
return true
}
func TestValidateMx(t *testing.T) {
ts := []struct {
MX []*net.MX
P Policy
WantMX []*net.MX
E error
}{
{
MX: []*net.MX{{
Host: "mx1.example.com."}, {Host: "mx2.example.com."}},
P: Policy{MXs: []string{"*.example.com"}},
WantMX: []*net.MX{{Host: "mx1.example.com."}, {Host: "mx2.example.com."}},
E: nil,
},
{
MX: []*net.MX{{
Host: "mx1.example.com."}, {Host: "mx2.example.com."}},
P: Policy{MXs: []string{"mx1.example.com"}},
WantMX: []*net.MX{{Host: "mx1.example.com."}},
E: fmt.Errorf("mx2.example.com. does not match allowed MXes"),
},
{
MX: []*net.MX{{
Host: "mx1.example.com."}, {Host: "mx2.example.net."}},
P: Policy{MXs: []string{"*.example.com"}},
WantMX: []*net.MX{{Host: "mx1.example.com."}},
E: fmt.Errorf("mx2.example.net. does not match allowed MXes"),
},
{
MX: []*net.MX{{
Host: "mx1.example.com."}, {Host: "mx2.example.net."}},
P: Policy{MXs: []string{"*.example.com", "*.example.net"}},
WantMX: []*net.MX{{Host: "mx1.example.com."}, {Host: "mx2.example.net."}},
E: nil,
},
}
for _, want := range ts {
gotMX, e := FilterMXs(want.MX, want.P)
if (e != nil && want.E == nil) || (e == nil && want.E != nil) ||
(e != nil && want.E != nil && e.Error() != want.E.Error()) ||
!mxsAreEqual(want.WantMX, gotMX) {
t.Errorf("FetchValidMX(...): want (%v, %v), got (%v, %v)", want.WantMX, want.E, gotMX, e)
}
}
}