Make ServerSocketChannelTest.test_bind_loopback more tolerant.

This test is designed to ensure that a server bound to
a loopback address is not reachable via an external IP
address. However it could receive false positives if some
other server was bound to the same port on an external IP.

This change ensures that the test only fails only if its
own server socket is reached via an external address.  If a
different server on an external IP is reached then the test
passes but does not re-test that external IP.  In practice
such port collisions happen very infrequently (approx once per
month in continuous CTS tests) so there is a very low chance
of the test passing more than once if it should be failing.

Even though this change adds a latch to synchronise the threads,
there is still a small window where a client connect can arrive
and get closed between the latch being released and the server
calling accept().  This is mitigated by adding a listen backlog
to the bind() call however there is still a very low probability
that this test will incorrectly pass if this happens.

Also convert to JUnit 4 and misc tidy-ups.

Fixes: 148476993
Test: atest --iterations 200 libcore.java.nio.channels.ServerSocketChannelTest
Change-Id: I8cad7a2ede549184aff399690c6484da9d12929e
1 file changed