blob: facb932e6c8a0e547592fe17315e6c847099ea5f [file] [log] [blame]
Junyu Lai626045a2023-08-28 18:49:44 +08001/*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.net
18
19import android.net.BpfNetMapsConstants.UID_RULES_CONFIGURATION_KEY
20import android.net.BpfNetMapsUtils.getMatchByFirewallChain
21import android.os.Build
22import com.android.net.module.util.IBpfMap
23import com.android.net.module.util.Struct.S32
24import com.android.net.module.util.Struct.U32
25import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo
26import com.android.testutils.DevSdkIgnoreRunner
27import com.android.testutils.TestBpfMap
28import kotlin.test.assertFalse
29import kotlin.test.assertTrue
30import org.junit.Test
31import org.junit.runner.RunWith
32
33// pre-T devices does not support Bpf.
34@RunWith(DevSdkIgnoreRunner::class)
35@IgnoreUpTo(Build.VERSION_CODES.S_V2)
36class BpfNetMapsReaderTest {
37 private val testConfigurationMap: IBpfMap<S32, U32> = TestBpfMap()
38 private val testUidOwnerMap: IBpfMap<S32, UidOwnerValue> = TestBpfMap()
39 private val bpfNetMapsReader = BpfNetMapsReader(
40 TestDependencies(testConfigurationMap, testUidOwnerMap))
41
42 class TestDependencies(
43 private val configMap: IBpfMap<S32, U32>,
44 private val uidOwnerMap: IBpfMap<S32, UidOwnerValue>
45 ) : BpfNetMapsReader.Dependencies() {
46 override fun getConfigurationMap() = configMap
47 override fun getUidOwnerMap() = uidOwnerMap
48 }
49
50 private fun doTestIsChainEnabled(chain: Int) {
51 testConfigurationMap.updateEntry(
52 UID_RULES_CONFIGURATION_KEY,
53 U32(getMatchByFirewallChain(chain))
54 )
55 assertTrue(bpfNetMapsReader.isChainEnabled(chain))
56 testConfigurationMap.updateEntry(UID_RULES_CONFIGURATION_KEY, U32(0))
57 assertFalse(bpfNetMapsReader.isChainEnabled(chain))
58 }
59
60 @Test
61 @Throws(Exception::class)
62 fun testIsChainEnabled() {
63 doTestIsChainEnabled(ConnectivityManager.FIREWALL_CHAIN_DOZABLE)
64 doTestIsChainEnabled(ConnectivityManager.FIREWALL_CHAIN_STANDBY)
65 doTestIsChainEnabled(ConnectivityManager.FIREWALL_CHAIN_POWERSAVE)
66 doTestIsChainEnabled(ConnectivityManager.FIREWALL_CHAIN_RESTRICTED)
67 doTestIsChainEnabled(ConnectivityManager.FIREWALL_CHAIN_LOW_POWER_STANDBY)
68 }
69}