1 package android.net.metrics 2 3 import android.net.metrics.DhcpErrorEvent.DHCP_INVALID_OPTION_LENGTH 4 import android.net.metrics.DhcpErrorEvent.errorCodeWithOption 5 import androidx.test.filters.SmallTest 6 import androidx.test.runner.AndroidJUnit4 7 import com.android.testutils.parcelingRoundTrip 8 import java.lang.reflect.Modifier 9 import org.junit.Assert.assertEquals 10 import org.junit.Assert.assertNotNull 11 import org.junit.Assert.assertTrue 12 import org.junit.Test 13 import org.junit.runner.RunWith 14 15 private const val TEST_ERROR_CODE = 12345 16 //DHCP Optional Type: DHCP Subnet Mask (Copy from DhcpPacket.java due to it's protected) 17 private const val DHCP_SUBNET_MASK = 1 18 19 @RunWith(AndroidJUnit4::class) 20 @SmallTest 21 class DhcpErrorEventTest { 22 23 @Test testConstructornull24 fun testConstructor() { 25 val event = DhcpErrorEvent(TEST_ERROR_CODE) 26 assertEquals(TEST_ERROR_CODE, event.errorCode) 27 } 28 29 @Test testParcelUnparcelnull30 fun testParcelUnparcel() { 31 val event = DhcpErrorEvent(TEST_ERROR_CODE) 32 val parceled = parcelingRoundTrip(event) 33 assertEquals(TEST_ERROR_CODE, parceled.errorCode) 34 } 35 36 @Test testErrorCodeWithOptionnull37 fun testErrorCodeWithOption() { 38 val errorCode = errorCodeWithOption(DHCP_INVALID_OPTION_LENGTH, DHCP_SUBNET_MASK); 39 assertTrue((DHCP_INVALID_OPTION_LENGTH and errorCode) == DHCP_INVALID_OPTION_LENGTH); 40 assertTrue((DHCP_SUBNET_MASK and errorCode) == DHCP_SUBNET_MASK); 41 } 42 43 @Test testToStringnull44 fun testToString() { 45 val names = listOf("L2_ERROR", "L3_ERROR", "L4_ERROR", "DHCP_ERROR", "MISC_ERROR") 46 val errorFields = DhcpErrorEvent::class.java.declaredFields.filter { 47 it.type == Int::class.javaPrimitiveType 48 && Modifier.isPublic(it.modifiers) && Modifier.isStatic(it.modifiers) 49 && it.name !in names 50 } 51 52 errorFields.forEach { 53 val intValue = it.getInt(null) 54 val stringValue = DhcpErrorEvent(intValue).toString() 55 assertTrue("Invalid string for error 0x%08X (field %s): %s".format(intValue, it.name, 56 stringValue), 57 stringValue.contains(it.name)) 58 } 59 } 60 61 @Test testToString_InvalidErrorCodenull62 fun testToString_InvalidErrorCode() { 63 assertNotNull(DhcpErrorEvent(TEST_ERROR_CODE).toString()) 64 } 65 } 66