Use random available port in CTS tests.

Let the OS pick a random unused port so that the test never gets
EADDRINUSE.

Bug: 68657805
Test: Run tests in android.net.cts.IpSecManagerTest
Change-Id: I7f6c68f3c92f5cbed856eda52adcdffb8adc4734
This commit is contained in:
Jonathan Basseri
2017-10-30 14:56:56 -07:00
parent fb02e14772
commit 382ea38fc0

View File

@@ -155,8 +155,9 @@ public class IpSecManagerTest extends AndroidTestCase {
CRYPT_KEY.length * 8))
.buildTransportModeTransform(local);
// Hack to ensure the socket doesn't block indefinitely on failure
DatagramSocket localSocket = new DatagramSocket(8888);
// Bind localSocket to a random available port.
DatagramSocket localSocket = new DatagramSocket(0);
int localPort = localSocket.getLocalPort();
localSocket.setSoTimeout(500);
ParcelFileDescriptor pin = ParcelFileDescriptor.fromDatagramSocket(localSocket);
FileDescriptor udpSocket = pin.getFileDescriptor();
@@ -165,7 +166,7 @@ public class IpSecManagerTest extends AndroidTestCase {
byte[] data = new String("Best test data ever!").getBytes("UTF-8");
byte[] in = new byte[data.length];
Os.sendto(udpSocket, data, 0, data.length, 0, local, 8888);
Os.sendto(udpSocket, data, 0, data.length, 0, local, localPort);
Os.read(udpSocket, in, 0, in.length);
assertTrue("Encapsulated data did not match.", Arrays.equals(data, in));
mISM.removeTransportModeTransform(udpSocket, transform);