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 26 import androidx.test.filters.SmallTest; 27 28 import com.google.common.collect.ImmutableList; 29 30 import org.junit.Test; 31 32 import java.util.Collections; 33 import java.util.List; 34 35 /** Unit tests for {@link com.android.server.wifi.WifiUriParser}. */ 36 @SmallTest 37 public class WifiUriParserTest { 38 39 private static final String TEST_DPP_INFORMATION = "Easy_Connect_Demo"; 40 private static final String TEST_DPP_PUBLIC_KEY = "MDkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDIg" 41 + "ACDmtXD1Sz6/5B4YRdmTkbkkFLDwk8f0yRnfm1Gokpx/0="; 42 private static final String TEST_DPP_URI = "DPP:C:81/1;I:" + TEST_DPP_INFORMATION 43 + ";K:" + TEST_DPP_PUBLIC_KEY + ";;"; 44 verifyZxParsing( UriParserResults uri, String expectedSSID, List<SecurityParams> expectedSecurityParamsList, String expectedPreShareKey, boolean isWep)45 private void verifyZxParsing( 46 UriParserResults uri, 47 String expectedSSID, 48 List<SecurityParams> expectedSecurityParamsList, 49 String expectedPreShareKey, 50 boolean isWep) { 51 assertNotNull(uri); 52 WifiConfiguration config = uri.getWifiConfiguration(); 53 assertNotNull(config); 54 assertThat(config.SSID).isEqualTo(expectedSSID); 55 if (isWep) { 56 assertThat(config.wepKeys[0]).isEqualTo(expectedPreShareKey); 57 } else { 58 assertThat(config.preSharedKey).isEqualTo(expectedPreShareKey); 59 } 60 List<SecurityParams> configSecurityParamsList = config.getSecurityParamsList(); 61 assertEquals(expectedSecurityParamsList, configSecurityParamsList); 62 assertNull(uri.getPublicKey()); 63 assertNull(uri.getInformation()); 64 assertEquals(UriParserResults.URI_SCHEME_ZXING_WIFI_NETWORK_CONFIG, uri.getUriScheme()); 65 } 66 67 @Test testZxParsing()68 public void testZxParsing() { 69 // Test no password 70 List<SecurityParams> expectedSecurityParamsList = 71 ImmutableList.of( 72 SecurityParams.createSecurityParamsBySecurityType( 73 WifiConfiguration.SECURITY_TYPE_OPEN), 74 SecurityParams.createSecurityParamsBySecurityType( 75 WifiConfiguration.SECURITY_TYPE_OWE)); 76 UriParserResults uri = WifiUriParser.parseUri("WIFI:S:testAbC;T:nopass"); 77 verifyZxParsing( 78 uri, 79 "\"testAbC\"", 80 expectedSecurityParamsList, 81 null, 82 false); 83 // invalid code but it should work. 84 uri = WifiUriParser.parseUri("WIFI:S:testAbC; T:nopass"); 85 verifyZxParsing( 86 uri, 87 "\"testAbC\"", 88 expectedSecurityParamsList, 89 null, 90 false); 91 92 // Test WEP 93 expectedSecurityParamsList = 94 ImmutableList.of( 95 SecurityParams.createSecurityParamsBySecurityType( 96 WifiConfiguration.SECURITY_TYPE_WEP)); 97 uri = WifiUriParser.parseUri("WIFI:S:reallyLONGone;T:WEP;P:somepasswo#%^**123rd"); 98 verifyZxParsing( 99 uri, 100 "\"reallyLONGone\"", 101 expectedSecurityParamsList, 102 "\"somepasswo#%^**123rd\"", 103 true); 104 // invalid code but it should work. 105 uri = WifiUriParser.parseUri("WIFI:S:reallyLONGone;T:WEP; P:somepassword"); 106 verifyZxParsing( 107 uri, 108 "\"reallyLONGone\"", 109 expectedSecurityParamsList, 110 "\"somepassword\"", 111 true); 112 113 // Test WPA 114 expectedSecurityParamsList = 115 ImmutableList.of( 116 SecurityParams.createSecurityParamsBySecurityType( 117 WifiConfiguration.SECURITY_TYPE_PSK)); 118 uri = WifiUriParser.parseUri("WIFI:S:anotherone;T:WPA;P:3#=3j9asicla"); 119 verifyZxParsing( 120 uri, 121 "\"anotherone\"", 122 expectedSecurityParamsList, 123 "\"3#=3j9asicla\"", 124 false); 125 // invalid code but it should work. 126 uri = WifiUriParser.parseUri("WIFI: S:anotherone;T:WPA;P:abcdefghihklmn"); 127 verifyZxParsing( 128 uri, 129 "\"anotherone\"", 130 expectedSecurityParamsList, 131 "\"abcdefghihklmn\"", 132 false); 133 134 // Test SAE 135 expectedSecurityParamsList = 136 ImmutableList.of( 137 SecurityParams.createSecurityParamsBySecurityType( 138 WifiConfiguration.SECURITY_TYPE_SAE)); 139 uri = WifiUriParser.parseUri("WIFI:S:xx;T:SAE;P:a"); 140 verifyZxParsing( 141 uri, 142 "\"xx\"", 143 expectedSecurityParamsList, 144 "\"a\"", 145 false); 146 // invalid code but it should work. 147 uri = WifiUriParser.parseUri("WIFI: S:xx; T:SAE; P:a"); 148 verifyZxParsing( 149 uri, 150 "\"xx\"", 151 expectedSecurityParamsList, 152 "\"a\"", 153 false); 154 // Test ADB 155 uri = WifiUriParser.parseUri("WIFI:T:ADB;S:myname;P:mypass;;"); 156 verifyZxParsing( 157 uri, 158 "\"myname\"", 159 Collections.emptyList(), 160 "\"mypass\"", 161 false); 162 // Test transition disable value 163 expectedSecurityParamsList = 164 ImmutableList.of( 165 SecurityParams.createSecurityParamsBySecurityType( 166 WifiConfiguration.SECURITY_TYPE_PSK)); 167 uri = WifiUriParser.parseUri("WIFI:S:anotherone;T:WPA;R:0;P:3#=3j9asicla"); 168 verifyZxParsing( 169 uri, 170 "\"anotherone\"", 171 expectedSecurityParamsList, 172 "\"3#=3j9asicla\"", 173 false); 174 175 SecurityParams pskButDisableed = SecurityParams.createSecurityParamsBySecurityType( 176 WifiConfiguration.SECURITY_TYPE_PSK); 177 pskButDisableed.setEnabled(false); 178 expectedSecurityParamsList = 179 ImmutableList.of(pskButDisableed, 180 SecurityParams.createSecurityParamsBySecurityType( 181 WifiConfiguration.SECURITY_TYPE_SAE)); 182 uri = WifiUriParser.parseUri("WIFI:S:anotherone;T:WPA;R:1;P:3#=3j9asicla"); 183 verifyZxParsing( 184 uri, 185 "\"anotherone\"", 186 expectedSecurityParamsList, 187 "\"3#=3j9asicla\"", 188 false); 189 } 190 191 @Test testDppParsing()192 public void testDppParsing() { 193 UriParserResults uri = WifiUriParser.parseUri(TEST_DPP_URI); 194 assertEquals(UriParserResults.URI_SCHEME_DPP, uri.getUriScheme()); 195 assertEquals(TEST_DPP_INFORMATION, uri.getInformation()); 196 assertEquals(TEST_DPP_PUBLIC_KEY, uri.getPublicKey()); 197 assertNull(uri.getWifiConfiguration()); 198 } 199 200 @Test testInvalidUriParsing()201 public void testInvalidUriParsing() { 202 assertThrows(IllegalArgumentException.class, 203 () -> WifiUriParser.parseUri("Invalid Uri")); 204 // Empty SSID 205 assertThrows(IllegalArgumentException.class, 206 () -> WifiUriParser.parseUri("WIFI:S:;T:nopass")); 207 // Empty passphrase 208 assertThrows(IllegalArgumentException.class, 209 () -> WifiUriParser.parseUri("WIFI: S:xx; T:SAE; P:")); 210 } 211 } 212