Fixed interruption of blocked SSLSocket via Socket.close()

Bug: 10599593
Change-Id: Iade24eed691756281dfd925abe57740a1ad4145b
diff --git a/NativeCode.mk b/NativeCode.mk
index 42319dc..1a47ee4 100644
--- a/NativeCode.mk
+++ b/NativeCode.mk
@@ -95,11 +95,10 @@
 LOCAL_CFLAGS += -DJNI_JARJAR_PREFIX="com/android/"
 LOCAL_CPPFLAGS += $(core_cppflags)
 LOCAL_SRC_FILES := \
-        crypto/src/main/native/org_conscrypt_NativeCrypto.cpp \
-        luni/src/main/native/AsynchronousSocketCloseMonitor.cpp
+        crypto/src/main/native/org_conscrypt_NativeCrypto.cpp
 LOCAL_C_INCLUDES += $(core_c_includes) \
         libcore/luni/src/main/native
-LOCAL_SHARED_LIBRARIES += $(core_shared_libraries) libcrypto libssl libnativehelper libz
+LOCAL_SHARED_LIBRARIES += $(core_shared_libraries) libcrypto libssl libnativehelper libz libjavacore
 LOCAL_STATIC_LIBRARIES += $(core_static_libraries)
 LOCAL_MODULE_TAGS := optional
 LOCAL_MODULE := libjavacrypto
@@ -146,8 +145,7 @@
     # Conscrypt native library for host
     include $(CLEAR_VARS)
     LOCAL_SRC_FILES += \
-            crypto/src/main/native/org_conscrypt_NativeCrypto.cpp \
-            luni/src/main/native/AsynchronousSocketCloseMonitor.cpp
+            crypto/src/main/native/org_conscrypt_NativeCrypto.cpp
     LOCAL_C_INCLUDES += $(core_c_includes) \
             libcore/luni/src/main/native
     LOCAL_CPPFLAGS += $(core_cppflags)
@@ -156,15 +154,14 @@
     LOCAL_MODULE := libjavacrypto
     LOCAL_CFLAGS += -DJNI_JARJAR_PREFIX="com/android/"
     LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/NativeCode.mk
-    LOCAL_SHARED_LIBRARIES += $(core_shared_libraries) libssl-host libcrypto-host
+    LOCAL_SHARED_LIBRARIES += $(core_shared_libraries) libssl-host libcrypto-host libjavacore
     LOCAL_STATIC_LIBRARIES += $(core_static_libraries)
     include $(BUILD_HOST_SHARED_LIBRARY)
 
     # Conscrypt native library for nojarjar'd version
     include $(CLEAR_VARS)
     LOCAL_SRC_FILES += \
-            crypto/src/main/native/org_conscrypt_NativeCrypto.cpp \
-            luni/src/main/native/AsynchronousSocketCloseMonitor.cpp
+            crypto/src/main/native/org_conscrypt_NativeCrypto.cpp
     LOCAL_C_INCLUDES += $(core_c_includes) \
             libcore/luni/src/main/native
     LOCAL_CPPFLAGS += $(core_cppflags)
@@ -172,7 +169,7 @@
     LOCAL_MODULE_TAGS := optional
     LOCAL_MODULE := libconscrypt_jni
     LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/NativeCode.mk
-    LOCAL_SHARED_LIBRARIES += $(core_shared_libraries) libssl-host libcrypto-host
+    LOCAL_SHARED_LIBRARIES += $(core_shared_libraries) libssl-host libcrypto-host libjavacore
     LOCAL_STATIC_LIBRARIES += $(core_static_libraries)
     include $(BUILD_HOST_SHARED_LIBRARY)
 
diff --git a/luni/src/test/java/libcore/javax/net/ssl/SSLSocketTest.java b/luni/src/test/java/libcore/javax/net/ssl/SSLSocketTest.java
index 04b81b0..dd56529 100644
--- a/luni/src/test/java/libcore/javax/net/ssl/SSLSocketTest.java
+++ b/luni/src/test/java/libcore/javax/net/ssl/SSLSocketTest.java
@@ -1239,40 +1239,29 @@
     }
 
     public void test_SSLSocket_interrupt() throws Exception {
-        ServerSocket listening = new ServerSocket(0);
-
-        for (int i = 0; i < 3; i++) {
-            Socket underlying = new Socket(listening.getInetAddress(), listening.getLocalPort());
-            Socket server = listening.accept();
-
-            SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault();
-            Socket clientWrapping = sf.createSocket(underlying, null, -1, true);
-
-            switch (i) {
-                case 0:
-                    test_SSLSocket_interrupt_case(underlying, underlying);
-                    break;
-                case 1:
-                    test_SSLSocket_interrupt_case(underlying, clientWrapping);
-                    break;
-                case 2:
-                    test_SSLSocket_interrupt_case(clientWrapping, underlying);
-                    break;
-                case 3:
-                    test_SSLSocket_interrupt_case(clientWrapping, clientWrapping);
-                    break;
-                default:
-                    fail();
-            }
-
-            server.close();
-            underlying.close();
+        test_SSLSocket_interrupt_case(true, true);
+        test_SSLSocket_interrupt_case(true, false);
+        test_SSLSocket_interrupt_case(false, true);
+        // Currently failing due to reader blocking closing thread http://b/10681815
+        if (StandardNames.IS_RI) {
+            test_SSLSocket_interrupt_case(false, false);
         }
-        listening.close();
     }
 
-    private void test_SSLSocket_interrupt_case(Socket toRead, final Socket toClose)
+    private void test_SSLSocket_interrupt_case(boolean readUnderlying, boolean closeUnderlying)
             throws Exception {
+
+        ServerSocket listening = new ServerSocket(0);
+
+        Socket underlying = new Socket(listening.getInetAddress(), listening.getLocalPort());
+        Socket server = listening.accept();
+
+        SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault();
+        Socket clientWrapping = sf.createSocket(underlying, null, -1, true);
+
+        final Socket toRead = (readUnderlying) ? underlying : clientWrapping;
+        final Socket toClose = (closeUnderlying) ? underlying : clientWrapping;
+
         ExecutorService executor = Executors.newSingleThreadExecutor();
         Future<Void> future = executor.submit(new Callable<Void>() {
             @Override public Void call() throws Exception {
@@ -1283,7 +1272,7 @@
         });
         executor.shutdown();
         try {
-            toRead.setSoTimeout(10 * 1000);
+            toRead.setSoTimeout(5 * 1000);
             toRead.getInputStream().read();
             fail();
         } catch (SocketTimeoutException e) {
@@ -1291,6 +1280,10 @@
         } catch (SocketException expected) {
         }
         future.get();
+
+        server.close();
+        underlying.close();
+        listening.close();
     }
 
     /**