192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao/*
292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao * Copyright (C) 2016 The Android Open Source Project
392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao *
492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao * Licensed under the Apache License, Version 2.0 (the "License");
592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao * you may not use this file except in compliance with the License.
692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao * You may obtain a copy of the License at
792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao *
892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao *      http://www.apache.org/licenses/LICENSE-2.0
992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao *
1092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao * Unless required by applicable law or agreed to in writing, software
1192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao * distributed under the License is distributed on an "AS IS" BASIS,
1292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao * See the License for the specific language governing permissions and
1492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao * limitations under the License.
1592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao */
1692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao
1792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Raopackage com.android.server.wifi.util;
1892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao
1992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Raoimport static org.junit.Assert.assertEquals;
20835e781643b79c30fabbab5595770cf01b5861fbRebecca Silbersteinimport static org.junit.Assert.assertFalse;
21835e781643b79c30fabbab5595770cf01b5861fbRebecca Silbersteinimport static org.junit.Assert.assertTrue;
221982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslinimport static org.junit.Assert.fail;
2325f5cad21b4cd0ad8aa302623463c5c861a885b0Jeremy Joslinimport static org.mockito.Matchers.anyInt;
2425f5cad21b4cd0ad8aa302623463c5c861a885b0Jeremy Joslinimport static org.mockito.Matchers.anyString;
2592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Raoimport static org.mockito.Mockito.doAnswer;
2692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Raoimport static org.mockito.Mockito.doThrow;
2792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Raoimport static org.mockito.Mockito.when;
2892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao
2992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Raoimport android.Manifest;
3092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Raoimport android.app.AppOpsManager;
311982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslinimport android.content.ComponentName;
3292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Raoimport android.content.ContentResolver;
3392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Raoimport android.content.Context;
3492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Raoimport android.content.pm.ApplicationInfo;
3592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Raoimport android.content.pm.PackageManager;
3692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Raoimport android.content.pm.UserInfo;
3725f5cad21b4cd0ad8aa302623463c5c861a885b0Jeremy Joslinimport android.net.NetworkScoreManager;
381982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslinimport android.net.NetworkScorerAppData;
391982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslinimport android.net.wifi.WifiConfiguration;
4092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Raoimport android.os.Build;
41835e781643b79c30fabbab5595770cf01b5861fbRebecca Silbersteinimport android.os.RemoteException;
4292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Raoimport android.os.UserHandle;
4392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Raoimport android.os.UserManager;
4492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Raoimport android.provider.Settings;
4592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao
4692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Raoimport com.android.server.wifi.BinderUtil;
47835e781643b79c30fabbab5595770cf01b5861fbRebecca Silbersteinimport com.android.server.wifi.FakeWifiLog;
481982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslinimport com.android.server.wifi.FrameworkFacade;
49f2d2a31c8c98f17545f72033abe0d281c829c055Rebecca Silbersteinimport com.android.server.wifi.WifiInjector;
5092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Raoimport com.android.server.wifi.WifiSettingsStore;
5192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao
5292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Raoimport org.junit.Before;
5392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Raoimport org.junit.Test;
5492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Raoimport org.junit.runner.RunWith;
5592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Raoimport org.junit.runners.JUnit4;
5692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Raoimport org.mockito.Mock;
5792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Raoimport org.mockito.MockitoAnnotations;
58835e781643b79c30fabbab5595770cf01b5861fbRebecca Silbersteinimport org.mockito.Spy;
5992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Raoimport org.mockito.invocation.InvocationOnMock;
6092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Raoimport org.mockito.stubbing.Answer;
6192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao
6292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Raoimport java.util.Arrays;
6392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Raoimport java.util.HashMap;
6492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao
6592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao/** Unit tests for {@link WifiPermissionsUtil}. */
6692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao@RunWith(JUnit4.class)
6792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Raopublic class WifiPermissionsUtilTest {
6892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    public static final String TAG = "WifiPermissionsUtilTest";
6992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao
7092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    // Mock objects for testing
7192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    @Mock private WifiPermissionsWrapper mMockPermissionsWrapper;
7292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    @Mock private Context mMockContext;
7392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    @Mock private PackageManager mMockPkgMgr;
7492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    @Mock private ApplicationInfo mMockApplInfo;
7592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    @Mock private AppOpsManager mMockAppOps;
7692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    @Mock private UserInfo mMockUserInfo;
7792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    @Mock private UserManager mMockUserManager;
7892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    @Mock private WifiSettingsStore mMockWifiSettingsStore;
7992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    @Mock private ContentResolver mMockContentResolver;
801982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    @Mock private NetworkScoreManager mMockNetworkScoreManager;
811982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    @Mock private WifiInjector mMockWifiInjector;
821982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    @Mock private FrameworkFacade mMockFrameworkFacade;
831982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    @Mock private WifiConfiguration mMockWifiConfig;
84835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein    @Spy private FakeWifiLog mWifiLog;
8592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao
8692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    private static final String TEST_PACKAGE_NAME = "com.google.somePackage";
8792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    private static final String INVALID_PACKAGE  = "BAD_PACKAGE";
8892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    private static final int MANAGED_PROFILE_UID = 1100000;
8992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    private static final int OTHER_USER_UID = 1200000;
9092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao
9192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    private final int mCallingUser = UserHandle.USER_CURRENT_OR_SELF;
9292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    private final String mMacAddressPermission = "android.permission.PEERS_MAC_ADDRESS";
9392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    private final String mInteractAcrossUsersFullPermission =
9492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao            "android.permission.INTERACT_ACROSS_USERS_FULL";
9592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    private final String mManifestStringCoarse =
9692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao            Manifest.permission.ACCESS_COARSE_LOCATION;
9792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao
9892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    // Test variables
9992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    private int mWifiScanAllowApps;
10092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    private int mUid;
10192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    private int mCoarseLocationPermission;
10292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    private int mAllowCoarseLocationApps;
10392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    private String mPkgNameOfTopActivity;
10492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    private int mCurrentUser;
10592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    private int mLocationModeSetting;
10692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    private boolean mThrowSecurityException;
10792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    private int mTargetVersion;
10892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    private boolean mActiveNwScorer;
10992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    private Answer<Integer> mReturnPermission;
11092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    private HashMap<String, Integer> mPermissionsList = new HashMap<String, Integer>();
1111982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    private String mUseOpenWifiPackage;
1121982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    private NetworkScorerAppData mNetworkScorerAppData;
1131982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    private boolean mGetActiveScorerThrowsSecurityException;
1141982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    private boolean mConfigIsOpen;
11592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao
11692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    /**
11792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    * Set up Mockito tests
11892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    */
11992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    @Before
12092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    public void setUp() {
12192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        MockitoAnnotations.initMocks(this);
12292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        initTestVars();
12392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    }
12492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao
12592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    private void setupTestCase() throws Exception {
12692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        setupMocks();
12792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        setupMockInterface();
12892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    }
12992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao
13092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    /**
131835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein     * Verify we return true when the UID does have the override config permission
132835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein     */
133835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein    @Test
134835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein    public void testCheckConfigOverridePermissionApproved() throws Exception {
135835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein        mUid = MANAGED_PROFILE_UID;  // do not really care about this value
136835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein        setupTestCase();
137835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein        WifiPermissionsUtil codeUnderTest = new WifiPermissionsUtil(mMockPermissionsWrapper,
1381982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockContext, mMockWifiSettingsStore, mMockUserManager, mMockNetworkScoreManager,
1391982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockWifiInjector);
140835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein        when(mMockPermissionsWrapper.getOverrideWifiConfigPermission(anyInt()))
141835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein                .thenReturn(PackageManager.PERMISSION_GRANTED);
142835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein        assertTrue(codeUnderTest.checkConfigOverridePermission(mUid));
143835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein    }
144835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein
145835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein    /**
146835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein     * Verify we return false when the UID does not have the override config permission.
147835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein     */
148835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein    @Test
149835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein    public void testCheckConfigOverridePermissionDenied() throws Exception {
150835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein        mUid = OTHER_USER_UID;  // do not really care about this value
151835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein        setupTestCase();
152835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein        WifiPermissionsUtil codeUnderTest = new WifiPermissionsUtil(mMockPermissionsWrapper,
1531982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockContext, mMockWifiSettingsStore, mMockUserManager, mMockNetworkScoreManager,
1541982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockWifiInjector);
155835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein        when(mMockPermissionsWrapper.getOverrideWifiConfigPermission(anyInt()))
156835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein                .thenReturn(PackageManager.PERMISSION_DENIED);
157835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein        assertFalse(codeUnderTest.checkConfigOverridePermission(mUid));
158835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein    }
159835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein
160835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein    /**
161835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein     * Verify we return false when the override config permission check throws a RemoteException.
162835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein     */
163835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein    @Test
164835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein    public void testCheckConfigOverridePermissionWithException() throws Exception {
165835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein        mUid = OTHER_USER_UID;  // do not really care about this value
166835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein        setupTestCase();
167835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein        WifiPermissionsUtil codeUnderTest = new WifiPermissionsUtil(mMockPermissionsWrapper,
1681982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockContext, mMockWifiSettingsStore, mMockUserManager, mMockNetworkScoreManager,
1691982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockWifiInjector);
170835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein        doThrow(new RemoteException("Failed to check permissions for " + mUid))
171835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein                .when(mMockPermissionsWrapper).getOverrideWifiConfigPermission(mUid);
172835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein        assertFalse(codeUnderTest.checkConfigOverridePermission(mUid));
173835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein    }
174835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein
175835e781643b79c30fabbab5595770cf01b5861fbRebecca Silberstein    /**
17692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     * Test case setting: Package is valid
17792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     *                    Caller can read peers mac address
17892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     *                    This App has permission to request WIFI_SCAN
17992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     *                    User is current
18092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     * Validate result is true
18192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     * - User has all the permissions
18292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     */
18392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    @Test
18492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    public void testCanReadPeersMacAddressCurrentUserAndAllPermissions() throws Exception {
18592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        boolean output = false;
18692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mThrowSecurityException = false;
18792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mUid = MANAGED_PROFILE_UID;
18892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mPermissionsList.put(mMacAddressPermission, mUid);
18992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mWifiScanAllowApps = AppOpsManager.MODE_ALLOWED;
19092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mCurrentUser = UserHandle.USER_CURRENT_OR_SELF;
19192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        setupTestCase();
19292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        WifiPermissionsUtil codeUnderTest = new WifiPermissionsUtil(mMockPermissionsWrapper,
1931982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockContext, mMockWifiSettingsStore, mMockUserManager, mMockNetworkScoreManager,
1941982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockWifiInjector);
19592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        try {
19692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao            output = codeUnderTest.canAccessScanResults(TEST_PACKAGE_NAME, mUid, mTargetVersion);
19792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        } catch (SecurityException e) {
19892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao            throw e;
19992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        }
20092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        assertEquals(output, true);
20192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    }
20292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao
20392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    /**
20492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     * Test case setting: Package is valid
20592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     *                    Caller can read peers mac address
20692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     *                    This App has permission to request WIFI_SCAN
20792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     *                    User profile is current
20892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     * Validate result is true
20992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     * - User has all the permissions
21092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     */
21192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    @Test
21292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    public void testCanReadPeersMacAddressCurrentProfileAndAllPermissions() throws Exception {
21392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        boolean output = false;
21492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mThrowSecurityException = false;
21592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mUid = MANAGED_PROFILE_UID;
21692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mPermissionsList.put(mMacAddressPermission, mUid);
21792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mWifiScanAllowApps = AppOpsManager.MODE_ALLOWED;
21892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mMockUserInfo.id = mCallingUser;
21992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        setupTestCase();
22092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        WifiPermissionsUtil codeUnderTest = new WifiPermissionsUtil(mMockPermissionsWrapper,
2211982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockContext, mMockWifiSettingsStore, mMockUserManager, mMockNetworkScoreManager,
2221982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockWifiInjector);
22392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        try {
22492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao            output = codeUnderTest.canAccessScanResults(TEST_PACKAGE_NAME, mUid, mTargetVersion);
22592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        } catch (SecurityException e) {
22692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao            throw e;
22792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        }
22892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        assertEquals(output, true);
22992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    }
23092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao
23192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    /**
23292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     * Test case setting: Package is valid
23392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     *                    Caller can read peers mac address
23492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     * Validate result is false
23592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     * - This App doesn't have permission to request Wifi Scan
23692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     */
23792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    @Test
23892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    public void testCannotAccessScanResult_AppNotAllowed() throws Exception {
23992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        boolean output = true;
24092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mThrowSecurityException = false;
24192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mPermissionsList.put(mMacAddressPermission, mUid);
24292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        setupTestCase();
24392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        WifiPermissionsUtil codeUnderTest = new WifiPermissionsUtil(mMockPermissionsWrapper,
2441982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockContext, mMockWifiSettingsStore, mMockUserManager, mMockNetworkScoreManager,
2451982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockWifiInjector);
24692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        try {
24792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao            output = codeUnderTest.canAccessScanResults(TEST_PACKAGE_NAME, mUid, mTargetVersion);
24892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        } catch (SecurityException e) {
24992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao            throw e;
25092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        }
25192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        assertEquals(output, false);
25292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    }
25392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao
25492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    /**
25592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     * Test case setting: Package is valid
25692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     *                    Caller can read peers mac address
25792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     *                    This App has permission to request WIFI_SCAN
25863a4c8e9abc4f75f7885331360e67e8ac8c4f7ddSohani Rao     *                    User or profile is not current but the uid has
25963a4c8e9abc4f75f7885331360e67e8ac8c4f7ddSohani Rao     *                    permission to INTERACT_ACROSS_USERS_FULL
26063a4c8e9abc4f75f7885331360e67e8ac8c4f7ddSohani Rao     * Validate result is true
26163a4c8e9abc4f75f7885331360e67e8ac8c4f7ddSohani Rao     * - User has all the permissions
26263a4c8e9abc4f75f7885331360e67e8ac8c4f7ddSohani Rao     */
26363a4c8e9abc4f75f7885331360e67e8ac8c4f7ddSohani Rao    @Test
26463a4c8e9abc4f75f7885331360e67e8ac8c4f7ddSohani Rao    public void testCanAccessScanResults_UserOrProfileNotCurrent() throws Exception {
26563a4c8e9abc4f75f7885331360e67e8ac8c4f7ddSohani Rao        boolean output = false;
26663a4c8e9abc4f75f7885331360e67e8ac8c4f7ddSohani Rao        mThrowSecurityException = false;
26763a4c8e9abc4f75f7885331360e67e8ac8c4f7ddSohani Rao        mUid = MANAGED_PROFILE_UID;
26863a4c8e9abc4f75f7885331360e67e8ac8c4f7ddSohani Rao        mPermissionsList.put(mMacAddressPermission, mUid);
26963a4c8e9abc4f75f7885331360e67e8ac8c4f7ddSohani Rao        mWifiScanAllowApps = AppOpsManager.MODE_ALLOWED;
27063a4c8e9abc4f75f7885331360e67e8ac8c4f7ddSohani Rao        mPermissionsList.put(mInteractAcrossUsersFullPermission, mUid);
27163a4c8e9abc4f75f7885331360e67e8ac8c4f7ddSohani Rao        setupTestCase();
27263a4c8e9abc4f75f7885331360e67e8ac8c4f7ddSohani Rao        WifiPermissionsUtil codeUnderTest = new WifiPermissionsUtil(mMockPermissionsWrapper,
2731982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockContext, mMockWifiSettingsStore, mMockUserManager, mMockNetworkScoreManager,
2741982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockWifiInjector);
27563a4c8e9abc4f75f7885331360e67e8ac8c4f7ddSohani Rao        try {
27663a4c8e9abc4f75f7885331360e67e8ac8c4f7ddSohani Rao            output = codeUnderTest.canAccessScanResults(TEST_PACKAGE_NAME, mUid, mTargetVersion);
27763a4c8e9abc4f75f7885331360e67e8ac8c4f7ddSohani Rao        } catch (SecurityException e) {
27863a4c8e9abc4f75f7885331360e67e8ac8c4f7ddSohani Rao            throw e;
27963a4c8e9abc4f75f7885331360e67e8ac8c4f7ddSohani Rao        }
28063a4c8e9abc4f75f7885331360e67e8ac8c4f7ddSohani Rao        assertEquals(output, true);
28163a4c8e9abc4f75f7885331360e67e8ac8c4f7ddSohani Rao    }
28263a4c8e9abc4f75f7885331360e67e8ac8c4f7ddSohani Rao
28363a4c8e9abc4f75f7885331360e67e8ac8c4f7ddSohani Rao    /**
28463a4c8e9abc4f75f7885331360e67e8ac8c4f7ddSohani Rao     * Test case setting: Package is valid
28563a4c8e9abc4f75f7885331360e67e8ac8c4f7ddSohani Rao     *                    Caller can read peers mac address
28663a4c8e9abc4f75f7885331360e67e8ac8c4f7ddSohani Rao     *                    This App has permission to request WIFI_SCAN
28763a4c8e9abc4f75f7885331360e67e8ac8c4f7ddSohani Rao     *                    User or profile is not Current
28892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     * Validate result is false
28963a4c8e9abc4f75f7885331360e67e8ac8c4f7ddSohani Rao     * - Calling uid doesn't have INTERACT_ACROSS_USERS_FULL permission
29092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     */
29192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    @Test
29263a4c8e9abc4f75f7885331360e67e8ac8c4f7ddSohani Rao    public void testCannotAccessScanResults_NoInteractAcrossUsersFullPermission() throws Exception {
29392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        boolean output = true;
29492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mThrowSecurityException = false;
29592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mUid = MANAGED_PROFILE_UID;
29692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mPermissionsList.put(mMacAddressPermission, mUid);
29792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mWifiScanAllowApps = AppOpsManager.MODE_ALLOWED;
29892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        setupTestCase();
29992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        WifiPermissionsUtil codeUnderTest = new WifiPermissionsUtil(mMockPermissionsWrapper,
3001982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockContext, mMockWifiSettingsStore, mMockUserManager, mMockNetworkScoreManager,
3011982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockWifiInjector);
30292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        try {
30392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao            output = codeUnderTest.canAccessScanResults(TEST_PACKAGE_NAME, mUid, mTargetVersion);
30492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        } catch (SecurityException e) {
30592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao            throw e;
30692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        }
30792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        assertEquals(output, false);
30892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    }
30992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao
31092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    /**
31192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     * Test case setting: Package is valid
31292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     *                    Caller is active network scorer
31392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     *                    This App has permission to request WIFI_SCAN
31492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     *                    User is current
31563a4c8e9abc4f75f7885331360e67e8ac8c4f7ddSohani Rao     * Validate result is true
31692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     */
31792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    @Test
31863a4c8e9abc4f75f7885331360e67e8ac8c4f7ddSohani Rao    public void testCanAccessScanResults_CallerIsActiveNwScorer() throws Exception {
31963a4c8e9abc4f75f7885331360e67e8ac8c4f7ddSohani Rao        boolean output = false;
32092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mThrowSecurityException = false;
32192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mActiveNwScorer = true;
32292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mWifiScanAllowApps = AppOpsManager.MODE_ALLOWED;
32392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mCurrentUser = UserHandle.USER_CURRENT_OR_SELF;
32492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        setupTestCase();
32592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        WifiPermissionsUtil codeUnderTest = new WifiPermissionsUtil(mMockPermissionsWrapper,
3261982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockContext, mMockWifiSettingsStore, mMockUserManager, mMockNetworkScoreManager,
3271982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockWifiInjector);
32892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        try {
32992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao            output = codeUnderTest.canAccessScanResults(TEST_PACKAGE_NAME, mUid, mTargetVersion);
33092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        } catch (SecurityException e) {
33192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao            throw e;
33292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        }
33363a4c8e9abc4f75f7885331360e67e8ac8c4f7ddSohani Rao        assertEquals(output, true);
33492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    }
33592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao
33692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    /**
3371982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     * Test case setting: Package is valid because it matches the USE_OPEN_WIFI_PACKAGE.
3381982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     *                    User is current
3391982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     *                    The current config is for an open network.
3401982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     * Validate result is true
3411982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     */
3421982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    @Test
3431982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    public void testCanAccessFullConnectionInfo_PackageIsUseOpenWifiPackage() throws Exception {
3441982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        final boolean output;
3451982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mThrowSecurityException = false;
3461982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mCurrentUser = UserHandle.USER_CURRENT_OR_SELF;
3471982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mUseOpenWifiPackage = TEST_PACKAGE_NAME;
3481982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        ComponentName useOpenWifiComponent = new ComponentName(TEST_PACKAGE_NAME, "TestClass");
3491982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mNetworkScorerAppData = new NetworkScorerAppData(0 /*packageUid*/,
3501982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                null /*recommendationServiceComp*/, null /*recommendationServiceLabel*/,
3511982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                useOpenWifiComponent, null /*networkAvailableNotificationChannelId*/);
3521982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        setupTestCase();
3531982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        WifiPermissionsUtil codeUnderTest = new WifiPermissionsUtil(mMockPermissionsWrapper,
3541982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockContext, mMockWifiSettingsStore, mMockUserManager, mMockNetworkScoreManager,
3551982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockWifiInjector);
3561982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin
3571982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        output = codeUnderTest.canAccessFullConnectionInfo(mMockWifiConfig, TEST_PACKAGE_NAME,
3581982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mUid, mTargetVersion);
3591982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin
3601982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        assertEquals(true, output);
3611982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    }
3621982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin
3631982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    /**
3641982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     * Test case setting: Package is valid because the caller has access to scan results.
3651982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     *                    Location mode is ON
3661982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     *                    User is current
3671982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     *                    The current config is not for an open network.
3681982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     * Validate result is true
3691982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     */
3701982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    @Test
3711982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    public void testCanAccessFullConnectionInfo_HasAccessToScanResults() throws Exception {
3721982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        final boolean output;
3731982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mThrowSecurityException = false;
3741982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mMockApplInfo.targetSdkVersion = Build.VERSION_CODES.GINGERBREAD;
3751982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mLocationModeSetting = Settings.Secure.LOCATION_MODE_HIGH_ACCURACY;
3761982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mCoarseLocationPermission = PackageManager.PERMISSION_GRANTED;
3771982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mAllowCoarseLocationApps = AppOpsManager.MODE_ALLOWED;
3781982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mWifiScanAllowApps = AppOpsManager.MODE_ALLOWED;
3791982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mCurrentUser = UserHandle.USER_CURRENT_OR_SELF;
3801982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mConfigIsOpen = false;
3811982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin
3821982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        setupTestCase();
3831982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        WifiPermissionsUtil codeUnderTest = new WifiPermissionsUtil(mMockPermissionsWrapper,
3841982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockContext, mMockWifiSettingsStore, mMockUserManager, mMockNetworkScoreManager,
3851982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockWifiInjector);
3861982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin
3871982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        output = codeUnderTest.canAccessFullConnectionInfo(mMockWifiConfig, TEST_PACKAGE_NAME,
3881982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mUid, mTargetVersion);
3891982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin
3901982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        assertEquals(true, output);
3911982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    }
3921982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin
3931982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    /**
3941982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     * Test case setting: Package is valid because it matches the USE_OPEN_WIFI_PACKAGE.
3951982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     *                    User or profile is not current but the uid has
3961982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     *                    permission to INTERACT_ACROSS_USERS_FULL
3971982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     *                    The current config is for an open network.
3981982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     * Validate result is true
3991982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     */
4001982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    @Test
4011982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    public void testCanAccessFullConnectionInfo_UserNotCurrentButHasInteractAcrossUsers()
4021982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin            throws Exception {
4031982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        final boolean output;
4041982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mThrowSecurityException = false;
4051982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mUid = MANAGED_PROFILE_UID;
4061982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mPermissionsList.put(mInteractAcrossUsersFullPermission, mUid);
4071982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mUseOpenWifiPackage = TEST_PACKAGE_NAME;
4081982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        ComponentName useOpenWifiComponent = new ComponentName(TEST_PACKAGE_NAME, "TestClass");
4091982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mNetworkScorerAppData = new NetworkScorerAppData(0 /*packageUid*/,
4101982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                null /*recommendationServiceComp*/, null /*recommendationServiceLabel*/,
4111982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                useOpenWifiComponent, null /*networkAvailableNotificationChannelId*/);
4121982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        setupTestCase();
4131982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        WifiPermissionsUtil codeUnderTest = new WifiPermissionsUtil(mMockPermissionsWrapper,
4141982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockContext, mMockWifiSettingsStore, mMockUserManager, mMockNetworkScoreManager,
4151982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockWifiInjector);
4161982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin
4171982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        output = codeUnderTest.canAccessFullConnectionInfo(mMockWifiConfig, TEST_PACKAGE_NAME,
4181982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mUid, mTargetVersion);
4191982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin
4201982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        assertEquals(true, output);
4211982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    }
4221982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin
4231982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    /**
4241982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     * Test case setting: Package is valid because it matches the USE_OPEN_WIFI_PACKAGE.
4251982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     *                    User or profile is NOT current
4261982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     *                    INTERACT_ACROSS_USERS_FULL NOT granted
4271982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     *                    The current config is for an open network.
4281982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     * Validate result is false
4291982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     */
4301982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    @Test
4311982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    public void testCanAccessFullConnectionInfo_UserNotCurrentNoInteractAcrossUsers()
4321982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin            throws Exception {
4331982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        final boolean output;
4341982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mThrowSecurityException = false;
4351982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mUid = MANAGED_PROFILE_UID;
4361982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mUseOpenWifiPackage = TEST_PACKAGE_NAME;
4371982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        setupTestCase();
4381982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        WifiPermissionsUtil codeUnderTest = new WifiPermissionsUtil(mMockPermissionsWrapper,
4391982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockContext, mMockWifiSettingsStore, mMockUserManager, mMockNetworkScoreManager,
4401982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockWifiInjector);
4411982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin
4421982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        output = codeUnderTest.canAccessFullConnectionInfo(mMockWifiConfig, TEST_PACKAGE_NAME,
4431982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mUid, mTargetVersion);
4441982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin
4451982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        assertEquals(false, output);
4461982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    }
4471982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin
4481982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    /**
4491982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     * Test case setting: Package is valid because it matches the USE_OPEN_WIFI_PACKAGE.
4501982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     *                    User is current
4511982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     *                    The current config is NULL.
4521982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     * Validate result is false
4531982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     */
4541982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    @Test
4551982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    public void testCanAccessFullConnectionInfo_WiFiConfigIsNull() throws Exception {
4561982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        final boolean output;
4571982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mThrowSecurityException = false;
4581982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mCurrentUser = UserHandle.USER_CURRENT_OR_SELF;
4591982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mUseOpenWifiPackage = TEST_PACKAGE_NAME;
4601982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        ComponentName useOpenWifiComponent = new ComponentName(TEST_PACKAGE_NAME, "TestClass");
4611982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mNetworkScorerAppData = new NetworkScorerAppData(0 /*packageUid*/,
4621982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                null /*recommendationServiceComp*/, null /*recommendationServiceLabel*/,
4631982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                useOpenWifiComponent, null /*networkAvailableNotificationChannelId*/);
4641982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        setupTestCase();
4651982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        WifiPermissionsUtil codeUnderTest = new WifiPermissionsUtil(mMockPermissionsWrapper,
4661982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockContext, mMockWifiSettingsStore, mMockUserManager, mMockNetworkScoreManager,
4671982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockWifiInjector);
4681982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin
4691982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        output = codeUnderTest.canAccessFullConnectionInfo(null /*config*/, TEST_PACKAGE_NAME,
4701982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mUid, mTargetVersion);
4711982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin
4721982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        assertEquals(false, output);
4731982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    }
4741982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin
4751982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    /**
4761982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     * Test case setting: Package is valid because it matches the USE_OPEN_WIFI_PACKAGE.
4771982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     *                    User is current
4781982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     *                    The current config is not for an open network.
4791982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     * Validate result is false
4801982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     */
4811982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    @Test
4821982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    public void testCanAccessFullConnectionInfo_WiFiConfigIsNotOpen() throws Exception {
4831982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        final boolean output;
4841982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mThrowSecurityException = false;
4851982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mCurrentUser = UserHandle.USER_CURRENT_OR_SELF;
4861982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mUseOpenWifiPackage = TEST_PACKAGE_NAME;
4871982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        ComponentName useOpenWifiComponent = new ComponentName(TEST_PACKAGE_NAME, "TestClass");
4881982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mNetworkScorerAppData = new NetworkScorerAppData(0 /*packageUid*/,
4891982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                null /*recommendationServiceComp*/, null /*recommendationServiceLabel*/,
4901982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                useOpenWifiComponent, null /*networkAvailableNotificationChannelId*/);
4911982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mConfigIsOpen = false;
4921982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        setupTestCase();
4931982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        WifiPermissionsUtil codeUnderTest = new WifiPermissionsUtil(mMockPermissionsWrapper,
4941982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockContext, mMockWifiSettingsStore, mMockUserManager, mMockNetworkScoreManager,
4951982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockWifiInjector);
4961982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin
4971982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        output = codeUnderTest.canAccessFullConnectionInfo(mMockWifiConfig, TEST_PACKAGE_NAME,
4981982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mUid, mTargetVersion);
4991982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin
5001982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        assertEquals(false, output);
5011982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    }
5021982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin
5031982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    /**
5041982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     * Test case setting: Package is valid because it matches the USE_OPEN_WIFI_PACKAGE.
5051982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     *                    User is current
5061982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     *                    The current config is for an open network.
5071982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     *                    There is no active scorer
5081982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     * Validate result is false
5091982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     */
5101982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    @Test
5111982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    public void testCanAccessFullConnectionInfo_UseOpenWifiPackageIsSetButNoActiveScorer()
5121982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin            throws Exception {
5131982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        final boolean output;
5141982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mThrowSecurityException = false;
5151982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mCurrentUser = UserHandle.USER_CURRENT_OR_SELF;
5161982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mUseOpenWifiPackage = TEST_PACKAGE_NAME;
5171982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mNetworkScorerAppData = null; // getActiveScorer() will return null
5181982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        setupTestCase();
5191982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        WifiPermissionsUtil codeUnderTest = new WifiPermissionsUtil(mMockPermissionsWrapper,
5201982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockContext, mMockWifiSettingsStore, mMockUserManager, mMockNetworkScoreManager,
5211982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockWifiInjector);
5221982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin
5231982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        output = codeUnderTest.canAccessFullConnectionInfo(mMockWifiConfig, TEST_PACKAGE_NAME,
5241982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mUid, mTargetVersion);
5251982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin
5261982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        assertEquals(false, output);
5271982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    }
5281982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin
5291982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    /**
5301982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     * Test case setting: Package is valid because it matches the USE_OPEN_WIFI_PACKAGE.
5311982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     *                    User is current
5321982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     *                    The current config is for an open network.
5331982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     *                    The scorer is active but the useOpenWiFi component name doesn't match
5341982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     *                    the provided package.
5351982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     * Validate result is false
5361982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     */
5371982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    @Test
5381982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    public void testCanAccessFullConnectionInfo_MismatchBetweenUseOpenWifiPackages()
5391982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin            throws Exception {
5401982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        final boolean output;
5411982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mThrowSecurityException = false;
5421982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mCurrentUser = UserHandle.USER_CURRENT_OR_SELF;
5431982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mUseOpenWifiPackage = TEST_PACKAGE_NAME;
5441982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        ComponentName useOpenWifiComponent =
5451982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                new ComponentName(mUseOpenWifiPackage + ".nomatch", "TestClass");
5461982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mNetworkScorerAppData = new NetworkScorerAppData(0 /*packageUid*/,
5471982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                null /*recommendationServiceComp*/, null /*recommendationServiceLabel*/,
5481982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                useOpenWifiComponent, null /*networkAvailableNotificationChannelId*/);
5491982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        setupTestCase();
5501982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        WifiPermissionsUtil codeUnderTest = new WifiPermissionsUtil(mMockPermissionsWrapper,
5511982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockContext, mMockWifiSettingsStore, mMockUserManager, mMockNetworkScoreManager,
5521982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockWifiInjector);
5531982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin
5541982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        output = codeUnderTest.canAccessFullConnectionInfo(mMockWifiConfig, TEST_PACKAGE_NAME,
5551982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mUid, mTargetVersion);
5561982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin
5571982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        assertEquals(false, output);
5581982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    }
5591982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin
5601982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    /**
5611982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     * Test case setting: Package is valid because it matches the USE_OPEN_WIFI_PACKAGE.
5621982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     *                    User is current
5631982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     *                    The current config is for an open network.
5641982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     *                    The scorer is active but the useOpenWiFi component name is null.
5651982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     * Validate result is false
5661982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     */
5671982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    @Test
5681982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    public void testCanAccessFullConnectionInfo_UseOpenWifiPackageFromScorerIsNull()
5691982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin            throws Exception {
5701982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        final boolean output;
5711982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mThrowSecurityException = false;
5721982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mCurrentUser = UserHandle.USER_CURRENT_OR_SELF;
5731982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mUseOpenWifiPackage = TEST_PACKAGE_NAME;
5741982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mNetworkScorerAppData = new NetworkScorerAppData(0 /*packageUid*/,
5751982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                null /*recommendationServiceComp*/, null /*recommendationServiceLabel*/,
5761982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                null /*useOpenWifiComponent*/, null /*networkAvailableNotificationChannelId*/);
5771982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        setupTestCase();
5781982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        WifiPermissionsUtil codeUnderTest = new WifiPermissionsUtil(mMockPermissionsWrapper,
5791982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockContext, mMockWifiSettingsStore, mMockUserManager, mMockNetworkScoreManager,
5801982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockWifiInjector);
5811982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin
5821982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        output = codeUnderTest.canAccessFullConnectionInfo(mMockWifiConfig, TEST_PACKAGE_NAME,
5831982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mUid, mTargetVersion);
5841982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin
5851982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        assertEquals(false, output);
5861982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    }
5871982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin
5881982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    /**
5891982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     * Test case setting: Package is invalid because USE_OPEN_WIFI_PACKAGE is an empty string.
5901982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     *                    Location mode is ON
5911982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     *                    User is current
5921982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     *                    The current config is for an open network.
5931982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     * Validate result is false
5941982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     */
5951982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    @Test
5961982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    public void testCanAccessFullConnectionInfo_UseOpenWifiPackageIsEmpty() throws Exception {
5971982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        final boolean output;
5981982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mThrowSecurityException = false;
5991982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mLocationModeSetting = Settings.Secure.LOCATION_MODE_HIGH_ACCURACY;
6001982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mCurrentUser = UserHandle.USER_CURRENT_OR_SELF;
6011982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mUseOpenWifiPackage = "";
6021982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        setupTestCase();
6031982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        WifiPermissionsUtil codeUnderTest = new WifiPermissionsUtil(mMockPermissionsWrapper,
6041982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockContext, mMockWifiSettingsStore, mMockUserManager, mMockNetworkScoreManager,
6051982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockWifiInjector);
6061982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin
6071982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        output = codeUnderTest.canAccessFullConnectionInfo(mMockWifiConfig, TEST_PACKAGE_NAME,
6081982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mUid, mTargetVersion);
6091982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin
6101982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        assertEquals(false, output);
6111982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    }
6121982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin
6131982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    /**
6141982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     * Test case setting: Package is invalid because it does not match the USE_OPEN_WIFI_PACKAGE.
6151982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     *                    User is current
6161982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     *                    The current config is for an open network.
6171982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     * Validate result is false
6181982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     */
6191982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    @Test
6201982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    public void testCanAccessFullConnectionInfo_DoesNotMatchUseOpenWifiPackage() throws Exception {
6211982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        final boolean output;
6221982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mThrowSecurityException = false;
6231982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mCurrentUser = UserHandle.USER_CURRENT_OR_SELF;
6241982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mUseOpenWifiPackage = TEST_PACKAGE_NAME + ".nomatch";
6251982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        setupTestCase();
6261982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        WifiPermissionsUtil codeUnderTest = new WifiPermissionsUtil(mMockPermissionsWrapper,
6271982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockContext, mMockWifiSettingsStore, mMockUserManager, mMockNetworkScoreManager,
6281982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockWifiInjector);
6291982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin
6301982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        output = codeUnderTest.canAccessFullConnectionInfo(mMockWifiConfig, TEST_PACKAGE_NAME,
6311982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mUid, mTargetVersion);
6321982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin
6331982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        assertEquals(false, output);
6341982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    }
6351982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin
6361982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    /**
6371982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     * Test case setting: The caller is invalid because its UID does not match the provided package.
6381982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     *
6391982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     * Validate a SecurityException is thrown.
6401982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     */
6411982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    @Test
6421982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    public void testCanAccessFullConnectionInfo_UidPackageCheckFails() throws Exception {
6431982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mThrowSecurityException = true;
6441982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        setupTestCase();
6451982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        WifiPermissionsUtil codeUnderTest = new WifiPermissionsUtil(mMockPermissionsWrapper,
6461982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockContext, mMockWifiSettingsStore, mMockUserManager, mMockNetworkScoreManager,
6471982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockWifiInjector);
6481982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin
6491982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        try {
6501982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin            codeUnderTest.canAccessFullConnectionInfo(mMockWifiConfig, TEST_PACKAGE_NAME, mUid,
6511982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                    mTargetVersion);
6521982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin            fail("SecurityException not thrown.");
6531982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        } catch (SecurityException e) {
6541982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin            // expected
6551982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        }
6561982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    }
6571982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin
6581982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    /**
6591982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     * Test case setting: The getActiveScorer() call fails with a SecurityException.
6601982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     *
6611982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     * Validate a SecurityException is thrown.
6621982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin     */
6631982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    @Test
6641982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    public void testCanAccessFullConnectionInfo_GetActiveScorerFails() throws Exception {
6651982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mThrowSecurityException = false;
6661982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mGetActiveScorerThrowsSecurityException = true;
6671982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mLocationModeSetting = Settings.Secure.LOCATION_MODE_HIGH_ACCURACY;
6681982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mCurrentUser = UserHandle.USER_CURRENT_OR_SELF;
6691982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mUseOpenWifiPackage = TEST_PACKAGE_NAME;
6701982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        setupTestCase();
6711982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        WifiPermissionsUtil codeUnderTest = new WifiPermissionsUtil(mMockPermissionsWrapper,
6721982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockContext, mMockWifiSettingsStore, mMockUserManager, mMockNetworkScoreManager,
6731982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockWifiInjector);
6741982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin
6751982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        try {
6761982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin            codeUnderTest.canAccessFullConnectionInfo(mMockWifiConfig, TEST_PACKAGE_NAME, mUid,
6771982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                    mTargetVersion);
6781982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin            fail("SecurityException not thrown.");
6791982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        } catch (SecurityException e) {
6801982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin            // expected
6811982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        }
6821982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    }
6831982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin
6841982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin    /**
68592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     * Test case Setting: Package is valid
68692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     *                    Legacy App
68792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     *                    Foreground
68892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     *                    This App has permission to request WIFI_SCAN
68992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     *                    User is current
69092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     *  Validate result is true - has all permissions
69192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     */
69292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    @Test
69392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    public void testLegacyForegroundAppAndAllPermissions() throws Exception {
69492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        boolean output = false;
69592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mThrowSecurityException = false;
69692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mMockApplInfo.targetSdkVersion = Build.VERSION_CODES.GINGERBREAD;
69792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mPkgNameOfTopActivity = TEST_PACKAGE_NAME;
69892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mWifiScanAllowApps = AppOpsManager.MODE_ALLOWED;
69992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mUid = MANAGED_PROFILE_UID;
70092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mCurrentUser = UserHandle.USER_CURRENT_OR_SELF;
70192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        setupTestCase();
70292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        WifiPermissionsUtil codeUnderTest = new WifiPermissionsUtil(mMockPermissionsWrapper,
7031982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockContext, mMockWifiSettingsStore, mMockUserManager, mMockNetworkScoreManager,
7041982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockWifiInjector);
70592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        try {
70692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao            output = codeUnderTest.canAccessScanResults(TEST_PACKAGE_NAME, mUid, mTargetVersion);
70792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        } catch (SecurityException e) {
70892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao            throw e;
70992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        }
71092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        assertEquals(output, true);
71192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    }
71292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao
71392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    /**
71492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     * Test case Setting: Package is valid
71592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     *                    Legacy App
71692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     *                    Location Mode Enabled
71792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     *                    Coarse Location Access
71892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     *                    This App has permission to request WIFI_SCAN
71992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     *                    User profile is current
72092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     *  Validate result is true - has all permissions
72192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     */
72292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    @Test
72392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    public void testLegacyAppHasLocationAndAllPermissions() throws Exception {
72492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        boolean output = false;
72592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mThrowSecurityException = false;
72692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mMockApplInfo.targetSdkVersion = Build.VERSION_CODES.GINGERBREAD;
72792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mLocationModeSetting = Settings.Secure.LOCATION_MODE_HIGH_ACCURACY;
72892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mCoarseLocationPermission = PackageManager.PERMISSION_GRANTED;
72992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mAllowCoarseLocationApps = AppOpsManager.MODE_ALLOWED;
73092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mWifiScanAllowApps = AppOpsManager.MODE_ALLOWED;
73192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mUid = MANAGED_PROFILE_UID;
73292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mMockUserInfo.id = mCallingUser;
73392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        setupTestCase();
73492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        WifiPermissionsUtil codeUnderTest = new WifiPermissionsUtil(mMockPermissionsWrapper,
7351982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockContext, mMockWifiSettingsStore, mMockUserManager, mMockNetworkScoreManager,
7361982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockWifiInjector);
73792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        try {
73892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao            output = codeUnderTest.canAccessScanResults(TEST_PACKAGE_NAME, mUid, mTargetVersion);
73992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        } catch (SecurityException e) {
74092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao            throw e;
74192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        }
74292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        assertEquals(output, true);
74392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    }
74492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao
74592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    /**
74692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     * Test case setting: Package is valid
74792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     *                    Location Mode Enabled
74892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     * Validate result is false
74992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     * - Doesn't have Peer Mac Address read permission
75092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     * - Uid is not an active network scorer
75192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     * - Location Mode is enabled but the uid
75292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     * - doesn't have Coarse Location Access
75392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     * - which implies No Location Permission
75492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     */
75592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    @Test
75692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    public void testCannotAccessScanResults_NoCoarseLocationPermission() throws Exception {
75792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        boolean output = true;
75892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mThrowSecurityException = false;
75992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mLocationModeSetting = Settings.Secure.LOCATION_MODE_HIGH_ACCURACY;
76092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        setupTestCase();
76192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        WifiPermissionsUtil codeUnderTest = new WifiPermissionsUtil(mMockPermissionsWrapper,
7621982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockContext, mMockWifiSettingsStore, mMockUserManager, mMockNetworkScoreManager,
7631982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockWifiInjector);
76492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        try {
76592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao            output = codeUnderTest.canAccessScanResults(TEST_PACKAGE_NAME, mUid, mTargetVersion);
76692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        } catch (SecurityException e) {
76792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao            throw e;
76892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        }
76992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        assertEquals(output, false);
77092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    }
77192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao
77292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    /**
77392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     * Test case setting: Invalid Package
77492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     * Expect a securityException
77592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao     */
77692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    @Test (expected = SecurityException.class)
77792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    public void testInvalidPackage() throws Exception {
77892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        boolean output = false;
77992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        setupTestCase();
78092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        WifiPermissionsUtil codeUnderTest = new WifiPermissionsUtil(mMockPermissionsWrapper,
7811982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockContext, mMockWifiSettingsStore, mMockUserManager, mMockNetworkScoreManager,
7821982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockWifiInjector);
78392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        try {
78492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao            output = codeUnderTest.canAccessScanResults(TEST_PACKAGE_NAME, mUid, mTargetVersion);
78592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        } catch (SecurityException e) {
78692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao            throw e;
78792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        }
78892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    }
78992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao
7902fd9436184ea37a297f2abd5e884d30d0014b620Rebecca Silberstein    /**
7912fd9436184ea37a297f2abd5e884d30d0014b620Rebecca Silberstein     * Test case setting: caller does have Location permission.
7922fd9436184ea37a297f2abd5e884d30d0014b620Rebecca Silberstein     * A SecurityException should not be thrown.
7932fd9436184ea37a297f2abd5e884d30d0014b620Rebecca Silberstein     */
7942fd9436184ea37a297f2abd5e884d30d0014b620Rebecca Silberstein    @Test
7952fd9436184ea37a297f2abd5e884d30d0014b620Rebecca Silberstein    public void testEnforceLocationPermission() throws Exception {
7962fd9436184ea37a297f2abd5e884d30d0014b620Rebecca Silberstein        mThrowSecurityException = false;
7972fd9436184ea37a297f2abd5e884d30d0014b620Rebecca Silberstein        mMockApplInfo.targetSdkVersion = Build.VERSION_CODES.GINGERBREAD;
7982fd9436184ea37a297f2abd5e884d30d0014b620Rebecca Silberstein        mLocationModeSetting = Settings.Secure.LOCATION_MODE_HIGH_ACCURACY;
7992fd9436184ea37a297f2abd5e884d30d0014b620Rebecca Silberstein        mCoarseLocationPermission = PackageManager.PERMISSION_GRANTED;
8002fd9436184ea37a297f2abd5e884d30d0014b620Rebecca Silberstein        mAllowCoarseLocationApps = AppOpsManager.MODE_ALLOWED;
8012fd9436184ea37a297f2abd5e884d30d0014b620Rebecca Silberstein        mWifiScanAllowApps = AppOpsManager.MODE_ALLOWED;
8022fd9436184ea37a297f2abd5e884d30d0014b620Rebecca Silberstein        mUid = MANAGED_PROFILE_UID;
8032fd9436184ea37a297f2abd5e884d30d0014b620Rebecca Silberstein        mMockUserInfo.id = mCallingUser;
8042fd9436184ea37a297f2abd5e884d30d0014b620Rebecca Silberstein        setupTestCase();
8052fd9436184ea37a297f2abd5e884d30d0014b620Rebecca Silberstein        WifiPermissionsUtil codeUnderTest = new WifiPermissionsUtil(mMockPermissionsWrapper,
8061982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockContext, mMockWifiSettingsStore, mMockUserManager, mMockNetworkScoreManager,
8071982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockWifiInjector);
8082fd9436184ea37a297f2abd5e884d30d0014b620Rebecca Silberstein        codeUnderTest.enforceLocationPermission(TEST_PACKAGE_NAME, mUid);
8092fd9436184ea37a297f2abd5e884d30d0014b620Rebecca Silberstein    }
8102fd9436184ea37a297f2abd5e884d30d0014b620Rebecca Silberstein
8112fd9436184ea37a297f2abd5e884d30d0014b620Rebecca Silberstein    /**
8122fd9436184ea37a297f2abd5e884d30d0014b620Rebecca Silberstein     * Test case setting: caller does not have Location permission.
8132fd9436184ea37a297f2abd5e884d30d0014b620Rebecca Silberstein     * Expect a SecurityException
8142fd9436184ea37a297f2abd5e884d30d0014b620Rebecca Silberstein     */
8152fd9436184ea37a297f2abd5e884d30d0014b620Rebecca Silberstein    @Test(expected = SecurityException.class)
8162fd9436184ea37a297f2abd5e884d30d0014b620Rebecca Silberstein    public void testEnforceLocationPermissionExpectSecurityException() throws Exception {
8172fd9436184ea37a297f2abd5e884d30d0014b620Rebecca Silberstein        setupTestCase();
8182fd9436184ea37a297f2abd5e884d30d0014b620Rebecca Silberstein        WifiPermissionsUtil codeUnderTest = new WifiPermissionsUtil(mMockPermissionsWrapper,
8191982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockContext, mMockWifiSettingsStore, mMockUserManager, mMockNetworkScoreManager,
8201982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                mMockWifiInjector);
8212fd9436184ea37a297f2abd5e884d30d0014b620Rebecca Silberstein        codeUnderTest.enforceLocationPermission(TEST_PACKAGE_NAME, mUid);
8222fd9436184ea37a297f2abd5e884d30d0014b620Rebecca Silberstein    }
8232fd9436184ea37a297f2abd5e884d30d0014b620Rebecca Silberstein
82492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    private Answer<Integer> createPermissionAnswer() {
82592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        return new Answer<Integer>() {
82692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao            @Override
82792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao            public Integer answer(InvocationOnMock invocation) {
82892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao                int myUid = (int) invocation.getArguments()[1];
82992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao                String myPermission = (String) invocation.getArguments()[0];
83092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao                mPermissionsList.get(myPermission);
83192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao                if (mPermissionsList.containsKey(myPermission)) {
83292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao                    int uid = mPermissionsList.get(myPermission);
83392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao                    if (myUid == uid) {
83492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao                        return PackageManager.PERMISSION_GRANTED;
83592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao                    }
83692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao                }
83792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao                return PackageManager.PERMISSION_DENIED;
83892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao            }
83992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        };
84092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    }
84192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao
84292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    private void setupMocks() throws Exception {
84392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        when(mMockPkgMgr.getApplicationInfo(TEST_PACKAGE_NAME, 0))
84492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao            .thenReturn(mMockApplInfo);
84592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        when(mMockContext.getPackageManager()).thenReturn(mMockPkgMgr);
84692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        when(mMockAppOps.noteOp(AppOpsManager.OP_WIFI_SCAN, mUid, TEST_PACKAGE_NAME))
84792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao            .thenReturn(mWifiScanAllowApps);
84892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        when(mMockAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, mUid, TEST_PACKAGE_NAME))
84992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao            .thenReturn(mAllowCoarseLocationApps);
85092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        if (mThrowSecurityException) {
85192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao            doThrow(new SecurityException("Package " + TEST_PACKAGE_NAME + " doesn't belong"
85292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao                    + " to application bound to user " + mUid))
85392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao                    .when(mMockAppOps).checkPackage(mUid, TEST_PACKAGE_NAME);
85492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        }
85592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        when(mMockContext.getSystemService(Context.APP_OPS_SERVICE))
85692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao            .thenReturn(mMockAppOps);
85792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        when(mMockUserManager.getProfiles(mCurrentUser))
85892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao            .thenReturn(Arrays.asList(mMockUserInfo));
85992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        when(mMockContext.getContentResolver()).thenReturn(mMockContentResolver);
86092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        when(mMockContext.getSystemService(Context.USER_SERVICE))
86192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao            .thenReturn(mMockUserManager);
8621982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        when(mMockWifiInjector.makeLog(anyString())).thenReturn(mWifiLog);
8631982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        when(mMockWifiInjector.getFrameworkFacade()).thenReturn(mMockFrameworkFacade);
8641982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        if (mGetActiveScorerThrowsSecurityException) {
8651982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin            when(mMockNetworkScoreManager.getActiveScorer()).thenThrow(
8661982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                    new SecurityException("Caller is neither the system process nor a "
8671982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                            + "score requester."));
8681982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        } else {
8691982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin            when(mMockNetworkScoreManager.getActiveScorer()).thenReturn(mNetworkScorerAppData);
8701982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        }
87192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    }
87292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao
87392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    private void initTestVars() {
87492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mPermissionsList.clear();
87592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mReturnPermission = createPermissionAnswer();
87692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mWifiScanAllowApps = AppOpsManager.MODE_ERRORED;
87792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mUid = OTHER_USER_UID;
87892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mThrowSecurityException = true;
87992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mMockUserInfo.id = UserHandle.USER_NULL;
88092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mMockApplInfo.targetSdkVersion = Build.VERSION_CODES.M;
88192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mTargetVersion = Build.VERSION_CODES.M;
88292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mPkgNameOfTopActivity = INVALID_PACKAGE;
88392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mLocationModeSetting = Settings.Secure.LOCATION_MODE_OFF;
88492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mCurrentUser = UserHandle.USER_SYSTEM;
88592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mCoarseLocationPermission = PackageManager.PERMISSION_DENIED;
88692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mAllowCoarseLocationApps = AppOpsManager.MODE_ERRORED;
88792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        mActiveNwScorer = false;
8881982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mUseOpenWifiPackage = null;
8891982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mNetworkScorerAppData = null;
8901982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mGetActiveScorerThrowsSecurityException = false;
8911982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        mConfigIsOpen = true;
89292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    }
89392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao
89492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    private void setupMockInterface() {
89592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        BinderUtil.setUid(mUid);
89692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        doAnswer(mReturnPermission).when(mMockPermissionsWrapper).getUidPermission(
89792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao                        anyString(), anyInt());
89892f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        doAnswer(mReturnPermission).when(mMockPermissionsWrapper).getUidPermission(
89992f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao                        anyString(), anyInt());
90092f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        when(mMockPermissionsWrapper.getCallingUserId(mUid)).thenReturn(mCallingUser);
90192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        when(mMockPermissionsWrapper.getCurrentUser()).thenReturn(mCurrentUser);
9021982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        when(mMockNetworkScoreManager.isCallerActiveScorer(mUid)).thenReturn(mActiveNwScorer);
90392f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        when(mMockPermissionsWrapper.getUidPermission(mManifestStringCoarse, mUid))
90492f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao            .thenReturn(mCoarseLocationPermission);
90592f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        when(mMockWifiSettingsStore.getLocationModeSetting(mMockContext))
90692f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao            .thenReturn(mLocationModeSetting);
90792f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao        when(mMockPermissionsWrapper.getTopPkgName()).thenReturn(mPkgNameOfTopActivity);
9081982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        when(mMockFrameworkFacade.getStringSetting(mMockContext,
9091982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin                Settings.Global.USE_OPEN_WIFI_PACKAGE)).thenReturn(mUseOpenWifiPackage);
9101982ffd9f5a9434623e1e6229df9d23c506e7491Jeremy Joslin        when(mMockWifiConfig.isOpenNetwork()).thenReturn(mConfigIsOpen);
91192f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao    }
91292f4d52b14d8847f6f81712bf3bbfa0f1203eef5Sohani Rao}
913