1 /* 2 * Copyright (C) 2020 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.settings.wifi; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Mockito.mock; 22 import static org.mockito.Mockito.when; 23 24 import android.content.Context; 25 import android.content.res.Resources; 26 import android.net.TetheringManager; 27 import android.net.wifi.SoftApConfiguration; 28 import android.net.wifi.WifiConfiguration; 29 import android.net.wifi.WifiManager; 30 31 import androidx.test.core.app.ApplicationProvider; 32 import androidx.test.ext.junit.runners.AndroidJUnit4; 33 34 import com.android.settings.R; 35 import com.android.wifitrackerlib.WifiEntry; 36 37 import org.junit.Before; 38 import org.junit.Ignore; 39 import org.junit.Rule; 40 import org.junit.Test; 41 import org.junit.runner.RunWith; 42 import org.mockito.Mock; 43 import org.mockito.Spy; 44 import org.mockito.junit.MockitoJUnit; 45 import org.mockito.junit.MockitoRule; 46 47 @RunWith(AndroidJUnit4.class) 48 public class WifiUtilsTest { 49 50 static final String[] WIFI_REGEXS = {"wifi_regexs"}; 51 52 @Rule 53 public final MockitoRule mMockitoRule = MockitoJUnit.rule(); 54 @Spy 55 Context mContext = ApplicationProvider.getApplicationContext(); 56 @Mock 57 Resources mResources; 58 @Mock 59 WifiManager mWifiManager; 60 @Mock 61 TetheringManager mTetheringManager; 62 63 @Before setUp()64 public void setUp() { 65 when(mContext.getResources()).thenReturn(mResources); 66 when(mResources.getBoolean(R.bool.config_show_wifi_hotspot_settings)).thenReturn(true); 67 when(mContext.getSystemService(WifiManager.class)).thenReturn(mWifiManager); 68 when(mContext.getSystemService(TetheringManager.class)).thenReturn(mTetheringManager); 69 when(mTetheringManager.getTetherableWifiRegexs()).thenReturn(WIFI_REGEXS); 70 } 71 72 @Test testSSID()73 public void testSSID() { 74 assertThat(WifiUtils.isSSIDTooLong("123")).isFalse(); 75 assertThat(WifiUtils.isSSIDTooLong("☎☎☎☎☎☎☎☎☎☎☎☎☎☎☎☎☎")).isTrue(); 76 77 assertThat(WifiUtils.isSSIDTooShort("123")).isFalse(); 78 assertThat(WifiUtils.isSSIDTooShort("")).isTrue(); 79 } 80 81 @Test testPassword()82 public void testPassword() { 83 final String longPassword = "123456789012345678901234567890" 84 + "1234567890123456789012345678901234567890"; 85 assertThat(WifiUtils.isHotspotPasswordValid("123", 86 SoftApConfiguration.SECURITY_TYPE_WPA2_PSK)).isFalse(); 87 assertThat(WifiUtils.isHotspotPasswordValid("12345678", 88 SoftApConfiguration.SECURITY_TYPE_WPA2_PSK)).isTrue(); 89 assertThat(WifiUtils.isHotspotPasswordValid("1234567890", 90 SoftApConfiguration.SECURITY_TYPE_WPA2_PSK)).isTrue(); 91 assertThat(WifiUtils.isHotspotPasswordValid(longPassword, 92 SoftApConfiguration.SECURITY_TYPE_WPA2_PSK)).isFalse(); 93 assertThat(WifiUtils.isHotspotPasswordValid("", 94 SoftApConfiguration.SECURITY_TYPE_WPA2_PSK)).isFalse(); 95 assertThat(WifiUtils.isHotspotPasswordValid("€¥£", 96 SoftApConfiguration.SECURITY_TYPE_WPA2_PSK)).isFalse(); 97 98 // The WPA3_SAE_TRANSITION password limitation should be same as WPA2_PSK 99 assertThat(WifiUtils.isHotspotPasswordValid("123", 100 SoftApConfiguration.SECURITY_TYPE_WPA3_SAE_TRANSITION)).isFalse(); 101 assertThat(WifiUtils.isHotspotPasswordValid("12345678", 102 SoftApConfiguration.SECURITY_TYPE_WPA3_SAE_TRANSITION)).isTrue(); 103 assertThat(WifiUtils.isHotspotPasswordValid("1234567890", 104 SoftApConfiguration.SECURITY_TYPE_WPA3_SAE_TRANSITION)).isTrue(); 105 assertThat(WifiUtils.isHotspotPasswordValid(longPassword, 106 SoftApConfiguration.SECURITY_TYPE_WPA3_SAE_TRANSITION)).isFalse(); 107 assertThat(WifiUtils.isHotspotPasswordValid("", 108 SoftApConfiguration.SECURITY_TYPE_WPA3_SAE_TRANSITION)).isFalse(); 109 assertThat(WifiUtils.isHotspotPasswordValid("€¥£", 110 SoftApConfiguration.SECURITY_TYPE_WPA3_SAE_TRANSITION)).isFalse(); 111 112 // The WA3_SAE password is requested that length > 1 only. 113 assertThat(WifiUtils.isHotspotPasswordValid("", 114 SoftApConfiguration.SECURITY_TYPE_WPA3_SAE)).isFalse(); 115 assertThat(WifiUtils.isHotspotPasswordValid("1", 116 SoftApConfiguration.SECURITY_TYPE_WPA3_SAE)).isTrue(); 117 assertThat(WifiUtils.isHotspotPasswordValid("123", 118 SoftApConfiguration.SECURITY_TYPE_WPA3_SAE)).isTrue(); 119 assertThat(WifiUtils.isHotspotPasswordValid("12345678", 120 SoftApConfiguration.SECURITY_TYPE_WPA3_SAE)).isTrue(); 121 assertThat(WifiUtils.isHotspotPasswordValid("1234567890", 122 SoftApConfiguration.SECURITY_TYPE_WPA3_SAE)).isTrue(); 123 assertThat(WifiUtils.isHotspotPasswordValid(longPassword, 124 SoftApConfiguration.SECURITY_TYPE_WPA3_SAE)).isTrue(); 125 assertThat(WifiUtils.isHotspotPasswordValid("€¥£", 126 SoftApConfiguration.SECURITY_TYPE_WPA3_SAE)).isTrue(); 127 } 128 129 @Test getWifiConfigByWifiEntry_shouldReturnCorrectConfig()130 public void getWifiConfigByWifiEntry_shouldReturnCorrectConfig() { 131 final String testSSID = "WifiUtilsTest"; 132 final WifiEntry wifiEntry = mock(WifiEntry.class); 133 when(wifiEntry.getSsid()).thenReturn(testSSID); 134 135 final WifiConfiguration config = WifiUtils.getWifiConfig(wifiEntry, null /* scanResult */); 136 137 assertThat(config).isNotNull(); 138 assertThat(config.SSID).isEqualTo("\"" + testSSID + "\""); 139 } 140 141 @Test(expected = IllegalArgumentException.class) getWifiConfigWithNullInput_ThrowIllegalArgumentException()142 public void getWifiConfigWithNullInput_ThrowIllegalArgumentException() { 143 WifiConfiguration config = WifiUtils.getWifiConfig(null /* wifiEntry */, 144 null /* scanResult */); 145 } 146 147 @Ignore 148 @Test checkShowWifiHotspot_allReady_returnTrue()149 public void checkShowWifiHotspot_allReady_returnTrue() { 150 assertThat(WifiUtils.checkShowWifiHotspot(mContext)).isTrue(); 151 } 152 153 @Test checkShowWifiHotspot_contextIsNull_returnFalse()154 public void checkShowWifiHotspot_contextIsNull_returnFalse() { 155 assertThat(WifiUtils.checkShowWifiHotspot(null)).isFalse(); 156 } 157 158 @Test checkShowWifiHotspot_configIsNotShow_returnFalse()159 public void checkShowWifiHotspot_configIsNotShow_returnFalse() { 160 when(mResources.getBoolean(R.bool.config_show_wifi_hotspot_settings)).thenReturn(false); 161 162 assertThat(WifiUtils.checkShowWifiHotspot(mContext)).isFalse(); 163 } 164 165 @Test checkShowWifiHotspot_wifiManagerIsNull_returnFalse()166 public void checkShowWifiHotspot_wifiManagerIsNull_returnFalse() { 167 when(mContext.getSystemService(WifiManager.class)).thenReturn(null); 168 169 assertThat(WifiUtils.checkShowWifiHotspot(mContext)).isFalse(); 170 } 171 172 @Test checkShowWifiHotspot_tetheringManagerIsNull_returnFalse()173 public void checkShowWifiHotspot_tetheringManagerIsNull_returnFalse() { 174 when(mContext.getSystemService(TetheringManager.class)).thenReturn(null); 175 176 assertThat(WifiUtils.checkShowWifiHotspot(mContext)).isFalse(); 177 } 178 179 @Test checkShowWifiHotspot_wifiRegexsIsEmpty_returnFalse()180 public void checkShowWifiHotspot_wifiRegexsIsEmpty_returnFalse() { 181 when(mTetheringManager.getTetherableWifiRegexs()).thenReturn(null); 182 183 assertThat(WifiUtils.checkShowWifiHotspot(mContext)).isFalse(); 184 } 185 186 @Test canShowWifiHotspot_cachedIsReady_returnCached()187 public void canShowWifiHotspot_cachedIsReady_returnCached() { 188 WifiUtils.setCanShowWifiHotspotCached(true); 189 190 assertThat(WifiUtils.canShowWifiHotspot(null)).isTrue(); 191 192 WifiUtils.setCanShowWifiHotspotCached(false); 193 194 assertThat(WifiUtils.canShowWifiHotspot(null)).isFalse(); 195 } 196 } 197