8func TestParseLocalpart(t *testing.T) {
9 good := func(s string) {
11 _, err := ParseLocalpart(s)
13 t.Fatalf("unexpected error for localpart %q: %v", s, err)
17 bad := func(s string) {
19 _, err := ParseLocalpart(s)
21 t.Fatalf("did not see expected error for localpart %q", s)
23 if !errors.Is(err, ErrBadLocalpart) {
24 t.Fatalf("expected ErrBadLocalpart, got %v", err)
35 bad(`"`) // missing ending dquot
36 bad("\x00") // control not allowed
37 bad("\"\\") // ending with backslash
38 bad("\"\x01") // control not allowed in dquote
39 bad(`""leftover`) // leftover data after close dquote
42func TestParseAddress(t *testing.T) {
43 good := func(s string) {
45 _, err := ParseAddress(s)
47 t.Fatalf("unexpected error for localpart %q: %v", s, err)
51 bad := func(s string) {
53 _, err := ParseAddress(s)
55 t.Fatalf("did not see expected error for localpart %q", s)
57 if !errors.Is(err, ErrBadAddress) {
58 t.Fatalf("expected ErrBadAddress, got %v", err)
62 good("user@example.com")
63 bad("user@@example.com")
64 bad("user") // missing @domain
65 bad("@example.com") // missing localpart
66 bad(`"@example.com`) // missing ending dquot or domain
67 bad("\x00@example.com") // control not allowed
68 bad("\"\\@example.com") // missing @domain
69 bad("\"\x01@example.com") // control not allowed in dquote
70 bad(`""leftover@example.com`) // leftover data after close dquot
73func TestPackLocalpart(t *testing.T) {
77 {``, `""`}, // No atom.
78 {`a.`, `"a."`}, // Empty atom not allowed.
79 {`a.b`, `a.b`}, // Fine.
80 {"azAZ09!#$%&'*+-/=?^_`{|}~", "azAZ09!#$%&'*+-/=?^_`{|}~"}, // All ascii that are fine as atom.
82 {"\x01", "\"\x01\""}, // todo: should probably return an error for control characters.
87 r := Localpart(e.input).String()
89 t.Fatalf("PackLocalpart for %q, expect %q, got %q", e.input, e.expect, r)