From be2ef97220619a3acf6c3dd4eb4b8add26ff452a Mon Sep 17 00:00:00 2001 From: Andy Stadler Date: Mon, 20 Sep 2010 16:21:32 -0700 Subject: [PATCH] DO NOT MERGE Send local IP address with EHLO instead of "localhost". Bug 1515345 Backport of: I181c9f0d79fbdf62f7df77f72a1ec9653797b6dd Change-Id: If9da2d9f717814bff6a3aa7e6f1a0a44a49f34d6 --- src/com/android/email/mail/Transport.java | 6 ++++++ .../email/mail/transport/MailTransport.java | 9 ++++++++- .../email/mail/transport/SmtpSender.java | 12 ++++-------- .../email/mail/transport/MockTransport.java | 18 ++++++++++++++++-- .../mail/transport/SmtpSenderUnitTests.java | 15 ++++++++++----- 5 files changed, 44 insertions(+), 16 deletions(-) diff --git a/src/com/android/email/mail/Transport.java b/src/com/android/email/mail/Transport.java index 48dc4d8bb..37b485473 100644 --- a/src/com/android/email/mail/Transport.java +++ b/src/com/android/email/mail/Transport.java @@ -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; } diff --git a/src/com/android/email/mail/transport/MailTransport.java b/src/com/android/email/mail/transport/MailTransport.java index 7639f9d73..a44212d58 100644 --- a/src/com/android/email/mail/transport/MailTransport.java +++ b/src/com/android/email/mail/transport/MailTransport.java @@ -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; + } + } } diff --git a/src/com/android/email/mail/transport/SmtpSender.java b/src/com/android/email/mail/transport/SmtpSender.java index 1e0cd4e5a..8f72631f8 100644 --- a/src/com/android/email/mail/transport/SmtpSender.java +++ b/src/com/android/email/mail/transport/SmtpSender.java @@ -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); /* diff --git a/tests/src/com/android/email/mail/transport/MockTransport.java b/tests/src/com/android/email/mail/transport/MockTransport.java index f8cd34d8c..f59330ba4 100644 --- a/tests/src/com/android/email/mail/transport/MockTransport.java +++ b/tests/src/com/android/email/mail/transport/MockTransport.java @@ -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,9 +50,10 @@ public class MockTransport implements Transport { private int mConnectionSecurity; private boolean mTrustCertificates; private String mHost; - + private InetAddress mLocalAddress; + private ArrayList mQueuedInput = new ArrayList(); - + private static class Transaction { public static final int ACTION_INJECT_TEXT = 0; public static final int ACTION_SERVER_CLOSE = 1; @@ -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; + } + } } \ No newline at end of file diff --git a/tests/src/com/android/email/mail/transport/SmtpSenderUnitTests.java b/tests/src/com/android/email/mail/transport/SmtpSenderUnitTests.java index 5f65cdca9..bdb89c730 100644 --- a/tests/src/com/android/email/mail/transport/SmtpSenderUnitTests.java +++ b/tests/src/com/android/email/mail/transport/SmtpSenderUnitTests.java @@ -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 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 { /** * 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 { /** * 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 { /** * 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 { // 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 { /** * 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; }