LegacyPasspointConfigParserTest.java revision ab83ab09b554e4012af5cf439efe081504a29b88
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
17package com.android.server.wifi.hotspot2;
18
19import static org.junit.Assert.*;
20
21import android.os.FileUtils;
22import android.test.suitebuilder.annotation.SmallTest;
23
24import org.junit.Test;
25
26import java.io.File;
27import java.io.IOException;
28import java.util.HashMap;
29import java.util.Map;
30
31/**
32 * Unit tests for {@link com.android.server.wifi.hotspot2.LegacyPasspointConfigParser}.
33 */
34@SmallTest
35public class LegacyPasspointConfigParserTest {
36    private static final String TEST_CONFIG =
37            "tree 3:1.2(urn:wfa:mo:hotspot2dot0-perprovidersubscription:1.0)\n"
38                    + "8:MgmtTree+\n"
39                    + "17:PerProviderSubscription+\n"
40                    + "4:r1i1+\n"
41                    + "6:HomeSP+\n"
42                    + "c:FriendlyName=12:Test Provider 1™\n"
43                    + "4:FQDN=9:test1.net\n"
44                    + "13:RoamingConsortiumOI=9:1234,5678\n"
45                    + ".\n"
46                    + "a:Credential+\n"
47                    + "10:UsernamePassword+\n"
48                    + "8:Username=5:user1\n"
49                    + "8:Password=5:pass1\n"
50                    + "\n"
51                    + "9:EAPMethod+\n"
52                    + "7:EAPType=2:21\n"
53                    + "b:InnerMethod=3:PAP\n"
54                    + ".\n"
55                    + ".\n"
56                    + "5:Realm=9:test1.com\n"
57                    + ".\n"
58                    + ".\n"
59                    + ".\n"
60                    + "17:PerProviderSubscription+\n"
61                    + "4:r1i2+\n"
62                    + "6:HomeSP+\n"
63                    + "c:FriendlyName=f:Test Provider 2\n"
64                    + "4:FQDN=9:test2.net\n"
65                    + ".\n"
66                    + "a:Credential+\n"
67                    + "3:SIM+\n"
68                    + "4:IMSI=4:1234\n"
69                    + "7:EAPType=2:18\n"
70                    + ".\n"
71                    + "5:Realm=9:test2.com\n"
72                    + ".\n"
73                    + ".\n"
74                    + ".\n"
75                    + ".\n";
76
77    /**
78     * Helper function for generating {@link LegacyPasspointConfig} objects based on the predefined
79     * test configuration string {@link #TEST_CONFIG}
80     *
81     * @return Map of FQDN to {@link LegacyPasspointConfig}
82     */
83    private Map<String, LegacyPasspointConfig> generateTestConfig() {
84        Map<String, LegacyPasspointConfig> configs = new HashMap<>();
85
86        LegacyPasspointConfig config1 = new LegacyPasspointConfig();
87        config1.mFqdn = "test1.net";
88        config1.mFriendlyName = "Test Provider 1™";
89        config1.mRoamingConsortiumOis = new long[] {0x1234, 0x5678};
90        config1.mRealm = "test1.com";
91        configs.put("test1.net", config1);
92
93        LegacyPasspointConfig config2 = new LegacyPasspointConfig();
94        config2.mFqdn = "test2.net";
95        config2.mFriendlyName = "Test Provider 2";
96        config2.mRealm = "test2.com";
97        config2.mImsi = "1234";
98        configs.put("test2.net", config2);
99
100        return configs;
101    }
102
103    /**
104     * Helper function for parsing configuration data.
105     *
106     * @param data The configuration data to parse
107     * @return Map of FQDN to {@link LegacyPasspointConfig}
108     * @throws Exception
109     */
110    private Map<String, LegacyPasspointConfig> parseConfig(String data) throws Exception {
111        // Write configuration data to file.
112        File configFile = File.createTempFile("LegacyPasspointConfig", "");
113        FileUtils.stringToFile(configFile, data);
114
115        // Parse the configuration file.
116        LegacyPasspointConfigParser parser = new LegacyPasspointConfigParser();
117        Map<String, LegacyPasspointConfig> configMap =
118                parser.parseConfig(configFile.getAbsolutePath());
119
120        configFile.delete();
121        return configMap;
122    }
123
124    /**
125     * Verify that the expected {@link LegacyPasspointConfig} objects are return when parsing
126     * predefined test configuration data {@link #TEST_CONFIG}.
127     *
128     * @throws Exception
129     */
130    @Test
131    public void parseTestConfig() throws Exception {
132        Map<String, LegacyPasspointConfig> parsedConfig = parseConfig(TEST_CONFIG);
133        assertEquals(generateTestConfig(), parsedConfig);
134    }
135
136    /**
137     * Verify that an empty map is return when parsing a configuration containing an empty
138     * configuration (MgmtTree).
139     *
140     * @throws Exception
141     */
142    @Test
143    public void parseEmptyConfig() throws Exception {
144        String emptyConfig = "tree 3:1.2(urn:wfa:mo:hotspot2dot0-perprovidersubscription:1.0)\n"
145                + "8:MgmtTree+\n"
146                + ".\n";
147        Map<String, LegacyPasspointConfig> parsedConfig = parseConfig(emptyConfig);
148        assertTrue(parsedConfig.isEmpty());
149    }
150
151    /**
152     * Verify that an empty map is return when parsing an empty configuration data.
153     *
154     * @throws Exception
155     */
156    @Test
157    public void parseEmptyData() throws Exception {
158        Map<String, LegacyPasspointConfig> parsedConfig = parseConfig("");
159        assertTrue(parsedConfig.isEmpty());
160    }
161
162    /**
163     * Verify that an IOException is thrown when parsing a configuration containing an unknown
164     * root name.  The expected root name is "MgmtTree".
165     *
166     * @throws Exception
167     */
168    @Test(expected = IOException.class)
169    public void parseConfigWithUnknownRootName() throws Exception {
170        String config = "tree 3:1.2(urn:wfa:mo:hotspot2dot0-perprovidersubscription:1.0)\n"
171                + "8:TestTest+\n"
172                + ".\n";
173        parseConfig(config);
174    }
175
176    /**
177     * Verify that an IOException is thrown when parsing a configuration containing a line with
178     * mismatched string length for the name.
179     *
180     * @throws Exception
181     */
182    @Test(expected = IOException.class)
183    public void parseConfigWithMismatchedStringLengthInName() throws Exception {
184        String config = "tree 3:1.2(urn:wfa:mo:hotspot2dot0-perprovidersubscription:1.0)\n"
185                + "9:MgmtTree+\n"
186                + ".\n";
187        parseConfig(config);
188    }
189
190    /**
191     * Verify that an IOException is thrown when parsing a configuration containing a line with
192     * mismatched string length for the value.
193     *
194     * @throws Exception
195     */
196    @Test(expected = IOException.class)
197    public void parseConfigWithMismatchedStringLengthInValue() throws Exception {
198        String config = "tree 3:1.2(urn:wfa:mo:hotspot2dot0-perprovidersubscription:1.0)\n"
199                + "8:MgmtTree+\n"
200                + "4:test=5:test\n"
201                + ".\n";
202        parseConfig(config);
203    }
204}
205