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.hotspot2.pps; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertFalse; 21 import static org.junit.Assert.assertTrue; 22 23 import android.os.Parcel; 24 25 import androidx.test.filters.SmallTest; 26 27 import org.junit.Test; 28 29 import java.nio.charset.StandardCharsets; 30 import java.util.ArrayList; 31 import java.util.Arrays; 32 import java.util.Collection; 33 import java.util.HashMap; 34 import java.util.Map; 35 36 /** 37 * Unit tests for {@link android.net.wifi.hotspot2.pps.HomeSp}. 38 */ 39 @SmallTest 40 public class HomeSpTest { 41 private static final String[] OTHER_HOME_PARTNER_LIST = new String[]{"partner1", "partner2"}; 42 43 /** 44 * Helper function for creating a map of home network IDs for testing. 45 * 46 * @return Map of home network IDs 47 */ createHomeNetworkIds()48 private static Map<String, Long> createHomeNetworkIds() { 49 Map<String, Long> homeNetworkIds = new HashMap<>(); 50 homeNetworkIds.put("ssid", 0x1234L); 51 homeNetworkIds.put("nullhessid", null); 52 return homeNetworkIds; 53 } 54 55 /** 56 * Helper function for creating a HomeSp for testing. 57 * 58 * @param homeNetworkIds The map of home network IDs associated with HomeSp 59 * @return {@link HomeSp} 60 */ createHomeSp(Map<String, Long> homeNetworkIds)61 private static HomeSp createHomeSp(Map<String, Long> homeNetworkIds) { 62 HomeSp homeSp = new HomeSp(); 63 homeSp.setFqdn("fqdn"); 64 homeSp.setFriendlyName("friendly name"); 65 homeSp.setIconUrl("icon.url"); 66 homeSp.setHomeNetworkIds(homeNetworkIds); 67 homeSp.setMatchAllOis(new long[] {0x11L, 0x22L}); 68 homeSp.setMatchAnyOis(new long[] {0x33L, 0x44L}); 69 homeSp.setOtherHomePartners(OTHER_HOME_PARTNER_LIST); 70 homeSp.setRoamingConsortiumOis(new long[] {0x55, 0x66}); 71 return homeSp; 72 } 73 74 /** 75 * Helper function for creating a HomeSp with home network IDs for testing. 76 * 77 * @return {@link HomeSp} 78 */ createHomeSpWithHomeNetworkIds()79 private static HomeSp createHomeSpWithHomeNetworkIds() { 80 return createHomeSp(createHomeNetworkIds()); 81 } 82 83 /** 84 * Helper function for creating a HomeSp without home network IDs for testing. 85 * 86 * @return {@link HomeSp} 87 */ createHomeSpWithoutHomeNetworkIds()88 private static HomeSp createHomeSpWithoutHomeNetworkIds() { 89 return createHomeSp(null); 90 } 91 92 /** 93 * Helper function for verifying HomeSp after parcel write then read. 94 * @param writeHomeSp 95 * @throws Exception 96 */ verifyParcel(HomeSp writeHomeSp)97 private static void verifyParcel(HomeSp writeHomeSp) throws Exception { 98 Parcel parcel = Parcel.obtain(); 99 writeHomeSp.writeToParcel(parcel, 0); 100 101 parcel.setDataPosition(0); // Rewind data position back to the beginning for read. 102 HomeSp readHomeSp = HomeSp.CREATOR.createFromParcel(parcel); 103 assertTrue(readHomeSp.equals(writeHomeSp)); 104 } 105 106 /** 107 * Verify parcel read/write for an empty HomeSp. 108 * 109 * @throws Exception 110 */ 111 @Test verifyParcelWithEmptyHomeSp()112 public void verifyParcelWithEmptyHomeSp() throws Exception { 113 verifyParcel(new HomeSp()); 114 } 115 116 /** 117 * Verify parcel read/write for a HomeSp containing Home Network IDs. 118 * 119 * @throws Exception 120 */ 121 @Test verifyParcelWithHomeNetworkIds()122 public void verifyParcelWithHomeNetworkIds() throws Exception { 123 verifyParcel(createHomeSpWithHomeNetworkIds()); 124 } 125 126 /** 127 * Verify parcel read/write for a HomeSp without Home Network IDs. 128 * 129 * @throws Exception 130 */ 131 @Test verifyParcelWithoutHomeNetworkIds()132 public void verifyParcelWithoutHomeNetworkIds() throws Exception { 133 verifyParcel(createHomeSpWithoutHomeNetworkIds()); 134 } 135 136 /** 137 * Verify that a HomeSp is valid when both FQDN and Friendly Name 138 * are provided. 139 * 140 * @throws Exception 141 */ 142 @Test validateValidHomeSp()143 public void validateValidHomeSp() throws Exception { 144 HomeSp homeSp = createHomeSpWithHomeNetworkIds(); 145 assertTrue(homeSp.validate()); 146 } 147 148 /** 149 * Verify that a HomeSp is not valid when FQDN is not provided 150 * 151 * @throws Exception 152 */ 153 @Test validateHomeSpWithoutFqdn()154 public void validateHomeSpWithoutFqdn() throws Exception { 155 HomeSp homeSp = createHomeSpWithHomeNetworkIds(); 156 homeSp.setFqdn(null); 157 assertFalse(homeSp.validate()); 158 } 159 160 /** 161 * Verify that a HomeSp is not valid when Friendly Name is not provided 162 * 163 * @throws Exception 164 */ 165 @Test validateHomeSpWithoutFriendlyName()166 public void validateHomeSpWithoutFriendlyName() throws Exception { 167 HomeSp homeSp = createHomeSpWithHomeNetworkIds(); 168 homeSp.setFriendlyName(null); 169 assertFalse(homeSp.validate()); 170 } 171 172 /** 173 * Verify that a HomeSp is valid when the optional Home Network IDs are 174 * not provided. 175 * 176 * @throws Exception 177 */ 178 @Test validateHomeSpWithoutHomeNetworkIds()179 public void validateHomeSpWithoutHomeNetworkIds() throws Exception { 180 HomeSp homeSp = createHomeSpWithoutHomeNetworkIds(); 181 assertTrue(homeSp.validate()); 182 } 183 184 /** 185 * Verify that a HomeSp is invalid when the optional Home Network IDs 186 * contained an invalid SSID (exceeding maximum number of bytes). 187 * 188 * @throws Exception 189 */ 190 @Test validateHomeSpWithInvalidHomeNetworkIds()191 public void validateHomeSpWithInvalidHomeNetworkIds() throws Exception { 192 HomeSp homeSp = createHomeSpWithoutHomeNetworkIds(); 193 // HomeNetworkID with SSID exceeding the maximum length. 194 Map<String, Long> homeNetworkIds = new HashMap<>(); 195 byte[] rawSsidBytes = new byte[33]; 196 Arrays.fill(rawSsidBytes, (byte) 'a'); 197 homeNetworkIds.put(new String(rawSsidBytes, StandardCharsets.UTF_8), 0x1234L); 198 homeSp.setHomeNetworkIds(homeNetworkIds); 199 assertFalse(homeSp.validate()); 200 } 201 202 /** 203 * Verify that copy constructor works when pass in a null source. 204 * 205 * @throws Exception 206 */ 207 @Test validateCopyConstructorFromNullSource()208 public void validateCopyConstructorFromNullSource() throws Exception { 209 HomeSp copySp = new HomeSp(null); 210 HomeSp defaultSp = new HomeSp(); 211 assertTrue(copySp.equals(defaultSp)); 212 } 213 214 /** 215 * Verify that copy constructor works when pass in a valid source. 216 * 217 * @throws Exception 218 */ 219 @Test validateCopyConstructorFromValidSource()220 public void validateCopyConstructorFromValidSource() throws Exception { 221 HomeSp sourceSp = createHomeSpWithHomeNetworkIds(); 222 HomeSp copySp = new HomeSp(sourceSp); 223 assertTrue(copySp.equals(sourceSp)); 224 } 225 226 /** 227 * Verify that the getOtherHomePartnersList gets the list of partners as expected. 228 * 229 * @throws Exception 230 */ 231 @Test validateGetOtherHomePartnersList()232 public void validateGetOtherHomePartnersList() throws Exception { 233 HomeSp homeSp = createHomeSpWithoutHomeNetworkIds(); 234 235 Collection<String> otherHomePartnersList = homeSp.getOtherHomePartnersList(); 236 assertEquals(2, otherHomePartnersList.size()); 237 assertTrue(Arrays.equals(OTHER_HOME_PARTNER_LIST, otherHomePartnersList.toArray())); 238 } 239 240 /** 241 * Verify that the setOtherHomePartnersList sets the list of partners as expected. 242 * 243 * @throws Exception 244 */ 245 @Test validateSetOtherHomePartnersList()246 public void validateSetOtherHomePartnersList() throws Exception { 247 HomeSp homeSp = createHomeSpWithoutHomeNetworkIds(); 248 249 final Collection<String> homePartners = 250 new ArrayList<>(Arrays.asList(OTHER_HOME_PARTNER_LIST)); 251 252 homeSp.setOtherHomePartnersList(homePartners); 253 assertTrue(Arrays.equals(homeSp.getOtherHomePartners(), OTHER_HOME_PARTNER_LIST)); 254 } 255 } 256