Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 47b86f61 authored by cketti's avatar cketti
Browse files

Merge pull request #2231

Unit tests for SmtpDataStuffing
parents e70295ef 238cf80e
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -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);
+55 −0
Original line number Diff line number Diff line
package com.fsck.k9.mail.filter;


import java.io.IOException;

import okio.Buffer;
import okio.ByteString;
import org.junit.Before;
import org.junit.Test;

import static junit.framework.Assert.assertEquals;


public class SmtpDataStuffingTest {
    private Buffer buffer;
    private SmtpDataStuffing smtpDataStuffing;

    @Before
    public void setUp() throws Exception {
        buffer = new Buffer();
        smtpDataStuffing = new SmtpDataStuffing(buffer.outputStream());
    }

    @Test
    public void dotAtStartOfLine() throws IOException {
        smtpDataStuffing.write(bytesFor("Hello dot\r\n."));

        assertEquals("Hello dot\r\n..", buffer.readUtf8());
    }

    @Test
    public void dotAtStartOfStream() throws IOException {
        smtpDataStuffing.write(bytesFor(".Hello dots"));

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