ANQPMatcherTest.java revision 87c6f1b149804685e46c18d2ad11262f611c9255
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 17package com.android.server.wifi.hotspot2; 18 19import static org.junit.Assert.assertEquals; 20import static org.junit.Assert.assertFalse; 21import static org.junit.Assert.assertTrue; 22 23import android.net.wifi.EAPConstants; 24import android.test.suitebuilder.annotation.SmallTest; 25 26import com.android.server.wifi.IMSIParameter; 27import com.android.server.wifi.hotspot2.anqp.CellularNetwork; 28import com.android.server.wifi.hotspot2.anqp.DomainNameElement; 29import com.android.server.wifi.hotspot2.anqp.NAIRealmData; 30import com.android.server.wifi.hotspot2.anqp.NAIRealmElement; 31import com.android.server.wifi.hotspot2.anqp.RoamingConsortiumElement; 32import com.android.server.wifi.hotspot2.anqp.ThreeGPPNetworkElement; 33import com.android.server.wifi.hotspot2.anqp.eap.AuthParam; 34import com.android.server.wifi.hotspot2.anqp.eap.EAPMethod; 35import com.android.server.wifi.hotspot2.anqp.eap.InnerAuthEAP; 36import com.android.server.wifi.hotspot2.anqp.eap.NonEAPInnerAuth; 37 38import org.junit.Test; 39 40import java.util.ArrayList; 41import java.util.Arrays; 42import java.util.HashMap; 43import java.util.HashSet; 44import java.util.List; 45import java.util.Map; 46import java.util.Set; 47 48/** 49 * Unit tests for {@link com.android.server.wifi.hotspot2.ANQPMatcher}. 50 */ 51@SmallTest 52public class ANQPMatcherTest { 53 /** 54 * Verify that domain name match will fail when a null Domain Name ANQP element is provided. 55 * 56 * @throws Exception 57 */ 58 @Test 59 public void matchDomainNameWithNullElement() throws Exception { 60 assertFalse(ANQPMatcher.matchDomainName(null, "test.com", null, null)); 61 } 62 63 /** 64 * Verify that domain name match will succeed when the specified FQDN matches a domain name 65 * in the Domain Name ANQP element. 66 * 67 * @throws Exception 68 */ 69 @Test 70 public void matchDomainNameUsingFQDN() throws Exception { 71 String fqdn = "test.com"; 72 String[] domains = new String[] {fqdn}; 73 DomainNameElement element = new DomainNameElement(Arrays.asList(domains)); 74 assertTrue(ANQPMatcher.matchDomainName(element, fqdn, null, null)); 75 } 76 77 /** 78 * Verify that domain name match will succeed when the specified IMSI parameter and IMSI list 79 * matches a 3GPP network domain in the Domain Name ANQP element. 80 * 81 * @throws Exception 82 */ 83 @Test 84 public void matchDomainNameUsingIMSI() throws Exception { 85 IMSIParameter imsiParam = new IMSIParameter("1234", true); 86 List<String> simImsiList = Arrays.asList(new String[] {"123457890", "123498723"}); 87 // 3GPP network domain with MCC=123 and MNC=456. 88 String[] domains = new String[] {"wlan.mnc457.mcc123.3gppnetwork.org"}; 89 DomainNameElement element = new DomainNameElement(Arrays.asList(domains)); 90 assertTrue(ANQPMatcher.matchDomainName(element, null, imsiParam, simImsiList)); 91 } 92 93 /** 94 * Verify that roaming consortium match will fail when a null Roaming Consortium ANQP 95 * element is provided. 96 * 97 * @throws Exception 98 */ 99 @Test 100 public void matchRoamingConsortiumWithNullElement() throws Exception { 101 assertFalse(ANQPMatcher.matchRoamingConsortium(null, new long[0])); 102 } 103 104 /** 105 * Verify that a roaming consortium match will succeed when the specified OI matches 106 * an OI in the Roaming Consortium ANQP element. 107 * 108 * @throws Exception 109 */ 110 @Test 111 public void matchRoamingConsortium() throws Exception { 112 long oi = 0x1234L; 113 RoamingConsortiumElement element = 114 new RoamingConsortiumElement(Arrays.asList(new Long[] {oi})); 115 assertTrue(ANQPMatcher.matchRoamingConsortium(element, new long[] {oi})); 116 } 117 118 /** 119 * Verify that an indeterminate match will be returned when matching a null NAI Realm 120 * ANQP element. 121 * 122 * @throws Exception 123 */ 124 @Test 125 public void matchNAIRealmWithNullElement() throws Exception { 126 assertEquals(AuthMatch.INDETERMINATE, ANQPMatcher.matchNAIRealm(null, "test.com", 127 EAPConstants.EAP_TLS, new InnerAuthEAP(EAPConstants.EAP_TTLS))); 128 } 129 130 /** 131 * Verify that an indeterminate match will be returned when matching a NAI Realm 132 * ANQP element contained no NAI realm data. 133 * 134 * @throws Exception 135 */ 136 @Test 137 public void matchNAIRealmWithEmtpyRealmData() throws Exception { 138 NAIRealmElement element = new NAIRealmElement(new ArrayList<NAIRealmData>()); 139 assertEquals(AuthMatch.INDETERMINATE, ANQPMatcher.matchNAIRealm(element, "test.com", 140 EAPConstants.EAP_TLS, null)); 141 } 142 143 /** 144 * Verify that a realm match will be returned when the specified realm matches a realm 145 * in the NAI Realm ANQP element with no EAP methods. 146 * 147 * @throws Exception 148 */ 149 @Test 150 public void matchNAIRealmWithRealmMatch() throws Exception { 151 String realm = "test.com"; 152 NAIRealmData realmData = new NAIRealmData( 153 Arrays.asList(new String[] {realm}), new ArrayList<EAPMethod>()); 154 NAIRealmElement element = new NAIRealmElement( 155 Arrays.asList(new NAIRealmData[] {realmData})); 156 assertEquals(AuthMatch.REALM, ANQPMatcher.matchNAIRealm(element, realm, 157 EAPConstants.EAP_TLS, null)); 158 } 159 160 /** 161 * Verify that a realm and method match will be returned when the specified realm and EAP 162 * method matches a realm in the NAI Realm ANQP element. 163 * 164 * @throws Exception 165 */ 166 @Test 167 public void matchNAIRealmWithRealmMethodMatch() throws Exception { 168 // Test data. 169 String realm = "test.com"; 170 int eapMethodID = EAPConstants.EAP_TLS; 171 172 // Setup NAI Realm element. 173 EAPMethod method = new EAPMethod(eapMethodID, new HashMap<Integer, Set<AuthParam>>()); 174 NAIRealmData realmData = new NAIRealmData( 175 Arrays.asList(new String[] {realm}), Arrays.asList(new EAPMethod[] {method})); 176 NAIRealmElement element = new NAIRealmElement( 177 Arrays.asList(new NAIRealmData[] {realmData})); 178 179 assertEquals(AuthMatch.REALM | AuthMatch.METHOD, 180 ANQPMatcher.matchNAIRealm(element, realm, eapMethodID, null)); 181 } 182 183 /** 184 * Verify that an exact match will be returned when the specified realm, EAP 185 * method, and the authentication parameter matches a realm with the associated EAP method and 186 * authentication parameter in the NAI Realm ANQP element. 187 * 188 * @throws Exception 189 */ 190 @Test 191 public void matchNAIRealmWithExactMatch() throws Exception { 192 // Test data. 193 String realm = "test.com"; 194 int eapMethodID = EAPConstants.EAP_TTLS; 195 NonEAPInnerAuth authParam = new NonEAPInnerAuth(NonEAPInnerAuth.AUTH_TYPE_MSCHAP); 196 Set<AuthParam> authSet = new HashSet<>(); 197 authSet.add(authParam); 198 Map<Integer, Set<AuthParam>> authMap = new HashMap<>(); 199 authMap.put(authParam.getAuthTypeID(), authSet); 200 201 // Setup NAI Realm element. 202 EAPMethod method = new EAPMethod(eapMethodID, authMap); 203 NAIRealmData realmData = new NAIRealmData( 204 Arrays.asList(new String[] {realm}), Arrays.asList(new EAPMethod[] {method})); 205 NAIRealmElement element = new NAIRealmElement( 206 Arrays.asList(new NAIRealmData[] {realmData})); 207 208 assertEquals(AuthMatch.EXACT, 209 ANQPMatcher.matchNAIRealm(element, realm, eapMethodID, authParam)); 210 } 211 212 /** 213 * Verify that a mismatch (AuthMatch.NONE) will be returned when the specified EAP method 214 * doesn't match with the corresponding EAP method in the NAI Realm ANQP element. 215 * 216 * @throws Exception 217 */ 218 @Test 219 public void matchNAIRealmWithEAPMethodMismatch() throws Exception { 220 // Test data. 221 String realm = "test.com"; 222 int eapMethodID = EAPConstants.EAP_TTLS; 223 NonEAPInnerAuth authParam = new NonEAPInnerAuth(NonEAPInnerAuth.AUTH_TYPE_MSCHAP); 224 Set<AuthParam> authSet = new HashSet<>(); 225 authSet.add(authParam); 226 Map<Integer, Set<AuthParam>> authMap = new HashMap<>(); 227 authMap.put(authParam.getAuthTypeID(), authSet); 228 229 // Setup NAI Realm element. 230 EAPMethod method = new EAPMethod(eapMethodID, authMap); 231 NAIRealmData realmData = new NAIRealmData( 232 Arrays.asList(new String[] {realm}), Arrays.asList(new EAPMethod[] {method})); 233 NAIRealmElement element = new NAIRealmElement( 234 Arrays.asList(new NAIRealmData[] {realmData})); 235 236 assertEquals(AuthMatch.NONE, 237 ANQPMatcher.matchNAIRealm(element, realm, EAPConstants.EAP_TLS, null)); 238 } 239 240 /** 241 * Verify that a mismatch (AuthMatch.NONE) will be returned when the specified authentication 242 * parameter doesn't match with the corresponding authentication parameter in the NAI Realm 243 * ANQP element. 244 * 245 * @throws Exception 246 */ 247 @Test 248 public void matchNAIRealmWithAuthTypeMismatch() throws Exception { 249 // Test data. 250 String realm = "test.com"; 251 int eapMethodID = EAPConstants.EAP_TTLS; 252 NonEAPInnerAuth authParam = new NonEAPInnerAuth(NonEAPInnerAuth.AUTH_TYPE_MSCHAP); 253 Set<AuthParam> authSet = new HashSet<>(); 254 authSet.add(authParam); 255 Map<Integer, Set<AuthParam>> authMap = new HashMap<>(); 256 authMap.put(authParam.getAuthTypeID(), authSet); 257 258 // Setup NAI Realm element. 259 EAPMethod method = new EAPMethod(eapMethodID, authMap); 260 NAIRealmData realmData = new NAIRealmData( 261 Arrays.asList(new String[] {realm}), Arrays.asList(new EAPMethod[] {method})); 262 NAIRealmElement element = new NAIRealmElement( 263 Arrays.asList(new NAIRealmData[] {realmData})); 264 265 // Mismatch in authentication type. 266 assertEquals(AuthMatch.NONE, 267 ANQPMatcher.matchNAIRealm(element, realm, EAPConstants.EAP_TTLS, 268 new NonEAPInnerAuth(NonEAPInnerAuth.AUTH_TYPE_PAP))); 269 } 270 271 /** 272 * Verify that 3GPP Network match will fail when a null element is provided. 273 * 274 * @throws Exception 275 */ 276 @Test 277 public void matchThreeGPPNetworkWithNullElement() throws Exception { 278 IMSIParameter imsiParam = new IMSIParameter("1234", true); 279 List<String> simImsiList = Arrays.asList(new String[] {"123456789", "123498723"}); 280 assertFalse(ANQPMatcher.matchThreeGPPNetwork(null, imsiParam, simImsiList)); 281 } 282 283 /** 284 * Verify that 3GPP network will succeed when the given 3GPP Network ANQP element contained 285 * a MCC-MNC that matches the both IMSI parameter and an IMSI from the IMSI list. 286 * 287 * @throws Exception 288 */ 289 @Test 290 public void matchThreeGPPNetwork() throws Exception { 291 IMSIParameter imsiParam = new IMSIParameter("1234", true); 292 List<String> simImsiList = Arrays.asList(new String[] {"123456789", "123498723"}); 293 294 CellularNetwork network = new CellularNetwork(Arrays.asList(new String[] {"123456"})); 295 ThreeGPPNetworkElement element = 296 new ThreeGPPNetworkElement(Arrays.asList(new CellularNetwork[] {network})); 297 // The MCC-MNC provided in 3GPP Network ANQP element matches both IMSI parameter 298 // and an IMSI from the installed SIM card. 299 assertTrue(ANQPMatcher.matchThreeGPPNetwork(element, imsiParam, simImsiList)); 300 } 301 302 /** 303 * Verify that 3GPP network will failed when the given 3GPP Network ANQP element contained 304 * a MCC-MNC that match the IMSI parameter but not the IMSI list. 305 * 306 * @throws Exception 307 */ 308 @Test 309 public void matchThreeGPPNetworkWithoutSimImsiMatch() throws Exception { 310 IMSIParameter imsiParam = new IMSIParameter("1234", true); 311 List<String> simImsiList = Arrays.asList(new String[] {"123457890", "123498723"}); 312 313 CellularNetwork network = new CellularNetwork(Arrays.asList(new String[] {"123456"})); 314 ThreeGPPNetworkElement element = 315 new ThreeGPPNetworkElement(Arrays.asList(new CellularNetwork[] {network})); 316 // The MCC-MNC provided in 3GPP Network ANQP element doesn't match any of the IMSIs 317 // from the installed SIM card. 318 assertFalse(ANQPMatcher.matchThreeGPPNetwork(element, imsiParam, simImsiList)); 319 } 320 321 /** 322 * Verify that 3GPP network will failed when the given 3GPP Network ANQP element contained 323 * a MCC-MNC that doesn't match with the IMSI parameter. 324 * 325 * @throws Exception 326 */ 327 @Test 328 public void matchThreeGPPNetworkWithImsiParamMismatch() throws Exception { 329 IMSIParameter imsiParam = new IMSIParameter("1234", true); 330 List<String> simImsiList = Arrays.asList(new String[] {"123457890", "123498723"}); 331 332 CellularNetwork network = new CellularNetwork(Arrays.asList(new String[] {"123356"})); 333 ThreeGPPNetworkElement element = 334 new ThreeGPPNetworkElement(Arrays.asList(new CellularNetwork[] {network})); 335 // The MCC-MNC provided in 3GPP Network ANQP element doesn't match the IMSI parameter. 336 assertFalse(ANQPMatcher.matchThreeGPPNetwork(element, imsiParam, simImsiList)); 337 } 338} 339