Merge "Separate test into reasonable multiple tests"

This commit is contained in:
Chiachang Wang
2021-09-07 02:40:13 +00:00
committed by Gerrit Code Review
2 changed files with 87 additions and 29 deletions

View File

@@ -852,7 +852,7 @@ public final class NetworkCapabilities implements Parcelable {
// SubIds are only allowed for Test Networks that only declare TRANSPORT_TEST. // SubIds are only allowed for Test Networks that only declare TRANSPORT_TEST.
setSubscriptionIds(originalSubIds); setSubscriptionIds(originalSubIds);
} else { } else {
// If the test transport is restricted, then it may declare any transport. // If the test network is restricted, then it may declare any transport.
mTransportTypes = (originalTransportTypes | (1 << TRANSPORT_TEST)); mTransportTypes = (originalTransportTypes | (1 << TRANSPORT_TEST));
} }
mNetworkCapabilities = originalCapabilities & TEST_NETWORKS_ALLOWED_CAPABILITIES; mNetworkCapabilities = originalCapabilities & TEST_NETWORKS_ALLOWED_CAPABILITIES;

View File

@@ -1170,46 +1170,104 @@ public class NetworkCapabilitiesTest {
} }
@Test @IgnoreUpTo(Build.VERSION_CODES.R) @ConnectivityModuleTest @Test @IgnoreUpTo(Build.VERSION_CODES.R) @ConnectivityModuleTest
public void testRestrictCapabilitiesForTestNetwork() { public void testRestrictCapabilitiesForTestNetworkByNotOwnerWithNonRestrictedNc() {
testRestrictCapabilitiesForTestNetworkWithNonRestrictedNc(false /* isOwner */);
}
@Test @IgnoreUpTo(Build.VERSION_CODES.R) @ConnectivityModuleTest
public void testRestrictCapabilitiesForTestNetworkByOwnerWithNonRestrictedNc() {
testRestrictCapabilitiesForTestNetworkWithNonRestrictedNc(true /* isOwner */);
}
private void testRestrictCapabilitiesForTestNetworkWithNonRestrictedNc(boolean isOwner) {
final int ownerUid = 1234; final int ownerUid = 1234;
final int signalStrength = -80;
final int[] administratorUids = {1001, ownerUid}; final int[] administratorUids = {1001, ownerUid};
final TelephonyNetworkSpecifier specifier = new TelephonyNetworkSpecifier(TEST_SUBID1);
final TransportInfo transportInfo = new TransportInfo() {};
final NetworkCapabilities nonRestrictedNc = new NetworkCapabilities.Builder() final NetworkCapabilities nonRestrictedNc = new NetworkCapabilities.Builder()
.addTransportType(TRANSPORT_CELLULAR) .addTransportType(TRANSPORT_CELLULAR)
.addTransportType(TRANSPORT_VPN)
.addCapability(NET_CAPABILITY_MMS) .addCapability(NET_CAPABILITY_MMS)
.addCapability(NET_CAPABILITY_NOT_METERED) .addCapability(NET_CAPABILITY_NOT_METERED)
.setAdministratorUids(administratorUids) .setAdministratorUids(administratorUids)
.setOwnerUid(ownerUid) .setOwnerUid(ownerUid)
.setNetworkSpecifier(specifier)
.setSignalStrength(signalStrength)
.setTransportInfo(transportInfo)
.setSubscriptionIds(Set.of(TEST_SUBID1)).build(); .setSubscriptionIds(Set.of(TEST_SUBID1)).build();
final int creatorUid = isOwner ? ownerUid : INVALID_UID;
nonRestrictedNc.restrictCapabilitiesForTestNetwork(creatorUid);
nonRestrictedNc.restrictCapabilitiesForTestNetwork(ownerUid); final NetworkCapabilities.Builder expectedNcBuilder = new NetworkCapabilities.Builder();
// TRANSPORT_TEST will be appended // Non-UNRESTRICTED_TEST_NETWORKS_ALLOWED_TRANSPORTS will be removed and TRANSPORT_TEST will
assertTrue(nonRestrictedNc.hasTransport(TRANSPORT_TEST)); // be appended for non-restricted net cap.
assertEquals(Set.of(TEST_SUBID1), nonRestrictedNc.getSubscriptionIds()); expectedNcBuilder.addTransportType(TRANSPORT_TEST);
// Non-UNRESTRICTED_TEST_NETWORKS_ALLOWED_TRANSPORTS will be removed // Only TEST_NETWORKS_ALLOWED_CAPABILITIES will be kept. SubIds are only allowed for Test
assertFalse(nonRestrictedNc.hasTransport(TRANSPORT_CELLULAR)); // Networks that only declare TRANSPORT_TEST.
assertTrue(nonRestrictedNc.hasTransport(TRANSPORT_VPN)); expectedNcBuilder.addCapability(NET_CAPABILITY_NOT_METERED)
// Only TEST_NETWORKS_ALLOWED_CAPABILITIES will be kept .removeCapability(NET_CAPABILITY_TRUSTED)
assertFalse(nonRestrictedNc.hasCapability(NET_CAPABILITY_MMS)); .setSubscriptionIds(Set.of(TEST_SUBID1));
assertTrue(nonRestrictedNc.hasCapability(NET_CAPABILITY_NOT_METERED));
final NetworkCapabilities restrictedNc = new NetworkCapabilities.Builder(nonRestrictedNc) expectedNcBuilder.setNetworkSpecifier(specifier)
.setSignalStrength(signalStrength).setTransportInfo(transportInfo);
if (creatorUid == ownerUid) {
// Only retain the owner and administrator UIDs if they match the app registering the
// remote caller that registered the network.
expectedNcBuilder.setAdministratorUids(new int[]{ownerUid}).setOwnerUid(ownerUid);
}
assertEquals(expectedNcBuilder.build(), nonRestrictedNc);
}
@Test @IgnoreUpTo(Build.VERSION_CODES.R) @ConnectivityModuleTest
public void testRestrictCapabilitiesForTestNetworkByNotOwnerWithRestrictedNc() {
testRestrictCapabilitiesForTestNetworkWithRestrictedNc(false /* isOwner */);
}
@Test @IgnoreUpTo(Build.VERSION_CODES.R) @ConnectivityModuleTest
public void testRestrictCapabilitiesForTestNetworkByOwnerWithRestrictedNc() {
testRestrictCapabilitiesForTestNetworkWithRestrictedNc(true /* isOwner */);
}
private void testRestrictCapabilitiesForTestNetworkWithRestrictedNc(boolean isOwner) {
final int ownerUid = 1234;
final int signalStrength = -80;
final int[] administratorUids = {1001, ownerUid};
final TransportInfo transportInfo = new TransportInfo() {};
// No NetworkSpecifier is set because after performing restrictCapabilitiesForTestNetwork
// the networkCapabilities will contain more than one transport type. However,
// networkCapabilities must have a single transport specified to use NetworkSpecifier. Thus,
// do not verify this part since it's verified in other tests.
final NetworkCapabilities restrictedNc = new NetworkCapabilities.Builder()
.removeCapability(NET_CAPABILITY_NOT_RESTRICTED) .removeCapability(NET_CAPABILITY_NOT_RESTRICTED)
.addTransportType(TRANSPORT_CELLULAR) .addTransportType(TRANSPORT_CELLULAR)
.addCapability(NET_CAPABILITY_MMS).build(); .addCapability(NET_CAPABILITY_MMS)
restrictedNc.restrictCapabilitiesForTestNetwork(ownerUid); .addCapability(NET_CAPABILITY_NOT_METERED)
// It may declare any transport if the net cap is restricted .setAdministratorUids(administratorUids)
assertTrue(restrictedNc.hasTransport(TRANSPORT_CELLULAR)); .setOwnerUid(ownerUid)
// SubIds will be cleared. .setSignalStrength(signalStrength)
assertEquals(new ArraySet<>(), restrictedNc.getSubscriptionIds()); .setTransportInfo(transportInfo)
// Only retain the owner and administrator UIDs if they match the app registering the remote .setSubscriptionIds(Set.of(TEST_SUBID1)).build();
// caller that registered the network. final int creatorUid = isOwner ? ownerUid : INVALID_UID;
assertEquals(ownerUid, restrictedNc.getOwnerUid()); restrictedNc.restrictCapabilitiesForTestNetwork(creatorUid);
assertArrayEquals(new int[] {ownerUid}, restrictedNc.getAdministratorUids());
// The creator UID does not match the owner and administrator UIDs will clear the owner and final NetworkCapabilities.Builder expectedNcBuilder = new NetworkCapabilities.Builder()
// administrator UIDs. .removeCapability(NET_CAPABILITY_NOT_RESTRICTED);
restrictedNc.restrictCapabilitiesForTestNetwork(5678); // If the test network is restricted, then the network may declare any transport, and
assertEquals(INVALID_UID, restrictedNc.getOwnerUid()); // appended with TRANSPORT_TEST.
assertArrayEquals(new int[0], restrictedNc.getAdministratorUids()); expectedNcBuilder.addTransportType(TRANSPORT_CELLULAR);
expectedNcBuilder.addTransportType(TRANSPORT_TEST);
// Only TEST_NETWORKS_ALLOWED_CAPABILITIES will be kept.
expectedNcBuilder.addCapability(NET_CAPABILITY_NOT_METERED);
expectedNcBuilder.removeCapability(NET_CAPABILITY_TRUSTED);
expectedNcBuilder.setSignalStrength(signalStrength).setTransportInfo(transportInfo);
if (creatorUid == ownerUid) {
// Only retain the owner and administrator UIDs if they match the app registering the
// remote caller that registered the network.
expectedNcBuilder.setAdministratorUids(new int[]{ownerUid}).setOwnerUid(ownerUid);
}
assertEquals(expectedNcBuilder.build(), restrictedNc);
} }
} }