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.settings.wifi.tether; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Mockito.spy; 22 import static org.mockito.Mockito.when; 23 24 import android.content.Context; 25 import android.net.wifi.SoftApConfiguration; 26 import android.net.wifi.WifiManager; 27 import android.os.Looper; 28 29 import androidx.preference.ListPreference; 30 import androidx.preference.PreferenceManager; 31 import androidx.preference.PreferenceScreen; 32 import androidx.test.core.app.ApplicationProvider; 33 import androidx.test.ext.junit.runners.AndroidJUnit4; 34 35 import org.junit.Before; 36 import org.junit.Rule; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 import org.mockito.Mock; 40 import org.mockito.junit.MockitoJUnit; 41 import org.mockito.junit.MockitoRule; 42 43 @RunWith(AndroidJUnit4.class) 44 public class WifiTetherSecurityPreferenceControllerTest { 45 46 private static final String PREF_KEY = "wifi_tether_security"; 47 private static final String WPA3_SAE = 48 String.valueOf(SoftApConfiguration.SECURITY_TYPE_WPA3_SAE); 49 private static final String WPA3_SAE_TRANSITION = 50 String.valueOf(SoftApConfiguration.SECURITY_TYPE_WPA3_SAE_TRANSITION); 51 private static final String WPA2_PSK = 52 String.valueOf(SoftApConfiguration.SECURITY_TYPE_WPA2_PSK); 53 private static final String NONE = String.valueOf(SoftApConfiguration.SECURITY_TYPE_OPEN); 54 55 @Rule 56 public final MockitoRule mMockitoRule = MockitoJUnit.rule(); 57 @Mock 58 private WifiManager mWifiManager; 59 @Mock 60 private WifiTetherBasePreferenceController.OnTetherConfigUpdateListener mListener; 61 62 private WifiTetherSecurityPreferenceController mController; 63 private ListPreference mPreference; 64 private SoftApConfiguration mConfig; 65 66 @Before setUp()67 public void setUp() { 68 final Context context = spy(ApplicationProvider.getApplicationContext()); 69 mConfig = new SoftApConfiguration.Builder().setSsid("test_1234") 70 .setPassphrase(null, SoftApConfiguration.SECURITY_TYPE_OPEN).build(); 71 when(context.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager); 72 when(mWifiManager.getSoftApConfiguration()).thenReturn(mConfig); 73 74 mController = new WifiTetherSecurityPreferenceController(context, mListener); 75 if (Looper.myLooper() == null) { 76 Looper.prepare(); 77 } 78 final PreferenceManager preferenceManager = new PreferenceManager(context); 79 final PreferenceScreen screen = preferenceManager.createPreferenceScreen(context); 80 mPreference = new ListPreference(context); 81 mPreference.setKey(PREF_KEY); 82 screen.addPreference(mPreference); 83 mController.displayPreference(screen); 84 } 85 86 @Test onPreferenceChange_toWpa3Sae_shouldUpdateSecurityValue()87 public void onPreferenceChange_toWpa3Sae_shouldUpdateSecurityValue() { 88 mController.onPreferenceChange(mPreference, WPA3_SAE); 89 90 assertThat(mController.getSecurityType()) 91 .isEqualTo(SoftApConfiguration.SECURITY_TYPE_WPA3_SAE); 92 assertThat(mPreference.getSummary().toString()).isEqualTo("WPA3-Personal"); 93 } 94 95 @Test onPreferenceChange_toWpa3SaeTransition_shouldUpdateSecurityValue()96 public void onPreferenceChange_toWpa3SaeTransition_shouldUpdateSecurityValue() { 97 mController.onPreferenceChange(mPreference, WPA3_SAE_TRANSITION); 98 99 assertThat(mController.getSecurityType()) 100 .isEqualTo(SoftApConfiguration.SECURITY_TYPE_WPA3_SAE_TRANSITION); 101 assertThat(mPreference.getSummary().toString()).isEqualTo("WPA2/WPA3-Personal"); 102 } 103 104 @Test onPreferenceChange_toWpa2Psk_shouldUpdateSecurityValue()105 public void onPreferenceChange_toWpa2Psk_shouldUpdateSecurityValue() { 106 mController.onPreferenceChange(mPreference, WPA2_PSK); 107 108 assertThat(mController.getSecurityType()) 109 .isEqualTo(SoftApConfiguration.SECURITY_TYPE_WPA2_PSK); 110 assertThat(mPreference.getSummary().toString()).isEqualTo("WPA2-Personal"); 111 } 112 113 @Test onPreferenceChange_toNone_shouldUpdateSecurityValue()114 public void onPreferenceChange_toNone_shouldUpdateSecurityValue() { 115 mController.onPreferenceChange(mPreference, NONE); 116 117 assertThat(mController.getSecurityType()) 118 .isEqualTo(SoftApConfiguration.SECURITY_TYPE_OPEN); 119 assertThat(mPreference.getSummary().toString()).isEqualTo("None"); 120 } 121 122 @Test updateDisplay_toWpa3Sae_shouldUpdateSecurityValue()123 public void updateDisplay_toWpa3Sae_shouldUpdateSecurityValue() { 124 SoftApConfiguration config = new SoftApConfiguration.Builder(mConfig) 125 .setPassphrase("test_password", 126 SoftApConfiguration.SECURITY_TYPE_WPA3_SAE).build(); 127 when(mWifiManager.getSoftApConfiguration()).thenReturn(config); 128 129 mController.updateDisplay(); 130 131 assertThat(mController.getSecurityType()) 132 .isEqualTo(SoftApConfiguration.SECURITY_TYPE_WPA3_SAE); 133 assertThat(mPreference.getSummary().toString()).isEqualTo("WPA3-Personal"); 134 } 135 136 @Test updateDisplay_toWpa3SaeTransition_shouldUpdateSecurityValue()137 public void updateDisplay_toWpa3SaeTransition_shouldUpdateSecurityValue() { 138 SoftApConfiguration config = new SoftApConfiguration.Builder(mConfig) 139 .setPassphrase("test_password", 140 SoftApConfiguration.SECURITY_TYPE_WPA3_SAE_TRANSITION).build(); 141 when(mWifiManager.getSoftApConfiguration()).thenReturn(config); 142 143 mController.updateDisplay(); 144 145 assertThat(mController.getSecurityType()) 146 .isEqualTo(SoftApConfiguration.SECURITY_TYPE_WPA3_SAE_TRANSITION); 147 assertThat(mPreference.getSummary().toString()).isEqualTo("WPA2/WPA3-Personal"); 148 } 149 150 @Test updateDisplay_toWpa2Psk_shouldUpdateSecurityValue()151 public void updateDisplay_toWpa2Psk_shouldUpdateSecurityValue() { 152 SoftApConfiguration config = new SoftApConfiguration.Builder(mConfig) 153 .setPassphrase("test_password", 154 SoftApConfiguration.SECURITY_TYPE_WPA2_PSK).build(); 155 when(mWifiManager.getSoftApConfiguration()).thenReturn(config); 156 157 mController.updateDisplay(); 158 159 assertThat(mController.getSecurityType()) 160 .isEqualTo(SoftApConfiguration.SECURITY_TYPE_WPA2_PSK); 161 assertThat(mPreference.getSummary().toString()).isEqualTo("WPA2-Personal"); 162 } 163 164 @Test updateDisplay_toNone_shouldUpdateSecurityValue()165 public void updateDisplay_toNone_shouldUpdateSecurityValue() { 166 SoftApConfiguration config = new SoftApConfiguration.Builder(mConfig) 167 .setPassphrase(null, SoftApConfiguration.SECURITY_TYPE_OPEN).build(); 168 when(mWifiManager.getSoftApConfiguration()).thenReturn(config); 169 170 mController.updateDisplay(); 171 172 assertThat(mController.getSecurityType()) 173 .isEqualTo(SoftApConfiguration.SECURITY_TYPE_OPEN); 174 assertThat(mPreference.getSummary().toString()).isEqualTo("None"); 175 } 176 177 @Test updateDisplay_toWpa3SaeButNotSupportWpa3_shouldBeDefaultToWpa2()178 public void updateDisplay_toWpa3SaeButNotSupportWpa3_shouldBeDefaultToWpa2() { 179 mController.mIsWpa3Supported = false; 180 SoftApConfiguration config = new SoftApConfiguration.Builder(mConfig) 181 .setPassphrase("test_password", 182 SoftApConfiguration.SECURITY_TYPE_WPA3_SAE).build(); 183 when(mWifiManager.getSoftApConfiguration()).thenReturn(config); 184 185 mController.updateDisplay(); 186 187 assertThat(mController.getSecurityType()) 188 .isEqualTo(SoftApConfiguration.SECURITY_TYPE_WPA2_PSK); 189 assertThat(mPreference.getSummary().toString()).isEqualTo("WPA2-Personal"); 190 } 191 192 @Test updateDisplay_toWpa3SaeTransitionButNotSupportWpa3_shouldBeDefaultToWpa2()193 public void updateDisplay_toWpa3SaeTransitionButNotSupportWpa3_shouldBeDefaultToWpa2() { 194 mController.mIsWpa3Supported = false; 195 SoftApConfiguration config = new SoftApConfiguration.Builder(mConfig) 196 .setPassphrase("test_password", 197 SoftApConfiguration.SECURITY_TYPE_WPA3_SAE_TRANSITION).build(); 198 when(mWifiManager.getSoftApConfiguration()).thenReturn(config); 199 200 mController.updateDisplay(); 201 202 assertThat(mController.getSecurityType()) 203 .isEqualTo(SoftApConfiguration.SECURITY_TYPE_WPA2_PSK); 204 assertThat(mPreference.getSummary().toString()).isEqualTo("WPA2-Personal"); 205 } 206 } 207