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

Commit 21d4ceb0 authored by cketti's avatar cketti Committed by GitHub
Browse files

Merge pull request #1649 from philipwhiuk/smtpTesting

SMTP: Testing SmtpTransport using new MockSmtpServer
parents 7956c3aa 81245150
Loading
Loading
Loading
Loading
+67 −0
Original line number Diff line number Diff line
package com.fsck.k9.mail.helpers;


import java.io.IOException;
import java.io.OutputStream;

import com.fsck.k9.mail.Address;
import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.internet.MimeMessage;
import okio.BufferedSink;
import okio.Okio;


class TestMessage extends MimeMessage {
    private final long messageSize;
    private final Address[] from;
    private final Address[] to;
    private final boolean hasAttachments;


    TestMessage(TestMessageBuilder builder) {
        from = toAddressArray(builder.from);
        to = toAddressArray(builder.to);
        hasAttachments = builder.hasAttachments;
        messageSize = builder.messageSize;
    }

    @Override
    public Address[] getFrom() {
        return from;
    }

    @Override
    public Address[] getRecipients(RecipientType type) {
        switch (type) {
            case TO:
                return to;
            case CC:
                return new Address[0];
            case BCC:
                return new Address[0];
        }

        throw new AssertionError("Missing switch case: " + type);
    }

    @Override
    public boolean hasAttachments() {
        return hasAttachments;
    }

    @Override
    public long calculateSize() {
        return messageSize;
    }

    @Override
    public void writeTo(OutputStream out) throws IOException, MessagingException {
        BufferedSink bufferedSink = Okio.buffer(Okio.sink(out));
        bufferedSink.writeUtf8("[message data]");
        bufferedSink.emit();
    }

    private static Address[] toAddressArray(String email) {
        return email == null ? new Address[0] : new Address[] { new Address(email) };
    }
}
+37 −0
Original line number Diff line number Diff line
package com.fsck.k9.mail.helpers;


import com.fsck.k9.mail.Message;


public class TestMessageBuilder {
    String from;
    String to;
    boolean hasAttachments;
    long messageSize;


    public TestMessageBuilder from(String email) {
        from = email;
        return this;
    }

    public TestMessageBuilder to(String email) {
        to = email;
        return this;
    }

    public TestMessageBuilder setHasAttachments(boolean hasAttachments) {
        this.hasAttachments = hasAttachments;
        return this;
    }

    public TestMessageBuilder messageSize(long messageSize) {
        this.messageSize = messageSize;
        return this;
    }
    
    public Message build() {
        return new TestMessage(this);
    }
}
+33 −0
Original line number Diff line number Diff line
package com.fsck.k9.mail.helpers;


import java.io.IOException;
import java.net.Socket;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;

import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.ssl.TrustedSocketFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;


public class TestTrustedSocketFactory implements TrustedSocketFactory {
    @Override
    public Socket createSocket(Socket socket, String host, int port, String clientCertificateAlias)
            throws NoSuchAlgorithmException, KeyManagementException, MessagingException, IOException {

        TrustManager[] trustManagers = new TrustManager[] { new VeryTrustingTrustManager() };

        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, trustManagers, null);

        SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
        return sslSocketFactory.createSocket(
                socket,
                socket.getInetAddress().getHostAddress(),
                socket.getPort(),
                true);
    }
}
+28 −0
Original line number Diff line number Diff line
package com.fsck.k9.mail.helpers;

import android.annotation.SuppressLint;

import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.X509TrustManager;


@SuppressLint("TrustAllX509TrustManager")
class VeryTrustingTrustManager implements X509TrustManager {
    @Override
    public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        // Accept all certificates
    }

    @Override
    public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        // Accept all certificates
    }

    @Override
    public X509Certificate[] getAcceptedIssuers() {
        return new X509Certificate[0];
    }
}
+2 −48
Original line number Diff line number Diff line
@@ -2,14 +2,8 @@ package com.fsck.k9.mail.store.imap;


import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import android.annotation.SuppressLint;
import android.net.ConnectivityManager;

import com.fsck.k9.mail.AuthType;
@@ -21,10 +15,8 @@ import com.fsck.k9.mail.K9MailLib;
import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.ssl.TrustedSocketFactory;
import com.fsck.k9.mail.store.imap.mockserver.MockImapServer;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import com.fsck.k9.mail.helpers.TestTrustedSocketFactory;

import okio.ByteString;
import org.junit.Before;
import org.junit.Test;
@@ -36,7 +28,6 @@ import org.robolectric.shadows.ShadowLog;
import static org.hamcrest.core.StringContains.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@@ -688,41 +679,4 @@ public class ImapConnectionTest {
        server.expect("2 LOGIN \"" + USERNAME + "\" \"" + PASSWORD + "\"");
        server.output("2 OK LOGIN completed");
    }

    private static class TestTrustedSocketFactory implements TrustedSocketFactory {
        @Override
        public Socket createSocket(Socket socket, String host, int port, String clientCertificateAlias)
                throws NoSuchAlgorithmException, KeyManagementException, MessagingException, IOException {

            TrustManager[] trustManagers = new TrustManager[] { new VeryTrustingTrustManager() };

            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, trustManagers, null);

            SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
            return sslSocketFactory.createSocket(
                    socket,
                    socket.getInetAddress().getHostAddress(),
                    socket.getPort(),
                    true);
        }
    }

    @SuppressLint("TrustAllX509TrustManager")
    private static class VeryTrustingTrustManager implements X509TrustManager {
        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            // Accept all certificates
        }

        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            // Accept all certificates
        }

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[0];
        }
    }
}
Loading