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 android.net.wifi; 18 19 import static org.junit.Assert.assertArrayEquals; 20 import static org.junit.Assert.assertEquals; 21 import static org.junit.Assert.assertNotEquals; 22 import static org.junit.Assert.assertTrue; 23 import static org.junit.Assert.assertFalse; 24 25 import android.os.Parcel; 26 import android.net.MacAddress; 27 import android.net.wifi.WifiConfiguration.NetworkSelectionStatus; 28 import android.net.wifi.WifiInfo; 29 30 import org.junit.Before; 31 import org.junit.Test; 32 33 /** 34 * Unit tests for {@link android.net.wifi.WifiConfiguration}. 35 */ 36 public class WifiConfigurationTest { 37 38 @Before 39 public void setUp() { 40 } 41 42 /** 43 * Check that parcel marshalling/unmarshalling works 44 * 45 * Create and populate a WifiConfiguration. 46 * Marshall and unmashall it, and expect to recover a copy of the original. 47 * Marshall the resulting object, and expect the bytes to match the 48 * first marshall result. 49 */ 50 @Test 51 public void testWifiConfigurationParcel() { 52 String cookie = "C O.o |<IE"; 53 WifiConfiguration config = new WifiConfiguration(); 54 config.setPasspointManagementObjectTree(cookie); 55 MacAddress macBeforeParcel = config.getOrCreateRandomizedMacAddress(); 56 Parcel parcelW = Parcel.obtain(); 57 config.writeToParcel(parcelW, 0); 58 byte[] bytes = parcelW.marshall(); 59 parcelW.recycle(); 60 61 Parcel parcelR = Parcel.obtain(); 62 parcelR.unmarshall(bytes, 0, bytes.length); 63 parcelR.setDataPosition(0); 64 WifiConfiguration reconfig = WifiConfiguration.CREATOR.createFromParcel(parcelR); 65 66 // lacking a useful config.equals, check two fields near the end. 67 assertEquals(cookie, reconfig.getMoTree()); 68 assertEquals(macBeforeParcel, reconfig.getOrCreateRandomizedMacAddress()); 69 70 Parcel parcelWW = Parcel.obtain(); 71 reconfig.writeToParcel(parcelWW, 0); 72 byte[] rebytes = parcelWW.marshall(); 73 parcelWW.recycle(); 74 75 assertArrayEquals(bytes, rebytes); 76 } 77 78 @Test 79 public void testNetworkSelectionStatusCopy() { 80 NetworkSelectionStatus networkSelectionStatus = new NetworkSelectionStatus(); 81 networkSelectionStatus.setNotRecommended(true); 82 83 NetworkSelectionStatus copy = new NetworkSelectionStatus(); 84 copy.copy(networkSelectionStatus); 85 86 assertEquals(networkSelectionStatus.isNotRecommended(), copy.isNotRecommended()); 87 } 88 89 @Test 90 public void testNetworkSelectionStatusParcel() { 91 NetworkSelectionStatus networkSelectionStatus = new NetworkSelectionStatus(); 92 networkSelectionStatus.setNotRecommended(true); 93 94 Parcel parcelW = Parcel.obtain(); 95 networkSelectionStatus.writeToParcel(parcelW); 96 byte[] bytes = parcelW.marshall(); 97 parcelW.recycle(); 98 99 Parcel parcelR = Parcel.obtain(); 100 parcelR.unmarshall(bytes, 0, bytes.length); 101 parcelR.setDataPosition(0); 102 103 NetworkSelectionStatus copy = new NetworkSelectionStatus(); 104 copy.readFromParcel(parcelR); 105 106 assertEquals(networkSelectionStatus.isNotRecommended(), copy.isNotRecommended()); 107 } 108 109 @Test 110 public void testIsOpenNetwork_IsOpen_NullWepKeys() { 111 WifiConfiguration config = new WifiConfiguration(); 112 config.allowedKeyManagement.clear(); 113 config.wepKeys = null; 114 115 assertTrue(config.isOpenNetwork()); 116 } 117 118 @Test 119 public void testIsOpenNetwork_IsOpen_ZeroLengthWepKeysArray() { 120 WifiConfiguration config = new WifiConfiguration(); 121 config.allowedKeyManagement.clear(); 122 config.wepKeys = new String[0]; 123 124 assertTrue(config.isOpenNetwork()); 125 } 126 127 @Test 128 public void testIsOpenNetwork_IsOpen_NullWepKeysArray() { 129 WifiConfiguration config = new WifiConfiguration(); 130 config.allowedKeyManagement.clear(); 131 config.wepKeys = new String[1]; 132 133 assertTrue(config.isOpenNetwork()); 134 } 135 136 @Test 137 public void testIsOpenNetwork_NotOpen_HasWepKeys() { 138 WifiConfiguration config = new WifiConfiguration(); 139 config.allowedKeyManagement.clear(); 140 config.wepKeys = new String[] {"test"}; 141 142 assertFalse(config.isOpenNetwork()); 143 } 144 145 @Test 146 public void testIsOpenNetwork_NotOpen_HasNullWepKeyFollowedByNonNullKey() { 147 WifiConfiguration config = new WifiConfiguration(); 148 config.allowedKeyManagement.clear(); 149 config.wepKeys = new String[] {null, null, "test"}; 150 151 assertFalse(config.isOpenNetwork()); 152 } 153 154 @Test 155 public void testIsOpenNetwork_NotOpen_HasAuthType() { 156 for (int keyMgmt = 0; keyMgmt < WifiConfiguration.KeyMgmt.strings.length; keyMgmt++) { 157 if (keyMgmt == WifiConfiguration.KeyMgmt.NONE) continue; 158 WifiConfiguration config = new WifiConfiguration(); 159 config.allowedKeyManagement.clear(); 160 config.allowedKeyManagement.set(keyMgmt); 161 config.wepKeys = null; 162 163 assertFalse("Open network reported when key mgmt was set to " 164 + WifiConfiguration.KeyMgmt.strings[keyMgmt], config.isOpenNetwork()); 165 } 166 } 167 168 @Test 169 public void testIsOpenNetwork_NotOpen_HasAuthTypeNoneAndMore() { 170 WifiConfiguration config = new WifiConfiguration(); 171 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); 172 config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP); 173 config.wepKeys = null; 174 175 assertFalse(config.isOpenNetwork()); 176 } 177 178 @Test 179 public void testGetOrCreateRandomizedMacAddress_SavesAndReturnsSameAddress() { 180 WifiConfiguration config = new WifiConfiguration(); 181 MacAddress defaultMac = MacAddress.fromString(WifiInfo.DEFAULT_MAC_ADDRESS); 182 assertEquals(defaultMac, config.getRandomizedMacAddress()); 183 184 MacAddress firstMacAddress = config.getOrCreateRandomizedMacAddress(); 185 MacAddress secondMacAddress = config.getOrCreateRandomizedMacAddress(); 186 187 assertNotEquals(defaultMac, firstMacAddress); 188 assertEquals(firstMacAddress, secondMacAddress); 189 } 190 191 @Test 192 public void testSetRandomizedMacAddress_ChangesSavedAddress() { 193 WifiConfiguration config = new WifiConfiguration(); 194 MacAddress defaultMac = MacAddress.fromString(WifiInfo.DEFAULT_MAC_ADDRESS); 195 assertEquals(defaultMac, config.getRandomizedMacAddress()); 196 197 MacAddress macToChangeInto = MacAddress.createRandomUnicastAddress(); 198 config.setRandomizedMacAddress(macToChangeInto); 199 MacAddress macAfterChange = config.getRandomizedMacAddress(); 200 201 assertEquals(macToChangeInto, macAfterChange); 202 } 203 204 @Test 205 public void testGetOrCreateRandomizedMacAddress_ReRandomizesInvalidAddress() { 206 WifiConfiguration config = new WifiConfiguration(); 207 208 MacAddress defaultMac = MacAddress.fromString(WifiInfo.DEFAULT_MAC_ADDRESS); 209 MacAddress macAddressZeroes = MacAddress.ALL_ZEROS_ADDRESS; 210 MacAddress macAddressMulticast = MacAddress.fromString("03:ff:ff:ff:ff:ff"); 211 MacAddress macAddressGlobal = MacAddress.fromString("fc:ff:ff:ff:ff:ff"); 212 213 config.setRandomizedMacAddress(null); 214 MacAddress macAfterChange = config.getOrCreateRandomizedMacAddress(); 215 assertNotEquals(macAfterChange, null); 216 217 config.setRandomizedMacAddress(defaultMac); 218 macAfterChange = config.getOrCreateRandomizedMacAddress(); 219 assertNotEquals(macAfterChange, defaultMac); 220 221 config.setRandomizedMacAddress(macAddressZeroes); 222 macAfterChange = config.getOrCreateRandomizedMacAddress(); 223 assertNotEquals(macAfterChange, macAddressZeroes); 224 225 config.setRandomizedMacAddress(macAddressMulticast); 226 macAfterChange = config.getOrCreateRandomizedMacAddress(); 227 assertNotEquals(macAfterChange, macAddressMulticast); 228 229 config.setRandomizedMacAddress(macAddressGlobal); 230 macAfterChange = config.getOrCreateRandomizedMacAddress(); 231 assertNotEquals(macAfterChange, macAddressGlobal); 232 } 233 234 @Test 235 public void testSetRandomizedMacAddress_DoesNothingWhenNull() { 236 WifiConfiguration config = new WifiConfiguration(); 237 MacAddress defaultMac = MacAddress.fromString(WifiInfo.DEFAULT_MAC_ADDRESS); 238 config.setRandomizedMacAddress(null); 239 assertEquals(defaultMac, config.getRandomizedMacAddress()); 240 } 241 } 242