Merge "Fix READ_DEVICE_CONFIG permission denied problem in TetheringTests"

This commit is contained in:
Treehugger Robot
2020-05-14 16:00:27 +00:00
committed by Gerrit Code Review
4 changed files with 33 additions and 34 deletions

View File

@@ -132,7 +132,7 @@ public class TetheringConfiguration {
isDunRequired = checkDunRequired(ctx); isDunRequired = checkDunRequired(ctx);
chooseUpstreamAutomatically = getResourceBoolean( chooseUpstreamAutomatically = getResourceBoolean(
res, R.bool.config_tether_upstream_automatic, false /** default value */); res, R.bool.config_tether_upstream_automatic, false /** defaultValue */);
preferredUpstreamIfaceTypes = getUpstreamIfaceTypes(res, isDunRequired); preferredUpstreamIfaceTypes = getUpstreamIfaceTypes(res, isDunRequired);
legacyDhcpRanges = getLegacyDhcpRanges(res); legacyDhcpRanges = getLegacyDhcpRanges(res);
@@ -375,30 +375,31 @@ public class TetheringConfiguration {
// Priority 1: Device config // Priority 1: Device config
// Priority 2: Resource config // Priority 2: Resource config
// Priority 3: Default value // Priority 3: Default value
final boolean resourceValue = getResourceBoolean( final boolean defaultValue = getResourceBoolean(
res, R.bool.config_tether_enable_bpf_offload, true /** default value */); res, R.bool.config_tether_enable_bpf_offload, true /** default value */);
// Due to the limitation of static mock for testing, using #getProperty directly instead return getDeviceConfigBoolean(OVERRIDE_TETHER_ENABLE_BPF_OFFLOAD, defaultValue);
// of getDeviceConfigBoolean. getDeviceConfigBoolean is not invoked because it uses
// #getBoolean to get the boolean device config. The test can't know that the returned
// boolean value comes from device config or default value (because of null property
// string). Because the test would like to verify null property boolean string case,
// use DeviceConfig.getProperty here. See also the test case testBpfOffload{*} in
// TetheringConfigurationTest.java.
final String value = DeviceConfig.getProperty(
NAMESPACE_CONNECTIVITY, OVERRIDE_TETHER_ENABLE_BPF_OFFLOAD);
return (value != null) ? Boolean.parseBoolean(value) : resourceValue;
} }
private boolean getEnableLegacyDhcpServer(final Resources res) { private boolean getEnableLegacyDhcpServer(final Resources res) {
return getResourceBoolean( return getResourceBoolean(
res, R.bool.config_tether_enable_legacy_dhcp_server, false /** default value */) res, R.bool.config_tether_enable_legacy_dhcp_server, false /** defaultValue */)
|| getDeviceConfigBoolean(TETHER_ENABLE_LEGACY_DHCP_SERVER); || getDeviceConfigBoolean(
TETHER_ENABLE_LEGACY_DHCP_SERVER, false /** defaultValue */);
}
private boolean getDeviceConfigBoolean(final String name, final boolean defaultValue) {
// Due to the limitation of static mock for testing, using #getDeviceConfigProperty instead
// of DeviceConfig#getBoolean. If using #getBoolean here, the test can't know that the
// returned boolean value comes from device config or default value (because of null
// property string). See the test case testBpfOffload{*} in TetheringConfigurationTest.java.
final String value = getDeviceConfigProperty(name);
return value != null ? Boolean.parseBoolean(value) : defaultValue;
} }
@VisibleForTesting @VisibleForTesting
protected boolean getDeviceConfigBoolean(final String name) { protected String getDeviceConfigProperty(String name) {
return DeviceConfig.getBoolean(NAMESPACE_CONNECTIVITY, name, false /** defaultValue */); return DeviceConfig.getProperty(NAMESPACE_CONNECTIVITY, name);
} }
private Resources getResources(Context ctx, int subId) { private Resources getResources(Context ctx, int subId) {

View File

@@ -147,9 +147,8 @@ public final class EntitlementManagerTest {
doReturn(false).when( doReturn(false).when(
() -> SystemProperties.getBoolean( () -> SystemProperties.getBoolean(
eq(EntitlementManager.DISABLE_PROVISIONING_SYSPROP_KEY), anyBoolean())); eq(EntitlementManager.DISABLE_PROVISIONING_SYSPROP_KEY), anyBoolean()));
doReturn(false).when( doReturn(null).when(
() -> DeviceConfig.getBoolean(eq(NAMESPACE_CONNECTIVITY), () -> DeviceConfig.getProperty(eq(NAMESPACE_CONNECTIVITY), anyString()));
eq(TetheringConfiguration.TETHER_ENABLE_LEGACY_DHCP_SERVER), anyBoolean()));
when(mResources.getStringArray(R.array.config_tether_dhcp_range)) when(mResources.getStringArray(R.array.config_tether_dhcp_range))
.thenReturn(new String[0]); .thenReturn(new String[0]);

View File

@@ -30,7 +30,6 @@ import static com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSess
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.eq; import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@@ -109,9 +108,9 @@ public class TetheringConfigurationTest {
.mockStatic(DeviceConfig.class) .mockStatic(DeviceConfig.class)
.strictness(Strictness.WARN) .strictness(Strictness.WARN)
.startMocking(); .startMocking();
doReturn(false).when( doReturn(null).when(
() -> DeviceConfig.getBoolean(eq(NAMESPACE_CONNECTIVITY), () -> DeviceConfig.getProperty(eq(NAMESPACE_CONNECTIVITY),
eq(TetheringConfiguration.TETHER_ENABLE_LEGACY_DHCP_SERVER), anyBoolean())); eq(TetheringConfiguration.TETHER_ENABLE_LEGACY_DHCP_SERVER)));
when(mResources.getStringArray(R.array.config_tether_dhcp_range)).thenReturn( when(mResources.getStringArray(R.array.config_tether_dhcp_range)).thenReturn(
new String[0]); new String[0]);
@@ -328,9 +327,9 @@ public class TetheringConfigurationTest {
public void testNewDhcpServerDisabled() { public void testNewDhcpServerDisabled() {
when(mResources.getBoolean(R.bool.config_tether_enable_legacy_dhcp_server)).thenReturn( when(mResources.getBoolean(R.bool.config_tether_enable_legacy_dhcp_server)).thenReturn(
true); true);
doReturn(false).when( doReturn("false").when(
() -> DeviceConfig.getBoolean(eq(NAMESPACE_CONNECTIVITY), () -> DeviceConfig.getProperty(eq(NAMESPACE_CONNECTIVITY),
eq(TetheringConfiguration.TETHER_ENABLE_LEGACY_DHCP_SERVER), anyBoolean())); eq(TetheringConfiguration.TETHER_ENABLE_LEGACY_DHCP_SERVER)));
final TetheringConfiguration enableByRes = final TetheringConfiguration enableByRes =
new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID); new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID);
@@ -338,9 +337,9 @@ public class TetheringConfigurationTest {
when(mResources.getBoolean(R.bool.config_tether_enable_legacy_dhcp_server)).thenReturn( when(mResources.getBoolean(R.bool.config_tether_enable_legacy_dhcp_server)).thenReturn(
false); false);
doReturn(true).when( doReturn("true").when(
() -> DeviceConfig.getBoolean(eq(NAMESPACE_CONNECTIVITY), () -> DeviceConfig.getProperty(eq(NAMESPACE_CONNECTIVITY),
eq(TetheringConfiguration.TETHER_ENABLE_LEGACY_DHCP_SERVER), anyBoolean())); eq(TetheringConfiguration.TETHER_ENABLE_LEGACY_DHCP_SERVER)));
final TetheringConfiguration enableByDevConfig = final TetheringConfiguration enableByDevConfig =
new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID); new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID);
@@ -351,9 +350,9 @@ public class TetheringConfigurationTest {
public void testNewDhcpServerEnabled() { public void testNewDhcpServerEnabled() {
when(mResources.getBoolean(R.bool.config_tether_enable_legacy_dhcp_server)).thenReturn( when(mResources.getBoolean(R.bool.config_tether_enable_legacy_dhcp_server)).thenReturn(
false); false);
doReturn(false).when( doReturn("false").when(
() -> DeviceConfig.getBoolean(eq(NAMESPACE_CONNECTIVITY), () -> DeviceConfig.getProperty(eq(NAMESPACE_CONNECTIVITY),
eq(TetheringConfiguration.TETHER_ENABLE_LEGACY_DHCP_SERVER), anyBoolean())); eq(TetheringConfiguration.TETHER_ENABLE_LEGACY_DHCP_SERVER)));
final TetheringConfiguration cfg = final TetheringConfiguration cfg =
new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID); new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID);

View File

@@ -312,8 +312,8 @@ public class TetheringTest {
} }
@Override @Override
protected boolean getDeviceConfigBoolean(final String name) { protected String getDeviceConfigProperty(final String name) {
return false; return null;
} }
@Override @Override