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; 18 19import static org.junit.Assert.assertEquals; 20import static org.junit.Assert.assertNotNull; 21import static org.junit.Assert.assertTrue; 22import static org.mockito.Mockito.mock; 23 24import android.net.wifi.PasspointManagementObjectDefinition; 25import android.net.wifi.WifiEnterpriseConfig; 26import android.security.KeyStore; 27import android.test.suitebuilder.annotation.SmallTest; 28 29import com.android.server.wifi.hotspot2.omadm.MOTree; 30import com.android.server.wifi.hotspot2.omadm.OMAParser; 31import com.android.server.wifi.hotspot2.omadm.PasspointManagementObjectManager; 32import com.android.server.wifi.hotspot2.omadm.XMLNode; 33import com.android.server.wifi.hotspot2.pps.Credential; 34import com.android.server.wifi.hotspot2.pps.HomeSP; 35 36import org.junit.Rule; 37import org.junit.Test; 38import org.junit.rules.TemporaryFolder; 39import org.xml.sax.SAXException; 40 41import java.io.BufferedReader; 42import java.io.File; 43import java.io.FileOutputStream; 44import java.io.IOException; 45import java.io.InputStream; 46import java.io.InputStreamReader; 47import java.nio.charset.StandardCharsets; 48import java.util.ArrayList; 49import java.util.Collections; 50import java.util.HashMap; 51import java.util.HashSet; 52import java.util.List; 53import java.util.Set; 54 55/** 56 * Unit tests for {@link com.android.server.wifi.hotspot2.omadm.PasspointManagementObjectManager}. 57 */ 58@SmallTest 59public class PasspointManagementObjectManagerTest { 60 61 private static final String TAG = "PasspointManagementObjectManagerTest"; 62 63 @Rule 64 public TemporaryFolder tempFolder = new TemporaryFolder(); 65 66 private File createFileFromResource(String configFile) throws Exception { 67 InputStream in = getClass().getClassLoader().getResourceAsStream(configFile); 68 File file = tempFolder.newFile(configFile.split("/")[1]); 69 70 BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 71 FileOutputStream out = new FileOutputStream(file); 72 73 String line; 74 75 while ((line = reader.readLine()) != null) { 76 out.write(line.getBytes(StandardCharsets.UTF_8)); 77 } 78 79 out.flush(); 80 out.close(); 81 return file; 82 } 83 84 private void addMoFromWifiConfig(PasspointManagementObjectManager moMgr) throws Exception { 85 HashMap<String, Long> ssidMap = new HashMap<>(); 86 String fqdn = "tunisia.org"; 87 HashSet<Long> roamingConsortiums = new HashSet<Long>(); 88 HashSet<String> otherHomePartners = new HashSet<String>(); 89 Set<Long> matchAnyOIs = new HashSet<Long>(); 90 List<Long> matchAllOIs = new ArrayList<Long>(); 91 String friendlyName = "Tunisian Passpoint Provider"; 92 String iconUrl = "http://www.tunisia.org/icons/base_icon.png"; 93 94 WifiEnterpriseConfig enterpriseConfig = new WifiEnterpriseConfig(); 95 enterpriseConfig.setEapMethod(WifiEnterpriseConfig.Eap.TTLS); 96 enterpriseConfig.setPhase2Method(WifiEnterpriseConfig.Phase2.PAP); 97 enterpriseConfig.setIdentity("testIdentity1"); 98 enterpriseConfig.setPassword("AnDrO1D"); 99 100 KeyStore keyStore = mock(KeyStore.class); 101 Credential credential = new Credential(enterpriseConfig, keyStore, false); 102 103 HomeSP newHomeSP = new HomeSP(Collections.<String, Long>emptyMap(), fqdn, 104 roamingConsortiums, Collections.<String>emptySet(), 105 Collections.<Long>emptySet(), Collections.<Long>emptyList(), 106 friendlyName, null, credential); 107 108 moMgr.addSP(newHomeSP); 109 } 110 111 private void addMoFromXml(PasspointManagementObjectManager moMgr) throws Exception { 112 InputStream in = getClass().getClassLoader().getResourceAsStream(R2_TTLS_XML_FILE); 113 BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 114 StringBuilder builder = new StringBuilder(); 115 String line; 116 while ((line = reader.readLine()) != null) { 117 builder.append(line).append("\n"); 118 } 119 120 String xml = builder.toString(); 121 moMgr.addSP(xml); 122 } 123 124 private static final String R1_CONFIG_FILE = "assets/r1.PerProviderSubscription.conf"; 125 private static final String R2_CONFIG_FILE = "assets/r2.PerProviderSubscription.conf"; 126 private static final String R2_TTLS_XML_FILE = "assets/r2-ttls-tree.xml"; 127 private static final String RUCKUS_CONFIG_FILE = "assets/ruckus.PerProviderSubscription.conf"; 128 129 /** ensure that loading R1 configs works */ 130 @Test 131 public void loadR1Configs() throws Exception { 132 File file = createFileFromResource(R1_CONFIG_FILE); 133 PasspointManagementObjectManager moMgr = new PasspointManagementObjectManager(file, true); 134 List<HomeSP> homeSPs = moMgr.loadAllSPs(); 135 assertEquals(2, homeSPs.size()); 136 137 /* TODO: Verify more attributes */ 138 HomeSP homeSP = moMgr.getHomeSP("twcwifi.com"); 139 assertNotNull(homeSP); 140 assertEquals("TWC-WiFi", homeSP.getFriendlyName()); 141 assertEquals("tushar4", homeSP.getCredential().getUserName()); 142 143 homeSP = moMgr.getHomeSP("wi-fi.org"); 144 assertNotNull(homeSP); 145 assertEquals("Wi-Fi Alliance", homeSP.getFriendlyName()); 146 assertEquals("sta006", homeSP.getCredential().getUserName()); 147 } 148 149 /** ensure that loading R2 configs works */ 150 @Test 151 public void loadR2Configs() throws Exception { 152 File file = createFileFromResource(R2_CONFIG_FILE); 153 PasspointManagementObjectManager moMgr = new PasspointManagementObjectManager(file, true); 154 List<HomeSP> homeSPs = moMgr.loadAllSPs(); 155 assertEquals(2, homeSPs.size()); 156 157 /* TODO: Verify more attributes */ 158 HomeSP homeSP = moMgr.getHomeSP("twcwifi.com"); 159 assertNotNull(homeSP); 160 assertEquals("TWC-WiFi", homeSP.getFriendlyName()); 161 assertEquals("tushar4", homeSP.getCredential().getUserName()); 162 163 homeSP = moMgr.getHomeSP("wi-fi.org"); 164 assertNotNull(homeSP); 165 assertEquals("Wi-Fi Alliance", homeSP.getFriendlyName()); 166 assertEquals("sta015", homeSP.getCredential().getUserName()); 167 } 168 169 /** verify adding a new service provider works */ 170 @Test 171 public void addSP() throws Exception { 172 File file = tempFolder.newFile("PerProviderSubscription.conf"); 173 PasspointManagementObjectManager moMgr = new PasspointManagementObjectManager(file, true); 174 List<HomeSP> homeSPs = moMgr.loadAllSPs(); 175 assertEquals(0, homeSPs.size()); 176 177 addMoFromWifiConfig(moMgr); 178 addMoFromXml(moMgr); 179 180 PasspointManagementObjectManager moMgr2 = new PasspointManagementObjectManager(file, true); 181 homeSPs = moMgr2.loadAllSPs(); 182 assertEquals(2, homeSPs.size()); 183 184 /* TODO: Verify more attributes */ 185 HomeSP homeSP = moMgr2.getHomeSP("rk-ttls.org"); 186 assertNotNull(homeSP); 187 assertEquals("RK TTLS", homeSP.getFriendlyName()); 188 assertEquals("sta020", homeSP.getCredential().getUserName()); 189 190 homeSP = moMgr.getHomeSP("tunisia.org"); 191 assertNotNull(homeSP); 192 assertEquals("Tunisian Passpoint Provider", homeSP.getFriendlyName()); 193 assertEquals("testIdentity1", homeSP.getCredential().getUserName()); 194 } 195 196 /** Verify IOException is thrown when trying to add a SP from a null XML string. */ 197 @Test(expected = IOException.class) 198 public void addSPFromNullXmlString() throws Exception { 199 File file = tempFolder.newFile("PerProviderSubscription.conf"); 200 PasspointManagementObjectManager moMgr = new PasspointManagementObjectManager(file, true); 201 String xml = null; // Needed to avoid ambiguity on function call. 202 moMgr.addSP(xml); 203 } 204 205 /** Verify IOException is thrown when trying to build a SP from a null XML string. */ 206 @Test(expected = IOException.class) 207 public void buildSPFromNullXmlString() throws Exception { 208 PasspointManagementObjectManager.buildSP(null); 209 } 210 211 /** verify that xml serialization/deserialization works */ 212 public void checkXml() throws Exception { 213 InputStream in = getClass().getClassLoader().getResourceAsStream(R2_TTLS_XML_FILE); 214 BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 215 StringBuilder builder = new StringBuilder(); 216 String line; 217 while ((line = reader.readLine()) != null) { 218 builder.append(line).append("\n"); 219 } 220 221 String xmlIn = builder.toString(); 222 223 try { 224 // Parse the file content: 225 OMAParser parser = new OMAParser(); 226 MOTree moTree = parser.parse(xmlIn, ""); 227 XMLNode rootIn = parser.getRoot(); 228 229 // Serialize it back out: 230 String xmlOut = moTree.toXml(); 231 parser = new OMAParser(); 232 // And parse it again: 233 parser.parse(xmlOut, ""); 234 XMLNode rootOut = parser.getRoot(); 235 236 // Compare the two roots: 237 assertTrue("Checking serialized XML", rootIn.equals(rootOut)); 238 } catch (SAXException se) { 239 throw new IOException(se); 240 } 241 } 242 243 /** verify modifying an existing service provider works */ 244 @Test 245 public void modifySP() throws Exception { 246 File file = createFileFromResource(R2_CONFIG_FILE); 247 PasspointManagementObjectManager moMgr = new PasspointManagementObjectManager(file, true); 248 List<HomeSP> homeSPs = moMgr.loadAllSPs(); 249 assertEquals(2, homeSPs.size()); 250 251 /* verify that wi-fi.org has update identifier of 1 */ 252 HomeSP homeSP = moMgr.getHomeSP("wi-fi.org"); 253 assertNotNull(homeSP); 254 assertEquals("Wi-Fi Alliance", homeSP.getFriendlyName()); 255 assertEquals("sta015", homeSP.getCredential().getUserName()); 256 assertEquals(1, homeSP.getUpdateIdentifier()); 257 258 /* PasspointManagementObjectDefinition to change update identifier */ 259 String urn = "wfa:mo:hotspot2dot0-perprovidersubscription:1.0"; 260 String baseUri = "./Wi-Fi/wi-fi.org/PerProviderSubscription/UpdateIdentifier"; 261 String xmlTree = 262 "<MgmtTree>\n" 263 + " <VerDTD>1.2</VerDTD>\n" 264 + " <Node>\n" 265 + " <NodeName>UpdateIdentifier</NodeName>\n" 266 + " <Value>9</Value>\n" 267 + " </Node>\n" 268 + "</MgmtTree>"; 269 270 PasspointManagementObjectDefinition moDef = 271 new PasspointManagementObjectDefinition(baseUri, urn, xmlTree); 272 List<PasspointManagementObjectDefinition> moDefs = 273 new ArrayList<PasspointManagementObjectDefinition>(); 274 moDefs.add(moDef); 275 moMgr.modifySP("wi-fi.org", moDefs); 276 277 /* reload all the SPs again */ 278 moMgr.loadAllSPs(); 279 280 homeSP = moMgr.getHomeSP("wi-fi.org"); 281 assertEquals("Wi-Fi Alliance", homeSP.getFriendlyName()); 282 assertEquals("sta015", homeSP.getCredential().getUserName()); 283 assertEquals(9, homeSP.getUpdateIdentifier()); 284 } 285 286 /** Verify IOException is thrown when trying to modify a SP using a null XML string. */ 287 @Test(expected = IOException.class) 288 public void modifySPFromNullXmlString() throws Exception { 289 File file = createFileFromResource(R2_CONFIG_FILE); 290 PasspointManagementObjectManager moMgr = new PasspointManagementObjectManager(file, true); 291 List<HomeSP> homeSPs = moMgr.loadAllSPs(); 292 assertEquals(2, homeSPs.size()); 293 294 /* PasspointManagementObjectDefinition with null xmlTree. */ 295 String urn = "wfa:mo:hotspot2dot0-perprovidersubscription:1.0"; 296 String baseUri = "./Wi-Fi/wi-fi.org/PerProviderSubscription/UpdateIdentifier"; 297 String xmlTree = null; 298 299 PasspointManagementObjectDefinition moDef = 300 new PasspointManagementObjectDefinition(baseUri, urn, xmlTree); 301 List<PasspointManagementObjectDefinition> moDefs = 302 new ArrayList<PasspointManagementObjectDefinition>(); 303 moDefs.add(moDef); 304 moMgr.modifySP("wi-fi.org", moDefs); 305 } 306 307 /** verify removing an existing service provider works */ 308 @Test 309 public void removeSP() throws Exception { 310 File file = createFileFromResource(R2_CONFIG_FILE); 311 PasspointManagementObjectManager moMgr = new PasspointManagementObjectManager(file, true); 312 List<HomeSP> homeSPs = moMgr.loadAllSPs(); 313 assertEquals(2, homeSPs.size()); 314 315 moMgr.removeSP("wi-fi.org"); 316 317 homeSPs = moMgr.loadAllSPs(); 318 assertEquals(null, moMgr.getHomeSP("wi-fi.org")); 319 } 320} 321 322 323 324 325 326 327 328 329 330