|
| 1 | +package emailaddr |
| 2 | + |
| 3 | +import ( |
| 4 | + "math/rand/v2" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "golang.org/x/net/idna" |
| 9 | +) |
| 10 | + |
| 11 | +func TestNormalize(t *testing.T) { |
| 12 | + t.Parallel() |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + email string |
| 16 | + want string |
| 17 | + }{ |
| 18 | + {name: "ascii", email: " User+Tag@Example.COM ", want: "user+tag@example.com"}, |
| 19 | + {name: "unicode domain", email: "User@BÜCHER.example", want: "user@xn--bcher-kva.example"}, |
| 20 | + {name: "decomposed domain", email: "User@BU\u0308CHER.example", want: "user@xn--bcher-kva.example"}, |
| 21 | + {name: "punycode domain", email: "User@XN--BCHER-KVA.example", want: "user@xn--bcher-kva.example"}, |
| 22 | + {name: "ideographic dot", email: "User@Example。COM", want: "user@example.com"}, |
| 23 | + {name: "sharp s is not transitional", email: "user@straße.de", want: "user@xn--strae-oqa.de"}, |
| 24 | + {name: "capital sharp s maps per UTS 46", email: "user@STRAẞE.de", want: "user@strasse.de"}, |
| 25 | + {name: "dotted capital I composed", email: "user@İstanbul.com", want: "user@xn--istanbul-o0e.com"}, |
| 26 | + {name: "dotted capital I decomposed", email: "user@I\u0307stanbul.com", want: "user@xn--istanbul-o0e.com"}, |
| 27 | + {name: "unicode local is lowercased only", email: "Üser@Example.com", want: "üser@example.com"}, |
| 28 | + {name: "decomposed local is not recomposed", email: "U\u0308ser@Example.com", want: "u\u0308ser@example.com"}, |
| 29 | + {name: "invalid hostname keeps legacy key", email: "User@exa_mple.com", want: "user@exa_mple.com"}, |
| 30 | + {name: "empty label keeps legacy key", email: "user@a..b.com", want: "user@a..b.com"}, |
| 31 | + {name: "bare ace prefix label keeps legacy key", email: "user@xn--.example", want: "user@xn--.example"}, |
| 32 | + {name: "host that maps to nothing keeps legacy key", email: "user@\u00ad", want: "user@\u00ad"}, |
| 33 | + { |
| 34 | + name: "over-long label keeps legacy key", |
| 35 | + email: "user@" + strings.Repeat("a", 64) + ".com", |
| 36 | + want: "user@" + strings.Repeat("a", 64) + ".com", |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "invalid unicode domain keeps spelling, ascii case folded", |
| 40 | + email: "User@BÜCHER..Example", |
| 41 | + want: "user@bÜcher..example", |
| 42 | + }, |
| 43 | + {name: "space before at is preserved", email: "a b @Example.com", want: "a b @example.com"}, |
| 44 | + {name: "space after at keeps legacy key", email: "User@ Example.com", want: "user@ example.com"}, |
| 45 | + {name: "missing local part", email: "@Example.com", want: "@example.com"}, |
| 46 | + {name: "missing domain", email: "User@", want: "user@"}, |
| 47 | + {name: "not an email", email: " Not-An-Email ", want: "not-an-email"}, |
| 48 | + {name: "empty", email: "", want: ""}, |
| 49 | + } |
| 50 | + for _, test := range tests { |
| 51 | + t.Run(test.name, func(t *testing.T) { |
| 52 | + t.Parallel() |
| 53 | + if got := Normalize(test.email); got != test.want { |
| 54 | + t.Fatalf("Normalize(%q) = %q, want %q", test.email, got, test.want) |
| 55 | + } |
| 56 | + }) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestEqual(t *testing.T) { |
| 61 | + t.Parallel() |
| 62 | + same := [][2]string{ |
| 63 | + {"User@BÜCHER.example", "user@bücher.example"}, |
| 64 | + {"user@bücher.example", "user@bu\u0308cher.example"}, |
| 65 | + {"user@bücher.example", "user@xn--bcher-kva.example"}, |
| 66 | + {"Üser@example.com", "üser@example.com"}, |
| 67 | + {" user@example.com ", "USER@EXAMPLE.COM"}, |
| 68 | + } |
| 69 | + for _, pair := range same { |
| 70 | + if !Equal(pair[0], pair[1]) { |
| 71 | + t.Errorf("Equal(%q, %q) = false, want true", pair[0], pair[1]) |
| 72 | + } |
| 73 | + } |
| 74 | + different := [][2]string{ |
| 75 | + {"user@example.com", "user@example.org"}, |
| 76 | + {"user@example.com", "other@example.com"}, |
| 77 | + {"user@example.com", "user@example.com."}, |
| 78 | + {"üser@example.com", "u\u0308ser@example.com"}, |
| 79 | + } |
| 80 | + for _, pair := range different { |
| 81 | + if Equal(pair[0], pair[1]) { |
| 82 | + t.Errorf("Equal(%q, %q) = true, want false", pair[0], pair[1]) |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func TestNormalizeIsIdempotent(t *testing.T) { |
| 88 | + t.Parallel() |
| 89 | + alphabet := []rune("aZ09.-_+@ üÜ\u0308ßẞİ。中文\u0301ex") |
| 90 | + r := rand.New(rand.NewPCG(7, 0)) |
| 91 | + for range 50000 { |
| 92 | + runes := make([]rune, r.IntN(24)) |
| 93 | + for j := range runes { |
| 94 | + runes[j] = alphabet[r.IntN(len(alphabet))] |
| 95 | + } |
| 96 | + input := string(runes) |
| 97 | + once := Normalize(input) |
| 98 | + if twice := Normalize(once); twice != once { |
| 99 | + t.Fatalf("Normalize is not idempotent for %q: %q then %q", input, once, twice) |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func asciiCorpus() []string { |
| 105 | + const ldh = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-" |
| 106 | + domains := []string{ |
| 107 | + "example.com", "EXAMPLE.COM", "example.com.", "-bad.com", "bad-.com", "ab--cd.com", |
| 108 | + "xn--bcher-kva.example", "XN--BCHER-KVA.example", "xn--zzzz.example", "xn--.example", |
| 109 | + "example.xn--", "a.xn--.b", "xn--", "a_b.com", "localhost", "1.2.3.4", "[1.2.3.4]", |
| 110 | + "exa mple.com", "ex!ample.com", "", ".", "a..b.com", ".example.com", |
| 111 | + strings.Repeat("a", 63) + ".com", strings.Repeat("a", 64) + ".com", |
| 112 | + strings.Repeat("a.", 130) + "com", |
| 113 | + } |
| 114 | + r := rand.New(rand.NewPCG(1, 0)) |
| 115 | + for range 50000 { |
| 116 | + labels := make([]string, 1+r.IntN(4)) |
| 117 | + for j := range labels { |
| 118 | + label := make([]byte, 1+r.IntN(12)) |
| 119 | + for k := range label { |
| 120 | + label[k] = ldh[r.IntN(len(ldh))] |
| 121 | + } |
| 122 | + if r.IntN(10) == 0 { |
| 123 | + labels[j] = "xn--" + string(label) |
| 124 | + } else { |
| 125 | + labels[j] = string(label) |
| 126 | + } |
| 127 | + } |
| 128 | + domains = append(domains, strings.Join(labels, ".")) |
| 129 | + } |
| 130 | + return domains |
| 131 | +} |
| 132 | + |
| 133 | +func TestNormalizeASCIIIsFixedPoint(t *testing.T) { |
| 134 | + t.Parallel() |
| 135 | + locals := []string{"User+Tag", "a b", "a ", "\"quoted@part\"", "x.y_z-9", "A"} |
| 136 | + for _, domain := range asciiCorpus() { |
| 137 | + for _, local := range locals { |
| 138 | + for _, input := range []string{" " + local + "@" + domain + " ", local + "@ " + domain} { |
| 139 | + if got, want := Normalize(input), strings.ToLower(strings.TrimSpace(input)); got != want { |
| 140 | + t.Fatalf("ASCII input %q changed: got %q, want %q", input, got, want) |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +func TestDomainProfileOnlyAddsFailuresToLookup(t *testing.T) { |
| 148 | + t.Parallel() |
| 149 | + inputs := append(asciiCorpus(), |
| 150 | + "bücher.example", "BÜCHER.example", "bu\u0308cher.example", "straße.de", "STRAẞE.de", |
| 151 | + "İstanbul.com", "I\u0307stanbul.com", "example。com", "\u0308example.com", "中文.中国", |
| 152 | + ) |
| 153 | + for _, input := range inputs { |
| 154 | + want, lookupErr := idna.Lookup.ToASCII(input) |
| 155 | + got, err := domain.ToASCII(input) |
| 156 | + if lookupErr != nil && err == nil { |
| 157 | + t.Fatalf("domain profile accepted %q which idna.Lookup rejects: %v", input, lookupErr) |
| 158 | + } |
| 159 | + if err == nil && got != want { |
| 160 | + t.Fatalf("domain profile ToASCII(%q) = %q, idna.Lookup = %q", input, got, want) |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments