Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0fb1a3b3 authored by Etan Cohen's avatar Etan Cohen
Browse files

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
parent 3e25353a
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -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());
            }
        }

+74 −0
Original line number Diff line number Diff line
/*
 * 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");
    }
}