Merge "Dup fds to stop finalizers from invalidating them." into nougat-mr1-cts-dev
am: bfabfe75f9
Change-Id: If02ea4f12870acb3e5e1bce9e9ddbc42c0fd05f0
This commit is contained in:
@@ -22,11 +22,15 @@ import android.content.Intent;
|
|||||||
import android.content.ServiceConnection;
|
import android.content.ServiceConnection;
|
||||||
import android.os.ConditionVariable;
|
import android.os.ConditionVariable;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
|
import android.os.ParcelFileDescriptor;
|
||||||
import android.os.RemoteException;
|
import android.os.RemoteException;
|
||||||
|
import android.system.ErrnoException;
|
||||||
|
import android.system.Os;
|
||||||
|
|
||||||
import com.android.cts.net.hostside.IRemoteSocketFactory;
|
import com.android.cts.net.hostside.IRemoteSocketFactory;
|
||||||
|
|
||||||
import java.io.FileDescriptor;
|
import java.io.FileDescriptor;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
public class RemoteSocketFactoryClient {
|
public class RemoteSocketFactoryClient {
|
||||||
private static final int TIMEOUT_MS = 5000;
|
private static final int TIMEOUT_MS = 5000;
|
||||||
@@ -76,9 +80,14 @@ public class RemoteSocketFactoryClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public FileDescriptor openSocketFd(
|
public FileDescriptor openSocketFd(String host, int port, int timeoutMs)
|
||||||
String host, int port, int timeoutMs) throws RemoteException {
|
throws RemoteException, ErrnoException, IOException {
|
||||||
return mService.openSocketFd(host, port, timeoutMs).getFileDescriptor();
|
// Dup the filedescriptor so ParcelFileDescriptor's finalizer doesn't garbage collect it
|
||||||
|
// and cause our fd to become invalid. http://b/35927643 .
|
||||||
|
ParcelFileDescriptor pfd = mService.openSocketFd(host, port, timeoutMs);
|
||||||
|
FileDescriptor fd = Os.dup(pfd.getFileDescriptor());
|
||||||
|
pfd.close();
|
||||||
|
return fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getPackageName() throws RemoteException {
|
public String getPackageName() throws RemoteException {
|
||||||
|
|||||||
@@ -476,7 +476,11 @@ public class VpnTest extends InstrumentationTestCase {
|
|||||||
private FileDescriptor openSocketFd(String host, int port, int timeoutMs) throws Exception {
|
private FileDescriptor openSocketFd(String host, int port, int timeoutMs) throws Exception {
|
||||||
Socket s = new Socket(host, port);
|
Socket s = new Socket(host, port);
|
||||||
s.setSoTimeout(timeoutMs);
|
s.setSoTimeout(timeoutMs);
|
||||||
return ParcelFileDescriptor.fromSocket(s).getFileDescriptor();
|
// Dup the filedescriptor so ParcelFileDescriptor's finalizer doesn't garbage collect it
|
||||||
|
// and cause our fd to become invalid. http://b/35927643 .
|
||||||
|
FileDescriptor fd = Os.dup(ParcelFileDescriptor.fromSocket(s).getFileDescriptor());
|
||||||
|
s.close();
|
||||||
|
return fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
private FileDescriptor openSocketFdInOtherApp(
|
private FileDescriptor openSocketFdInOtherApp(
|
||||||
@@ -506,7 +510,9 @@ public class VpnTest extends InstrumentationTestCase {
|
|||||||
|
|
||||||
private void assertSocketStillOpen(FileDescriptor fd, String host) throws Exception {
|
private void assertSocketStillOpen(FileDescriptor fd, String host) throws Exception {
|
||||||
try {
|
try {
|
||||||
|
assertTrue(fd.valid());
|
||||||
sendRequest(fd, host);
|
sendRequest(fd, host);
|
||||||
|
assertTrue(fd.valid());
|
||||||
} finally {
|
} finally {
|
||||||
Os.close(fd);
|
Os.close(fd);
|
||||||
}
|
}
|
||||||
@@ -514,10 +520,12 @@ public class VpnTest extends InstrumentationTestCase {
|
|||||||
|
|
||||||
private void assertSocketClosed(FileDescriptor fd, String host) throws Exception {
|
private void assertSocketClosed(FileDescriptor fd, String host) throws Exception {
|
||||||
try {
|
try {
|
||||||
|
assertTrue(fd.valid());
|
||||||
sendRequest(fd, host);
|
sendRequest(fd, host);
|
||||||
fail("Socket opened before VPN connects should be closed when VPN connects");
|
fail("Socket opened before VPN connects should be closed when VPN connects");
|
||||||
} catch (ErrnoException expected) {
|
} catch (ErrnoException expected) {
|
||||||
assertEquals(ECONNABORTED, expected.errno);
|
assertEquals(ECONNABORTED, expected.errno);
|
||||||
|
assertTrue(fd.valid());
|
||||||
} finally {
|
} finally {
|
||||||
Os.close(fd);
|
Os.close(fd);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user