diff options
author | Erik Kline <ek@google.com> | 2015-06-16 03:43:53 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2015-06-16 03:43:56 +0000 |
commit | 0d16281a03722f3f66a076718251c2166518d122 (patch) | |
tree | a6200a218696d825edda6678f2dfc1eaa120a04e | |
parent | 100e1c9235720ac020a20009da8b85f64efd4c11 (diff) | |
parent | e1a6cf2caa628a02032be9b26462958f3769bd70 (diff) | |
download | frameworks_base-0d16281a03722f3f66a076718251c2166518d122.zip frameworks_base-0d16281a03722f3f66a076718251c2166518d122.tar.gz frameworks_base-0d16281a03722f3f66a076718251c2166518d122.tar.bz2 |
Merge "Add a test that Network#getNetworkHandle() behaves sanely." into mnc-dev
-rw-r--r-- | core/java/android/net/Network.java | 3 | ||||
-rw-r--r-- | core/tests/coretests/src/android/net/NetworkTest.java | 50 |
2 files changed, 53 insertions, 0 deletions
diff --git a/core/java/android/net/Network.java b/core/java/android/net/Network.java index 9628bae..fe69320 100644 --- a/core/java/android/net/Network.java +++ b/core/java/android/net/Network.java @@ -378,6 +378,9 @@ public class Network implements Parcelable { // // The HANDLE_MAGIC value MUST be kept in sync with the corresponding // value in the native/android/net.c NDK implementation. + if (netId == 0) { + return 0L; // make this zero condition obvious for debugging + } final long HANDLE_MAGIC = 0xfacade; return (((long) netId) << 32) | HANDLE_MAGIC; } diff --git a/core/tests/coretests/src/android/net/NetworkTest.java b/core/tests/coretests/src/android/net/NetworkTest.java index b0ecb04..74b6d98 100644 --- a/core/tests/coretests/src/android/net/NetworkTest.java +++ b/core/tests/coretests/src/android/net/NetworkTest.java @@ -16,11 +16,14 @@ package android.net; +import static android.test.MoreAsserts.assertNotEqual; + import android.net.LocalServerSocket; import android.net.LocalSocket; import android.net.LocalSocketAddress; import android.net.Network; import android.test.suitebuilder.annotation.SmallTest; + import java.io.File; import java.io.FileDescriptor; import java.io.FileInputStream; @@ -29,6 +32,7 @@ import java.net.DatagramSocket; import java.net.InetAddress; import java.net.Inet6Address; import java.net.SocketException; + import junit.framework.TestCase; public class NetworkTest extends TestCase { @@ -93,4 +97,50 @@ public class NetworkTest extends TestCase { fail("SocketException not thrown"); } catch (SocketException expected) {} } + + @SmallTest + public void testZeroIsObviousForDebugging() { + Network zero = new Network(0); + assertEquals(0, zero.hashCode()); + assertEquals(0, zero.getNetworkHandle()); + assertEquals("0", zero.toString()); + } + + @SmallTest + public void testGetNetworkHandle() { + Network one = new Network(1); + Network two = new Network(2); + Network three = new Network(3); + + // None of the hashcodes are zero. + assertNotEqual(0, one.hashCode()); + assertNotEqual(0, two.hashCode()); + assertNotEqual(0, three.hashCode()); + + // All the hashcodes are distinct. + assertNotEqual(one.hashCode(), two.hashCode()); + assertNotEqual(one.hashCode(), three.hashCode()); + assertNotEqual(two.hashCode(), three.hashCode()); + + // None of the handles are zero. + assertNotEqual(0, one.getNetworkHandle()); + assertNotEqual(0, two.getNetworkHandle()); + assertNotEqual(0, three.getNetworkHandle()); + + // All the handles are distinct. + assertNotEqual(one.getNetworkHandle(), two.getNetworkHandle()); + assertNotEqual(one.getNetworkHandle(), three.getNetworkHandle()); + assertNotEqual(two.getNetworkHandle(), three.getNetworkHandle()); + + // The handles are not equal to the hashcodes. + assertNotEqual(one.hashCode(), one.getNetworkHandle()); + assertNotEqual(two.hashCode(), two.getNetworkHandle()); + assertNotEqual(three.hashCode(), three.getNetworkHandle()); + + // Adjust as necessary to test an implementation's specific constants. + // When running with runtest, "adb logcat -s TestRunner" can be useful. + assertEquals(4311403230L, one.getNetworkHandle()); + assertEquals(8606370526L, two.getNetworkHandle()); + assertEquals(12901337822L, three.getNetworkHandle()); + } } |