• R/O
  • HTTP
  • SSH
  • HTTPS

提交

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

packages/apps/Settings


Commit MetaInfo

修订版69cae4fefbf141beb57c2a2afbf0e5ed3b358afb (tree)
时间2021-08-16 13:15:13
作者Weng Su <wengsu@goog...>
CommiterAndroid Build Coastguard Worker

Log Message

[DO NOT MERGE] Add permission checking to WifiDialogActivity

- Use getCallingPackage() to get calling package.

- Check if the calling package has ACCESS_COARSE_LOCATION or
ACCESS_COARSE_LOCATION permission.

- Only set result data to permission granted callers

Bug: 185126813
Test: manual test
make RunSettingsRoboTests ROBOTEST_FILTER=WifiDialogActivityTest

Merged-In: If7ca069c842ed2bd1aed23f9d4041473c68a4dad
Change-Id: If7ca069c842ed2bd1aed23f9d4041473c68a4dad
(cherry picked from commit 71e728e934bec9c5d121b285e5c0089f658723d2)
(cherry picked from commit f973f2ac69ef0c90c1c45b1d8495dea3bb024e03)

更改概述

差异

--- a/src/com/android/settings/wifi/WifiDialogActivity.java
+++ b/src/com/android/settings/wifi/WifiDialogActivity.java
@@ -16,9 +16,13 @@
1616
1717 package com.android.settings.wifi;
1818
19+import static android.Manifest.permission.ACCESS_COARSE_LOCATION;
20+import static android.Manifest.permission.ACCESS_FINE_LOCATION;
21+
1922 import android.app.Activity;
2023 import android.content.DialogInterface;
2124 import android.content.Intent;
25+import android.content.pm.PackageManager;
2226 import android.net.NetworkInfo;
2327 import android.net.wifi.WifiConfiguration;
2428 import android.net.wifi.WifiManager;
@@ -53,10 +57,12 @@ public class WifiDialogActivity extends Activity implements WifiDialog.WifiDialo
5357
5458 public static final String KEY_WIFI_CONFIGURATION = "wifi_configuration";
5559
56- private static final int RESULT_CONNECTED = RESULT_FIRST_USER;
60+ @VisibleForTesting
61+ static final int RESULT_CONNECTED = RESULT_FIRST_USER;
5762 private static final int RESULT_FORGET = RESULT_FIRST_USER + 1;
5863
59- private static final int REQUEST_CODE_WIFI_DPP_ENROLLEE_QR_CODE_SCANNER = 0;
64+ @VisibleForTesting
65+ static final int REQUEST_CODE_WIFI_DPP_ENROLLEE_QR_CODE_SCANNER = 0;
6066
6167 private WifiDialog mDialog;
6268
@@ -156,17 +162,22 @@ public class WifiDialogActivity extends Activity implements WifiDialog.WifiDialo
156162 }
157163 }
158164
159- Intent resultData = new Intent();
165+ Intent resultData = hasPermissionForResult() ? createResultData(config, accessPoint) : null;
166+ setResult(RESULT_CONNECTED, resultData);
167+ finish();
168+ }
169+
170+ protected Intent createResultData(WifiConfiguration config, AccessPoint accessPoint) {
171+ Intent result = new Intent();
160172 if (accessPoint != null) {
161173 Bundle accessPointState = new Bundle();
162174 accessPoint.saveWifiState(accessPointState);
163- resultData.putExtra(KEY_ACCESS_POINT_STATE, accessPointState);
175+ result.putExtra(KEY_ACCESS_POINT_STATE, accessPointState);
164176 }
165177 if (config != null) {
166- resultData.putExtra(KEY_WIFI_CONFIGURATION, config);
178+ result.putExtra(KEY_WIFI_CONFIGURATION, config);
167179 }
168- setResult(RESULT_CONNECTED, resultData);
169- finish();
180+ return result;
170181 }
171182
172183 @Override
@@ -192,9 +203,35 @@ public class WifiDialogActivity extends Activity implements WifiDialog.WifiDialo
192203 if (resultCode != RESULT_OK) {
193204 return;
194205 }
195-
196- setResult(RESULT_CONNECTED, data);
206+ if (hasPermissionForResult()) {
207+ setResult(RESULT_CONNECTED, data);
208+ } else {
209+ setResult(RESULT_CONNECTED);
210+ }
197211 finish();
198212 }
199213 }
214+
215+ protected boolean hasPermissionForResult() {
216+ final String callingPackage = getCallingPackage();
217+ if (callingPackage == null) {
218+ Log.d(TAG, "Failed to get the calling package, don't return the result.");
219+ return false;
220+ }
221+
222+ if (getPackageManager().checkPermission(ACCESS_COARSE_LOCATION, callingPackage)
223+ == PackageManager.PERMISSION_GRANTED) {
224+ Log.d(TAG, "The calling package has ACCESS_COARSE_LOCATION permission for result.");
225+ return true;
226+ }
227+
228+ if (getPackageManager().checkPermission(ACCESS_FINE_LOCATION, callingPackage)
229+ == PackageManager.PERMISSION_GRANTED) {
230+ Log.d(TAG, "The calling package has ACCESS_FINE_LOCATION permission for result.");
231+ return true;
232+ }
233+
234+ Log.d(TAG, "The calling package does not have the necessary permissions for result.");
235+ return false;
236+ }
200237 }
--- a/tests/robotests/src/com/android/settings/wifi/WifiDialogActivityTest.java
+++ b/tests/robotests/src/com/android/settings/wifi/WifiDialogActivityTest.java
@@ -16,17 +16,31 @@
1616
1717 package com.android.settings.wifi;
1818
19+import static android.Manifest.permission.ACCESS_COARSE_LOCATION;
20+import static android.Manifest.permission.ACCESS_FINE_LOCATION;
21+
22+import static com.android.settings.wifi.WifiDialogActivity.REQUEST_CODE_WIFI_DPP_ENROLLEE_QR_CODE_SCANNER;
23+import static com.android.settings.wifi.WifiDialogActivity.RESULT_CONNECTED;
24+import static com.android.settings.wifi.WifiDialogActivity.RESULT_OK;
25+
1926 import static com.google.common.truth.Truth.assertThat;
2027
28+import static org.mockito.ArgumentMatchers.any;
2129 import static org.mockito.Mockito.doReturn;
30+import static org.mockito.Mockito.spy;
31+import static org.mockito.Mockito.verify;
32+import static org.mockito.Mockito.when;
2233
2334 import android.content.Intent;
35+import android.content.pm.PackageManager;
2436 import android.net.wifi.WifiConfiguration;
37+import android.net.wifi.WifiManager;
2538
2639 import com.android.settings.R;
2740 import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
2841 import com.android.settings.testutils.shadow.ShadowConnectivityManager;
2942 import com.android.settings.testutils.shadow.ShadowWifiManager;
43+import com.android.settingslib.wifi.AccessPoint;
3044
3145 import com.google.android.setupcompat.util.WizardManagerHelper;
3246
@@ -48,13 +62,30 @@ import org.robolectric.util.ReflectionHelpers;
4862 })
4963 public class WifiDialogActivityTest {
5064
65+ private static final String CALLING_PACKAGE = "calling_package";
5166 private static final String AP1_SSID = "\"ap1\"";
67+
68+ @Mock
69+ PackageManager mPackageManager;
70+ @Mock
71+ WifiManager mWifiManager;
72+ @Mock
73+ WifiDialog mWifiDialog;
74+ @Mock
75+ WifiConfiguration mWifiConfiguration;
76+ @Mock
77+ AccessPoint mAccessPoint;
78+ @Mock
79+ Intent mResultData;
5280 @Mock
5381 private WifiConfigController mController;
5482
5583 @Before
5684 public void setUp() {
5785 MockitoAnnotations.initMocks(this);
86+ when(mWifiDialog.getController()).thenReturn(mController);
87+ when(mController.getConfig()).thenReturn(mWifiConfiguration);
88+ when(mController.getAccessPoint()).thenReturn(mAccessPoint);
5889
5990 WifiConfiguration wifiConfig = new WifiConfiguration();
6091 wifiConfig.SSID = AP1_SSID;
@@ -75,6 +106,29 @@ public class WifiDialogActivityTest {
75106 }
76107
77108 @Test
109+ public void onSubmit_noPermissionForResult_setResultWithoutData() {
110+ WifiDialogActivity activity = spy(Robolectric.setupActivity(WifiDialogActivity.class));
111+ when(activity.hasPermissionForResult()).thenReturn(false);
112+ when(activity.getSystemService(WifiManager.class)).thenReturn(mWifiManager);
113+
114+ activity.onSubmit(mWifiDialog);
115+
116+ verify(activity).setResult(RESULT_CONNECTED, null);
117+ }
118+
119+ @Test
120+ public void onSubmit_hasPermissionForResult_setResultWithData() {
121+ WifiDialogActivity activity = spy(Robolectric.setupActivity(WifiDialogActivity.class));
122+ when(activity.hasPermissionForResult()).thenReturn(true);
123+ when(activity.createResultData(any(), any())).thenReturn(mResultData);
124+ when(activity.getSystemService(WifiManager.class)).thenReturn(mWifiManager);
125+
126+ activity.onSubmit(mWifiDialog);
127+
128+ verify(activity).setResult(RESULT_CONNECTED, mResultData);
129+ }
130+
131+ @Test
78132 public void onSubmit_whenConnectForCallerIsFalse_shouldNotConnectToNetwork() {
79133 WifiDialogActivity activity =
80134 Robolectric.buildActivity(
@@ -111,4 +165,97 @@ public class WifiDialogActivityTest {
111165 assertThat(dialog.getContext().getThemeResId())
112166 .isEqualTo(R.style.SuwAlertDialogThemeCompat_Light);
113167 }
168+
169+ @Test
170+ public void onActivityResult_noPermissionForResult_setResultWithoutData() {
171+ WifiDialogActivity activity = spy(Robolectric.setupActivity(WifiDialogActivity.class));
172+ when(activity.hasPermissionForResult()).thenReturn(false);
173+ final Intent data = new Intent();
174+
175+ activity.onActivityResult(REQUEST_CODE_WIFI_DPP_ENROLLEE_QR_CODE_SCANNER, RESULT_OK,
176+ data);
177+
178+ verify(activity).setResult(RESULT_CONNECTED);
179+ }
180+
181+ @Test
182+ public void onActivityResult_hasPermissionForResult_setResultWithData() {
183+ WifiDialogActivity activity = spy(Robolectric.setupActivity(WifiDialogActivity.class));
184+ when(activity.hasPermissionForResult()).thenReturn(true);
185+ final Intent data = new Intent();
186+
187+ activity.onActivityResult(REQUEST_CODE_WIFI_DPP_ENROLLEE_QR_CODE_SCANNER, RESULT_OK,
188+ data);
189+
190+ verify(activity).setResult(RESULT_CONNECTED, data);
191+ }
192+
193+ @Test
194+ public void hasPermissionForResult_noCallingPackage_returnFalse() {
195+ WifiDialogActivity activity = spy(Robolectric.setupActivity(WifiDialogActivity.class));
196+ when(activity.getCallingPackage()).thenReturn(null);
197+
198+ final boolean result = activity.hasPermissionForResult();
199+
200+ assertThat(result).isFalse();
201+ }
202+
203+ @Test
204+ public void hasPermissionForResult_noPermission_returnFalse() {
205+ WifiDialogActivity activity = spy(Robolectric.setupActivity(WifiDialogActivity.class));
206+ when(activity.getCallingPackage()).thenReturn(null);
207+ when(mPackageManager.checkPermission(ACCESS_COARSE_LOCATION, CALLING_PACKAGE))
208+ .thenReturn(PackageManager.PERMISSION_DENIED);
209+ when(mPackageManager.checkPermission(ACCESS_FINE_LOCATION, CALLING_PACKAGE))
210+ .thenReturn(PackageManager.PERMISSION_DENIED);
211+
212+ final boolean result = activity.hasPermissionForResult();
213+
214+ assertThat(result).isFalse();
215+ }
216+
217+ @Test
218+ public void hasPermissionForResult_hasCoarseLocationPermission_returnTrue() {
219+ WifiDialogActivity activity = spy(Robolectric.setupActivity(WifiDialogActivity.class));
220+ when(activity.getCallingPackage()).thenReturn(CALLING_PACKAGE);
221+ when(activity.getPackageManager()).thenReturn(mPackageManager);
222+ when(mPackageManager.checkPermission(ACCESS_COARSE_LOCATION, CALLING_PACKAGE))
223+ .thenReturn(PackageManager.PERMISSION_GRANTED);
224+ when(mPackageManager.checkPermission(ACCESS_FINE_LOCATION, CALLING_PACKAGE))
225+ .thenReturn(PackageManager.PERMISSION_DENIED);
226+
227+ final boolean result = activity.hasPermissionForResult();
228+
229+ assertThat(result).isTrue();
230+ }
231+
232+ @Test
233+ public void hasPermissionForResult_hasFineLocationPermission_returnTrue() {
234+ WifiDialogActivity activity = spy(Robolectric.setupActivity(WifiDialogActivity.class));
235+ when(activity.getCallingPackage()).thenReturn(CALLING_PACKAGE);
236+ when(activity.getPackageManager()).thenReturn(mPackageManager);
237+ when(mPackageManager.checkPermission(ACCESS_COARSE_LOCATION, CALLING_PACKAGE))
238+ .thenReturn(PackageManager.PERMISSION_DENIED);
239+ when(mPackageManager.checkPermission(ACCESS_FINE_LOCATION, CALLING_PACKAGE))
240+ .thenReturn(PackageManager.PERMISSION_GRANTED);
241+
242+ final boolean result = activity.hasPermissionForResult();
243+
244+ assertThat(result).isTrue();
245+ }
246+
247+ @Test
248+ public void hasPermissionForResult_haveBothLocationPermissions_returnTrue() {
249+ WifiDialogActivity activity = spy(Robolectric.setupActivity(WifiDialogActivity.class));
250+ when(activity.getCallingPackage()).thenReturn(CALLING_PACKAGE);
251+ when(activity.getPackageManager()).thenReturn(mPackageManager);
252+ when(mPackageManager.checkPermission(ACCESS_COARSE_LOCATION, CALLING_PACKAGE))
253+ .thenReturn(PackageManager.PERMISSION_GRANTED);
254+ when(mPackageManager.checkPermission(ACCESS_FINE_LOCATION, CALLING_PACKAGE))
255+ .thenReturn(PackageManager.PERMISSION_GRANTED);
256+
257+ final boolean result = activity.hasPermissionForResult();
258+
259+ assertThat(result).isTrue();
260+ }
114261 }