1 /* 2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.cts.deviceowner; 18 19 import static com.android.compatibility.common.util.WifiConfigCreator.SECURITY_TYPE_NONE; 20 21 import static com.google.common.truth.Truth.assertWithMessage; 22 23 import android.Manifest; 24 import android.content.pm.PackageManager; 25 import android.net.wifi.WifiConfiguration; 26 import android.os.SystemClock; 27 import android.util.Log; 28 29 import com.android.compatibility.common.util.SystemUtil; 30 31 import java.util.List; 32 import java.util.concurrent.TimeUnit; 33 34 public class WifiNetworkConfigurationWithoutFineLocationPermissionTest extends BaseDeviceOwnerTest { 35 private static final String TAG = "WifiNetworkConfigurationWithoutFineLocationPermissionTest"; 36 37 // Unique SSID to use for this test (max SSID length is 32) 38 private static final String NETWORK_SSID = "com.android.cts.abcdefghijklmnop"; 39 private static final int INVALID_NETWORK_ID = -1; 40 41 // Time duration to allow before assuming that a WiFi operation failed and ceasing to wait. 42 private static final long UPDATE_TIMEOUT_MS = TimeUnit.MINUTES.toMillis(5); 43 private static final long UPDATE_INTERVAL_MS = TimeUnit.SECONDS.toMillis(1); 44 45 @Override setUp()46 public void setUp() throws Exception { 47 super.setUp(); 48 49 // WiFi is supposed to be a prerequisite of CTS but sometimes it's not enabled 50 // for some unknown reason. Check it here just in case. 51 if (!mWifiManager.isWifiEnabled()) { 52 Log.d(TAG, "Enabling wifi using shell"); 53 SystemUtil.runShellCommand("svc wifi enable"); 54 awaitWifiEnabled(); 55 Log.d(TAG, "Done: " + mWifiManager.isWifiEnabled()); 56 } 57 } 58 testAddAndRetrieveCallerConfiguredNetworks()59 public void testAddAndRetrieveCallerConfiguredNetworks() throws Exception { 60 assertWithMessage("wifi is enabled").that(mWifiManager.isWifiEnabled()).isTrue(); 61 assertWithMessage("permission status (denied=%s) for %s on user %s", 62 PackageManager.PERMISSION_DENIED, Manifest.permission.ACCESS_FINE_LOCATION, mUserId) 63 .that(mContext.checkSelfPermission( 64 Manifest.permission.ACCESS_FINE_LOCATION)) 65 .isEqualTo(PackageManager.PERMISSION_DENIED); 66 67 int netId = mWifiConfigCreator.addNetwork(NETWORK_SSID, /* hidden */ false, 68 SECURITY_TYPE_NONE, /* password */ null); 69 assertWithMessage("id of added network").that(netId).isNotEqualTo(INVALID_NETWORK_ID); 70 71 try { 72 List<WifiConfiguration> configs = mWifiManager.getCallerConfiguredNetworks(); 73 assertWithMessage("configured networks").that(configs).isNotEmpty(); 74 assertWithMessage("SSID of configured networks").that(configs.get(0).SSID) 75 .isEqualTo('"' + NETWORK_SSID + '"'); 76 } finally { 77 Log.d(TAG, "Removing network " + netId); 78 mWifiManager.removeNetwork(netId); 79 } 80 } 81 awaitWifiEnabled()82 private void awaitWifiEnabled() { 83 for (int probes = 0; probes * UPDATE_INTERVAL_MS <= UPDATE_TIMEOUT_MS; probes++) { 84 if (probes != 0) { 85 SystemClock.sleep(UPDATE_INTERVAL_MS); 86 } 87 if (mWifiManager.isWifiEnabled()) { 88 return; 89 } 90 } 91 fail("Waited too long for wifi enabled"); 92 } 93 } 94