mirror of
https://git.sr.ht/~rjarry/aerc
synced 2025-07-06 19:30:22 +02:00

Add a direct mailserver poker that tries to connect to the mailserver with the highest priority retrieved from via DNS. Signed-off-by: Moritz Poldrack <git@moritz.sh> Acked-by: Robin Jarry <robin@jarry.cc>
76 lines
1.4 KiB
Go
76 lines
1.4 KiB
Go
package autoconfig
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestMX(t *testing.T) {
|
|
tests := []struct {
|
|
address string
|
|
correctConfig *Config
|
|
}{
|
|
{
|
|
address: "poldrack.dev",
|
|
correctConfig: &Config{
|
|
Found: ProtocolIMAP,
|
|
IMAP: Credentials{
|
|
Encryption: EncryptionSTARTTLS,
|
|
Address: "mail.moritz.sh",
|
|
Port: 143,
|
|
Username: "john@poldrack.dev",
|
|
},
|
|
SMTP: Credentials{
|
|
Encryption: EncryptionSTARTTLS,
|
|
Address: "mail.moritz.sh",
|
|
Port: 587,
|
|
Username: "john@poldrack.dev",
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
netDial = mxTestDialer
|
|
lookupMX = mxTestLookup
|
|
defer func() {
|
|
netDial = net.Dial
|
|
lookupMX = net.LookupMX
|
|
}()
|
|
for _, test := range tests {
|
|
test := test
|
|
t.Run(test.address, func(t *testing.T) {
|
|
result := make(chan *Config)
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
go guessMX(ctx, "john", test.address, result)
|
|
|
|
select {
|
|
case res := <-result:
|
|
if res == nil {
|
|
t.Log("no result")
|
|
t.FailNow()
|
|
}
|
|
assert.Equal(t, test.correctConfig, res)
|
|
case <-ctx.Done():
|
|
t.Error("retrieval timed out!")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func mxTestLookup(address string) ([]*net.MX, error) {
|
|
switch address {
|
|
case "poldrack.dev":
|
|
return []*net.MX{
|
|
{Host: "mail.moritz.sh", Pref: 1},
|
|
}, nil
|
|
default:
|
|
return nil, errors.New("unknown address")
|
|
}
|
|
}
|