| 1 |
package autoconfig |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"fmt" |
| 6 |
"net" |
| 7 |
"testing" |
| 8 |
"time" |
| 9 |
|
| 10 |
"github.com/stretchr/testify/assert" |
| 11 |
) |
| 12 |
|
| 13 |
func TestDNSSRV(t *testing.T) { |
| 14 |
tests := []struct { |
| 15 |
address string |
| 16 |
correctConfig *Config |
| 17 |
}{ |
| 18 |
{ |
| 19 |
address: "gmail.com", |
| 20 |
correctConfig: &Config{ |
| 21 |
Found: ProtocolIMAP, |
| 22 |
IMAP: Credentials{ |
| 23 |
Encryption: EncryptionTLS, |
| 24 |
Address: "imap.gmail.com", |
| 25 |
Port: 993, |
| 26 |
Username: "[email protected]", |
| 27 |
}, |
| 28 |
SMTP: Credentials{ |
| 29 |
Encryption: EncryptionSTARTTLS, |
| 30 |
Address: "smtp.gmail.com", |
| 31 |
Port: 587, |
| 32 |
Username: "[email protected]", |
| 33 |
}, |
| 34 |
}, |
| 35 |
}, |
| 36 |
{ |
| 37 |
address: "fastmail.com", |
| 38 |
correctConfig: &Config{ |
| 39 |
Found: ProtocolJMAP, |
| 40 |
JMAP: Credentials{ |
| 41 |
Encryption: EncryptionTLS, |
| 42 |
Address: "api.fastmail.com", |
| 43 |
Port: 443, |
| 44 |
Username: "[email protected]", |
| 45 |
}, |
| 46 |
IMAP: Credentials{ |
| 47 |
Encryption: EncryptionTLS, |
| 48 |
Address: "imap.fastmail.com", |
| 49 |
Port: 993, |
| 50 |
Username: "[email protected]", |
| 51 |
}, |
| 52 |
SMTP: Credentials{ |
| 53 |
Encryption: EncryptionSTARTTLS, |
| 54 |
Address: "smtp.fastmail.com", |
| 55 |
Port: 587, |
| 56 |
Username: "[email protected]", |
| 57 |
}, |
| 58 |
}, |
| 59 |
}, |
| 60 |
} |
| 61 |
|
| 62 |
lookupSRV = customLookupSRV |
| 63 |
defer func() { |
| 64 |
lookupSRV = net.LookupSRV |
| 65 |
}() |
| 66 |
for _, test := range tests { |
| 67 |
t.Run(test.address, func(t *testing.T) { |
| 68 |
result := make(chan *Config) |
| 69 |
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 70 |
defer cancel() |
| 71 |
|
| 72 |
go getFromProviderDNS(ctx, "john", test.address, result) |
| 73 |
|
| 74 |
select { |
| 75 |
case res := <-result: |
| 76 |
if res == nil { |
| 77 |
t.Log("no result") |
| 78 |
t.FailNow() |
| 79 |
} |
| 80 |
assert.Equal(t, test.correctConfig, res) |
| 81 |
case <-ctx.Done(): |
| 82 |
t.Error("retrieval timed out!") |
| 83 |
} |
| 84 |
}) |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
func customLookupSRV(service, proto, name string) (cname string, addrs []*net.SRV, err error) { |
| 89 |
switch name { |
| 90 |
case "gmail.com": |
| 91 |
switch service { |
| 92 |
case "imap": |
| 93 |
return "_imap.gmail.com", []*net.SRV{ |
| 94 |
{ |
| 95 |
Target: ".", |
| 96 |
}, |
| 97 |
}, nil |
| 98 |
case "imaps": |
| 99 |
return "_simap.gmail.com", []*net.SRV{ |
| 100 |
{ |
| 101 |
Target: "imap.gmail.com.", |
| 102 |
Port: 993, |
| 103 |
Priority: 5, |
| 104 |
}, |
| 105 |
}, nil |
| 106 |
case "submission": |
| 107 |
return "_submission._tcp.gmail.com", []*net.SRV{ |
| 108 |
{ |
| 109 |
Target: "smtp.gmail.com.", |
| 110 |
Port: 587, |
| 111 |
Priority: 5, |
| 112 |
}, |
| 113 |
}, nil |
| 114 |
} |
| 115 |
case "fastmail.com": |
| 116 |
switch service { |
| 117 |
case "jmap": |
| 118 |
return "_jmap._tcp.fastmail.com", []*net.SRV{ |
| 119 |
{ |
| 120 |
Target: "api.fastmail.com.", |
| 121 |
Port: 443, |
| 122 |
Weight: 1, |
| 123 |
}, |
| 124 |
}, nil |
| 125 |
case "imap": |
| 126 |
return "_imap._tcp.fastmail.com", []*net.SRV{ |
| 127 |
{ |
| 128 |
Target: ".", |
| 129 |
}, |
| 130 |
}, nil |
| 131 |
case "imaps": |
| 132 |
return "_jmap._tcp.fastmail.com", []*net.SRV{ |
| 133 |
{ |
| 134 |
Target: "imap.fastmail.com.", |
| 135 |
Port: 993, |
| 136 |
Weight: 1, |
| 137 |
}, |
| 138 |
}, nil |
| 139 |
case "submission": |
| 140 |
return "_submission._tcp.fastmail.com", []*net.SRV{ |
| 141 |
{ |
| 142 |
Target: "smtp.fastmail.com.", |
| 143 |
Port: 587, |
| 144 |
Weight: 1, |
| 145 |
}, |
| 146 |
}, nil |
| 147 |
} |
| 148 |
case "timeout": |
| 149 |
<-time.After(5 * time.Minute) |
| 150 |
return "_._tcp.", []*net.SRV{ |
| 151 |
{}, |
| 152 |
}, nil |
| 153 |
} |
| 154 |
return "", nil, fmt.Errorf("lookup _%s._tcp.%s on 127.0.0.53:53: no such host", service, name) |
| 155 |
} |