Merge "Make LinkPropertiesTest backwards compatible"

This commit is contained in:
Remi NGUYEN VAN
2020-03-17 02:27:27 +00:00
committed by Gerrit Code Review
2 changed files with 72 additions and 30 deletions

View File

@@ -20,6 +20,7 @@ java_library {
name: "FrameworksNetCommonTests",
srcs: ["java/**/*.java", "java/**/*.kt"],
static_libs: [
"androidx.core_core",
"androidx.test.rules",
"junit",
"mockito-target-minus-junit4",

View File

@@ -29,12 +29,19 @@ import static org.junit.Assert.fail;
import android.net.LinkProperties.ProvisioningChange;
import android.net.util.LinkPropertiesUtils.CompareResult;
import android.os.Build;
import android.system.OsConstants;
import android.util.ArraySet;
import androidx.core.os.BuildCompat;
import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;
import com.android.testutils.DevSdkIgnoreRule;
import com.android.testutils.DevSdkIgnoreRule.IgnoreAfter;
import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -50,6 +57,9 @@ import java.util.Set;
@RunWith(AndroidJUnit4.class)
@SmallTest
public class LinkPropertiesTest {
@Rule
public final DevSdkIgnoreRule ignoreRule = new DevSdkIgnoreRule();
private static final InetAddress ADDRV4 = address("75.208.6.1");
private static final InetAddress ADDRV6 = address("2001:0db8:85a3:0000:0000:8a2e:0370:7334");
private static final InetAddress DNS1 = address("75.208.7.1");
@@ -76,13 +86,23 @@ public class LinkPropertiesTest {
private static final LinkAddress LINKADDRV6 = new LinkAddress(ADDRV6, 128);
private static final LinkAddress LINKADDRV6LINKLOCAL = new LinkAddress("fe80::1/64");
private static final Uri CAPPORT_API_URL = Uri.parse("https://test.example.com/capportapi");
private static final CaptivePortalData CAPPORT_DATA = new CaptivePortalData.Builder()
.setVenueInfoUrl(Uri.parse("https://test.example.com/venue")).build();
// CaptivePortalData cannot be in a constant as it does not exist on Q.
// The test runner also crashes when scanning for tests if it is a return type.
private static Object getCaptivePortalData() {
return new CaptivePortalData.Builder()
.setVenueInfoUrl(Uri.parse("https://test.example.com/venue")).build();
}
private static InetAddress address(String addrString) {
return InetAddresses.parseNumericAddress(addrString);
}
private static boolean isAtLeastR() {
// BuildCompat.isAtLeastR is documented to return false on release SDKs (including R)
return Build.VERSION.SDK_INT > Build.VERSION_CODES.Q || BuildCompat.isAtLeastR();
}
private void checkEmpty(final LinkProperties lp) {
assertEquals(0, lp.getAllInterfaceNames().size());
assertEquals(0, lp.getAllAddresses().size());
@@ -98,14 +118,17 @@ public class LinkPropertiesTest {
assertNull(lp.getHttpProxy());
assertNull(lp.getTcpBufferSizes());
assertNull(lp.getNat64Prefix());
assertNull(lp.getDhcpServerAddress());
assertFalse(lp.isProvisioned());
assertFalse(lp.isIpv4Provisioned());
assertFalse(lp.isIpv6Provisioned());
assertFalse(lp.isPrivateDnsActive());
assertFalse(lp.isWakeOnLanSupported());
assertNull(lp.getCaptivePortalApiUrl());
assertNull(lp.getCaptivePortalData());
if (isAtLeastR()) {
assertNull(lp.getDhcpServerAddress());
assertFalse(lp.isWakeOnLanSupported());
assertNull(lp.getCaptivePortalApiUrl());
assertNull(lp.getCaptivePortalData());
}
}
private LinkProperties makeTestObject() {
@@ -127,10 +150,12 @@ public class LinkPropertiesTest {
lp.setMtu(MTU);
lp.setTcpBufferSizes(TCP_BUFFER_SIZES);
lp.setNat64Prefix(new IpPrefix("2001:db8:0:64::/96"));
lp.setDhcpServerAddress(DHCPSERVER);
lp.setWakeOnLanSupported(true);
lp.setCaptivePortalApiUrl(CAPPORT_API_URL);
lp.setCaptivePortalData(CAPPORT_DATA);
if (isAtLeastR()) {
lp.setDhcpServerAddress(DHCPSERVER);
lp.setWakeOnLanSupported(true);
lp.setCaptivePortalApiUrl(CAPPORT_API_URL);
lp.setCaptivePortalData((CaptivePortalData) getCaptivePortalData());
}
return lp;
}
@@ -169,14 +194,19 @@ public class LinkPropertiesTest {
assertTrue(source.isIdenticalTcpBufferSizes(target));
assertTrue(target.isIdenticalTcpBufferSizes(source));
assertTrue(source.isIdenticalWakeOnLan(target));
assertTrue(target.isIdenticalWakeOnLan(source));
if (isAtLeastR()) {
assertTrue(source.isIdenticalDhcpServerAddress(target));
assertTrue(source.isIdenticalDhcpServerAddress(source));
assertTrue(source.isIdenticalCaptivePortalApiUrl(target));
assertTrue(target.isIdenticalCaptivePortalApiUrl(source));
assertTrue(source.isIdenticalWakeOnLan(target));
assertTrue(target.isIdenticalWakeOnLan(source));
assertTrue(source.isIdenticalCaptivePortalData(target));
assertTrue(target.isIdenticalCaptivePortalData(source));
assertTrue(source.isIdenticalCaptivePortalApiUrl(target));
assertTrue(target.isIdenticalCaptivePortalApiUrl(source));
assertTrue(source.isIdenticalCaptivePortalData(target));
assertTrue(target.isIdenticalCaptivePortalData(source));
}
// Check result of equals().
assertTrue(source.equals(target));
@@ -943,8 +973,7 @@ public class LinkPropertiesTest {
assertEquals(new ArraySet<>(expectRemoved), (new ArraySet<>(result.removed)));
}
@Test
public void testLinkPropertiesParcelable() throws Exception {
private static LinkProperties makeLinkPropertiesForParceling() {
LinkProperties source = new LinkProperties();
source.setInterfaceName(NAME);
@@ -978,16 +1007,27 @@ public class LinkPropertiesTest {
source.setNat64Prefix(new IpPrefix("2001:db8:1:2:64:64::/96"));
source.setWakeOnLanSupported(true);
source.setCaptivePortalApiUrl(CAPPORT_API_URL);
source.setCaptivePortalData(CAPPORT_DATA);
source.setDhcpServerAddress((Inet4Address) GATEWAY1);
final LinkProperties stacked = new LinkProperties();
stacked.setInterfaceName("test-stacked");
source.addStackedLink(stacked);
return source;
}
@Test @IgnoreAfter(Build.VERSION_CODES.Q)
public void testLinkPropertiesParcelable_Q() throws Exception {
final LinkProperties source = makeLinkPropertiesForParceling();
assertParcelSane(source, 14 /* fieldCount */);
}
@Test @IgnoreUpTo(Build.VERSION_CODES.Q)
public void testLinkPropertiesParcelable() throws Exception {
final LinkProperties source = makeLinkPropertiesForParceling();
source.setWakeOnLanSupported(true);
source.setCaptivePortalApiUrl(CAPPORT_API_URL);
source.setCaptivePortalData((CaptivePortalData) getCaptivePortalData());
source.setDhcpServerAddress((Inet4Address) GATEWAY1);
assertParcelSane(source.makeSensitiveFieldsParcelingCopy(), 18 /* fieldCount */);
// Verify that without using a sensitiveFieldsParcelingCopy, sensitive fields are cleared.
@@ -997,7 +1037,8 @@ public class LinkPropertiesTest {
assertEquals(sanitized, parcelingRoundTrip(source));
}
@Test
// Parceling of the scope was broken until Q-QPR2
@Test @IgnoreUpTo(Build.VERSION_CODES.Q)
public void testLinkLocalDnsServerParceling() throws Exception {
final String strAddress = "fe80::1%lo";
final LinkProperties lp = new LinkProperties();
@@ -1120,7 +1161,7 @@ public class LinkPropertiesTest {
assertFalse(lp.isPrivateDnsActive());
}
@Test
@Test @IgnoreUpTo(Build.VERSION_CODES.Q)
public void testDhcpServerAddress() {
final LinkProperties lp = makeTestObject();
assertEquals(DHCPSERVER, lp.getDhcpServerAddress());
@@ -1129,7 +1170,7 @@ public class LinkPropertiesTest {
assertNull(lp.getDhcpServerAddress());
}
@Test
@Test @IgnoreUpTo(Build.VERSION_CODES.Q)
public void testWakeOnLanSupported() {
final LinkProperties lp = makeTestObject();
assertTrue(lp.isWakeOnLanSupported());
@@ -1138,7 +1179,7 @@ public class LinkPropertiesTest {
assertFalse(lp.isWakeOnLanSupported());
}
@Test
@Test @IgnoreUpTo(Build.VERSION_CODES.Q)
public void testCaptivePortalApiUrl() {
final LinkProperties lp = makeTestObject();
assertEquals(CAPPORT_API_URL, lp.getCaptivePortalApiUrl());
@@ -1147,10 +1188,10 @@ public class LinkPropertiesTest {
assertNull(lp.getCaptivePortalApiUrl());
}
@Test
@Test @IgnoreUpTo(Build.VERSION_CODES.Q)
public void testCaptivePortalData() {
final LinkProperties lp = makeTestObject();
assertEquals(CAPPORT_DATA, lp.getCaptivePortalData());
assertEquals(getCaptivePortalData(), lp.getCaptivePortalData());
lp.clear();
assertNull(lp.getCaptivePortalData());