blob: a70065cce0748f4864e5dab903a3fa6d22137e46 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.location.cts.none;
import static android.app.AppOpsManager.MODE_ALLOWED;
import static android.app.AppOpsManager.OPSTR_FINE_LOCATION;
import static com.android.compatibility.common.util.SystemUtil.eventually;
import static com.android.compatibility.common.util.SystemUtil.runWithShellPermissionIdentity;
import android.app.ActivityManager;
import android.app.AppOpsManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.location.LocationManager;
import android.os.PackageTagsList;
import android.os.Process;
import android.os.UserHandle;
import androidx.test.InstrumentationRegistry;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
public class LocationDisabledAppOpsTest {
private final Context mContext = InstrumentationRegistry.getContext();
private LocationManager mLm;
private AppOpsManager mAom;
@Before
public void setUp() {
mLm = mContext.getSystemService(LocationManager.class);
mAom = mContext.getSystemService(AppOpsManager.class);
}
@Test
public void testLocationAppOpIsIgnoredForAppsWhenLocationIsDisabled() {
PackageTagsList ignoreList = mLm.getIgnoreSettingsAllowlist();
UserHandle[] userArr = {UserHandle.SYSTEM};
runWithShellPermissionIdentity(() -> {
userArr[0] = UserHandle.of(ActivityManager.getCurrentUser());
});
UserHandle user = userArr[0];
boolean wasEnabled = mLm.isLocationEnabledForUser(user);
try {
runWithShellPermissionIdentity(() -> {
mLm.setLocationEnabledForUser(false, user);
});
List<PackageInfo> pkgs =
mContext.getPackageManager().getInstalledPackagesAsUser(
0, user.getIdentifier());
eventually(() -> {
List<String> bypassedNoteOps = new ArrayList<>();
List<String> bypassedCheckOps = new ArrayList<>();
for (PackageInfo pi : pkgs) {
ApplicationInfo ai = pi.applicationInfo;
if (ai.uid != Process.SYSTEM_UID) {
final int[] mode = {MODE_ALLOWED};
runWithShellPermissionIdentity(() -> {
mode[0] = mAom.noteOpNoThrow(
OPSTR_FINE_LOCATION, ai.uid, ai.packageName);
});
if (mode[0] == MODE_ALLOWED && !ignoreList.containsAll(pi.packageName)) {
bypassedNoteOps.add(pi.packageName);
}
mode[0] = MODE_ALLOWED;
runWithShellPermissionIdentity(() -> {
mode[0] = mAom
.checkOpNoThrow(OPSTR_FINE_LOCATION, ai.uid, ai.packageName);
});
if (mode[0] == MODE_ALLOWED && !ignoreList.includes(pi.packageName)) {
bypassedCheckOps.add(pi.packageName);
}
}
}
String msg = "";
if (!bypassedNoteOps.isEmpty()) {
msg += "Apps which still have access from noteOp " + bypassedNoteOps;
}
if (!bypassedCheckOps.isEmpty()) {
msg += (msg.isEmpty() ? "" : "\n\n")
+ "Apps which still have access from checkOp " + bypassedCheckOps;
}
if (!msg.isEmpty()) {
Assert.fail(msg);
}
});
} finally {
runWithShellPermissionIdentity(() -> {
mLm.setLocationEnabledForUser(wasEnabled, user);
});
}
}
}
|