Commit 93af6778 authored by Daniel Cormier's avatar Daniel Cormier Committed by Brad Fitzpatrick

net/textproto: properly write terminating sequence if DotWriter is closed with no writes

Fixed textproto.Writer.DotWriter() to properly write \r\n.\r\n to the buffer
when Close() is called without any bytes written. This properly writes the
terminating sequence outlined in RFC 5321 section 4.1.1.4 and RFC 3977
section 3.1.1, even when no other bytes are written.

Change-Id: I262fd2963ee76fff7ffae8e3cb0e86255694b361
Reviewed-on: https://go-review.googlesource.com/c/go/+/77350Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 4d23cbc6
......@@ -58,7 +58,8 @@ type dotWriter struct {
}
const (
wstateBeginLine = iota // beginning of line; initial state; must be zero
wstateBegin = iota // initial state; must be zero
wstateBeginLine // beginning of line
wstateCR // wrote \r (possibly at end of line)
wstateData // writing data in middle of line
)
......@@ -68,7 +69,7 @@ func (d *dotWriter) Write(b []byte) (n int, err error) {
for n < len(b) {
c := b[n]
switch d.state {
case wstateBeginLine:
case wstateBegin, wstateBeginLine:
d.state = wstateData
if c == '.' {
// escape leading dot
......
......@@ -33,3 +33,29 @@ func TestDotWriter(t *testing.T) {
t.Fatalf("wrote %q", s)
}
}
func TestDotWriterCloseEmptyWrite(t *testing.T) {
var buf bytes.Buffer
w := NewWriter(bufio.NewWriter(&buf))
d := w.DotWriter()
n, err := d.Write([]byte{})
if n != 0 || err != nil {
t.Fatalf("Write: %d, %s", n, err)
}
d.Close()
want := "\r\n.\r\n"
if s := buf.String(); s != want {
t.Fatalf("wrote %q; want %q", s, want)
}
}
func TestDotWriterCloseNoWrite(t *testing.T) {
var buf bytes.Buffer
w := NewWriter(bufio.NewWriter(&buf))
d := w.DotWriter()
d.Close()
want := "\r\n.\r\n"
if s := buf.String(); s != want {
t.Fatalf("wrote %q; want %q", s, want)
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment