From: Remi NGUYEN VAN Date: Fri, 12 Apr 2019 03:35:55 +0000 (+0900) Subject: Add tests for DhcpErrorEvent X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=a7b8452e883c6f2a6a4b6e592150c7607ac6f81b;p=android-x86%2Fframeworks-base.git Add tests for DhcpErrorEvent The tests are run both in unit and CTS tests. Test: atest FrameworksNetTests NetworkStackTestCases Bug: 129200175 Change-Id: I52976bbbaca26fb317836e8461e372c25df02a22 --- diff --git a/tests/net/common/Android.bp b/tests/net/common/Android.bp index 9ee58583779d..3b2e34a159ee 100644 --- a/tests/net/common/Android.bp +++ b/tests/net/common/Android.bp @@ -18,7 +18,7 @@ // They must be fast and stable, and exercise public or test APIs. java_library { name: "FrameworksNetCommonTests", - srcs: ["java/**/*.java"], + srcs: ["java/**/*.java", "java/**/*.kt"], static_libs: [ "androidx.test.rules", "frameworks-net-testutils", diff --git a/tests/net/common/java/android/net/LinkPropertiesTest.java b/tests/net/common/java/android/net/LinkPropertiesTest.java index 417729150be8..709f5f69aa2b 100644 --- a/tests/net/common/java/android/net/LinkPropertiesTest.java +++ b/tests/net/common/java/android/net/LinkPropertiesTest.java @@ -868,12 +868,12 @@ public class LinkPropertiesTest { source.setNat64Prefix(new IpPrefix("2001:db8:1:2:64:64::/96")); - TestUtils.assertParcelingIsLossless(source, LinkProperties.CREATOR); + TestUtils.assertParcelingIsLossless(source); } @Test public void testParcelUninitialized() throws Exception { LinkProperties empty = new LinkProperties(); - TestUtils.assertParcelingIsLossless(empty, LinkProperties.CREATOR); + TestUtils.assertParcelingIsLossless(empty); } } diff --git a/tests/net/common/java/android/net/apf/ApfCapabilitiesTest.java b/tests/net/common/java/android/net/apf/ApfCapabilitiesTest.java index 7238895b3657..3ed8a86b2fb5 100644 --- a/tests/net/common/java/android/net/apf/ApfCapabilitiesTest.java +++ b/tests/net/common/java/android/net/apf/ApfCapabilitiesTest.java @@ -36,7 +36,7 @@ public class ApfCapabilitiesTest { final ApfCapabilities caps = new ApfCapabilities(123, 456, 789); ParcelableTestUtil.assertFieldCountEquals(3, ApfCapabilities.class); - TestUtils.assertParcelingIsLossless(caps, ApfCapabilities.CREATOR); + TestUtils.assertParcelingIsLossless(caps); } @Test diff --git a/tests/net/common/java/android/net/metrics/DhcpErrorEventTest.kt b/tests/net/common/java/android/net/metrics/DhcpErrorEventTest.kt new file mode 100644 index 000000000000..e19195322e1d --- /dev/null +++ b/tests/net/common/java/android/net/metrics/DhcpErrorEventTest.kt @@ -0,0 +1,67 @@ +package android.net.metrics + +import android.net.metrics.DhcpErrorEvent.errorCodeWithOption +import android.net.metrics.DhcpErrorEvent.DHCP_INVALID_OPTION_LENGTH +import androidx.test.filters.SmallTest +import androidx.test.runner.AndroidJUnit4 +import com.android.internal.util.TestUtils.parcelingRoundTrip +import java.lang.reflect.Modifier +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNotNull +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith + +private const val TEST_ERROR_CODE = 12345 +/** + * DHCP Optional Type: DHCP Subnet Mask (Copy from DhcpPacket.java) + */ +private const val DHCP_SUBNET_MASK = 1 + +@RunWith(AndroidJUnit4::class) +@SmallTest +class DhcpErrorEventTest { + + @Test + fun testConstructor() { + val event = DhcpErrorEvent(TEST_ERROR_CODE) + assertEquals(TEST_ERROR_CODE, event.errorCode) + } + + @Test + fun testParcelUnparcel() { + val event = DhcpErrorEvent(TEST_ERROR_CODE) + val parceled = parcelingRoundTrip(event) + assertEquals(TEST_ERROR_CODE, parceled.errorCode) + } + + @Test + fun testErrorCodeWithOption() { + val errorCode = errorCodeWithOption(DHCP_INVALID_OPTION_LENGTH, DHCP_SUBNET_MASK); + assertTrue((DHCP_INVALID_OPTION_LENGTH and errorCode) == DHCP_INVALID_OPTION_LENGTH); + assertTrue((DHCP_SUBNET_MASK and errorCode) == DHCP_SUBNET_MASK); + } + + @Test + fun testToString() { + val names = listOf("L2_ERROR", "L3_ERROR", "L4_ERROR", "DHCP_ERROR", "MISC_ERROR") + val errorFields = DhcpErrorEvent::class.java.declaredFields.filter { + it.type == Int::class.javaPrimitiveType + && Modifier.isPublic(it.modifiers) && Modifier.isStatic(it.modifiers) + && it.name !in names + } + + errorFields.forEach { + val intValue = it.getInt(null) + val stringValue = DhcpErrorEvent(intValue).toString() + assertTrue("Invalid string for error 0x%08X (field %s): %s".format(intValue, it.name, + stringValue), + stringValue.contains(it.name)) + } + } + + @Test + fun testToString_InvalidErrorCode() { + assertNotNull(DhcpErrorEvent(TEST_ERROR_CODE).toString()) + } +} \ No newline at end of file diff --git a/tests/net/java/android/net/TcpKeepalivePacketDataTest.java b/tests/net/java/android/net/TcpKeepalivePacketDataTest.java index e0b722761c34..583d3fd536aa 100644 --- a/tests/net/java/android/net/TcpKeepalivePacketDataTest.java +++ b/tests/net/java/android/net/TcpKeepalivePacketDataTest.java @@ -79,7 +79,7 @@ public final class TcpKeepalivePacketDataTest { assertEquals(testInfo.tos, resultData.ipTos); assertEquals(testInfo.ttl, resultData.ipTtl); - TestUtils.assertParcelingIsLossless(resultData, TcpKeepalivePacketData.CREATOR); + TestUtils.assertParcelingIsLossless(resultData); final byte[] packet = resultData.getPacket(); // IP version and IHL diff --git a/tests/net/util/java/com/android/internal/util/TestUtils.java b/tests/net/util/java/com/android/internal/util/TestUtils.java index 75329a805606..a99cd4716f9a 100644 --- a/tests/net/util/java/com/android/internal/util/TestUtils.java +++ b/tests/net/util/java/com/android/internal/util/TestUtils.java @@ -69,9 +69,17 @@ public final class TestUtils { } } - // TODO : fetch the creator through reflection or something instead of passing it - public static > - void assertParcelingIsLossless(T source, C creator) { + /** + * Return a new instance of {@code T} after being parceled then unparceled. + */ + public static T parcelingRoundTrip(T source) { + final Parcelable.Creator creator; + try { + creator = (Parcelable.Creator) source.getClass().getField("CREATOR").get(null); + } catch (IllegalAccessException | NoSuchFieldException e) { + fail("Missing CREATOR field: " + e.getMessage()); + return null; + } Parcel p = Parcel.obtain(); source.writeToParcel(p, /* flags */ 0); p.setDataPosition(0); @@ -79,7 +87,14 @@ public final class TestUtils { p = Parcel.obtain(); p.unmarshall(marshalled, 0, marshalled.length); p.setDataPosition(0); - T dest = creator.createFromParcel(p); - assertEquals(source, dest); + return creator.createFromParcel(p); + } + + /** + * Assert that after being parceled then unparceled, {@code source} is equal to the original + * object. + */ + public static void assertParcelingIsLossless(T source) { + assertEquals(source, parcelingRoundTrip(source)); } }