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