Wait for network to be validated when setting a valid private DNS

The previous design just set a valid private DNS and checked
whether onCapabilitiesChanged() has received and
NetworkCapabilities#isPrivateDnsBroken is false. It cannot ensure
that the private DNS is working until the network is validated.
So change the design to waiting the network to be validated after
setting a valid private DNS.

Bug: 196769780
Test: atest CtsNetTestCases:ConnectivityManagerTest
Change-Id: I9c173a70e2b81788efa2ecbf928acda0379846da
This commit is contained in:
lucaslin 2021-10-21 16:47:26 +08:00
commit 4d441a9b8d

View file

@ -714,6 +714,12 @@ public class ConnectivityManagerTest {
.build();
}
private boolean hasPrivateDnsValidated(CallbackEntry entry, Network networkForPrivateDns) {
if (!networkForPrivateDns.equals(entry.getNetwork())) return false;
final NetworkCapabilities nc = ((CallbackEntry.CapabilitiesChanged) entry).getCaps();
return !nc.isPrivateDnsBroken() && nc.hasCapability(NET_CAPABILITY_VALIDATED);
}
@AppModeFull(reason = "WRITE_SECURE_SETTINGS permission can't be granted to instant apps")
@Test @IgnoreUpTo(Build.VERSION_CODES.Q)
public void testIsPrivateDnsBroken() throws InterruptedException {
@ -727,8 +733,7 @@ public class ConnectivityManagerTest {
mCtsNetUtils.setPrivateDnsStrictMode(goodPrivateDnsServer);
final Network networkForPrivateDns = mCtsNetUtils.ensureWifiConnected();
cb.eventuallyExpect(CallbackEntry.NETWORK_CAPS_UPDATED, NETWORK_CALLBACK_TIMEOUT_MS,
entry -> (!((CallbackEntry.CapabilitiesChanged) entry).getCaps()
.isPrivateDnsBroken()) && networkForPrivateDns.equals(entry.getNetwork()));
entry -> hasPrivateDnsValidated(entry, networkForPrivateDns));
// Verifying the broken private DNS sever
mCtsNetUtils.setPrivateDnsStrictMode(invalidPrivateDnsServer);