From f23b199a024e2da4042e7f2ee23c3d0f11f7c5c9 Mon Sep 17 00:00:00 2001 From: Benedict Wong Date: Mon, 2 Apr 2018 18:12:34 -0700 Subject: [PATCH] Force creation of Socket upon Transform application This change forces Socket and DatagramSocket to populate the SocketImpl, ensuring that the socket file descriptor can be retrieved when applying Transport mode Transforms This is done by calling getSoLinger(), triggering a getImpl(), which triggers setImpl() if needed. Bug: 77491294 Test: Added tests in IpSecManagerTest, ran on walleye Merged-In: I40da08b031357710eb794e0f866aec5660c79594 Change-Id: I40da08b031357710eb794e0f866aec5660c79594 (cherry picked from commit de3816b30d3ac9c4bb673cee5a99a00511c6c7bf) --- .../java/android/net/IpSecManagerTest.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/net/java/android/net/IpSecManagerTest.java b/tests/net/java/android/net/IpSecManagerTest.java index a946e50585..88d47ba0f2 100644 --- a/tests/net/java/android/net/IpSecManagerTest.java +++ b/tests/net/java/android/net/IpSecManagerTest.java @@ -38,6 +38,7 @@ import android.system.Os; import com.android.server.IpSecService; import java.net.InetAddress; +import java.net.Socket; import java.net.UnknownHostException; import org.junit.Before; @@ -194,6 +195,33 @@ public class IpSecManagerTest { verify(mMockIpSecService).closeUdpEncapsulationSocket(DUMMY_RESOURCE_ID); } + @Test + public void testApplyTransportModeTransformEnsuresSocketCreation() throws Exception { + Socket socket = new Socket(); + IpSecConfig dummyConfig = new IpSecConfig(); + IpSecTransform dummyTransform = new IpSecTransform(null, dummyConfig); + + // Even if underlying SocketImpl is not initalized, this should force the init, and + // thereby succeed. + mIpSecManager.applyTransportModeTransform( + socket, IpSecManager.DIRECTION_IN, dummyTransform); + + // Check to make sure the FileDescriptor is non-null + assertNotNull(socket.getFileDescriptor$()); + } + + @Test + public void testRemoveTransportModeTransformsForcesSocketCreation() throws Exception { + Socket socket = new Socket(); + + // Even if underlying SocketImpl is not initalized, this should force the init, and + // thereby succeed. + mIpSecManager.removeTransportModeTransforms(socket); + + // Check to make sure the FileDescriptor is non-null + assertNotNull(socket.getFileDescriptor$()); + } + @Test public void testOpenEncapsulationSocketOnRandomPort() throws Exception { IpSecUdpEncapResponse udpEncapResp =