packages/apps/Settings
Revision | 69cae4fefbf141beb57c2a2afbf0e5ed3b358afb (tree) |
---|---|
Zeit | 2021-08-16 13:15:13 |
Autor | Weng Su <wengsu@goog...> |
Commiter | Android Build Coastguard Worker |
[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)
@@ -16,9 +16,13 @@ | ||
16 | 16 | |
17 | 17 | package com.android.settings.wifi; |
18 | 18 | |
19 | +import static android.Manifest.permission.ACCESS_COARSE_LOCATION; | |
20 | +import static android.Manifest.permission.ACCESS_FINE_LOCATION; | |
21 | + | |
19 | 22 | import android.app.Activity; |
20 | 23 | import android.content.DialogInterface; |
21 | 24 | import android.content.Intent; |
25 | +import android.content.pm.PackageManager; | |
22 | 26 | import android.net.NetworkInfo; |
23 | 27 | import android.net.wifi.WifiConfiguration; |
24 | 28 | import android.net.wifi.WifiManager; |
@@ -53,10 +57,12 @@ public class WifiDialogActivity extends Activity implements WifiDialog.WifiDialo | ||
53 | 57 | |
54 | 58 | public static final String KEY_WIFI_CONFIGURATION = "wifi_configuration"; |
55 | 59 | |
56 | - private static final int RESULT_CONNECTED = RESULT_FIRST_USER; | |
60 | + @VisibleForTesting | |
61 | + static final int RESULT_CONNECTED = RESULT_FIRST_USER; | |
57 | 62 | private static final int RESULT_FORGET = RESULT_FIRST_USER + 1; |
58 | 63 | |
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; | |
60 | 66 | |
61 | 67 | private WifiDialog mDialog; |
62 | 68 |
@@ -156,17 +162,22 @@ public class WifiDialogActivity extends Activity implements WifiDialog.WifiDialo | ||
156 | 162 | } |
157 | 163 | } |
158 | 164 | |
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(); | |
160 | 172 | if (accessPoint != null) { |
161 | 173 | Bundle accessPointState = new Bundle(); |
162 | 174 | accessPoint.saveWifiState(accessPointState); |
163 | - resultData.putExtra(KEY_ACCESS_POINT_STATE, accessPointState); | |
175 | + result.putExtra(KEY_ACCESS_POINT_STATE, accessPointState); | |
164 | 176 | } |
165 | 177 | if (config != null) { |
166 | - resultData.putExtra(KEY_WIFI_CONFIGURATION, config); | |
178 | + result.putExtra(KEY_WIFI_CONFIGURATION, config); | |
167 | 179 | } |
168 | - setResult(RESULT_CONNECTED, resultData); | |
169 | - finish(); | |
180 | + return result; | |
170 | 181 | } |
171 | 182 | |
172 | 183 | @Override |
@@ -192,9 +203,35 @@ public class WifiDialogActivity extends Activity implements WifiDialog.WifiDialo | ||
192 | 203 | if (resultCode != RESULT_OK) { |
193 | 204 | return; |
194 | 205 | } |
195 | - | |
196 | - setResult(RESULT_CONNECTED, data); | |
206 | + if (hasPermissionForResult()) { | |
207 | + setResult(RESULT_CONNECTED, data); | |
208 | + } else { | |
209 | + setResult(RESULT_CONNECTED); | |
210 | + } | |
197 | 211 | finish(); |
198 | 212 | } |
199 | 213 | } |
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 | + } | |
200 | 237 | } |
@@ -16,17 +16,31 @@ | ||
16 | 16 | |
17 | 17 | package com.android.settings.wifi; |
18 | 18 | |
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 | + | |
19 | 26 | import static com.google.common.truth.Truth.assertThat; |
20 | 27 | |
28 | +import static org.mockito.ArgumentMatchers.any; | |
21 | 29 | 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; | |
22 | 33 | |
23 | 34 | import android.content.Intent; |
35 | +import android.content.pm.PackageManager; | |
24 | 36 | import android.net.wifi.WifiConfiguration; |
37 | +import android.net.wifi.WifiManager; | |
25 | 38 | |
26 | 39 | import com.android.settings.R; |
27 | 40 | import com.android.settings.testutils.shadow.ShadowAlertDialogCompat; |
28 | 41 | import com.android.settings.testutils.shadow.ShadowConnectivityManager; |
29 | 42 | import com.android.settings.testutils.shadow.ShadowWifiManager; |
43 | +import com.android.settingslib.wifi.AccessPoint; | |
30 | 44 | |
31 | 45 | import com.google.android.setupcompat.util.WizardManagerHelper; |
32 | 46 |
@@ -48,13 +62,30 @@ import org.robolectric.util.ReflectionHelpers; | ||
48 | 62 | }) |
49 | 63 | public class WifiDialogActivityTest { |
50 | 64 | |
65 | + private static final String CALLING_PACKAGE = "calling_package"; | |
51 | 66 | 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; | |
52 | 80 | @Mock |
53 | 81 | private WifiConfigController mController; |
54 | 82 | |
55 | 83 | @Before |
56 | 84 | public void setUp() { |
57 | 85 | MockitoAnnotations.initMocks(this); |
86 | + when(mWifiDialog.getController()).thenReturn(mController); | |
87 | + when(mController.getConfig()).thenReturn(mWifiConfiguration); | |
88 | + when(mController.getAccessPoint()).thenReturn(mAccessPoint); | |
58 | 89 | |
59 | 90 | WifiConfiguration wifiConfig = new WifiConfiguration(); |
60 | 91 | wifiConfig.SSID = AP1_SSID; |
@@ -75,6 +106,29 @@ public class WifiDialogActivityTest { | ||
75 | 106 | } |
76 | 107 | |
77 | 108 | @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 | |
78 | 132 | public void onSubmit_whenConnectForCallerIsFalse_shouldNotConnectToNetwork() { |
79 | 133 | WifiDialogActivity activity = |
80 | 134 | Robolectric.buildActivity( |
@@ -111,4 +165,97 @@ public class WifiDialogActivityTest { | ||
111 | 165 | assertThat(dialog.getContext().getThemeResId()) |
112 | 166 | .isEqualTo(R.style.SuwAlertDialogThemeCompat_Light); |
113 | 167 | } |
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 | + } | |
114 | 261 | } |