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);
    }
}

@@ -27,16 +27,17 @@
 ilter;%0A%0A
+%0A
 import j
@@ -60,46 +60,28 @@
 on;%0A
+%0A
 import 
-java.io.ByteArrayOutputStream;%0A
+okio.Buffer;
 %0Aimp
@@ -101,16 +101,40 @@
 String;%0A
+import org.junit.Before;
 %0Aimport 
@@ -202,16 +202,17 @@
 quals;%0A%0A
+%0A
 public c
@@ -247,45 +247,59 @@
     
-@Test%0A    public void s
+private Buffer buffer;%0A    private S
 mtpD
-ot
+ata
 Stuffing
 _stu
@@ -298,28 +298,61 @@
 fing
-_stuffingOnlyDotTest
+ smtpDataStuffing;%0A%0A    @Before%0A    public void setUp
 () t
@@ -349,34 +349,32 @@
  setUp() throws 
-IO
 Exception %7B%0A    
@@ -381,172 +381,38 @@
     
-String expectedMessageAfterStuffing = %22Hello dot%5Cr%5Cn..%22;%0A        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();%0A        SmtpDataStuffing
+buffer = new Buffer();%0A       
  smt
@@ -416,38 +416,32 @@
 smtpDataStuffing
-Stream
  = new SmtpDataS
@@ -445,33 +445,31 @@
 taStuffing(b
-yteArrayO
+uffer.o
 utputStream)
@@ -471,532 +471,322 @@
 ream
+()
 );%0A    
+%7D%0A%0A
     
-byte%5B%5D data = ByteString.encodeUtf8(%22Hello dot%5Cr%5Cn.%22).toByteArray();%0A%0A        smtpDataStuffingStream.write(data);%0A        smtpDataStuffingStream.close();%0A        byteArrayOutputStream.close();%0A        String actualMessageAfterStuffing = ByteString.of(byteArrayOutputStream.toByteArray()).utf8();%0A%0A        assertEquals(expectedMessageAfterStuffing, actualMessageAfterStuffing);%0A    %7D%0A%0A    @Test%0A    public void smtpDotStuffing_noStuffingDotTest() throws IOException %7B%0A        String expectedMessageAfterStuffing = 
+@Test%0A    public void dotAtStartOfLine() throws IOException %7B%0A        smtpDataStuffing.write(bytesFor(%22Hello dot%5Cr%5Cn.%22));%0A%0A        assertEquals(%22Hello dot%5Cr%5Cn..%22, buffer.readUtf8());%0A    %7D%0A%0A    @Test%0A    public void dotAtStartOfStream() throws IOException %7B%0A        smtpDataStuffing.write(bytesFor(
 %22...
@@ -800,17 +800,20 @@
 . dots.%22
-;
+));%0A
 %0A       
@@ -817,1339 +817,580 @@
     
-ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();%0A        SmtpDataStuffing smtpDataStuffingStream = new SmtpDataStuffing(byteArrayOutputStream);%0A        byte%5B%5D data = ByteString.encodeUtf8(%22...Hello .. dots.%22).toByteArray();%0A%0A        smtpDataStuffingStream.write(data);%0A        smtpDataStuffingStream.close();%0A        byteArrayOutputStream.close();%0A        String actualMessageAfterStuffing = ByteString.of(byteArrayOutputStream.toByteArray()).utf8();%0A%0A        assertEquals(expectedMessageAfterStuffing, actualMessageAfterStuffing);%0A    %7D%0A%0A    @Test%0A    public void smtpDotStuffing_stuffingAndNoStuffingDotTest() throws IOException %7B%0A        String expectedMessageAfterStuffing = %22%5Cr%5Cn..Hello . dots.%5Cr%5Cn...%5Cr%5Cn..%5Cr%5Cn....%22;%0A        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();%0A        SmtpDataStuffing smtpDataStuffingStream = new SmtpDataStuffing(byteArrayOutputStream);%0A        byte%5B%5D data = ByteString.encodeUtf8(%22%5Cr%5Cn.Hello . dots.%5Cr%5Cn..%5Cr%5Cn.%5Cr%5Cn...%22).toByteArray();%0A%0A        smtpDataStuffingStream.write(data);%0A        smtpDataStuffingStream.close();%0A        byteArrayOutputStream.close();%0A        String actualMessageAfterStuffing = ByteString.of(byteArrayOutputStream.toByteArray()).utf8();%0A%0A        assertEquals(expectedMessageAfterStuffing, actualMessageAfterStuffing
+//FIXME: The first line is a line, too. So This should be dot stuffed.%0A        // See https://tools.ietf.org/html/rfc5321#section-4.5.2%0A        assertEquals(%22...Hello .. dots.%22, buffer.readUtf8());%0A    %7D%0A%0A    @Test%0A    public void dotsThatNeedStuffingMixedWithOnesThatDoNot() throws IOException %7B%0A        smtpDataStuffing.write(bytesFor(%22%5Cr%5Cn.Hello . dots.%5Cr%5Cn..%5Cr%5Cn.%5Cr%5Cn...%22));%0A%0A        assertEquals(%22%5Cr%5Cn..Hello . dots.%5Cr%5Cn...%5Cr%5Cn..%5Cr%5Cn....%22, buffer.readUtf8());%0A    %7D%0A%0A    private byte%5B%5D bytesFor(String input) %7B%0A        return ByteString.encodeUtf8(input).toByteArray(
 );%0A 

Refactor tests