101 lines
2.5 KiB
Go
101 lines
2.5 KiB
Go
package checker
|
|
|
|
import (
|
|
"testing"
|
|
|
|
tlsct "git.happydns.org/checker-tls/contract"
|
|
)
|
|
|
|
func TestProviderKey(t *testing.T) {
|
|
if (&smtpProvider{}).Key() != ObservationKeySMTP {
|
|
t.Error("Key mismatch")
|
|
}
|
|
}
|
|
|
|
func TestEndpointKey(t *testing.T) {
|
|
if got := endpointKey("mx.example.com", 25); got != "mx.example.com:25" {
|
|
t.Errorf("got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestDiscoverEntries_NilOrWrongType(t *testing.T) {
|
|
p := &smtpProvider{}
|
|
if out, _ := p.DiscoverEntries(nil); out != nil {
|
|
t.Errorf("nil → %+v", out)
|
|
}
|
|
if out, _ := p.DiscoverEntries("not-smtp-data"); out != nil {
|
|
t.Errorf("wrong type → %+v", out)
|
|
}
|
|
}
|
|
|
|
func TestDiscoverEntries_NullMX(t *testing.T) {
|
|
d := &SMTPData{MX: MXLookup{NullMX: true, Records: []MXRecord{{Target: "."}}}}
|
|
out, err := (&smtpProvider{}).DiscoverEntries(d)
|
|
if err != nil || out != nil {
|
|
t.Errorf("null MX should publish nothing, got %+v err=%v", out, err)
|
|
}
|
|
}
|
|
|
|
func TestDiscoverEntries_DedupesAndSkipsIPLiterals(t *testing.T) {
|
|
d := &SMTPData{MX: MXLookup{Records: []MXRecord{
|
|
{Target: "mx1.example.com"},
|
|
{Target: "mx1.example.com"}, // duplicate
|
|
{Target: "192.0.2.1", IsIPLiteral: true},
|
|
{Target: ""}, // skipped
|
|
{Target: "mx2.example.com"},
|
|
}}}
|
|
out, err := (&smtpProvider{}).DiscoverEntries(d)
|
|
if err != nil {
|
|
t.Fatalf("err: %v", err)
|
|
}
|
|
if len(out) != 2 {
|
|
t.Fatalf("want 2 entries, got %d", len(out))
|
|
}
|
|
for _, e := range out {
|
|
ep, err := tlsct.ParseEntry(e)
|
|
if err != nil {
|
|
t.Fatalf("parse entry: %v", err)
|
|
}
|
|
if ep.Port != smtpPort {
|
|
t.Errorf("port: got %d", ep.Port)
|
|
}
|
|
if ep.STARTTLS != "smtp" {
|
|
t.Errorf("starttls: got %q", ep.STARTTLS)
|
|
}
|
|
if ep.RequireSTARTTLS {
|
|
t.Errorf("RequireSTARTTLS should be false (opportunistic)")
|
|
}
|
|
if ep.SNI != ep.Host {
|
|
t.Errorf("SNI should default to host: %q vs %q", ep.SNI, ep.Host)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDefinition(t *testing.T) {
|
|
def := (&smtpProvider{}).Definition()
|
|
if def.ID != "smtp" {
|
|
t.Errorf("ID: %q", def.ID)
|
|
}
|
|
if !def.HasHTMLReport {
|
|
t.Error("expected HasHTMLReport")
|
|
}
|
|
if len(def.Rules) == 0 {
|
|
t.Error("expected rules")
|
|
}
|
|
if len(def.ObservationKeys) == 0 || def.ObservationKeys[0] != ObservationKeySMTP {
|
|
t.Errorf("ObservationKeys: %+v", def.ObservationKeys)
|
|
}
|
|
// Required option "domain" must be present.
|
|
var sawDomain bool
|
|
for _, o := range def.Options.RunOpts {
|
|
if o.Id == "domain" && o.Required {
|
|
sawDomain = true
|
|
}
|
|
}
|
|
if !sawDomain {
|
|
t.Error("expected required 'domain' option in RunOpts")
|
|
}
|
|
if def.Interval == nil || def.Interval.Default == 0 {
|
|
t.Error("expected Interval.Default")
|
|
}
|
|
}
|