DO NOT MERGE Send local IP address with EHLO instead of "localhost".
Bug 1515345 Backport of: I181c9f0d79fbdf62f7df77f72a1ec9653797b6dd Change-Id: If9da2d9f717814bff6a3aa7e6f1a0a44a49f34d6
This commit is contained in:
parent
d711bab919
commit
be2ef97220
|
@ -19,6 +19,7 @@ package com.android.email.mail;
|
|||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.InetAddress;
|
||||
import java.net.SocketException;
|
||||
import java.net.URI;
|
||||
|
||||
|
@ -159,4 +160,9 @@ public interface Transport {
|
|||
*/
|
||||
String readLine() throws IOException;
|
||||
|
||||
/**
|
||||
* @return The local address. If we have an open socket, get the local address from this.
|
||||
* Otherwise simply use {@link InetAddress#getLocalHost}.
|
||||
*/
|
||||
InetAddress getLocalAddress() throws IOException;
|
||||
}
|
||||
|
|
|
@ -29,6 +29,7 @@ import java.io.BufferedOutputStream;
|
|||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketAddress;
|
||||
|
@ -342,5 +343,11 @@ public class MailTransport implements Transport {
|
|||
return ret;
|
||||
}
|
||||
|
||||
|
||||
public InetAddress getLocalAddress() {
|
||||
if (isOpen()) {
|
||||
return mSocket.getLocalAddress();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -120,15 +120,11 @@ public class SmtpSender extends Sender {
|
|||
executeSimpleCommand(null);
|
||||
|
||||
String localHost = "localhost";
|
||||
try {
|
||||
InetAddress localAddress = InetAddress.getLocalHost();
|
||||
localHost = localAddress.getHostName();
|
||||
} catch (Exception e) {
|
||||
if (Config.LOGD && Email.DEBUG) {
|
||||
Log.d(Email.LOG_TAG, "Unable to look up localhost");
|
||||
}
|
||||
// Try to get local address in the X.X.X.X format.
|
||||
InetAddress localAddress = mTransport.getLocalAddress();
|
||||
if (localAddress != null) {
|
||||
localHost = localAddress.getHostAddress();
|
||||
}
|
||||
|
||||
String result = executeSimpleCommand("EHLO " + localHost);
|
||||
|
||||
/*
|
||||
|
|
|
@ -23,6 +23,7 @@ import android.util.Log;
|
|||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.InetAddress;
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
@ -49,6 +50,7 @@ public class MockTransport implements Transport {
|
|||
private int mConnectionSecurity;
|
||||
private boolean mTrustCertificates;
|
||||
private String mHost;
|
||||
private InetAddress mLocalAddress;
|
||||
|
||||
private ArrayList<String> mQueuedInput = new ArrayList<String>();
|
||||
|
||||
|
@ -186,6 +188,10 @@ public class MockTransport implements Transport {
|
|||
return mHost;
|
||||
}
|
||||
|
||||
public void setMockLocalAddress(InetAddress address) {
|
||||
mLocalAddress = address;
|
||||
}
|
||||
|
||||
public InputStream getInputStream() {
|
||||
SmtpSenderUnitTests.assertTrue(mOpen);
|
||||
return new MockInputStream();
|
||||
|
@ -360,4 +366,12 @@ public class MockTransport implements Transport {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
public InetAddress getLocalAddress() {
|
||||
if (isOpen()) {
|
||||
return mLocalAddress;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -36,6 +36,9 @@ import java.io.FileOutputStream;
|
|||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* This is a series of unit tests for the SMTP Sender class. These tests must be locally
|
||||
|
@ -50,6 +53,7 @@ public class SmtpSenderUnitTests extends ProviderTestCase2<EmailProvider> {
|
|||
EmailProvider mProvider;
|
||||
Context mProviderContext;
|
||||
Context mContext;
|
||||
private static final String LOCAL_ADDRESS = "1.2.3.4";
|
||||
|
||||
/* These values are provided by setUp() */
|
||||
private SmtpSender mSender = null;
|
||||
|
@ -79,7 +83,7 @@ public class SmtpSenderUnitTests extends ProviderTestCase2<EmailProvider> {
|
|||
/**
|
||||
* Confirms simple non-SSL non-TLS login
|
||||
*/
|
||||
public void testSimpleLogin() throws MessagingException {
|
||||
public void testSimpleLogin() throws Exception {
|
||||
|
||||
MockTransport mockTransport = openAndInjectMockTransport();
|
||||
|
||||
|
@ -100,7 +104,7 @@ public class SmtpSenderUnitTests extends ProviderTestCase2<EmailProvider> {
|
|||
/**
|
||||
* Test: Open and send a single message (sunny day)
|
||||
*/
|
||||
public void testSendMessageWithBody() throws MessagingException {
|
||||
public void testSendMessageWithBody() throws Exception {
|
||||
MockTransport mockTransport = openAndInjectMockTransport();
|
||||
|
||||
// Since SmtpSender.sendMessage() does a close then open, we need to preset for the open
|
||||
|
@ -341,7 +345,7 @@ public class SmtpSenderUnitTests extends ProviderTestCase2<EmailProvider> {
|
|||
/**
|
||||
* Test: Recover from a server closing early (or returning an empty string)
|
||||
*/
|
||||
public void testEmptyLineResponse() {
|
||||
public void testEmptyLineResponse() throws Exception {
|
||||
MockTransport mockTransport = openAndInjectMockTransport();
|
||||
|
||||
// Since SmtpSender.sendMessage() does a close then open, we need to preset for the open
|
||||
|
@ -349,7 +353,7 @@ public class SmtpSenderUnitTests extends ProviderTestCase2<EmailProvider> {
|
|||
|
||||
// Load up just the bare minimum to expose the error
|
||||
mockTransport.expect(null, "220 MockTransport 2000 Ready To Assist You Peewee");
|
||||
mockTransport.expect("EHLO .*", "");
|
||||
mockTransport.expect("EHLO " + Pattern.quote(LOCAL_ADDRESS), "");
|
||||
|
||||
// Now trigger the transmission
|
||||
// Note, a null message is sufficient here, as we won't even get past open()
|
||||
|
@ -365,11 +369,12 @@ public class SmtpSenderUnitTests extends ProviderTestCase2<EmailProvider> {
|
|||
/**
|
||||
* Set up a basic MockTransport. open it, and inject it into mStore
|
||||
*/
|
||||
private MockTransport openAndInjectMockTransport() {
|
||||
private MockTransport openAndInjectMockTransport() throws UnknownHostException {
|
||||
// Create mock transport and inject it into the SmtpSender that's already set up
|
||||
MockTransport mockTransport = new MockTransport();
|
||||
mockTransport.setSecurity(Transport.CONNECTION_SECURITY_NONE, false);
|
||||
mSender.setTransport(mockTransport);
|
||||
mockTransport.setMockLocalAddress(InetAddress.getByName(LOCAL_ADDRESS));
|
||||
return mockTransport;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue