• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 
17 package com.android.server;
18 
19 import static android.net.INetd.PERMISSION_INTERNET;
20 
21 import static org.junit.Assume.assumeFalse;
22 import static org.mockito.Mockito.verify;
23 
24 import android.net.INetd;
25 import android.os.Build;
26 
27 import androidx.test.filters.SmallTest;
28 
29 import com.android.modules.utils.build.SdkLevel;
30 import com.android.testutils.DevSdkIgnoreRule;
31 import com.android.testutils.DevSdkIgnoreRunner;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 
39 @RunWith(DevSdkIgnoreRunner.class)
40 @SmallTest
41 @DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.R)
42 public final class BpfNetMapsTest {
43     private static final String TAG = "BpfNetMapsTest";
44     private static final int TEST_UID = 10086;
45     private static final int[] TEST_UIDS = {10002, 10003};
46     private static final String IFNAME = "wlan0";
47     private static final String CHAINNAME = "fw_dozable";
48     private BpfNetMaps mBpfNetMaps;
49 
50     @Mock INetd mNetd;
51 
52     @Before
setUp()53     public void setUp() {
54         MockitoAnnotations.initMocks(this);
55         mBpfNetMaps = new BpfNetMaps(mNetd);
56     }
57 
58     @Test
testBpfNetMapsBeforeT()59     public void testBpfNetMapsBeforeT() throws Exception {
60         assumeFalse(SdkLevel.isAtLeastT());
61         mBpfNetMaps.addUidInterfaceRules(IFNAME, TEST_UIDS);
62         verify(mNetd).firewallAddUidInterfaceRules(IFNAME, TEST_UIDS);
63         mBpfNetMaps.removeUidInterfaceRules(TEST_UIDS);
64         verify(mNetd).firewallRemoveUidInterfaceRules(TEST_UIDS);
65         mBpfNetMaps.setNetPermForUids(PERMISSION_INTERNET, TEST_UIDS);
66         verify(mNetd).trafficSetNetPermForUids(PERMISSION_INTERNET, TEST_UIDS);
67     }
68 }
69