• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import static org.junit.Assume.assumeTrue;
23 
24 import android.Manifest;
25 import android.content.pm.PackageManager;
26 import android.net.wifi.WifiConfiguration;
27 import android.os.SystemClock;
28 import android.util.Log;
29 
30 import com.android.compatibility.common.util.SystemUtil;
31 
32 import java.util.List;
33 import java.util.concurrent.TimeUnit;
34 
35 public class WifiNetworkConfigurationWithoutFineLocationPermissionTest extends BaseDeviceOwnerTest {
36     private static final String TAG = "WifiNetworkConfigurationWithoutFineLocationPermissionTest";
37 
38     // Unique SSID to use for this test (max SSID length is 32)
39     private static final String NETWORK_SSID = "com.android.cts.abcdefghijklmnop";
40     private static final int INVALID_NETWORK_ID = -1;
41 
42     // Time duration to allow before assuming that a WiFi operation failed and ceasing to wait.
43     private static final long UPDATE_TIMEOUT_MS = TimeUnit.MINUTES.toMillis(5);
44     private static final long UPDATE_INTERVAL_MS = TimeUnit.SECONDS.toMillis(1);
45 
46     @SuppressWarnings("JUnit4ClassUsedInJUnit3")
47     @Override
setUp()48     public void setUp() throws Exception {
49         super.setUp();
50 
51         // Skip if Wifi feature is disabled
52         assumeTrue(mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI));
53 
54         // WiFi is supposed to be a prerequisite of CTS but sometimes it's not enabled
55         // for some unknown reason. Check it here just in case.
56         if (!mWifiManager.isWifiEnabled()) {
57             Log.d(TAG, "Enabling wifi using shell");
58             SystemUtil.runShellCommand("svc wifi enable");
59             awaitWifiEnabled();
60             Log.d(TAG, "Done: " + mWifiManager.isWifiEnabled());
61         }
62     }
63 
testAddAndRetrieveCallerConfiguredNetworks()64     public void testAddAndRetrieveCallerConfiguredNetworks() throws Exception {
65         assertWithMessage("wifi is enabled").that(mWifiManager.isWifiEnabled()).isTrue();
66         assertWithMessage("permission status (denied=%s) for %s on user %s",
67                 PackageManager.PERMISSION_DENIED, Manifest.permission.ACCESS_FINE_LOCATION, mUserId)
68                         .that(mContext.checkSelfPermission(
69                                         Manifest.permission.ACCESS_FINE_LOCATION))
70                         .isEqualTo(PackageManager.PERMISSION_DENIED);
71 
72         int netId = mWifiConfigCreator.addNetwork(NETWORK_SSID, /* hidden */ false,
73                 SECURITY_TYPE_NONE, /* password */ null);
74         assertWithMessage("id of added network").that(netId).isNotEqualTo(INVALID_NETWORK_ID);
75 
76         try {
77             List<WifiConfiguration> configs = mWifiManager.getCallerConfiguredNetworks();
78             assertWithMessage("configured networks").that(configs).isNotEmpty();
79             assertWithMessage("SSID of configured networks").that(configs.get(0).SSID)
80                     .isEqualTo('"' + NETWORK_SSID + '"');
81         } finally {
82             Log.d(TAG, "Removing network " + netId);
83             mWifiManager.removeNetwork(netId);
84         }
85     }
86 
awaitWifiEnabled()87     private void awaitWifiEnabled()  {
88         for (int probes = 0; probes * UPDATE_INTERVAL_MS <= UPDATE_TIMEOUT_MS; probes++) {
89             if (probes != 0) {
90                 SystemClock.sleep(UPDATE_INTERVAL_MS);
91             }
92             if (mWifiManager.isWifiEnabled()) {
93                 return;
94             }
95         }
96         fail("Waited too long for wifi enabled");
97     }
98 }
99