1 /* 2 * Copyright (C) 2017 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.cts; 18 19 import android.net.wifi.hotspot2.PasspointConfiguration; 20 import android.net.wifi.hotspot2.omadm.PpsMoParser; 21 import android.net.wifi.hotspot2.pps.Credential; 22 import android.net.wifi.hotspot2.pps.HomeSp; 23 import android.test.AndroidTestCase; 24 25 import java.io.BufferedReader; 26 import java.io.IOException; 27 import java.io.InputStream; 28 import java.io.InputStreamReader; 29 import java.text.DateFormat; 30 import java.text.SimpleDateFormat; 31 import java.util.ArrayList; 32 import java.util.Arrays; 33 import java.util.HashMap; 34 import java.util.List; 35 import java.util.Map; 36 37 /** 38 * CTS tests for PPS MO (PerProviderSubscription Management Object) XML string parsing API. 39 */ 40 public class PpsMoParserTest extends AndroidTestCase { 41 private static final String PPS_MO_XML_FILE = "assets/PerProviderSubscription.xml"; 42 43 /** 44 * Read the content of the given resource file into a String. 45 * 46 * @param filename String name of the file 47 * @return String 48 * @throws IOException 49 */ 50 private String loadResourceFile(String filename) throws IOException { 51 InputStream in = getClass().getClassLoader().getResourceAsStream(filename); 52 BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 53 StringBuilder builder = new StringBuilder(); 54 String line; 55 while ((line = reader.readLine()) != null) { 56 builder.append(line).append("\n"); 57 } 58 return builder.toString(); 59 } 60 61 /** 62 * Generate a {@link PasspointConfiguration} that matches the configuration specified in the 63 * XML file {@link #PPS_MO_XML_FILE}. 64 * 65 * @return {@link PasspointConfiguration} 66 */ 67 private PasspointConfiguration generateConfigurationFromPPSMOTree() throws Exception { 68 DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); 69 byte[] certFingerprint = new byte[32]; 70 Arrays.fill(certFingerprint, (byte) 0x1f); 71 72 PasspointConfiguration config = new PasspointConfiguration(); 73 74 // HomeSP configuration. 75 HomeSp homeSp = new HomeSp(); 76 homeSp.setFriendlyName("Century House"); 77 assertEquals("Century House", homeSp.getFriendlyName()); 78 homeSp.setFqdn("mi6.co.uk"); 79 assertEquals("mi6.co.uk", homeSp.getFqdn()); 80 homeSp.setRoamingConsortiumOis(new long[] {0x112233L, 0x445566L}); 81 assertTrue(Arrays.equals(new long[] {0x112233L, 0x445566L}, 82 homeSp.getRoamingConsortiumOis())); 83 config.setHomeSp(homeSp); 84 assertEquals(homeSp, config.getHomeSp()); 85 86 // Credential configuration. 87 Credential credential = new Credential(); 88 credential.setRealm("shaken.stirred.com"); 89 assertEquals("shaken.stirred.com", credential.getRealm()); 90 Credential.UserCredential userCredential = new Credential.UserCredential(); 91 userCredential.setUsername("james"); 92 assertEquals("james", userCredential.getUsername()); 93 userCredential.setPassword("Ym9uZDAwNw=="); 94 assertEquals("Ym9uZDAwNw==", userCredential.getPassword()); 95 userCredential.setEapType(21); 96 assertEquals(21, userCredential.getEapType()); 97 userCredential.setNonEapInnerMethod("MS-CHAP-V2"); 98 assertEquals("MS-CHAP-V2", userCredential.getNonEapInnerMethod()); 99 credential.setUserCredential(userCredential); 100 assertEquals(userCredential, credential.getUserCredential()); 101 Credential.CertificateCredential certCredential = new Credential.CertificateCredential(); 102 certCredential.setCertType("x509v3"); 103 assertEquals("x509v3", certCredential.getCertType()); 104 certCredential.setCertSha256Fingerprint(certFingerprint); 105 assertTrue(Arrays.equals(certFingerprint, certCredential.getCertSha256Fingerprint())); 106 credential.setCertCredential(certCredential); 107 assertEquals(certCredential, credential.getCertCredential()); 108 Credential.SimCredential simCredential = new Credential.SimCredential(); 109 simCredential.setImsi("imsi"); 110 assertEquals("imsi", simCredential.getImsi()); 111 simCredential.setEapType(24); 112 assertEquals(24, simCredential.getEapType()); 113 credential.setSimCredential(simCredential); 114 assertEquals(simCredential, credential.getSimCredential()); 115 config.setCredential(credential); 116 assertEquals(credential, config.getCredential()); 117 return config; 118 } 119 120 /** 121 * Parse and verify all supported fields under PPS MO tree. 122 * 123 * @throws Exception 124 */ 125 public void testParsePPSMOTree() throws Exception { 126 String ppsMoTree = loadResourceFile(PPS_MO_XML_FILE); 127 PasspointConfiguration expectedConfig = generateConfigurationFromPPSMOTree(); 128 PasspointConfiguration actualConfig = PpsMoParser.parseMoText(ppsMoTree); 129 assertTrue(actualConfig.equals(expectedConfig)); 130 } 131 } 132