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
diff --git a/tests/cts/net/src/android/net/cts/IpSecManagerTest.java b/tests/cts/net/src/android/net/cts/IpSecManagerTest.java
index 599102c..6348d04 100644
--- a/tests/cts/net/src/android/net/cts/IpSecManagerTest.java
+++ b/tests/cts/net/src/android/net/cts/IpSecManagerTest.java
@@ -155,8 +155,9 @@
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 @@
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);