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 that xml serialization/deserialization works */
197    public void checkXml() throws Exception {
198        InputStream in = getClass().getClassLoader().getResourceAsStream(R2_TTLS_XML_FILE);
199        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
200        StringBuilder builder = new StringBuilder();
201        String line;
202        while ((line = reader.readLine()) != null) {
203            builder.append(line).append("\n");
204        }
205
206        String xmlIn = builder.toString();
207
208        try {
209            // Parse the file content:
210            OMAParser parser = new OMAParser();
211            MOTree moTree = parser.parse(xmlIn, "");
212            XMLNode rootIn = parser.getRoot();
213
214            // Serialize it back out:
215            String xmlOut = moTree.toXml();
216            parser = new OMAParser();
217            // And parse it again:
218            parser.parse(xmlOut, "");
219            XMLNode rootOut = parser.getRoot();
220
221            // Compare the two roots:
222            assertTrue("Checking serialized XML", rootIn.equals(rootOut));
223        } catch (SAXException se) {
224            throw new IOException(se);
225        }
226    }
227
228    /** verify modifying an existing service provider works */
229    @Test
230    public void modifySP() throws Exception {
231        File file = createFileFromResource(R2_CONFIG_FILE);
232        PasspointManagementObjectManager moMgr = new PasspointManagementObjectManager(file, true);
233        List<HomeSP> homeSPs = moMgr.loadAllSPs();
234        assertEquals(2, homeSPs.size());
235
236        /* verify that wi-fi.org has update identifier of 1 */
237        HomeSP homeSP = moMgr.getHomeSP("wi-fi.org");
238        assertNotNull(homeSP);
239        assertEquals("Wi-Fi Alliance", homeSP.getFriendlyName());
240        assertEquals("sta015", homeSP.getCredential().getUserName());
241        assertEquals(1, homeSP.getUpdateIdentifier());
242
243        /* PasspointManagementObjectDefinition to change update identifier */
244        String urn = "wfa:mo:hotspot2dot0-perprovidersubscription:1.0";
245        String baseUri = "./Wi-Fi/wi-fi.org/PerProviderSubscription/UpdateIdentifier";
246        String xmlTree =
247                  "<MgmtTree>\n"
248                + "     <VerDTD>1.2</VerDTD>\n"
249                + "     <Node>\n"
250                + "         <NodeName>UpdateIdentifier</NodeName>\n"
251                + "         <Value>9</Value>\n"
252                + "     </Node>\n"
253                + "</MgmtTree>";
254
255        PasspointManagementObjectDefinition moDef =
256                new PasspointManagementObjectDefinition(baseUri, urn, xmlTree);
257        List<PasspointManagementObjectDefinition> moDefs =
258                new ArrayList<PasspointManagementObjectDefinition>();
259        moDefs.add(moDef);
260        moMgr.modifySP("wi-fi.org", moDefs);
261
262        /* reload all the SPs again */
263        moMgr.loadAllSPs();
264
265        homeSP = moMgr.getHomeSP("wi-fi.org");
266        assertEquals("Wi-Fi Alliance", homeSP.getFriendlyName());
267        assertEquals("sta015", homeSP.getCredential().getUserName());
268        assertEquals(9, homeSP.getUpdateIdentifier());
269    }
270
271    /** verify removing an existing service provider works */
272    @Test
273    public void removeSP() throws Exception {
274        File file = createFileFromResource(R2_CONFIG_FILE);
275        PasspointManagementObjectManager moMgr = new PasspointManagementObjectManager(file, true);
276        List<HomeSP> homeSPs = moMgr.loadAllSPs();
277        assertEquals(2, homeSPs.size());
278
279        moMgr.removeSP("wi-fi.org");
280
281        homeSPs = moMgr.loadAllSPs();
282        assertEquals(null, moMgr.getHomeSP("wi-fi.org"));
283    }
284}
285
286
287
288
289
290
291
292
293
294