146 lines
5.8 KiB
Go
146 lines
5.8 KiB
Go
package domain
|
||
|
||
import (
|
||
"bytes"
|
||
"errors"
|
||
"fmt"
|
||
"log/slog"
|
||
"strings"
|
||
"testing"
|
||
)
|
||
|
||
func TestNewEmailAddressNormalizes(t *testing.T) {
|
||
tests := []struct {
|
||
name string
|
||
raw string
|
||
wantAddress string
|
||
wantNormalized string
|
||
}{
|
||
{name: "plain", raw: "candidate@example.com", wantAddress: "candidate@example.com", wantNormalized: "candidate@example.com"},
|
||
{name: "trims and keeps case for delivery", raw: " \tAlice.Smith@Example.COM\n", wantAddress: "Alice.Smith@Example.COM", wantNormalized: "alice.smith@example.com"},
|
||
{name: "atext characters", raw: "O'Neil+Tag_1!#$%&*/=?^`{|}~-x@example.com", wantAddress: "O'Neil+Tag_1!#$%&*/=?^`{|}~-x@example.com", wantNormalized: "o'neil+tag_1!#$%&*/=?^`{|}~-x@example.com"},
|
||
{name: "punycode domain stays ASCII", raw: "Info@XN--E1AFMKFD.xn--p1ai", wantAddress: "Info@XN--E1AFMKFD.xn--p1ai", wantNormalized: "info@xn--e1afmkfd.xn--p1ai"},
|
||
{name: "minimum length", raw: "a@b.c", wantAddress: "a@b.c", wantNormalized: "a@b.c"},
|
||
}
|
||
for _, test := range tests {
|
||
t.Run(test.name, func(t *testing.T) {
|
||
email, err := NewEmailAddress(test.raw)
|
||
if err != nil {
|
||
t.Fatalf("NewEmailAddress(%q) error = %v", test.raw, err)
|
||
}
|
||
if email.Address() != test.wantAddress || email.Normalized() != test.wantNormalized {
|
||
t.Fatalf("NewEmailAddress(%q) = (%q, %q), want (%q, %q)",
|
||
test.raw, email.Address(), email.Normalized(), test.wantAddress, test.wantNormalized)
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
func TestNewEmailAddressCaseVariantsShareTheKey(t *testing.T) {
|
||
variants := []string{"Candidate@Example.com", "CANDIDATE@EXAMPLE.COM", " candidate@example.com "}
|
||
first, err := NewEmailAddress(variants[0])
|
||
if err != nil {
|
||
t.Fatalf("NewEmailAddress(%q) error = %v", variants[0], err)
|
||
}
|
||
for _, variant := range variants[1:] {
|
||
email, err := NewEmailAddress(variant)
|
||
if err != nil {
|
||
t.Fatalf("NewEmailAddress(%q) error = %v", variant, err)
|
||
}
|
||
if email.Normalized() != first.Normalized() {
|
||
t.Fatalf("Normalized(%q) = %q, want %q", variant, email.Normalized(), first.Normalized())
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestNewEmailAddressRejectsInvalidInput(t *testing.T) {
|
||
longLocal := strings.Repeat("a", maxEmailLocalPartSize+1) + "@example.com"
|
||
longLabel := "a@" + strings.Repeat("b", maxEmailDomainLabel+1) + ".com"
|
||
tooLong := strings.Repeat("a", 60) + "@" + strings.Repeat(strings.Repeat("b", 60)+".", 3) + "example.com"
|
||
tests := map[string]string{
|
||
"empty": "",
|
||
"blank": " ",
|
||
"too short": "a@",
|
||
"too long": tooLong,
|
||
"no at": "candidate.example.com",
|
||
"two ats": "a@b@example.com",
|
||
"empty local part": "@example.com",
|
||
"empty domain": "candidate@",
|
||
"single-label domain": "candidate@localhost",
|
||
"leading dot": ".candidate@example.com",
|
||
"trailing dot": "candidate.@example.com",
|
||
"consecutive dots": "can..didate@example.com",
|
||
"inner space": "can didate@example.com",
|
||
"quoted local part": `"can didate"@example.com`,
|
||
"display name": "Candidate <candidate@example.com>",
|
||
"comment": "candidate(comment)@example.com",
|
||
"control character": "cand\x00idate@example.com",
|
||
"empty domain label": "candidate@example..com",
|
||
"hyphen edge label": "candidate@-example.com",
|
||
"ip literal": "candidate@[127.0.0.1]",
|
||
"underscore in domain": "candidate@exa_mple.com",
|
||
"local part too long": longLocal,
|
||
"domain label too long": longLabel,
|
||
"invalid utf-8": "cand\xffidate@example.com",
|
||
// Non-ASCII input is rejected until IDNA and SMTPUTF8 are supported.
|
||
"dotted capital I": "İnfo@x.com",
|
||
"fullwidth domain": "info@example.com",
|
||
"mixed-script homoglyph": "pаypal@example.com",
|
||
"cyrillic domain": "info@пример.рф",
|
||
"decomposed accent": "José@example.com",
|
||
"non-breaking space trim": " candidate@example.com",
|
||
}
|
||
for name, raw := range tests {
|
||
t.Run(name, func(t *testing.T) {
|
||
if _, err := NewEmailAddress(raw); !errors.Is(err, ErrInvalidEmailAddress) {
|
||
t.Fatalf("NewEmailAddress(%q) error = %v, want ErrInvalidEmailAddress", raw, err)
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
func TestRestoreEmailAddressChecksOnlyConsistency(t *testing.T) {
|
||
// A stored address that today's rule would reject still restores, as long
|
||
// as its key matches.
|
||
restored, err := RestoreEmailAddress("Legacy@Host", "legacy@host")
|
||
if err != nil {
|
||
t.Fatalf("RestoreEmailAddress(consistent) error = %v", err)
|
||
}
|
||
if restored.Address() != "Legacy@Host" || restored.Normalized() != "legacy@host" {
|
||
t.Fatalf("RestoreEmailAddress() = (%q, %q)", restored.Address(), restored.Normalized())
|
||
}
|
||
for _, pair := range [][2]string{{"Legacy@Host", "Legacy@Host"}, {"a@example.com", "b@example.com"}, {"", ""}} {
|
||
if _, err := RestoreEmailAddress(pair[0], pair[1]); !errors.Is(err, ErrInvalidEmailAddress) {
|
||
t.Fatalf("RestoreEmailAddress(%q, %q) error = %v, want ErrInvalidEmailAddress", pair[0], pair[1], err)
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestEmailAddressZeroValue(t *testing.T) {
|
||
if !(EmailAddress{}).IsZero() {
|
||
t.Fatal("zero EmailAddress is not reported as zero")
|
||
}
|
||
email, err := NewEmailAddress("candidate@example.com")
|
||
if err != nil {
|
||
t.Fatalf("NewEmailAddress() error = %v", err)
|
||
}
|
||
if email.IsZero() {
|
||
t.Fatal("valid EmailAddress is reported as zero")
|
||
}
|
||
}
|
||
|
||
func TestEmailAddressIsRedacted(t *testing.T) {
|
||
email, err := NewEmailAddress("Secret.Person@Example.com")
|
||
if err != nil {
|
||
t.Fatalf("NewEmailAddress() error = %v", err)
|
||
}
|
||
var logs bytes.Buffer
|
||
slog.New(slog.NewJSONHandler(&logs, nil)).Info("redaction", "email", email)
|
||
formatted := fmt.Sprintf("%v %+v %#v %s %q %x %d", email, email, email, email, email, email, email) + logs.String()
|
||
for _, secret := range []string{email.Address(), email.Normalized(), "Secret", "secret"} {
|
||
if strings.Contains(formatted, secret) {
|
||
t.Fatalf("formatted output exposes the address: %s", formatted)
|
||
}
|
||
}
|
||
}
|