From 7a2c62c6a029d7902196714347eddc44267a690d Mon Sep 17 00:00:00 2001 From: Motomu Utsumi Date: Mon, 3 Mar 2025 15:21:11 +0900 Subject: [PATCH] Avoid fd to be closed by ParcelFileDescriptor's finalizer testBlockIncomingPacketsis flaky since ParcelFileDescriptor's finalizer closes fd and makes fd invalid. This CL fixes this issue by keeping the reference to ParcelFileDescriptor. Bug: 394503660 Test: testBlockIncomingPackets Change-Id: I1c1645d95b9d9ae1bd5af4ada500f50f350b6b0b --- .../src/com/android/cts/net/hostside/VpnTest.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/VpnTest.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/VpnTest.java index 34301966f3..d55df6fd48 100755 --- a/tests/cts/hostside/app/src/com/android/cts/net/hostside/VpnTest.java +++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/VpnTest.java @@ -1843,11 +1843,11 @@ public class VpnTest { final DetailedBlockedStatusCallback remoteUidCallback = new DetailedBlockedStatusCallback(); // Create a TUN interface - final FileDescriptor tunFd = runWithShellPermissionIdentity(() -> { + final ParcelFileDescriptor tunFd = runWithShellPermissionIdentity(() -> { final TestNetworkManager tnm = mTestContext.getSystemService(TestNetworkManager.class); final TestNetworkInterface iface = tnm.createTunInterface(List.of( TEST_IP4_DST_ADDR, TEST_IP6_DST_ADDR)); - return iface.getFileDescriptor().getFileDescriptor(); + return iface.getFileDescriptor(); }, MANAGE_TEST_NETWORKS); // Create a remote UDP socket @@ -1861,7 +1861,7 @@ public class VpnTest { remoteUidCallback.expectAvailableCallbacksWithBlockedReasonNone(network); // The remote UDP socket can receive packets coming from the TUN interface - checkBlockIncomingPacket(tunFd, remoteUdpFd, EXPECT_PASS); + checkBlockIncomingPacket(tunFd.getFileDescriptor(), remoteUdpFd, EXPECT_PASS); // Lockdown uid that has the remote UDP socket runWithShellPermissionIdentity(() -> { @@ -1877,7 +1877,7 @@ public class VpnTest { if (SdkLevel.isAtLeastT()) { // On T and above, lockdown rule drop packets not coming from lo regardless of the // VPN connectivity. - checkBlockIncomingPacket(tunFd, remoteUdpFd, EXPECT_BLOCK); + checkBlockIncomingPacket(tunFd.getFileDescriptor(), remoteUdpFd, EXPECT_BLOCK); } // Start the VPN that has default routes. This VPN should have interface filtering rule @@ -1889,9 +1889,9 @@ public class VpnTest { null /* proxyInfo */, null /* underlyingNetworks */, false /* isAlwaysMetered */); - checkBlockIncomingPacket(tunFd, remoteUdpFd, EXPECT_BLOCK); + checkBlockIncomingPacket(tunFd.getFileDescriptor(), remoteUdpFd, EXPECT_BLOCK); }, /* cleanup */ () -> { - Os.close(tunFd); + tunFd.close(); }, /* cleanup */ () -> { Os.close(remoteUdpFd); }, /* cleanup */ () -> {