1 /* 2 * Copyright (C) 2016 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.server.wifi; 18 19 import static org.junit.Assert.*; 20 import static org.mockito.Mockito.*; 21 22 import android.net.IpConfiguration; 23 import android.net.wifi.WifiConfiguration; 24 import android.os.Process; 25 26 import androidx.test.filters.SmallTest; 27 28 import com.android.server.wifi.util.WifiPermissionsUtil; 29 30 import org.junit.After; 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.mockito.Mock; 34 import org.mockito.MockitoAnnotations; 35 36 import java.io.ByteArrayOutputStream; 37 import java.io.DataOutputStream; 38 import java.io.FileDescriptor; 39 import java.io.IOException; 40 import java.io.OutputStreamWriter; 41 import java.io.PrintWriter; 42 import java.io.StringWriter; 43 import java.util.ArrayList; 44 import java.util.HashMap; 45 import java.util.List; 46 import java.util.Map; 47 import java.util.Random; 48 49 /** 50 * Unit tests for {@link com.android.server.wifi.WifiBackupRestore}. 51 */ 52 @SmallTest 53 public class WifiBackupRestoreTest extends WifiBaseTest { 54 55 private static final String WIFI_BACKUP_DATA_WITH_UNSUPPORTED_TAG = 56 "<?xml version='1.0' encoding='utf-8' standalone='yes' ?>\n" 57 + "<WifiBackupData>\n" 58 + "<int name=\"Version\" value=\"1\" />\n" 59 + "<NetworkList>\n" 60 + "<Network>\n" 61 + "<WifiConfiguration>\n" 62 + "<string name=\"ConfigKey\">"" + WifiConfigurationTestUtil.TEST_SSID 63 + ""NONE</string>\n" 64 + "<string name=\"SSID\">"" + WifiConfigurationTestUtil.TEST_SSID 65 + ""</string>\n" 66 + "<null name=\"BSSID\" />\n" 67 + "<null name=\"PreSharedKey\" />\n" 68 + "<null name=\"WEPKeys\" />\n" 69 + "<int name=\"WEPTxKeyIndex\" value=\"0\" />\n" 70 + "<boolean name=\"HiddenSSID\" value=\"false\" />\n" 71 + "<boolean name=\"RequirePMF\" value=\"false\" />\n" 72 + "<byte-array name=\"AllowedKeyMgmt\" num=\"1\">01</byte-array>\n" 73 + "<byte-array name=\"AllowedProtocols\" num=\"1\">03</byte-array>\n" 74 + "<byte-array name=\"AllowedAuthAlgos\" num=\"1\">01</byte-array>\n" 75 + "<byte-array name=\"AllowedGroupCiphers\" num=\"1\">0f</byte-array>\n" 76 + "<byte-array name=\"AllowedPairwiseCiphers\" num=\"1\">06</byte-array>\n" 77 + "<boolean name=\"Shared\" value=\"true\" />\n" 78 + "<null name=\"SimSlot\" />\n" 79 + "</WifiConfiguration>\n" 80 + "<IpConfiguration>\n" 81 + "<string name=\"IpAssignment\">DHCP</string>\n" 82 + "<string name=\"ProxySettings\">NONE</string>\n" 83 + "</IpConfiguration>\n" 84 + "</Network>\n" 85 + "</NetworkList>\n" 86 + "</WifiBackupData>\n"; 87 88 // |AllowedKeyMgmt|, |AllowedProtocols|, |AllowedAuthAlgorithms|, |AllowedGroupCiphers| and 89 // |AllowedPairwiseCiphers| fields have invalid values in them. 90 // NOTE: The byte values are encoded in little endian 91 private static final String WIFI_BACKUP_DATA_WITH_UNSUPPORTED_VALUES_IN_BITSETS = 92 "<?xml version='1.0' encoding='utf-8' standalone='yes' ?>\n" 93 + "<WifiBackupData>\n" 94 + "<int name=\"Version\" value=\"1\" />\n" 95 + "<NetworkList>\n" 96 + "<Network>\n" 97 + "<WifiConfiguration>\n" 98 + "<string name=\"ConfigKey\">"" + WifiConfigurationTestUtil.TEST_SSID 99 + ""NONE</string>\n" 100 + "<string name=\"SSID\">"" + WifiConfigurationTestUtil.TEST_SSID 101 + ""</string>\n" 102 + "<null name=\"BSSID\" />\n" 103 + "<null name=\"PreSharedKey\" />\n" 104 + "<null name=\"WEPKeys\" />\n" 105 + "<int name=\"WEPTxKeyIndex\" value=\"0\" />\n" 106 + "<boolean name=\"HiddenSSID\" value=\"false\" />\n" 107 + "<boolean name=\"RequirePMF\" value=\"false\" />\n" 108 // Valid Value: 01 109 + "<byte-array name=\"AllowedKeyMgmt\" num=\"3\">010002</byte-array>\n" 110 // Valid Value: 03 111 + "<byte-array name=\"AllowedProtocols\" num=\"1\">13</byte-array>\n" 112 // Valid Value: 01 113 + "<byte-array name=\"AllowedAuthAlgos\" num=\"1\">11</byte-array>\n" 114 // Valid Value: 0f 115 + "<byte-array name=\"AllowedGroupCiphers\" num=\"1\">8f</byte-array>\n" 116 // Valid Value: 06 117 + "<byte-array name=\"AllowedPairwiseCiphers\" num=\"1\">26</byte-array>\n" 118 + "<boolean name=\"Shared\" value=\"true\" />\n" 119 + "<null name=\"SimSlot\" />\n" 120 + "</WifiConfiguration>\n" 121 + "<IpConfiguration>\n" 122 + "<string name=\"IpAssignment\">DHCP</string>\n" 123 + "<string name=\"ProxySettings\">NONE</string>\n" 124 + "</IpConfiguration>\n" 125 + "</Network>\n" 126 + "</NetworkList>\n" 127 + "</WifiBackupData>\n"; 128 129 private static final String WIFI_BACKUP_DATA_V1_0 = 130 "<?xml version='1.0' encoding='utf-8' standalone='yes' ?>\n" 131 + "<WifiBackupData>\n" 132 + "<float name=\"Version\" value=\"1.0\" />\n" 133 + "<NetworkList>\n" 134 + "<Network>\n" 135 + "<WifiConfiguration>\n" 136 + "<string name=\"ConfigKey\">"" + WifiConfigurationTestUtil.TEST_SSID 137 + ""NONE</string>\n" 138 + "<string name=\"SSID\">"" + WifiConfigurationTestUtil.TEST_SSID 139 + ""</string>\n" 140 + "<null name=\"BSSID\" />\n" 141 + "<null name=\"PreSharedKey\" />\n" 142 + "<null name=\"WEPKeys\" />\n" 143 + "<int name=\"WEPTxKeyIndex\" value=\"0\" />\n" 144 + "<boolean name=\"HiddenSSID\" value=\"false\" />\n" 145 + "<boolean name=\"RequirePMF\" value=\"false\" />\n" 146 + "<byte-array name=\"AllowedKeyMgmt\" num=\"1\">01</byte-array>\n" 147 + "<byte-array name=\"AllowedProtocols\" num=\"1\">03</byte-array>\n" 148 + "<byte-array name=\"AllowedAuthAlgos\" num=\"1\">01</byte-array>\n" 149 + "<byte-array name=\"AllowedGroupCiphers\" num=\"1\">0f</byte-array>\n" 150 + "<byte-array name=\"AllowedPairwiseCiphers\" num=\"1\">06</byte-array>\n" 151 + "<boolean name=\"Shared\" value=\"true\" />\n" 152 + "</WifiConfiguration>\n" 153 + "<IpConfiguration>\n" 154 + "<string name=\"IpAssignment\">DHCP</string>\n" 155 + "<string name=\"ProxySettings\">NONE</string>\n" 156 + "</IpConfiguration>\n" 157 + "</Network>\n" 158 + "</NetworkList>\n" 159 + "</WifiBackupData>\n"; 160 161 private static final String WIFI_BACKUP_DATA_V1_1 = 162 "<?xml version='1.0' encoding='utf-8' standalone='yes' ?>\n" 163 + "<WifiBackupData>\n" 164 + "<float name=\"Version\" value=\"1.1\" />\n" 165 + "<NetworkList>\n" 166 + "<Network>\n" 167 + "<WifiConfiguration>\n" 168 + "<string name=\"ConfigKey\">"" + WifiConfigurationTestUtil.TEST_SSID 169 + ""NONE</string>\n" 170 + "<string name=\"SSID\">"" + WifiConfigurationTestUtil.TEST_SSID 171 + ""</string>\n" 172 + "<null name=\"BSSID\" />\n" 173 + "<null name=\"PreSharedKey\" />\n" 174 + "<null name=\"WEPKeys\" />\n" 175 + "<int name=\"WEPTxKeyIndex\" value=\"0\" />\n" 176 + "<boolean name=\"HiddenSSID\" value=\"false\" />\n" 177 + "<boolean name=\"RequirePMF\" value=\"false\" />\n" 178 + "<byte-array name=\"AllowedKeyMgmt\" num=\"1\">01</byte-array>\n" 179 + "<byte-array name=\"AllowedProtocols\" num=\"1\">03</byte-array>\n" 180 + "<byte-array name=\"AllowedAuthAlgos\" num=\"1\">01</byte-array>\n" 181 + "<byte-array name=\"AllowedGroupCiphers\" num=\"1\">0f</byte-array>\n" 182 + "<byte-array name=\"AllowedPairwiseCiphers\" num=\"1\">06</byte-array>\n" 183 + "<boolean name=\"Shared\" value=\"true\" />\n" 184 + "<int name=\"MeteredOverride\" value=\"1\" />\n" 185 + "</WifiConfiguration>\n" 186 + "<IpConfiguration>\n" 187 + "<string name=\"IpAssignment\">DHCP</string>\n" 188 + "<string name=\"ProxySettings\">NONE</string>\n" 189 + "</IpConfiguration>\n" 190 + "</Network>\n" 191 + "</NetworkList>\n" 192 + "</WifiBackupData>\n"; 193 194 private static final String WIFI_BACKUP_DATA_V1_2 = 195 "<?xml version='1.0' encoding='utf-8' standalone='yes' ?>\n" 196 + "<WifiBackupData>\n" 197 + "<float name=\"Version\" value=\"1.2\" />\n" 198 + "<NetworkList>\n" 199 + "<Network>\n" 200 + "<WifiConfiguration>\n" 201 + "<string name=\"ConfigKey\">"" + WifiConfigurationTestUtil.TEST_SSID 202 + ""NONE</string>\n" 203 + "<string name=\"SSID\">"" + WifiConfigurationTestUtil.TEST_SSID 204 + ""</string>\n" 205 + "<null name=\"PreSharedKey\" />\n" 206 + "<null name=\"WEPKeys\" />\n" 207 + "<int name=\"WEPTxKeyIndex\" value=\"0\" />\n" 208 + "<boolean name=\"HiddenSSID\" value=\"false\" />\n" 209 + "<boolean name=\"RequirePMF\" value=\"false\" />\n" 210 + "<byte-array name=\"AllowedKeyMgmt\" num=\"1\">01</byte-array>\n" 211 + "<byte-array name=\"AllowedProtocols\" num=\"1\">03</byte-array>\n" 212 + "<byte-array name=\"AllowedAuthAlgos\" num=\"1\">01</byte-array>\n" 213 + "<byte-array name=\"AllowedGroupCiphers\" num=\"1\">0f</byte-array>\n" 214 + "<byte-array name=\"AllowedPairwiseCiphers\" num=\"1\">06</byte-array>\n" 215 + "<byte-array name=\"AllowedGroupMgmtCiphers\" num=\"0\"></byte-array>\n" 216 + "<byte-array name=\"AllowedSuiteBCiphers\" num=\"0\"></byte-array>\n" 217 + "<boolean name=\"Shared\" value=\"true\" />\n" 218 + "<boolean name=\"AutoJoinEnabled\" value=\"false\" />\n" 219 + "<int name=\"MeteredOverride\" value=\"1\" />\n" 220 + "</WifiConfiguration>\n" 221 + "<IpConfiguration>\n" 222 + "<string name=\"IpAssignment\">DHCP</string>\n" 223 + "<string name=\"ProxySettings\">NONE</string>\n" 224 + "</IpConfiguration>\n" 225 + "</Network>\n" 226 + "</NetworkList>\n" 227 + "</WifiBackupData>\n"; 228 229 @Mock WifiPermissionsUtil mWifiPermissionsUtil; 230 private WifiBackupRestore mWifiBackupRestore; 231 private boolean mCheckDump = true; 232 233 @Before setUp()234 public void setUp() throws Exception { 235 MockitoAnnotations.initMocks(this); 236 when(mWifiPermissionsUtil.checkConfigOverridePermission(anyInt())).thenReturn(true); 237 mWifiBackupRestore = new WifiBackupRestore(mWifiPermissionsUtil); 238 // Enable verbose logging before tests to check the backup data dumps. 239 mWifiBackupRestore.enableVerboseLogging(1); 240 } 241 242 @After cleanUp()243 public void cleanUp() throws Exception { 244 if (mCheckDump) { 245 StringWriter stringWriter = new StringWriter(); 246 mWifiBackupRestore.dump( 247 new FileDescriptor(), new PrintWriter(stringWriter), new String[0]); 248 String dumpString = stringWriter.toString(); 249 // Ensure that the SSID was dumped out. 250 assertTrue("Dump: " + dumpString, 251 dumpString.contains(WifiConfigurationTestUtil.TEST_SSID)); 252 // Ensure that the password wasn't dumped out. 253 assertFalse("Dump: " + dumpString, 254 dumpString.contains(WifiConfigurationTestUtil.TEST_PSK)); 255 assertFalse("Dump: " + dumpString, 256 dumpString.contains(WifiConfigurationTestUtil.TEST_WEP_KEYS[0])); 257 assertFalse("Dump: " + dumpString, 258 dumpString.contains(WifiConfigurationTestUtil.TEST_WEP_KEYS[1])); 259 assertFalse("Dump: " + dumpString, 260 dumpString.contains(WifiConfigurationTestUtil.TEST_WEP_KEYS[2])); 261 assertFalse("Dump: " + dumpString, 262 dumpString.contains(WifiConfigurationTestUtil.TEST_WEP_KEYS[3])); 263 } 264 } 265 266 /** 267 * Verify that a null network list is serialized correctly. 268 */ 269 @Test testNullNetworkListBackup()270 public void testNullNetworkListBackup() { 271 byte[] backupData = mWifiBackupRestore.retrieveBackupDataFromConfigurations(null); 272 assertTrue(backupData != null); 273 assertEquals(backupData.length, 0); 274 // No valid data to check in dump. 275 mCheckDump = false; 276 } 277 278 /** 279 * Verify that a single open network configuration is serialized & deserialized correctly. 280 */ 281 @Test testSingleOpenNetworkBackupRestore()282 public void testSingleOpenNetworkBackupRestore() { 283 List<WifiConfiguration> configurations = new ArrayList<>(); 284 configurations.add(WifiConfigurationTestUtil.createOpenNetwork()); 285 286 byte[] backupData = mWifiBackupRestore.retrieveBackupDataFromConfigurations(configurations); 287 List<WifiConfiguration> retrievedConfigurations = 288 mWifiBackupRestore.retrieveConfigurationsFromBackupData(backupData); 289 WifiConfigurationTestUtil.assertConfigurationsEqualForBackup( 290 configurations, retrievedConfigurations); 291 } 292 293 /** 294 * Verify that a single open hidden network configuration is serialized & deserialized 295 * correctly. 296 */ 297 @Test testSingleOpenHiddenNetworkBackupRestore()298 public void testSingleOpenHiddenNetworkBackupRestore() { 299 List<WifiConfiguration> configurations = new ArrayList<>(); 300 configurations.add(WifiConfigurationTestUtil.createOpenHiddenNetwork()); 301 302 byte[] backupData = mWifiBackupRestore.retrieveBackupDataFromConfigurations(configurations); 303 List<WifiConfiguration> retrievedConfigurations = 304 mWifiBackupRestore.retrieveConfigurationsFromBackupData(backupData); 305 WifiConfigurationTestUtil.assertConfigurationsEqualForBackup( 306 configurations, retrievedConfigurations); 307 } 308 309 /** 310 * Verify that a single PSK network configuration is serialized & deserialized correctly. 311 */ 312 @Test testSinglePskNetworkBackupRestore()313 public void testSinglePskNetworkBackupRestore() { 314 List<WifiConfiguration> configurations = new ArrayList<>(); 315 configurations.add(WifiConfigurationTestUtil.createPskNetwork()); 316 317 byte[] backupData = mWifiBackupRestore.retrieveBackupDataFromConfigurations(configurations); 318 List<WifiConfiguration> retrievedConfigurations = 319 mWifiBackupRestore.retrieveConfigurationsFromBackupData(backupData); 320 WifiConfigurationTestUtil.assertConfigurationsEqualForBackup( 321 configurations, retrievedConfigurations); 322 } 323 324 /** 325 * Verify that a single PSK hidden network configuration is serialized & deserialized correctly. 326 */ 327 @Test testSinglePskHiddenNetworkBackupRestore()328 public void testSinglePskHiddenNetworkBackupRestore() { 329 List<WifiConfiguration> configurations = new ArrayList<>(); 330 configurations.add(WifiConfigurationTestUtil.createPskHiddenNetwork()); 331 332 byte[] backupData = mWifiBackupRestore.retrieveBackupDataFromConfigurations(configurations); 333 List<WifiConfiguration> retrievedConfigurations = 334 mWifiBackupRestore.retrieveConfigurationsFromBackupData(backupData); 335 WifiConfigurationTestUtil.assertConfigurationsEqualForBackup( 336 configurations, retrievedConfigurations); 337 } 338 339 /** 340 * Verify that a single WEP network configuration is serialized & deserialized correctly. 341 */ 342 @Test testSingleWepNetworkBackupRestore()343 public void testSingleWepNetworkBackupRestore() { 344 List<WifiConfiguration> configurations = new ArrayList<>(); 345 configurations.add(WifiConfigurationTestUtil.createWepNetwork()); 346 347 byte[] backupData = mWifiBackupRestore.retrieveBackupDataFromConfigurations(configurations); 348 List<WifiConfiguration> retrievedConfigurations = 349 mWifiBackupRestore.retrieveConfigurationsFromBackupData(backupData); 350 WifiConfigurationTestUtil.assertConfigurationsEqualForBackup( 351 configurations, retrievedConfigurations); 352 } 353 354 /** 355 * Verify that restoring of configuration that contains unsupported tags works correctly 356 * (unsupported tags are ignored). 357 */ 358 @Test testConfigurationWithUnsupportedTagsRestore()359 public void testConfigurationWithUnsupportedTagsRestore() { 360 List<WifiConfiguration> configurations = new ArrayList<>(); 361 configurations.add(createNetworkForConfigurationWithUnsupportedTag()); 362 363 byte[] backupData = WIFI_BACKUP_DATA_WITH_UNSUPPORTED_TAG.getBytes(); 364 List<WifiConfiguration> retrievedConfigurations = 365 mWifiBackupRestore.retrieveConfigurationsFromBackupData(backupData); 366 WifiConfigurationTestUtil.assertConfigurationsEqualForBackup( 367 configurations, retrievedConfigurations); 368 369 // No valid data to check in dump. 370 mCheckDump = false; 371 } 372 373 /** 374 * Creates correct WiFiConfiguration that should be parsed out of 375 * {@link #WIFI_BACKUP_DATA_WITH_UNSUPPORTED_TAG} configuration which contains unsupported tag. 376 */ createNetworkForConfigurationWithUnsupportedTag()377 private static WifiConfiguration createNetworkForConfigurationWithUnsupportedTag() { 378 final WifiConfiguration config = new WifiConfiguration(); 379 config.SSID = "\"" + WifiConfigurationTestUtil.TEST_SSID + "\""; 380 config.wepTxKeyIndex = 0; 381 config.hiddenSSID = false; 382 config.requirePmf = false; 383 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); 384 config.allowedProtocols.set(WifiConfiguration.Protocol.WPA); 385 config.allowedProtocols.set(WifiConfiguration.Protocol.RSN); 386 config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); 387 config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); 388 config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104); 389 config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); 390 config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); 391 config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); 392 config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); 393 config.shared = true; 394 395 IpConfiguration ipConfiguration = new IpConfiguration(); 396 ipConfiguration.setIpAssignment(IpConfiguration.IpAssignment.DHCP); 397 ipConfiguration.setProxySettings(IpConfiguration.ProxySettings.NONE); 398 config.setIpConfiguration(ipConfiguration); 399 400 return config; 401 } 402 403 /** 404 * Verify that restoring of configuration that contains unsupported values in bitsets works 405 * correctly (unsupported values are ignored). 406 */ 407 @Test testConfigurationWithUnsupportedValuesInBitsetsRestore()408 public void testConfigurationWithUnsupportedValuesInBitsetsRestore() { 409 List<WifiConfiguration> configurations = new ArrayList<>(); 410 configurations.add(createNetworkForConfigurationWithUnsupportedValuesInBitsetsInRestore()); 411 412 byte[] backupData = WIFI_BACKUP_DATA_WITH_UNSUPPORTED_VALUES_IN_BITSETS.getBytes(); 413 List<WifiConfiguration> retrievedConfigurations = 414 mWifiBackupRestore.retrieveConfigurationsFromBackupData(backupData); 415 WifiConfigurationTestUtil.assertConfigurationsEqualForBackup( 416 configurations, retrievedConfigurations); 417 418 // No valid data to check in dump. 419 mCheckDump = false; 420 } 421 422 /** 423 * Creates correct WiFiConfiguration that should be parsed out of 424 * {@link #WIFI_BACKUP_DATA_WITH_UNSUPPORTED_VALUES_IN_BITSETS} configuration which contains 425 * unsupported values. 426 * |AllowedKeyMgmt|, |AllowedProtocols|, |AllowedAuthAlgorithms|, |AllowedGroupCiphers| and 427 * |AllowedPairwiseCiphers| fields have invalid values in them. 428 */ 429 private static WifiConfiguration createNetworkForConfigurationWithUnsupportedValuesInBitsetsInRestore()430 createNetworkForConfigurationWithUnsupportedValuesInBitsetsInRestore() { 431 final WifiConfiguration config = new WifiConfiguration(); 432 config.SSID = "\"" + WifiConfigurationTestUtil.TEST_SSID + "\""; 433 config.wepTxKeyIndex = 0; 434 config.hiddenSSID = false; 435 config.requirePmf = false; 436 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); 437 config.allowedProtocols.set(WifiConfiguration.Protocol.WPA); 438 config.allowedProtocols.set(WifiConfiguration.Protocol.RSN); 439 config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); 440 config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); 441 config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104); 442 config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); 443 config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); 444 config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); 445 config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); 446 config.shared = true; 447 448 IpConfiguration ipConfiguration = new IpConfiguration(); 449 ipConfiguration.setIpAssignment(IpConfiguration.IpAssignment.DHCP); 450 ipConfiguration.setProxySettings(IpConfiguration.ProxySettings.NONE); 451 config.setIpConfiguration(ipConfiguration); 452 453 return config; 454 } 455 456 /** 457 * Verify that a single WEP network configuration with only 1 key is serialized & deserialized 458 * correctly. 459 */ 460 @Test testSingleWepNetworkWithSingleKeyBackupRestore()461 public void testSingleWepNetworkWithSingleKeyBackupRestore() { 462 List<WifiConfiguration> configurations = new ArrayList<>(); 463 configurations.add(WifiConfigurationTestUtil.createWepNetworkWithSingleKey()); 464 465 byte[] backupData = mWifiBackupRestore.retrieveBackupDataFromConfigurations(configurations); 466 List<WifiConfiguration> retrievedConfigurations = 467 mWifiBackupRestore.retrieveConfigurationsFromBackupData(backupData); 468 WifiConfigurationTestUtil.assertConfigurationsEqualForBackup( 469 configurations, retrievedConfigurations); 470 } 471 472 /** 473 * Verify that a single enterprise network configuration is not serialized. 474 */ 475 @Test testSingleEnterpriseNetworkNotBackupRestore()476 public void testSingleEnterpriseNetworkNotBackupRestore() { 477 List<WifiConfiguration> configurations = new ArrayList<>(); 478 configurations.add(WifiConfigurationTestUtil.createEapNetwork()); 479 configurations.add(WifiConfigurationTestUtil.createEapSuiteBNetwork()); 480 481 byte[] backupData = mWifiBackupRestore.retrieveBackupDataFromConfigurations(configurations); 482 List<WifiConfiguration> retrievedConfigurations = 483 mWifiBackupRestore.retrieveConfigurationsFromBackupData(backupData); 484 assertTrue(retrievedConfigurations.isEmpty()); 485 // No valid data to check in dump. 486 mCheckDump = false; 487 } 488 489 /** 490 * Verify that a single PSK network configuration with static ip/proxy settings is serialized & 491 * deserialized correctly. 492 */ 493 @Test testSinglePskNetworkWithStaticIpAndStaticProxyBackupRestore()494 public void testSinglePskNetworkWithStaticIpAndStaticProxyBackupRestore() { 495 List<WifiConfiguration> configurations = new ArrayList<>(); 496 WifiConfiguration pskNetwork = WifiConfigurationTestUtil.createPskNetwork(); 497 pskNetwork.setIpConfiguration( 498 WifiConfigurationTestUtil.createStaticIpConfigurationWithStaticProxy()); 499 configurations.add(pskNetwork); 500 501 byte[] backupData = mWifiBackupRestore.retrieveBackupDataFromConfigurations(configurations); 502 List<WifiConfiguration> retrievedConfigurations = 503 mWifiBackupRestore.retrieveConfigurationsFromBackupData(backupData); 504 WifiConfigurationTestUtil.assertConfigurationsEqualForBackup( 505 configurations, retrievedConfigurations); 506 } 507 508 /** 509 * Verify that a single PSK network configuration with static ip & PAC proxy settings is 510 * serialized & deserialized correctly. 511 */ 512 @Test testSinglePskNetworkWithStaticIpAndPACProxyBackupRestore()513 public void testSinglePskNetworkWithStaticIpAndPACProxyBackupRestore() { 514 List<WifiConfiguration> configurations = new ArrayList<>(); 515 WifiConfiguration pskNetwork = WifiConfigurationTestUtil.createPskNetwork(); 516 pskNetwork.setIpConfiguration( 517 WifiConfigurationTestUtil.createStaticIpConfigurationWithPacProxy()); 518 configurations.add(pskNetwork); 519 520 byte[] backupData = mWifiBackupRestore.retrieveBackupDataFromConfigurations(configurations); 521 List<WifiConfiguration> retrievedConfigurations = 522 mWifiBackupRestore.retrieveConfigurationsFromBackupData(backupData); 523 WifiConfigurationTestUtil.assertConfigurationsEqualForBackup( 524 configurations, retrievedConfigurations); 525 } 526 527 /** 528 * Verify that a single PSK network configuration with DHCP ip & PAC proxy settings is 529 * serialized & deserialized correctly. 530 */ 531 @Test testSinglePskNetworkWithDHCPIpAndPACProxyBackupRestore()532 public void testSinglePskNetworkWithDHCPIpAndPACProxyBackupRestore() { 533 List<WifiConfiguration> configurations = new ArrayList<>(); 534 WifiConfiguration pskNetwork = WifiConfigurationTestUtil.createPskNetwork(); 535 pskNetwork.setIpConfiguration( 536 WifiConfigurationTestUtil.createDHCPIpConfigurationWithPacProxy()); 537 configurations.add(pskNetwork); 538 539 byte[] backupData = mWifiBackupRestore.retrieveBackupDataFromConfigurations(configurations); 540 List<WifiConfiguration> retrievedConfigurations = 541 mWifiBackupRestore.retrieveConfigurationsFromBackupData(backupData); 542 WifiConfigurationTestUtil.assertConfigurationsEqualForBackup( 543 configurations, retrievedConfigurations); 544 } 545 546 /** 547 * Verify that a single PSK network configuration with partial static ip settings is serialized 548 * & deserialized correctly. 549 */ 550 @Test testSinglePskNetworkWithPartialStaticIpBackupRestore()551 public void testSinglePskNetworkWithPartialStaticIpBackupRestore() { 552 List<WifiConfiguration> configurations = new ArrayList<>(); 553 WifiConfiguration pskNetwork = WifiConfigurationTestUtil.createPskNetwork(); 554 pskNetwork.setIpConfiguration( 555 WifiConfigurationTestUtil.createPartialStaticIpConfigurationWithPacProxy()); 556 configurations.add(pskNetwork); 557 558 byte[] backupData = mWifiBackupRestore.retrieveBackupDataFromConfigurations(configurations); 559 List<WifiConfiguration> retrievedConfigurations = 560 mWifiBackupRestore.retrieveConfigurationsFromBackupData(backupData); 561 WifiConfigurationTestUtil.assertConfigurationsEqualForBackup( 562 configurations, retrievedConfigurations); 563 } 564 565 /** 566 * Verify that multiple networks of different types are serialized and deserialized correctly. 567 */ 568 @Test testMultipleNetworksAllBackupRestore()569 public void testMultipleNetworksAllBackupRestore() { 570 List<WifiConfiguration> configurations = new ArrayList<>(); 571 configurations.add(WifiConfigurationTestUtil.createWepNetwork()); 572 configurations.add(WifiConfigurationTestUtil.createWepNetwork()); 573 configurations.add(WifiConfigurationTestUtil.createPskNetwork()); 574 configurations.add(WifiConfigurationTestUtil.createOpenNetwork()); 575 configurations.add(WifiConfigurationTestUtil.createOweNetwork()); 576 configurations.add(WifiConfigurationTestUtil.createSaeNetwork()); 577 578 byte[] backupData = mWifiBackupRestore.retrieveBackupDataFromConfigurations(configurations); 579 List<WifiConfiguration> retrievedConfigurations = 580 mWifiBackupRestore.retrieveConfigurationsFromBackupData(backupData); 581 WifiConfigurationTestUtil.assertConfigurationsEqualForBackup( 582 configurations, retrievedConfigurations); 583 } 584 585 /** 586 * Verify that multiple networks of different types except enterprise ones are serialized and 587 * deserialized correctly 588 */ 589 @Test testMultipleNetworksNonEnterpriseBackupRestore()590 public void testMultipleNetworksNonEnterpriseBackupRestore() { 591 List<WifiConfiguration> configurations = new ArrayList<>(); 592 List<WifiConfiguration> expectedConfigurations = new ArrayList<>(); 593 594 WifiConfiguration wepNetwork = WifiConfigurationTestUtil.createWepNetwork(); 595 configurations.add(wepNetwork); 596 expectedConfigurations.add(wepNetwork); 597 598 configurations.add(WifiConfigurationTestUtil.createEapNetwork()); 599 configurations.add(WifiConfigurationTestUtil.createEapSuiteBNetwork()); 600 601 WifiConfiguration pskNetwork = WifiConfigurationTestUtil.createPskNetwork(); 602 configurations.add(pskNetwork); 603 expectedConfigurations.add(pskNetwork); 604 605 WifiConfiguration openNetwork = WifiConfigurationTestUtil.createOpenNetwork(); 606 configurations.add(openNetwork); 607 expectedConfigurations.add(openNetwork); 608 609 WifiConfiguration saeNetwork = WifiConfigurationTestUtil.createSaeNetwork(); 610 configurations.add(saeNetwork); 611 expectedConfigurations.add(saeNetwork); 612 613 WifiConfiguration oweNetwork = WifiConfigurationTestUtil.createOweNetwork(); 614 configurations.add(oweNetwork); 615 expectedConfigurations.add(oweNetwork); 616 617 byte[] backupData = mWifiBackupRestore.retrieveBackupDataFromConfigurations(configurations); 618 List<WifiConfiguration> retrievedConfigurations = 619 mWifiBackupRestore.retrieveConfigurationsFromBackupData(backupData); 620 WifiConfigurationTestUtil.assertConfigurationsEqualForBackup( 621 expectedConfigurations, retrievedConfigurations); 622 } 623 624 /** 625 * Verify that multiple networks with different credential types and IpConfiguration types are 626 * serialized and deserialized correctly. 627 */ 628 @Test testMultipleNetworksWithDifferentIpConfigurationsAllBackupRestore()629 public void testMultipleNetworksWithDifferentIpConfigurationsAllBackupRestore() { 630 List<WifiConfiguration> configurations = new ArrayList<>(); 631 632 WifiConfiguration wepNetwork = WifiConfigurationTestUtil.createWepNetwork(); 633 wepNetwork.setIpConfiguration( 634 WifiConfigurationTestUtil.createDHCPIpConfigurationWithPacProxy()); 635 configurations.add(wepNetwork); 636 637 WifiConfiguration pskNetwork = WifiConfigurationTestUtil.createPskNetwork(); 638 pskNetwork.setIpConfiguration( 639 WifiConfigurationTestUtil.createStaticIpConfigurationWithPacProxy()); 640 configurations.add(pskNetwork); 641 642 WifiConfiguration openNetwork = WifiConfigurationTestUtil.createOpenNetwork(); 643 openNetwork.setIpConfiguration( 644 WifiConfigurationTestUtil.createStaticIpConfigurationWithStaticProxy()); 645 configurations.add(openNetwork); 646 647 byte[] backupData = mWifiBackupRestore.retrieveBackupDataFromConfigurations(configurations); 648 List<WifiConfiguration> retrievedConfigurations = 649 mWifiBackupRestore.retrieveConfigurationsFromBackupData(backupData); 650 WifiConfigurationTestUtil.assertConfigurationsEqualForBackup( 651 configurations, retrievedConfigurations); 652 } 653 654 /** 655 * Verify that multiple networks of different types except the non system app created ones are 656 * serialized and deserialized correctly. 657 */ 658 @Test testMultipleNetworksSystemAppBackupRestore()659 public void testMultipleNetworksSystemAppBackupRestore() { 660 int systemAppUid = Process.SYSTEM_UID; 661 int nonSystemAppUid = Process.FIRST_APPLICATION_UID + 556; 662 when(mWifiPermissionsUtil.checkConfigOverridePermission(eq(systemAppUid))) 663 .thenReturn(true); 664 when(mWifiPermissionsUtil.checkConfigOverridePermission(eq(nonSystemAppUid))) 665 .thenReturn(false); 666 667 List<WifiConfiguration> configurations = new ArrayList<>(); 668 List<WifiConfiguration> expectedConfigurations = new ArrayList<>(); 669 670 WifiConfiguration wepNetwork = WifiConfigurationTestUtil.createWepNetwork(); 671 wepNetwork.creatorUid = systemAppUid; 672 configurations.add(wepNetwork); 673 expectedConfigurations.add(wepNetwork); 674 675 // These should not be in |expectedConfigurations|. 676 WifiConfiguration nonSystemAppWepNetwork = WifiConfigurationTestUtil.createWepNetwork(); 677 nonSystemAppWepNetwork.creatorUid = nonSystemAppUid; 678 configurations.add(nonSystemAppWepNetwork); 679 680 WifiConfiguration pskNetwork = WifiConfigurationTestUtil.createPskNetwork(); 681 pskNetwork.creatorUid = systemAppUid; 682 configurations.add(pskNetwork); 683 expectedConfigurations.add(pskNetwork); 684 685 // These should not be in |expectedConfigurations|. 686 WifiConfiguration nonSystemAppPskNetwork = WifiConfigurationTestUtil.createPskNetwork(); 687 nonSystemAppPskNetwork.creatorUid = nonSystemAppUid; 688 configurations.add(nonSystemAppPskNetwork); 689 690 WifiConfiguration openNetwork = WifiConfigurationTestUtil.createOpenNetwork(); 691 configurations.add(openNetwork); 692 expectedConfigurations.add(openNetwork); 693 694 byte[] backupData = mWifiBackupRestore.retrieveBackupDataFromConfigurations(configurations); 695 List<WifiConfiguration> retrievedConfigurations = 696 mWifiBackupRestore.retrieveConfigurationsFromBackupData(backupData); 697 WifiConfigurationTestUtil.assertConfigurationsEqualForBackup( 698 expectedConfigurations, retrievedConfigurations); 699 } 700 701 /** 702 * Verify that a single open network configuration is serialized & deserialized correctly from 703 * old backups. 704 */ 705 @Test testSingleOpenNetworkSupplicantBackupRestore()706 public void testSingleOpenNetworkSupplicantBackupRestore() { 707 List<WifiConfiguration> configurations = new ArrayList<>(); 708 configurations.add(WifiConfigurationTestUtil.createOpenNetwork()); 709 710 byte[] supplicantData = createWpaSupplicantConfBackupData(configurations); 711 byte[] ipConfigData = createIpConfBackupData(configurations); 712 List<WifiConfiguration> retrievedConfigurations = 713 mWifiBackupRestore.retrieveConfigurationsFromSupplicantBackupData( 714 supplicantData, ipConfigData); 715 WifiConfigurationTestUtil.assertConfigurationsEqualForBackup( 716 configurations, retrievedConfigurations); 717 } 718 719 /** 720 * Verify that a single open hidden network configuration is serialized & deserialized 721 * correctly from old backups. 722 */ 723 @Test testSingleOpenHiddenNetworkSupplicantBackupRestore()724 public void testSingleOpenHiddenNetworkSupplicantBackupRestore() { 725 List<WifiConfiguration> configurations = new ArrayList<>(); 726 configurations.add(WifiConfigurationTestUtil.createOpenHiddenNetwork()); 727 728 byte[] supplicantData = createWpaSupplicantConfBackupData(configurations); 729 byte[] ipConfigData = createIpConfBackupData(configurations); 730 List<WifiConfiguration> retrievedConfigurations = 731 mWifiBackupRestore.retrieveConfigurationsFromSupplicantBackupData( 732 supplicantData, ipConfigData); 733 WifiConfigurationTestUtil.assertConfigurationsEqualForBackup( 734 configurations, retrievedConfigurations); 735 } 736 737 /** 738 * Verify that a single PSK network configuration is serialized & deserialized correctly from 739 * old backups. 740 */ 741 @Test testSinglePskNetworkSupplicantBackupRestore()742 public void testSinglePskNetworkSupplicantBackupRestore() { 743 List<WifiConfiguration> configurations = new ArrayList<>(); 744 configurations.add(WifiConfigurationTestUtil.createPskNetwork()); 745 746 byte[] supplicantData = createWpaSupplicantConfBackupData(configurations); 747 byte[] ipConfigData = createIpConfBackupData(configurations); 748 List<WifiConfiguration> retrievedConfigurations = 749 mWifiBackupRestore.retrieveConfigurationsFromSupplicantBackupData( 750 supplicantData, ipConfigData); 751 WifiConfigurationTestUtil.assertConfigurationsEqualForBackup( 752 configurations, retrievedConfigurations); 753 } 754 755 /** 756 * Verify that a single PSK hidden network configuration is serialized & deserialized correctly 757 * from old backups. 758 */ 759 @Test testSinglePskHiddenNetworkSupplicantBackupRestore()760 public void testSinglePskHiddenNetworkSupplicantBackupRestore() { 761 List<WifiConfiguration> configurations = new ArrayList<>(); 762 configurations.add(WifiConfigurationTestUtil.createPskHiddenNetwork()); 763 764 byte[] supplicantData = createWpaSupplicantConfBackupData(configurations); 765 byte[] ipConfigData = createIpConfBackupData(configurations); 766 List<WifiConfiguration> retrievedConfigurations = 767 mWifiBackupRestore.retrieveConfigurationsFromSupplicantBackupData( 768 supplicantData, ipConfigData); 769 WifiConfigurationTestUtil.assertConfigurationsEqualForBackup( 770 configurations, retrievedConfigurations); 771 } 772 773 /** 774 * Verify that a single WEP network configuration is serialized & deserialized correctly from 775 * old backups. 776 */ 777 @Test testSingleWepNetworkSupplicantBackupRestore()778 public void testSingleWepNetworkSupplicantBackupRestore() { 779 List<WifiConfiguration> configurations = new ArrayList<>(); 780 configurations.add(WifiConfigurationTestUtil.createWepNetwork()); 781 782 byte[] supplicantData = createWpaSupplicantConfBackupData(configurations); 783 byte[] ipConfigData = createIpConfBackupData(configurations); 784 List<WifiConfiguration> retrievedConfigurations = 785 mWifiBackupRestore.retrieveConfigurationsFromSupplicantBackupData( 786 supplicantData, ipConfigData); 787 WifiConfigurationTestUtil.assertConfigurationsEqualForBackup( 788 configurations, retrievedConfigurations); 789 } 790 791 /** 792 * Verify that a single WEP network configuration with only 1 key is serialized & deserialized 793 * correctly from old backups. 794 */ 795 @Test testSingleWepNetworkWithSingleKeySupplicantBackupRestore()796 public void testSingleWepNetworkWithSingleKeySupplicantBackupRestore() { 797 List<WifiConfiguration> configurations = new ArrayList<>(); 798 configurations.add(WifiConfigurationTestUtil.createWepNetworkWithSingleKey()); 799 800 byte[] supplicantData = createWpaSupplicantConfBackupData(configurations); 801 byte[] ipConfigData = createIpConfBackupData(configurations); 802 List<WifiConfiguration> retrievedConfigurations = 803 mWifiBackupRestore.retrieveConfigurationsFromSupplicantBackupData( 804 supplicantData, ipConfigData); 805 WifiConfigurationTestUtil.assertConfigurationsEqualForBackup( 806 configurations, retrievedConfigurations); 807 } 808 809 /** 810 * Verify that a single enterprise network configuration is not serialized from old backups. 811 */ 812 @Test testSingleEnterpriseNetworkNotSupplicantBackupRestore()813 public void testSingleEnterpriseNetworkNotSupplicantBackupRestore() { 814 List<WifiConfiguration> configurations = new ArrayList<>(); 815 configurations.add(WifiConfigurationTestUtil.createEapNetwork()); 816 817 byte[] supplicantData = createWpaSupplicantConfBackupData(configurations); 818 byte[] ipConfigData = createIpConfBackupData(configurations); 819 List<WifiConfiguration> retrievedConfigurations = 820 mWifiBackupRestore.retrieveConfigurationsFromSupplicantBackupData( 821 supplicantData, ipConfigData); 822 assertTrue(retrievedConfigurations.isEmpty()); 823 } 824 825 /** 826 * Verify that multiple networks with different credential types and IpConfiguration types are 827 * serialized and deserialized correctly from old backups 828 */ 829 @Test testMultipleNetworksWithDifferentIpConfigurationsAllSupplicantBackupRestore()830 public void testMultipleNetworksWithDifferentIpConfigurationsAllSupplicantBackupRestore() { 831 List<WifiConfiguration> configurations = new ArrayList<>(); 832 833 WifiConfiguration wepNetwork = WifiConfigurationTestUtil.createWepNetwork(); 834 wepNetwork.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED); 835 wepNetwork.setIpConfiguration( 836 WifiConfigurationTestUtil.createDHCPIpConfigurationWithPacProxy()); 837 configurations.add(wepNetwork); 838 839 WifiConfiguration pskNetwork = WifiConfigurationTestUtil.createPskNetwork(); 840 pskNetwork.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); 841 pskNetwork.setIpConfiguration( 842 WifiConfigurationTestUtil.createStaticIpConfigurationWithPacProxy()); 843 configurations.add(pskNetwork); 844 845 WifiConfiguration openNetwork = WifiConfigurationTestUtil.createOpenNetwork(); 846 openNetwork.setIpConfiguration( 847 WifiConfigurationTestUtil.createStaticIpConfigurationWithStaticProxy()); 848 configurations.add(openNetwork); 849 850 byte[] supplicantData = createWpaSupplicantConfBackupData(configurations); 851 byte[] ipConfigData = createIpConfBackupData(configurations); 852 List<WifiConfiguration> retrievedConfigurations = 853 mWifiBackupRestore.retrieveConfigurationsFromSupplicantBackupData( 854 supplicantData, ipConfigData); 855 WifiConfigurationTestUtil.assertConfigurationsEqualForBackup( 856 configurations, retrievedConfigurations); 857 } 858 859 /** 860 * Verify that a single open network configuration is serialized & deserialized correctly from 861 * old backups with no ipconfig data. 862 */ 863 @Test testSingleOpenNetworkSupplicantBackupRestoreWithNoIpConfigData()864 public void testSingleOpenNetworkSupplicantBackupRestoreWithNoIpConfigData() { 865 List<WifiConfiguration> configurations = new ArrayList<>(); 866 configurations.add(WifiConfigurationTestUtil.createOpenNetwork()); 867 868 byte[] supplicantData = createWpaSupplicantConfBackupData(configurations); 869 List<WifiConfiguration> retrievedConfigurations = 870 mWifiBackupRestore.retrieveConfigurationsFromSupplicantBackupData( 871 supplicantData, null); 872 WifiConfigurationTestUtil.assertConfigurationsEqualForBackup( 873 configurations, retrievedConfigurations); 874 } 875 876 /** 877 * Verify that multiple networks with different credential types are serialized and 878 * deserialized correctly from old backups with no ipconfig data. 879 */ 880 @Test testMultipleNetworksAllSupplicantBackupRestoreWithNoIpConfigData()881 public void testMultipleNetworksAllSupplicantBackupRestoreWithNoIpConfigData() { 882 List<WifiConfiguration> configurations = new ArrayList<>(); 883 884 WifiConfiguration wepNetwork = WifiConfigurationTestUtil.createWepNetwork(); 885 configurations.add(wepNetwork); 886 887 WifiConfiguration pskNetwork = WifiConfigurationTestUtil.createPskNetwork(); 888 configurations.add(pskNetwork); 889 890 WifiConfiguration openNetwork = WifiConfigurationTestUtil.createOpenNetwork(); 891 configurations.add(openNetwork); 892 893 byte[] supplicantData = createWpaSupplicantConfBackupData(configurations); 894 List<WifiConfiguration> retrievedConfigurations = 895 mWifiBackupRestore.retrieveConfigurationsFromSupplicantBackupData( 896 supplicantData, null); 897 WifiConfigurationTestUtil.assertConfigurationsEqualForBackup( 898 configurations, retrievedConfigurations); 899 } 900 901 /** 902 * Verify that multiple networks of different types except the non system app created ones are 903 * serialized and deserialized correctly from old backups. 904 */ 905 @Test testMultipleNetworksSystemAppSupplicantBackupRestore()906 public void testMultipleNetworksSystemAppSupplicantBackupRestore() { 907 List<WifiConfiguration> configurations = new ArrayList<>(); 908 List<WifiConfiguration> expectedConfigurations = new ArrayList<>(); 909 910 WifiConfiguration wepNetwork = WifiConfigurationTestUtil.createWepNetwork(); 911 configurations.add(wepNetwork); 912 expectedConfigurations.add(wepNetwork); 913 914 // These should not be in |expectedConfigurations|. 915 WifiConfiguration nonSystemAppWepNetwork = WifiConfigurationTestUtil.createWepNetwork(); 916 nonSystemAppWepNetwork.creatorUid = Process.FIRST_APPLICATION_UID; 917 configurations.add(nonSystemAppWepNetwork); 918 919 WifiConfiguration pskNetwork = WifiConfigurationTestUtil.createPskNetwork(); 920 configurations.add(pskNetwork); 921 expectedConfigurations.add(pskNetwork); 922 923 // These should not be in |expectedConfigurations|. 924 WifiConfiguration nonSystemAppPskNetwork = WifiConfigurationTestUtil.createPskNetwork(); 925 nonSystemAppPskNetwork.creatorUid = Process.FIRST_APPLICATION_UID + 1; 926 configurations.add(nonSystemAppPskNetwork); 927 928 WifiConfiguration openNetwork = WifiConfigurationTestUtil.createOpenNetwork(); 929 configurations.add(openNetwork); 930 expectedConfigurations.add(openNetwork); 931 932 byte[] supplicantData = createWpaSupplicantConfBackupData(configurations); 933 byte[] ipConfigData = createIpConfBackupData(configurations); 934 List<WifiConfiguration> retrievedConfigurations = 935 mWifiBackupRestore.retrieveConfigurationsFromSupplicantBackupData( 936 supplicantData, ipConfigData); 937 WifiConfigurationTestUtil.assertConfigurationsEqualForBackup( 938 expectedConfigurations, retrievedConfigurations); 939 } 940 941 /** 942 * Verifying that backup data containing some unknown keys is properly restored. 943 * The backup data used here is a PII masked version of a backup data seen in a reported bug. 944 */ 945 @Test testSingleNetworkSupplicantBackupRestoreWithUnknownEAPKey()946 public void testSingleNetworkSupplicantBackupRestoreWithUnknownEAPKey() { 947 String backupSupplicantConfNetworkBlock = "network={\n" 948 + "ssid=" + WifiConfigurationTestUtil.TEST_SSID + "\n" 949 + "psk=" + WifiConfigurationTestUtil.TEST_PSK + "\n" 950 + "key_mgmt=WPA-PSK WPA-PSK-SHA256\n" 951 + "priority=18\n" 952 + "id_str=\"%7B%22creatorUid%22%3A%221000%22%2C%22configKey" 953 + "%22%3A%22%5C%22BLAH%5C%22WPA_PSK%22%7D\"\n" 954 + "eapRetryCount=6\n"; 955 byte[] supplicantData = backupSupplicantConfNetworkBlock.getBytes(); 956 List<WifiConfiguration> retrievedConfigurations = 957 mWifiBackupRestore.retrieveConfigurationsFromSupplicantBackupData( 958 supplicantData, null); 959 960 final WifiConfiguration expectedConfiguration = new WifiConfiguration(); 961 expectedConfiguration.SSID = WifiConfigurationTestUtil.TEST_SSID; 962 expectedConfiguration.preSharedKey = WifiConfigurationTestUtil.TEST_PSK; 963 expectedConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); 964 965 ArrayList<WifiConfiguration> expectedConfigurations = new ArrayList<WifiConfiguration>() {{ 966 add(expectedConfiguration); 967 }}; 968 WifiConfigurationTestUtil.assertConfigurationsEqualForBackup( 969 expectedConfigurations, retrievedConfigurations); 970 } 971 972 /** 973 * Verify that any corrupted data provided by Backup/Restore is ignored correctly. 974 */ 975 @Test testCorruptBackupRestore()976 public void testCorruptBackupRestore() { 977 Random random = new Random(); 978 byte[] backupData = new byte[100]; 979 random.nextBytes(backupData); 980 981 List<WifiConfiguration> retrievedConfigurations = 982 mWifiBackupRestore.retrieveConfigurationsFromBackupData(backupData); 983 assertNull(retrievedConfigurations); 984 // No valid data to check in dump. 985 mCheckDump = false; 986 } 987 988 /** 989 * Verify that restoring of configuration from a 1.0 version backup data. 990 */ 991 @Test testRestoreFromV1_0BackupData()992 public void testRestoreFromV1_0BackupData() { 993 List<WifiConfiguration> configurations = new ArrayList<>(); 994 configurations.add(createNetworkForConfigurationWithV1_0Data()); 995 996 byte[] backupData = WIFI_BACKUP_DATA_V1_0.getBytes(); 997 List<WifiConfiguration> retrievedConfigurations = 998 mWifiBackupRestore.retrieveConfigurationsFromBackupData(backupData); 999 WifiConfigurationTestUtil.assertConfigurationsEqualForBackup( 1000 configurations, retrievedConfigurations); 1001 } 1002 1003 /** 1004 * Verify that restoring of configuration from a 1.1 version backup data. 1005 */ 1006 @Test testRestoreFromV1_1BackupData()1007 public void testRestoreFromV1_1BackupData() { 1008 List<WifiConfiguration> configurations = new ArrayList<>(); 1009 configurations.add(createNetworkForConfigurationWithV1_1Data()); 1010 1011 byte[] backupData = WIFI_BACKUP_DATA_V1_1.getBytes(); 1012 List<WifiConfiguration> retrievedConfigurations = 1013 mWifiBackupRestore.retrieveConfigurationsFromBackupData(backupData); 1014 WifiConfigurationTestUtil.assertConfigurationsEqualForBackup( 1015 configurations, retrievedConfigurations); 1016 } 1017 1018 /** 1019 * Verify that restoring of configuration from a 1.2 version backup data. 1020 */ 1021 @Test testRestoreFromV1_2BackupData()1022 public void testRestoreFromV1_2BackupData() { 1023 List<WifiConfiguration> configurations = new ArrayList<>(); 1024 configurations.add(createNetworkForConfigurationWithV1_2Data()); 1025 1026 byte[] backupData = WIFI_BACKUP_DATA_V1_2.getBytes(); 1027 List<WifiConfiguration> retrievedConfigurations = 1028 mWifiBackupRestore.retrieveConfigurationsFromBackupData(backupData); 1029 WifiConfigurationTestUtil.assertConfigurationsEqualForBackup( 1030 configurations, retrievedConfigurations); 1031 1032 // Also, assert in the reverse direction to ensure the serialization logic matches. 1033 // Note: This will stop working when we bump up the version. Then we'll need to copy 1034 // the below assert to the test for the latest version. 1035 assertEquals(WIFI_BACKUP_DATA_V1_2, 1036 new String(mWifiBackupRestore.retrieveBackupDataFromConfigurations( 1037 retrievedConfigurations))); 1038 } 1039 1040 /** 1041 * Creates correct WiFiConfiguration that should be parsed out of 1042 * {@link #WIFI_BACKUP_DATA_V1_0} configuration which contains 1.0 version backup. 1043 */ createNetworkForConfigurationWithV1_0Data()1044 private static WifiConfiguration createNetworkForConfigurationWithV1_0Data() { 1045 final WifiConfiguration config = new WifiConfiguration(); 1046 config.SSID = "\"" + WifiConfigurationTestUtil.TEST_SSID + "\""; 1047 config.wepTxKeyIndex = 0; 1048 config.hiddenSSID = false; 1049 config.requirePmf = false; 1050 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); 1051 config.allowedProtocols.set(WifiConfiguration.Protocol.WPA); 1052 config.allowedProtocols.set(WifiConfiguration.Protocol.RSN); 1053 config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); 1054 config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); 1055 config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104); 1056 config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); 1057 config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); 1058 config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); 1059 config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); 1060 config.shared = true; 1061 1062 IpConfiguration ipConfiguration = new IpConfiguration(); 1063 ipConfiguration.setIpAssignment(IpConfiguration.IpAssignment.DHCP); 1064 ipConfiguration.setProxySettings(IpConfiguration.ProxySettings.NONE); 1065 config.setIpConfiguration(ipConfiguration); 1066 1067 return config; 1068 } 1069 1070 /** 1071 * Creates correct WiFiConfiguration that should be parsed out of 1072 * {@link #WIFI_BACKUP_DATA_V1_1} configuration which contains 1.1 version backup. 1073 */ createNetworkForConfigurationWithV1_1Data()1074 private static WifiConfiguration createNetworkForConfigurationWithV1_1Data() { 1075 final WifiConfiguration config = createNetworkForConfigurationWithV1_0Data(); 1076 config.meteredOverride = WifiConfiguration.METERED_OVERRIDE_METERED; 1077 return config; 1078 } 1079 1080 /** 1081 * Creates correct WiFiConfiguration that should be parsed out of 1082 * {@link #WIFI_BACKUP_DATA_V1_1} configuration which contains 1.2 version backup. 1083 */ createNetworkForConfigurationWithV1_2Data()1084 private static WifiConfiguration createNetworkForConfigurationWithV1_2Data() { 1085 final WifiConfiguration config = createNetworkForConfigurationWithV1_1Data(); 1086 config.allowAutojoin = false; 1087 return config; 1088 } 1089 1090 /** 1091 * Helper method to write a list of networks in wpa_supplicant.conf format to the output stream. 1092 */ createWpaSupplicantConfBackupData(List<WifiConfiguration> configurations)1093 private byte[] createWpaSupplicantConfBackupData(List<WifiConfiguration> configurations) { 1094 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 1095 OutputStreamWriter out = new OutputStreamWriter(bos); 1096 try { 1097 for (WifiConfiguration configuration : configurations) { 1098 writeConfigurationToWpaSupplicantConf(out, configuration); 1099 } 1100 out.flush(); 1101 return bos.toByteArray(); 1102 } catch (IOException e) { 1103 return null; 1104 } 1105 } 1106 1107 /** 1108 * Helper method to write a network in wpa_supplicant.conf format to the output stream. 1109 * This was created using a sample wpa_supplicant.conf file. Using the raw key strings here 1110 * (instead of consts in WifiBackupRestore). 1111 */ writeConfigurationToWpaSupplicantConf( OutputStreamWriter out, WifiConfiguration configuration)1112 private void writeConfigurationToWpaSupplicantConf( 1113 OutputStreamWriter out, WifiConfiguration configuration) 1114 throws IOException { 1115 out.write("network={\n"); 1116 out.write(" " + "ssid=" + configuration.SSID + "\n"); 1117 if (configuration.hiddenSSID) { 1118 out.write(" " + "scan_ssid=1" + "\n"); 1119 } 1120 String allowedKeyManagement = ""; 1121 if (configuration.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.NONE)) { 1122 allowedKeyManagement += "NONE"; 1123 } 1124 if (configuration.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_PSK)) { 1125 allowedKeyManagement += "WPA-PSK "; 1126 } 1127 if (configuration.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_EAP)) { 1128 allowedKeyManagement += "WPA-EAP "; 1129 } 1130 if (configuration.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.IEEE8021X)) { 1131 allowedKeyManagement += "IEEE8021X "; 1132 } 1133 out.write(" " + "key_mgmt=" + allowedKeyManagement + "\n"); 1134 String allowedAuthAlgorithm = ""; 1135 if (configuration.allowedAuthAlgorithms.get(WifiConfiguration.AuthAlgorithm.OPEN)) { 1136 allowedAuthAlgorithm += "OPEN "; 1137 } 1138 if (configuration.allowedAuthAlgorithms.get(WifiConfiguration.AuthAlgorithm.SHARED)) { 1139 allowedAuthAlgorithm += "SHARED "; 1140 } 1141 out.write(" " + "auth_alg=" + allowedAuthAlgorithm + "\n"); 1142 if (configuration.preSharedKey != null) { 1143 out.write(" " + "psk=" + configuration.preSharedKey + "\n"); 1144 } 1145 if (configuration.wepKeys[0] != null) { 1146 out.write(" " + "wep_key0=" + configuration.wepKeys[0] + "\n"); 1147 } 1148 if (configuration.wepKeys[1] != null) { 1149 out.write(" " + "wep_key1=" + configuration.wepKeys[1] + "\n"); 1150 } 1151 if (configuration.wepKeys[2] != null) { 1152 out.write(" " + "wep_key2=" + configuration.wepKeys[2] + "\n"); 1153 } 1154 if (configuration.wepKeys[3] != null) { 1155 out.write(" " + "wep_key3=" + configuration.wepKeys[3] + "\n"); 1156 } 1157 if (configuration.wepKeys[0] != null || configuration.wepKeys[1] != null 1158 || configuration.wepKeys[2] != null || configuration.wepKeys[3] != null) { 1159 out.write(" " + "wep_tx_keyidx=" + configuration.wepTxKeyIndex + "\n"); 1160 } 1161 Map<String, String> extras = new HashMap<>(); 1162 extras.put(SupplicantStaNetworkHal.ID_STRING_KEY_CONFIG_KEY, configuration.getKey()); 1163 extras.put(SupplicantStaNetworkHal.ID_STRING_KEY_CREATOR_UID, 1164 Integer.toString(configuration.creatorUid)); 1165 String idString = "\"" + SupplicantStaNetworkHal.createNetworkExtra(extras) + "\""; 1166 if (idString != null) { 1167 out.write(" " + "id_str=" + idString + "\n"); 1168 } 1169 out.write("}\n"); 1170 out.write("\n"); 1171 } 1172 1173 /** 1174 * Helper method to write a list of networks in ipconfig.txt format to the output stream. 1175 */ createIpConfBackupData(List<WifiConfiguration> configurations)1176 private byte[] createIpConfBackupData(List<WifiConfiguration> configurations) { 1177 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 1178 DataOutputStream out = new DataOutputStream(bos); 1179 final int configStoreVersion = 2; 1180 try { 1181 // write version first. 1182 out.writeInt(configStoreVersion); 1183 for (WifiConfiguration configuration : configurations) { 1184 // TODO: store configKey as a string instead of calculating its hash 1185 IpConfigStoreTestWriter.writeConfig( 1186 out, 1187 String.valueOf(configuration.getKey().hashCode()), 1188 configuration.getIpConfiguration(), 1189 configStoreVersion); 1190 } 1191 out.flush(); 1192 return bos.toByteArray(); 1193 } catch (IOException e) { 1194 return null; 1195 } 1196 } 1197 } 1198