Fix WiFi (ZX or DPP) QR code parsing to ignore leading spaces

Increase robustness of QR code parsing: while leading spaces aren't
expected (or allowed) they are observed in the wild and can be safely
ignored.

Bug: 292331368
Test: validated that QR code with scan is now parsed correctly
Test: atest WifiQrCodeTest
Change-Id: Ifff79870bea2ec9924f7b2e8100c2c01fd350846
This commit is contained in:
Etan Cohen
2023-08-03 03:06:14 +00:00
parent 3e25353a21
commit 0fb1a3b3a7
2 changed files with 77 additions and 2 deletions

View File

@@ -160,8 +160,9 @@ public class WifiQrCode {
private String getValueOrNull(List<String> keyValueList, String prefix) {
for (String keyValue : keyValueList) {
if (keyValue.startsWith(prefix)) {
return keyValue.substring(prefix.length());
String strippedKeyValue = keyValue.stripLeading();
if (strippedKeyValue.startsWith(prefix)) {
return strippedKeyValue.substring(prefix.length());
}
}

View File

@@ -0,0 +1,74 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.wifi.dpp;
import static com.google.common.truth.Truth.assertThat;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class)
public class WifiQrCodeTest {
@Test
public void testZxParsing_validCode() {
WifiNetworkConfig config = new WifiQrCode("WIFI:S:testAbC;T:nopass").getWifiNetworkConfig();
assertThat(config.getSsid()).isEqualTo("testAbC");
assertThat(config.getSecurity()).isEqualTo("nopass");
config = new WifiQrCode(
"WIFI:S:reallyLONGone;T:WEP;P:somepasswo#%^**123rd").getWifiNetworkConfig();
assertThat(config.getSsid()).isEqualTo("reallyLONGone");
assertThat(config.getSecurity()).isEqualTo("WEP");
assertThat(config.getPreSharedKey()).isEqualTo("somepasswo#%^**123rd");
config = new WifiQrCode("WIFI:S:anotherone;T:WPA;P:3#=3j9asicla").getWifiNetworkConfig();
assertThat(config.getSsid()).isEqualTo("anotherone");
assertThat(config.getSecurity()).isEqualTo("WPA");
assertThat(config.getPreSharedKey()).isEqualTo("3#=3j9asicla");
config = new WifiQrCode("WIFI:S:xx;T:SAE;P:a").getWifiNetworkConfig();
assertThat(config.getSsid()).isEqualTo("xx");
assertThat(config.getSecurity()).isEqualTo("SAE");
assertThat(config.getPreSharedKey()).isEqualTo("a");
}
@Test
public void testZxParsing_invalidCodeButShouldWork() {
WifiNetworkConfig config = new WifiQrCode(
"WIFI:S:testAbC; T:nopass").getWifiNetworkConfig();
assertThat(config.getSsid()).isEqualTo("testAbC");
assertThat(config.getSecurity()).isEqualTo("nopass");
config = new WifiQrCode(
"WIFI:S:reallyLONGone;T:WEP; P:somepassword").getWifiNetworkConfig();
assertThat(config.getSsid()).isEqualTo("reallyLONGone");
assertThat(config.getSecurity()).isEqualTo("WEP");
assertThat(config.getPreSharedKey()).isEqualTo("somepassword");
config = new WifiQrCode("WIFI: S:anotherone;T:WPA;P:abcdefghihklmn").getWifiNetworkConfig();
assertThat(config.getSsid()).isEqualTo("anotherone");
assertThat(config.getSecurity()).isEqualTo("WPA");
assertThat(config.getPreSharedKey()).isEqualTo("abcdefghihklmn");
config = new WifiQrCode("WIFI: S:xx; T:SAE; P:a").getWifiNetworkConfig();
assertThat(config.getSsid()).isEqualTo("xx");
assertThat(config.getSecurity()).isEqualTo("SAE");
assertThat(config.getPreSharedKey()).isEqualTo("a");
}
}