| 1 |
package autoconfig |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"errors" |
| 6 |
"net" |
| 7 |
"testing" |
| 8 |
"time" |
| 9 |
|
| 10 |
"github.com/stretchr/testify/assert" |
| 11 |
) |
| 12 |
|
| 13 |
func TestMX(t *testing.T) { |
| 14 |
tests := []struct { |
| 15 |
address string |
| 16 |
correctConfig *Config |
| 17 |
}{ |
| 18 |
{ |
| 19 |
address: "poldrack.dev", |
| 20 |
correctConfig: &Config{ |
| 21 |
Found: ProtocolIMAP, |
| 22 |
IMAP: Credentials{ |
| 23 |
Encryption: EncryptionSTARTTLS, |
| 24 |
Address: "mail.moritz.sh", |
| 25 |
Port: 143, |
| 26 |
Username: "[email protected]", |
| 27 |
}, |
| 28 |
SMTP: Credentials{ |
| 29 |
Encryption: EncryptionSTARTTLS, |
| 30 |
Address: "mail.moritz.sh", |
| 31 |
Port: 587, |
| 32 |
Username: "[email protected]", |
| 33 |
}, |
| 34 |
}, |
| 35 |
}, |
| 36 |
} |
| 37 |
|
| 38 |
netDial = mxTestDialer |
| 39 |
lookupMX = mxTestLookup |
| 40 |
defer func() { |
| 41 |
netDial = net.Dial |
| 42 |
lookupMX = net.LookupMX |
| 43 |
}() |
| 44 |
for _, test := range tests { |
| 45 |
t.Run(test.address, func(t *testing.T) { |
| 46 |
result := make(chan *Config) |
| 47 |
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 48 |
defer cancel() |
| 49 |
|
| 50 |
go guessMX(ctx, "john", test.address, result) |
| 51 |
|
| 52 |
select { |
| 53 |
case res := <-result: |
| 54 |
if res == nil { |
| 55 |
t.Log("no result") |
| 56 |
t.FailNow() |
| 57 |
} |
| 58 |
assert.Equal(t, test.correctConfig, res) |
| 59 |
case <-ctx.Done(): |
| 60 |
t.Error("retrieval timed out!") |
| 61 |
} |
| 62 |
}) |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
func mxTestLookup(address string) ([]*net.MX, error) { |
| 67 |
switch address { |
| 68 |
case "poldrack.dev": |
| 69 |
return []*net.MX{ |
| 70 |
{Host: "mail.moritz.sh", Pref: 1}, |
| 71 |
}, nil |
| 72 |
default: |
| 73 |
return nil, errors.New("unknown address") |
| 74 |
} |
| 75 |
} |