From 0fafe3dc187ba0a74a0d5e3dda16e12ef332042e Mon Sep 17 00:00:00 2001 From: shonramamurthy Date: Mon, 13 Feb 2017 19:46:33 +0530 Subject: [PATCH 1/3] Issue#2210 : Unit Tests for SmtpDataStuffing. --- .../k9/mail/filter/SmtpDataStuffingTest.java | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 k9mail-library/src/test/java/com/fsck/k9/mail/filter/SmtpDataStuffingTest.java diff --git a/k9mail-library/src/test/java/com/fsck/k9/mail/filter/SmtpDataStuffingTest.java b/k9mail-library/src/test/java/com/fsck/k9/mail/filter/SmtpDataStuffingTest.java new file mode 100644 index 000000000..d8fe48463 --- /dev/null +++ b/k9mail-library/src/test/java/com/fsck/k9/mail/filter/SmtpDataStuffingTest.java @@ -0,0 +1,57 @@ +package com.fsck.k9.mail.filter; + +import java.io.IOException; +import java.io.ByteArrayOutputStream; + +import okio.ByteString; + +import org.junit.Test; + +import static junit.framework.Assert.assertEquals; + +public class SmtpDataStuffingTest { + @Test + public void smtpDotStuffing_stuffingOnlyDotTest() throws IOException { + String expectedMessageAfterStuffing = "Hello dot\r\n.."; + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + SmtpDataStuffing smtpDataStuffingStream = new SmtpDataStuffing(byteArrayOutputStream); + byte[] data = ByteString.encodeUtf8("Hello dot\r\n.").toByteArray(); + + smtpDataStuffingStream.write(data); + smtpDataStuffingStream.close(); + byteArrayOutputStream.close(); + String actualMessageAfterStuffing = ByteString.of(byteArrayOutputStream.toByteArray()).utf8(); + + assertEquals(expectedMessageAfterStuffing, actualMessageAfterStuffing); + } + + @Test + public void smtpDotStuffing_noStuffingDotTest() throws IOException { + String expectedMessageAfterStuffing = "...Hello .. dots."; + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + SmtpDataStuffing smtpDataStuffingStream = new SmtpDataStuffing(byteArrayOutputStream); + byte[] data = ByteString.encodeUtf8("...Hello .. dots.").toByteArray(); + + smtpDataStuffingStream.write(data); + smtpDataStuffingStream.close(); + byteArrayOutputStream.close(); + String actualMessageAfterStuffing = ByteString.of(byteArrayOutputStream.toByteArray()).utf8(); + + assertEquals(expectedMessageAfterStuffing, actualMessageAfterStuffing); + } + + @Test + public void smtpDotStuffing_stuffingAndNoStuffingDotTest() throws IOException { + String expectedMessageAfterStuffing = "\r\n..Hello . dots.\r\n...\r\n..\r\n...."; + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + SmtpDataStuffing smtpDataStuffingStream = new SmtpDataStuffing(byteArrayOutputStream); + byte[] data = ByteString.encodeUtf8("\r\n.Hello . dots.\r\n..\r\n.\r\n...").toByteArray(); + + smtpDataStuffingStream.write(data); + smtpDataStuffingStream.close(); + byteArrayOutputStream.close(); + String actualMessageAfterStuffing = ByteString.of(byteArrayOutputStream.toByteArray()).utf8(); + + assertEquals(expectedMessageAfterStuffing, actualMessageAfterStuffing); + } +} From 69ba305f2a414e99ab27201f4f6a6d150a031c3e Mon Sep 17 00:00:00 2001 From: cketti Date: Tue, 21 Feb 2017 10:14:18 +0100 Subject: [PATCH 2/3] Refactor tests --- .../k9/mail/filter/SmtpDataStuffingTest.java | 63 +++++++++---------- 1 file changed, 28 insertions(+), 35 deletions(-) diff --git a/k9mail-library/src/test/java/com/fsck/k9/mail/filter/SmtpDataStuffingTest.java b/k9mail-library/src/test/java/com/fsck/k9/mail/filter/SmtpDataStuffingTest.java index d8fe48463..4a4c0194d 100644 --- a/k9mail-library/src/test/java/com/fsck/k9/mail/filter/SmtpDataStuffingTest.java +++ b/k9mail-library/src/test/java/com/fsck/k9/mail/filter/SmtpDataStuffingTest.java @@ -1,57 +1,50 @@ package com.fsck.k9.mail.filter; + import java.io.IOException; -import java.io.ByteArrayOutputStream; +import okio.Buffer; import okio.ByteString; - +import org.junit.Before; import org.junit.Test; import static junit.framework.Assert.assertEquals; + public class SmtpDataStuffingTest { - @Test - public void smtpDotStuffing_stuffingOnlyDotTest() throws IOException { - String expectedMessageAfterStuffing = "Hello dot\r\n.."; - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - SmtpDataStuffing smtpDataStuffingStream = new SmtpDataStuffing(byteArrayOutputStream); - byte[] data = ByteString.encodeUtf8("Hello dot\r\n.").toByteArray(); + private Buffer buffer; + private SmtpDataStuffing smtpDataStuffing; - smtpDataStuffingStream.write(data); - smtpDataStuffingStream.close(); - byteArrayOutputStream.close(); - String actualMessageAfterStuffing = ByteString.of(byteArrayOutputStream.toByteArray()).utf8(); - - assertEquals(expectedMessageAfterStuffing, actualMessageAfterStuffing); + @Before + public void setUp() throws Exception { + buffer = new Buffer(); + smtpDataStuffing = new SmtpDataStuffing(buffer.outputStream()); } @Test - public void smtpDotStuffing_noStuffingDotTest() throws IOException { - String expectedMessageAfterStuffing = "...Hello .. dots."; - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - SmtpDataStuffing smtpDataStuffingStream = new SmtpDataStuffing(byteArrayOutputStream); - byte[] data = ByteString.encodeUtf8("...Hello .. dots.").toByteArray(); + public void dotAtStartOfLine() throws IOException { + smtpDataStuffing.write(bytesFor("Hello dot\r\n.")); - smtpDataStuffingStream.write(data); - smtpDataStuffingStream.close(); - byteArrayOutputStream.close(); - String actualMessageAfterStuffing = ByteString.of(byteArrayOutputStream.toByteArray()).utf8(); - - assertEquals(expectedMessageAfterStuffing, actualMessageAfterStuffing); + assertEquals("Hello dot\r\n..", buffer.readUtf8()); } @Test - public void smtpDotStuffing_stuffingAndNoStuffingDotTest() throws IOException { - String expectedMessageAfterStuffing = "\r\n..Hello . dots.\r\n...\r\n..\r\n...."; - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - SmtpDataStuffing smtpDataStuffingStream = new SmtpDataStuffing(byteArrayOutputStream); - byte[] data = ByteString.encodeUtf8("\r\n.Hello . dots.\r\n..\r\n.\r\n...").toByteArray(); + public void dotAtStartOfStream() throws IOException { + smtpDataStuffing.write(bytesFor("...Hello .. dots.")); - smtpDataStuffingStream.write(data); - smtpDataStuffingStream.close(); - byteArrayOutputStream.close(); - String actualMessageAfterStuffing = ByteString.of(byteArrayOutputStream.toByteArray()).utf8(); + //FIXME: The first line is a line, too. So This should be dot stuffed. + // See https://tools.ietf.org/html/rfc5321#section-4.5.2 + assertEquals("...Hello .. dots.", buffer.readUtf8()); + } - assertEquals(expectedMessageAfterStuffing, actualMessageAfterStuffing); + @Test + public void dotsThatNeedStuffingMixedWithOnesThatDoNot() throws IOException { + smtpDataStuffing.write(bytesFor("\r\n.Hello . dots.\r\n..\r\n.\r\n...")); + + assertEquals("\r\n..Hello . dots.\r\n...\r\n..\r\n....", buffer.readUtf8()); + } + + private byte[] bytesFor(String input) { + return ByteString.encodeUtf8(input).toByteArray(); } } From 238cf80ec915329c23ed751b6dbf2074e836f829 Mon Sep 17 00:00:00 2001 From: cketti Date: Tue, 21 Feb 2017 10:18:13 +0100 Subject: [PATCH 3/3] Fix SMTP dot stuffing at start of stream --- .../com/fsck/k9/mail/filter/SmtpDataStuffing.java | 2 +- .../fsck/k9/mail/filter/SmtpDataStuffingTest.java | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/filter/SmtpDataStuffing.java b/k9mail-library/src/main/java/com/fsck/k9/mail/filter/SmtpDataStuffing.java index bc155c6a1..dfd7afd47 100644 --- a/k9mail-library/src/main/java/com/fsck/k9/mail/filter/SmtpDataStuffing.java +++ b/k9mail-library/src/main/java/com/fsck/k9/mail/filter/SmtpDataStuffing.java @@ -9,7 +9,7 @@ public class SmtpDataStuffing extends FilterOutputStream { private static final int STATE_CR = 1; private static final int STATE_CRLF = 2; - private int state = STATE_NORMAL; + private int state = STATE_CRLF; public SmtpDataStuffing(OutputStream out) { super(out); diff --git a/k9mail-library/src/test/java/com/fsck/k9/mail/filter/SmtpDataStuffingTest.java b/k9mail-library/src/test/java/com/fsck/k9/mail/filter/SmtpDataStuffingTest.java index 4a4c0194d..1b20217b8 100644 --- a/k9mail-library/src/test/java/com/fsck/k9/mail/filter/SmtpDataStuffingTest.java +++ b/k9mail-library/src/test/java/com/fsck/k9/mail/filter/SmtpDataStuffingTest.java @@ -30,11 +30,16 @@ public class SmtpDataStuffingTest { @Test public void dotAtStartOfStream() throws IOException { - smtpDataStuffing.write(bytesFor("...Hello .. dots.")); + smtpDataStuffing.write(bytesFor(".Hello dots")); - //FIXME: The first line is a line, too. So This should be dot stuffed. - // See https://tools.ietf.org/html/rfc5321#section-4.5.2 - assertEquals("...Hello .. dots.", buffer.readUtf8()); + assertEquals("..Hello dots", buffer.readUtf8()); + } + + @Test + public void linesNotStartingWithDot() throws IOException { + smtpDataStuffing.write(bytesFor("Hello\r\nworld\r\n")); + + assertEquals("Hello\r\nworld\r\n", buffer.readUtf8()); } @Test