• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assert.assertNull;
24 import static org.junit.Assert.assertThrows;
25 import static org.mockito.Mockito.CALLS_REAL_METHODS;
26 
27 import androidx.test.filters.SmallTest;
28 
29 import com.android.dx.mockito.inline.extended.ExtendedMockito;
30 
31 import com.google.common.collect.ImmutableList;
32 
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.mockito.MockitoSession;
37 
38 import java.util.Collections;
39 import java.util.List;
40 
41 /** Unit tests for {@link com.android.server.wifi.WifiUriParser}. */
42 @SmallTest
43 public class WifiUriParserTest {
44 
45     private static final String TEST_DPP_INFORMATION = "Easy_Connect_Demo";
46     private static final String TEST_DPP_PUBLIC_KEY = "MDkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDIg"
47             + "ACDmtXD1Sz6/5B4YRdmTkbkkFLDwk8f0yRnfm1Gokpx/0=";
48     private static final String TEST_DPP_URI = "DPP:C:81/1;I:" + TEST_DPP_INFORMATION
49             + ";K:" + TEST_DPP_PUBLIC_KEY + ";;";
50 
51     private MockitoSession mSession;
52 
53     @Before
setUp()54     public void setUp() throws Exception {
55         mSession = ExtendedMockito.mockitoSession()
56                 .initMocks(this)
57                 .mockStatic(WifiUriParser.class, CALLS_REAL_METHODS)
58                 .startMocking();
59     }
60 
61     /**
62      * Called after each test
63      */
64     @After
cleanup()65     public void cleanup() {
66         if (mSession != null) {
67             mSession.finishMocking();
68         }
69     }
70 
verifyZxParsing( UriParserResults uri, String expectedSSID, List<SecurityParams> expectedSecurityParamsList, String expectedPreShareKey, boolean isWep)71     private void verifyZxParsing(
72             UriParserResults uri,
73             String expectedSSID,
74             List<SecurityParams> expectedSecurityParamsList,
75             String expectedPreShareKey,
76             boolean isWep) {
77         assertNotNull(uri);
78         WifiConfiguration config = uri.getWifiConfiguration();
79         assertNotNull(config);
80         assertThat(config.SSID).isEqualTo(expectedSSID);
81         if (isWep) {
82             assertThat(config.wepKeys[0]).isEqualTo(expectedPreShareKey);
83         } else {
84             assertThat(config.preSharedKey).isEqualTo(expectedPreShareKey);
85         }
86         List<SecurityParams> configSecurityParamsList = config.getSecurityParamsList();
87         assertEquals(expectedSecurityParamsList, configSecurityParamsList);
88         assertNull(uri.getPublicKey());
89         assertNull(uri.getInformation());
90         assertEquals(UriParserResults.URI_SCHEME_ZXING_WIFI_NETWORK_CONFIG, uri.getUriScheme());
91     }
92 
93     @Test
testZxParsing()94     public void testZxParsing() {
95         ExtendedMockito.when(WifiUriParser.mockableIsFlagNewUriParsingForEscapeCharacterEnabled())
96                 .thenReturn(false);
97         testZxParsing(false);
98         ExtendedMockito.when(WifiUriParser.mockableIsFlagNewUriParsingForEscapeCharacterEnabled())
99                 .thenReturn(true);
100         testZxParsing(true);
101     }
102 
testZxParsing(boolean isNewParserSupported)103     public void testZxParsing(boolean isNewParserSupported) {
104         // Test no password
105         List<SecurityParams> expectedSecurityParamsList =
106                 ImmutableList.of(
107                         SecurityParams.createSecurityParamsBySecurityType(
108                                 WifiConfiguration.SECURITY_TYPE_OPEN),
109                         SecurityParams.createSecurityParamsBySecurityType(
110                                 WifiConfiguration.SECURITY_TYPE_OWE));
111         UriParserResults uri = WifiUriParser.parseUri("WIFI:S:testAbC;T:nopass");
112         verifyZxParsing(
113                 uri,
114                 "\"testAbC\"",
115                 expectedSecurityParamsList,
116                 null,
117                 false);
118         // invalid code but it should work.
119         uri = WifiUriParser.parseUri("WIFI:S:testAbC; T:nopass");
120         verifyZxParsing(
121                 uri,
122                 "\"testAbC\"",
123                 expectedSecurityParamsList,
124                 null,
125                 false);
126         // Unknown prefix tag & extra ; should keep work. (either new or old parsing)
127         uri = WifiUriParser.parseUri("WIFI:SSS:456 ;; S: test 123\\;; T:nopass");
128         verifyZxParsing(
129                 uri,
130                 "\" test 123;\"",
131                 expectedSecurityParamsList,
132                 null,
133                 false);
134 
135         if (isNewParserSupported) {
136             // The \\ in the end of ssid but it should work.
137             uri = WifiUriParser.parseUri("WIFI:S:testAbC\\\\; T:nopass");
138             verifyZxParsing(
139                     uri,
140                     "\"testAbC\\\"",
141                     expectedSecurityParamsList,
142                     null,
143                     false);
144 
145             // The \; and \\ in the end of ssid but it should work.
146             uri = WifiUriParser.parseUri("WIFI:S:test 123\\;\\\\\\;; T:nopass");
147             verifyZxParsing(
148                     uri,
149                     "\"test 123;\\;\"",
150                     expectedSecurityParamsList,
151                     null,
152                     false);
153         }
154         // Test WEP
155         expectedSecurityParamsList =
156                 ImmutableList.of(
157                         SecurityParams.createSecurityParamsBySecurityType(
158                                 WifiConfiguration.SECURITY_TYPE_WEP));
159         uri = WifiUriParser.parseUri("WIFI:S:reallyLONGone;T:WEP;P:somepasswo#%^**123rd");
160         verifyZxParsing(
161                 uri,
162                 "\"reallyLONGone\"",
163                 expectedSecurityParamsList,
164                 "\"somepasswo#%^**123rd\"",
165                 true);
166         // invalid code (space before pre-fix) but it should work.
167         uri = WifiUriParser.parseUri("WIFI:S:reallyLONGone;T:WEP; P:somepassword");
168         verifyZxParsing(
169                 uri,
170                 "\"reallyLONGone\"",
171                 expectedSecurityParamsList,
172                 "\"somepassword\"",
173                 true);
174 
175         // Test WPA & space as part of SSID and passphrase.
176         expectedSecurityParamsList =
177                 ImmutableList.of(
178                         SecurityParams.createSecurityParamsBySecurityType(
179                                 WifiConfiguration.SECURITY_TYPE_PSK));
180         uri = WifiUriParser.parseUri("WIFI:S:another one;T:WPA;P:3#=3j9 asicla");
181         verifyZxParsing(
182                 uri,
183                 "\"another one\"",
184                 expectedSecurityParamsList,
185                 "\"3#=3j9 asicla\"",
186                 false);
187 
188         if (isNewParserSupported) {
189             // The " in the start and end of ssid but it should work.
190             uri = WifiUriParser.parseUri("WIFI:S:\"\"\"\"; T:WPA; P:\"\"");
191             verifyZxParsing(
192                     uri,
193                     "\"\"\"\"\"\"",
194                     expectedSecurityParamsList,
195                     "\"\"\"\"",
196                     false);
197         }
198 
199         // invalid code but it should work.
200         uri = WifiUriParser.parseUri("WIFI: S:anotherone;T:WPA;P:abcdefghihklmn");
201         verifyZxParsing(
202                 uri,
203                 "\"anotherone\"",
204                 expectedSecurityParamsList,
205                 "\"abcdefghihklmn\"",
206                 false);
207 
208         // Test SAE
209         expectedSecurityParamsList =
210                 ImmutableList.of(
211                         SecurityParams.createSecurityParamsBySecurityType(
212                                 WifiConfiguration.SECURITY_TYPE_SAE));
213         uri = WifiUriParser.parseUri("WIFI:S:xx;T:SAE;P:a");
214         verifyZxParsing(
215                 uri,
216                 "\"xx\"",
217                 expectedSecurityParamsList,
218                 "\"a\"",
219                 false);
220         // invalid code but it should work.
221         uri = WifiUriParser.parseUri("WIFI: S:xx; T:SAE;   P:a");
222         verifyZxParsing(
223                 uri,
224                 "\"xx\"",
225                 expectedSecurityParamsList,
226                 "\"a\"",
227                 false);
228         // Test ADB
229         uri = WifiUriParser.parseUri("WIFI:T:ADB;S:myname;P:mypass;;");
230         verifyZxParsing(
231                 uri,
232                 "myname",
233                 Collections.emptyList(),
234                 "mypass",
235                 false);
236         // Test transition disable value
237         expectedSecurityParamsList =
238                 ImmutableList.of(
239                         SecurityParams.createSecurityParamsBySecurityType(
240                                 WifiConfiguration.SECURITY_TYPE_PSK));
241         uri = WifiUriParser.parseUri("WIFI:S:anotherone;T:WPA;R:0;P:3#=3j9asicla");
242         verifyZxParsing(
243                 uri,
244                 "\"anotherone\"",
245                 expectedSecurityParamsList,
246                 "\"3#=3j9asicla\"",
247                 false);
248 
249         SecurityParams pskButDisableed = SecurityParams.createSecurityParamsBySecurityType(
250                                 WifiConfiguration.SECURITY_TYPE_PSK);
251         pskButDisableed.setEnabled(false);
252         expectedSecurityParamsList =
253                 ImmutableList.of(pskButDisableed,
254                         SecurityParams.createSecurityParamsBySecurityType(
255                                 WifiConfiguration.SECURITY_TYPE_SAE));
256         uri = WifiUriParser.parseUri("WIFI:S:anotherone;T:WPA;R:1;P:3#=3j9asicla");
257         verifyZxParsing(
258                 uri,
259                 "\"anotherone\"",
260                 expectedSecurityParamsList,
261                 "\"3#=3j9asicla\"",
262                 false);
263     }
264 
265     @Test
testDppParsing()266     public void testDppParsing() {
267         UriParserResults uri = WifiUriParser.parseUri(TEST_DPP_URI);
268         assertEquals(UriParserResults.URI_SCHEME_DPP, uri.getUriScheme());
269         assertEquals(TEST_DPP_INFORMATION, uri.getInformation());
270         assertEquals(TEST_DPP_PUBLIC_KEY, uri.getPublicKey());
271         assertNull(uri.getWifiConfiguration());
272     }
273 
274     @Test
testInvalidUriParsing()275     public void testInvalidUriParsing() {
276         assertThrows(IllegalArgumentException.class,
277                 () -> WifiUriParser.parseUri("Invalid Uri"));
278         // Empty SSID
279         assertThrows(IllegalArgumentException.class,
280                 () -> WifiUriParser.parseUri("WIFI:S:;T:nopass"));
281         // Empty passphrase
282         assertThrows(IllegalArgumentException.class,
283                 () -> WifiUriParser.parseUri("WIFI: S:xx; T:SAE;   P:"));
284     }
285 }
286