1package smtpclient
2
3import (
4 "context"
5 "net"
6 "reflect"
7 "testing"
8 "time"
9
10 "github.com/mjl-/mox/dns"
11 "github.com/mjl-/mox/mlog"
12)
13
14func TestDialHost(t *testing.T) {
15 // We mostly want to test that dialing a second time switches to the other address family.
16 ctxbg := context.Background()
17 log := mlog.New("smtpclient", nil)
18
19 resolver := dns.MockResolver{
20 A: map[string][]string{
21 "dualstack.example.": {"10.0.0.1"},
22 },
23 AAAA: map[string][]string{
24 "dualstack.example.": {"2001:db8::1"},
25 },
26 }
27
28 DialHook = func(ctx context.Context, dialer Dialer, timeout time.Duration, addr string, laddr net.Addr) (net.Conn, error) {
29 return nil, nil // No error, nil connection isn't used.
30 }
31 defer func() {
32 DialHook = nil
33 }()
34
35 ipdomain := func(s string) dns.IPDomain {
36 return dns.IPDomain{Domain: dns.Domain{ASCII: s}}
37 }
38
39 dialedIPs := map[string][]net.IP{}
40 _, _, _, ips, dualstack, err := GatherIPs(ctxbg, log.Logger, resolver, "ip", ipdomain("dualstack.example"), dialedIPs)
41 if err != nil || !reflect.DeepEqual(ips, []net.IP{net.ParseIP("10.0.0.1"), net.ParseIP("2001:db8::1")}) || !dualstack {
42 t.Fatalf("expected err nil, address 10.0.0.1,2001:db8::1, dualstack true, got %v %v %v", err, ips, dualstack)
43 }
44 _, ip, err := Dial(ctxbg, log.Logger, nil, ipdomain("dualstack.example"), ips, 25, dialedIPs, nil)
45 if err != nil || ip.String() != "10.0.0.1" {
46 t.Fatalf("expected err nil, address 10.0.0.1, dualstack true, got %v %v %v", err, ip, dualstack)
47 }
48
49 _, _, _, ips, dualstack, err = GatherIPs(ctxbg, log.Logger, resolver, "ip", ipdomain("dualstack.example"), dialedIPs)
50 if err != nil || !reflect.DeepEqual(ips, []net.IP{net.ParseIP("2001:db8::1"), net.ParseIP("10.0.0.1")}) || !dualstack {
51 t.Fatalf("expected err nil, address 2001:db8::1,10.0.0.1, dualstack true, got %v %v %v", err, ips, dualstack)
52 }
53 _, ip, err = Dial(ctxbg, log.Logger, nil, ipdomain("dualstack.example"), ips, 25, dialedIPs, nil)
54 if err != nil || ip.String() != "2001:db8::1" {
55 t.Fatalf("expected err nil, address 2001:db8::1, dualstack true, got %v %v %v", err, ip, dualstack)
56 }
57}
58