diff options
| author | Motomu Utsumi <motomuman@google.com> | 2022-06-01 13:57:27 +0000 |
|---|---|---|
| committer | Cherrypicker Worker <android-build-cherrypicker-worker@google.com> | 2022-06-03 11:06:46 +0000 |
| commit | 9cd4726890a84bf3f34170a4b08a280060fdbd2a (patch) | |
| tree | 578f5f8bcf0b78c1886e13dd9a9b9074c2350a91 /tests/unit/java/com/android/server/ConnectivityServiceTest.java | |
| parent | 87a9bc7edba09fe74241cdf20232d05fd27a6b31 (diff) | |
Add deny firewall chain for OEM
Bug: 207773349
Bug: 208371987
Test: atest
CtsNetTestCases:android.net.cts.ConnectivityManagerTest#testFirewallBlocking
--iterations 50 && atest ConnectivityServiceTest --iterations 10
Change-Id: I60d5540821abcced03356f366775f16ee369d7f9
(cherry picked from commit d980149817948d11de0631caee8aee3172e4e159)
Merged-In: I60d5540821abcced03356f366775f16ee369d7f9
Diffstat (limited to 'tests/unit/java/com/android/server/ConnectivityServiceTest.java')
| -rw-r--r-- | tests/unit/java/com/android/server/ConnectivityServiceTest.java | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/tests/unit/java/com/android/server/ConnectivityServiceTest.java b/tests/unit/java/com/android/server/ConnectivityServiceTest.java index 44550e6513..74731c35c5 100644 --- a/tests/unit/java/com/android/server/ConnectivityServiceTest.java +++ b/tests/unit/java/com/android/server/ConnectivityServiceTest.java @@ -52,8 +52,16 @@ import static android.net.ConnectivityManager.BLOCKED_REASON_NONE; import static android.net.ConnectivityManager.CONNECTIVITY_ACTION; import static android.net.ConnectivityManager.EXTRA_NETWORK_INFO; import static android.net.ConnectivityManager.EXTRA_NETWORK_TYPE; +import static android.net.ConnectivityManager.FIREWALL_CHAIN_DOZABLE; import static android.net.ConnectivityManager.FIREWALL_CHAIN_LOCKDOWN_VPN; +import static android.net.ConnectivityManager.FIREWALL_CHAIN_LOW_POWER_STANDBY; +import static android.net.ConnectivityManager.FIREWALL_CHAIN_OEM_DENY_1; +import static android.net.ConnectivityManager.FIREWALL_CHAIN_OEM_DENY_2; +import static android.net.ConnectivityManager.FIREWALL_CHAIN_POWERSAVE; +import static android.net.ConnectivityManager.FIREWALL_CHAIN_RESTRICTED; +import static android.net.ConnectivityManager.FIREWALL_CHAIN_STANDBY; import static android.net.ConnectivityManager.FIREWALL_RULE_ALLOW; +import static android.net.ConnectivityManager.FIREWALL_RULE_DEFAULT; import static android.net.ConnectivityManager.FIREWALL_RULE_DENY; import static android.net.ConnectivityManager.PROFILE_NETWORK_PREFERENCE_DEFAULT; import static android.net.ConnectivityManager.PROFILE_NETWORK_PREFERENCE_ENTERPRISE; @@ -9547,6 +9555,95 @@ public class ConnectivityServiceTest { verify(mBpfNetMaps, never()).removeUidInterfaceRules(any()); } + private void doTestSetUidFirewallRule(final int chain, final int defaultRule) { + final int uid = 1001; + mCm.setUidFirewallRule(chain, uid, FIREWALL_RULE_ALLOW); + verify(mBpfNetMaps).setUidRule(chain, uid, FIREWALL_RULE_ALLOW); + reset(mBpfNetMaps); + + mCm.setUidFirewallRule(chain, uid, FIREWALL_RULE_DENY); + verify(mBpfNetMaps).setUidRule(chain, uid, FIREWALL_RULE_DENY); + reset(mBpfNetMaps); + + mCm.setUidFirewallRule(chain, uid, FIREWALL_RULE_DEFAULT); + verify(mBpfNetMaps).setUidRule(chain, uid, defaultRule); + reset(mBpfNetMaps); + } + + @Test @IgnoreUpTo(SC_V2) + public void testSetUidFirewallRule() throws Exception { + doTestSetUidFirewallRule(FIREWALL_CHAIN_DOZABLE, FIREWALL_RULE_DENY); + doTestSetUidFirewallRule(FIREWALL_CHAIN_STANDBY, FIREWALL_RULE_ALLOW); + doTestSetUidFirewallRule(FIREWALL_CHAIN_POWERSAVE, FIREWALL_RULE_DENY); + doTestSetUidFirewallRule(FIREWALL_CHAIN_RESTRICTED, FIREWALL_RULE_DENY); + doTestSetUidFirewallRule(FIREWALL_CHAIN_LOW_POWER_STANDBY, FIREWALL_RULE_DENY); + doTestSetUidFirewallRule(FIREWALL_CHAIN_OEM_DENY_1, FIREWALL_RULE_ALLOW); + doTestSetUidFirewallRule(FIREWALL_CHAIN_OEM_DENY_2, FIREWALL_RULE_ALLOW); + } + + @Test @IgnoreUpTo(SC_V2) + public void testSetFirewallChainEnabled() throws Exception { + final List<Integer> firewallChains = Arrays.asList( + FIREWALL_CHAIN_DOZABLE, + FIREWALL_CHAIN_STANDBY, + FIREWALL_CHAIN_POWERSAVE, + FIREWALL_CHAIN_RESTRICTED, + FIREWALL_CHAIN_LOW_POWER_STANDBY, + FIREWALL_CHAIN_OEM_DENY_1, + FIREWALL_CHAIN_OEM_DENY_2); + for (final int chain: firewallChains) { + mCm.setFirewallChainEnabled(chain, true /* enabled */); + verify(mBpfNetMaps).setChildChain(chain, true /* enable */); + reset(mBpfNetMaps); + + mCm.setFirewallChainEnabled(chain, false /* enabled */); + verify(mBpfNetMaps).setChildChain(chain, false /* enable */); + reset(mBpfNetMaps); + } + } + + private void doTestReplaceFirewallChain(final int chain, final String chainName, + final boolean allowList) { + final int[] uids = new int[] {1001, 1002}; + mCm.replaceFirewallChain(chain, uids); + verify(mBpfNetMaps).replaceUidChain(chainName, allowList, uids); + reset(mBpfNetMaps); + } + + @Test @IgnoreUpTo(SC_V2) + public void testReplaceFirewallChain() { + doTestReplaceFirewallChain(FIREWALL_CHAIN_DOZABLE, "fw_dozable", true); + doTestReplaceFirewallChain(FIREWALL_CHAIN_STANDBY, "fw_standby", false); + doTestReplaceFirewallChain(FIREWALL_CHAIN_POWERSAVE, "fw_powersave", true); + doTestReplaceFirewallChain(FIREWALL_CHAIN_RESTRICTED, "fw_restricted", true); + doTestReplaceFirewallChain(FIREWALL_CHAIN_LOW_POWER_STANDBY, "fw_low_power_standby", true); + doTestReplaceFirewallChain(FIREWALL_CHAIN_OEM_DENY_1, "fw_oem_deny_1", false); + doTestReplaceFirewallChain(FIREWALL_CHAIN_OEM_DENY_2, "fw_oem_deny_2", false); + } + + @Test @IgnoreUpTo(SC_V2) + public void testInvalidFirewallChain() throws Exception { + final int uid = 1001; + final Class<IllegalArgumentException> expected = IllegalArgumentException.class; + assertThrows(expected, + () -> mCm.setUidFirewallRule(-1 /* chain */, uid, FIREWALL_RULE_ALLOW)); + assertThrows(expected, + () -> mCm.setUidFirewallRule(100 /* chain */, uid, FIREWALL_RULE_ALLOW)); + assertThrows(expected, () -> mCm.replaceFirewallChain(-1 /* chain */, new int[]{uid})); + assertThrows(expected, () -> mCm.replaceFirewallChain(100 /* chain */, new int[]{uid})); + } + + @Test @IgnoreUpTo(SC_V2) + public void testInvalidFirewallRule() throws Exception { + final Class<IllegalArgumentException> expected = IllegalArgumentException.class; + assertThrows(expected, + () -> mCm.setUidFirewallRule(FIREWALL_CHAIN_DOZABLE, + 1001 /* uid */, -1 /* rule */)); + assertThrows(expected, + () -> mCm.setUidFirewallRule(FIREWALL_CHAIN_DOZABLE, + 1001 /* uid */, 100 /* rule */)); + } + /** * Test mutable and requestable network capabilities such as * {@link NetworkCapabilities#NET_CAPABILITY_TRUSTED} and |
