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.net.wifi.SoftApConfiguration; 25 import android.net.wifi.WifiConfiguration; 26 27 import androidx.test.ext.junit.runners.AndroidJUnit4; 28 29 import com.android.wifitrackerlib.WifiEntry; 30 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 34 @RunWith(AndroidJUnit4.class) 35 public class WifiUtilsTest { 36 37 @Test testSSID()38 public void testSSID() { 39 assertThat(WifiUtils.isSSIDTooLong("123")).isFalse(); 40 assertThat(WifiUtils.isSSIDTooLong("☎☎☎☎☎☎☎☎☎☎☎☎☎☎☎☎☎")).isTrue(); 41 42 assertThat(WifiUtils.isSSIDTooShort("123")).isFalse(); 43 assertThat(WifiUtils.isSSIDTooShort("")).isTrue(); 44 } 45 46 @Test testPassword()47 public void testPassword() { 48 final String longPassword = "123456789012345678901234567890" 49 + "1234567890123456789012345678901234567890"; 50 assertThat(WifiUtils.isHotspotPasswordValid("123", 51 SoftApConfiguration.SECURITY_TYPE_WPA2_PSK)).isFalse(); 52 assertThat(WifiUtils.isHotspotPasswordValid("12345678", 53 SoftApConfiguration.SECURITY_TYPE_WPA2_PSK)).isTrue(); 54 assertThat(WifiUtils.isHotspotPasswordValid("1234567890", 55 SoftApConfiguration.SECURITY_TYPE_WPA2_PSK)).isTrue(); 56 assertThat(WifiUtils.isHotspotPasswordValid(longPassword, 57 SoftApConfiguration.SECURITY_TYPE_WPA2_PSK)).isFalse(); 58 assertThat(WifiUtils.isHotspotPasswordValid("", 59 SoftApConfiguration.SECURITY_TYPE_WPA2_PSK)).isFalse(); 60 assertThat(WifiUtils.isHotspotPasswordValid("€¥£", 61 SoftApConfiguration.SECURITY_TYPE_WPA2_PSK)).isFalse(); 62 63 // The WPA3_SAE_TRANSITION password limitation should be same as WPA2_PSK 64 assertThat(WifiUtils.isHotspotPasswordValid("123", 65 SoftApConfiguration.SECURITY_TYPE_WPA3_SAE_TRANSITION)).isFalse(); 66 assertThat(WifiUtils.isHotspotPasswordValid("12345678", 67 SoftApConfiguration.SECURITY_TYPE_WPA3_SAE_TRANSITION)).isTrue(); 68 assertThat(WifiUtils.isHotspotPasswordValid("1234567890", 69 SoftApConfiguration.SECURITY_TYPE_WPA3_SAE_TRANSITION)).isTrue(); 70 assertThat(WifiUtils.isHotspotPasswordValid(longPassword, 71 SoftApConfiguration.SECURITY_TYPE_WPA3_SAE_TRANSITION)).isFalse(); 72 assertThat(WifiUtils.isHotspotPasswordValid("", 73 SoftApConfiguration.SECURITY_TYPE_WPA3_SAE_TRANSITION)).isFalse(); 74 assertThat(WifiUtils.isHotspotPasswordValid("€¥£", 75 SoftApConfiguration.SECURITY_TYPE_WPA3_SAE_TRANSITION)).isFalse(); 76 77 // The WA3_SAE password is requested that length > 1 only. 78 assertThat(WifiUtils.isHotspotPasswordValid("", 79 SoftApConfiguration.SECURITY_TYPE_WPA3_SAE)).isFalse(); 80 assertThat(WifiUtils.isHotspotPasswordValid("1", 81 SoftApConfiguration.SECURITY_TYPE_WPA3_SAE)).isTrue(); 82 assertThat(WifiUtils.isHotspotPasswordValid("123", 83 SoftApConfiguration.SECURITY_TYPE_WPA3_SAE)).isTrue(); 84 assertThat(WifiUtils.isHotspotPasswordValid("12345678", 85 SoftApConfiguration.SECURITY_TYPE_WPA3_SAE)).isTrue(); 86 assertThat(WifiUtils.isHotspotPasswordValid("1234567890", 87 SoftApConfiguration.SECURITY_TYPE_WPA3_SAE)).isTrue(); 88 assertThat(WifiUtils.isHotspotPasswordValid(longPassword, 89 SoftApConfiguration.SECURITY_TYPE_WPA3_SAE)).isTrue(); 90 assertThat(WifiUtils.isHotspotPasswordValid("€¥£", 91 SoftApConfiguration.SECURITY_TYPE_WPA3_SAE)).isTrue(); 92 } 93 94 @Test getWifiConfigByWifiEntry_shouldReturnCorrectConfig()95 public void getWifiConfigByWifiEntry_shouldReturnCorrectConfig() { 96 final String testSSID = "WifiUtilsTest"; 97 final WifiEntry wifiEntry = mock(WifiEntry.class); 98 when(wifiEntry.getSsid()).thenReturn(testSSID); 99 100 final WifiConfiguration config = WifiUtils.getWifiConfig(wifiEntry, null /* scanResult */); 101 102 assertThat(config).isNotNull(); 103 assertThat(config.SSID).isEqualTo("\"" + testSSID + "\""); 104 } 105 106 @Test(expected = IllegalArgumentException.class) getWifiConfigWithNullInput_ThrowIllegalArgumentException()107 public void getWifiConfigWithNullInput_ThrowIllegalArgumentException() { 108 WifiConfiguration config = WifiUtils.getWifiConfig(null /* wifiEntry */, 109 null /* scanResult */); 110 } 111 } 112