From 42a0048119d9a8f99710fa0e971b540fae17de0d Mon Sep 17 00:00:00 2001 From: IKEDA Soji Date: Tue, 29 Aug 2023 08:31:53 +0900 Subject: [PATCH] Fix broken encodeXtext() --- client_test.go | 26 ++++++++++++++++++++++++++ conn.go | 12 +++++------- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/client_test.go b/client_test.go index 17eac3c..ba8dfe3 100644 --- a/client_test.go +++ b/client_test.go @@ -931,3 +931,29 @@ Goodbye.` t.Fatalf("QUIT failed: %s", err) } } + +func TestClientXtext(t *testing.T) { + server := "220 hello world\r\n" + + "200 some more" + var wrote bytes.Buffer + var fake faker + fake.ReadWriter = struct { + io.Reader + io.Writer + }{ + strings.NewReader(server), + &wrote, + } + c, err := NewClient(fake, "fake.host") + if err != nil { + t.Fatalf("NewClient: %v", err) + } + c.didHello = true + c.ext = map[string]string{"AUTH": "PLAIN"} + email := "e=mc2@example.com" + c.Mail(email, &MailOptions{Auth: &email}) + c.Close() + if got, want := wrote.String(), "MAIL FROM: AUTH=e+3Dmc2@example.com\r\n"; got != want { + t.Errorf("wrote %q; want %q", got, want) + } +} diff --git a/conn.go b/conn.go index d060bbe..1254175 100644 --- a/conn.go +++ b/conn.go @@ -426,16 +426,14 @@ func encodeXtext(raw string) string { out.Grow(len(raw)) for _, ch := range raw { - if ch == '+' || ch == '=' { + switch { + case ch >= '!' && ch <= '~' && ch != '+' && ch != '=': + // printable non-space US-ASCII except '+' and '=' + out.WriteRune(ch) + default: out.WriteRune('+') out.WriteString(strings.ToUpper(strconv.FormatInt(int64(ch), 16))) } - if ch > '!' && ch < '~' { // printable non-space US-ASCII - out.WriteRune(ch) - } - // Non-ASCII. - out.WriteRune('+') - out.WriteString(strings.ToUpper(strconv.FormatInt(int64(ch), 16))) } return out.String() }