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