-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
transaction_resolver_test.go
116 lines (112 loc) · 2.87 KB
/
transaction_resolver_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
package msmtpd
import (
"context"
"net"
"net/smtp"
"testing"
"time"
)
func TestTransaction_Resolver(t *testing.T) {
tr := Transaction{
ID: "testTransactionResolver",
}
resolver := tr.Resolver()
addrs, err := resolver.LookupMX(context.Background(), "yandex.ru")
if err != nil {
t.Errorf("%s : while resolving MX for yandex.ru", err)
}
for i := range addrs {
t.Logf("MX %s %v", addrs[i].Host, addrs[i].Pref)
}
}
func TestTransaction_Resolver_In_Server(t *testing.T) {
addr, closer := RunTestServerWithoutTLS(t, &Server{
HeloCheckers: []HelloChecker{
func(tr *Transaction) error {
name := tr.HeloName
addrs, err := tr.Resolver().LookupMX(context.Background(), name)
if err != nil {
return err
}
if name != "yandex.ru" {
t.Errorf("wrong HELO provided %s", name)
}
for i := range addrs {
t.Logf("MX %s %v resolved for %s", addrs[i].Host, addrs[i].Pref, name)
}
if len(addrs) != 1 {
t.Errorf("not enough mx resolved")
return ErrorSMTP{Code: 555, Message: "not enough mx resolved"}
}
if addrs[0].Host != "mx.yandex.ru." {
t.Errorf("wrong mx resolved - %s", addrs[0].Host)
}
if addrs[0].Pref != 10 {
t.Errorf("wrong mx priority - %v", addrs[0].Pref)
}
return nil
},
},
})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Errorf("Dial failed: %v", err)
}
if err = c.Hello("yandex.ru"); err != nil {
t.Errorf("HELO failed: %v", err)
}
if err = c.Quit(); err != nil {
t.Errorf("Quit failed: %v", err)
}
}
func TestTransaction_Resolver_In_Server_Custom(t *testing.T) {
addr, closer := RunTestServerWithoutTLS(t, &Server{
Resolver: &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{
Timeout: time.Millisecond * time.Duration(10000),
}
return d.DialContext(ctx, network, "8.8.8.8:53")
},
},
HeloCheckers: []HelloChecker{
func(tr *Transaction) error {
name := tr.HeloName
addrs, err := tr.Resolver().LookupMX(context.Background(), name)
if err != nil {
return err
}
if name != "yandex.ru" {
t.Errorf("wrong HELO provided %s", name)
}
for i := range addrs {
t.Logf("MX %s %v resolved for %s", addrs[i].Host, addrs[i].Pref, name)
}
if len(addrs) != 1 {
t.Errorf("not enough mx resolved")
return ErrorSMTP{Code: 555, Message: "not enough mx resolved"}
}
if addrs[0].Host != "mx.yandex.ru." {
t.Errorf("wrong mx resolved - %s", addrs[0].Host)
}
if addrs[0].Pref != 10 {
t.Errorf("wrong mx priority - %v", addrs[0].Pref)
}
return nil
},
},
})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Errorf("Dial failed: %v", err)
}
if err = c.Hello("yandex.ru"); err != nil {
t.Errorf("HELO failed: %v", err)
}
if err = c.Quit(); err != nil {
t.Errorf("Quit failed: %v", err)
}
}