10 "github.com/mjl-/mox/dns"
11 "github.com/mjl-/mox/mlog"
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)
19 resolver := dns.MockResolver{
20 A: map[string][]string{
21 "dualstack.example.": {"10.0.0.1"},
23 AAAA: map[string][]string{
24 "dualstack.example.": {"2001:db8::1"},
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.
35 ipdomain := func(s string) dns.IPDomain {
36 return dns.IPDomain{Domain: dns.Domain{ASCII: s}}
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)
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)
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)
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)