Merge "wifi: Indicate AUTH_FAILURE for WEP password error" into rvc-dev
diff --git a/service/java/com/android/server/wifi/SupplicantStaIfaceCallbackImpl.java b/service/java/com/android/server/wifi/SupplicantStaIfaceCallbackImpl.java
index 2d78514..92ba43b 100644
--- a/service/java/com/android/server/wifi/SupplicantStaIfaceCallbackImpl.java
+++ b/service/java/com/android/server/wifi/SupplicantStaIfaceCallbackImpl.java
@@ -258,28 +258,35 @@
     public void onAssociationRejected(byte[/* 6 */] bssid, int statusCode, boolean timedOut) {
         synchronized (mLock) {
             mStaIfaceHal.logCallback("onAssociationRejected");
+            boolean isWrongPwd = false;
             WifiConfiguration curConfiguration =
                     mStaIfaceHal.getCurrentNetworkLocalConfig(mIfaceName);
 
-            if (curConfiguration != null && !timedOut) {
-                Log.d(TAG, "flush PMK cache due to association rejection for config id "
-                        + curConfiguration.networkId + ".");
-                mStaIfaceHal.removePmkCacheEntry(curConfiguration.networkId);
+            if (curConfiguration != null) {
+                if (!timedOut) {
+                    Log.d(TAG, "flush PMK cache due to association rejection for config id "
+                            + curConfiguration.networkId + ".");
+                    mStaIfaceHal.removePmkCacheEntry(curConfiguration.networkId);
+                }
+                // Special handling for WPA3-Personal networks. If the password is
+                // incorrect, the AP will send association rejection, with status code 1
+                // (unspecified failure). In SAE networks, the password authentication
+                // is not related to the 4-way handshake. In this case, we will send an
+                // authentication failure event up.
+                if (statusCode == StatusCode.UNSPECIFIED_FAILURE
+                        && WifiConfigurationUtil.isConfigForSaeNetwork(curConfiguration)) {
+                    mStaIfaceHal.logCallback("SAE incorrect password");
+                    isWrongPwd = true;
+                } else if (statusCode == StatusCode.CHALLENGE_FAIL
+                        && WifiConfigurationUtil.isConfigForWepNetwork(curConfiguration)) {
+                    mStaIfaceHal.logCallback("WEP incorrect password");
+                    isWrongPwd = true;
+                }
             }
 
-            if (statusCode == StatusCode.UNSPECIFIED_FAILURE) {
-                if (curConfiguration != null
-                        && curConfiguration.allowedKeyManagement
-                                .get(WifiConfiguration.KeyMgmt.SAE)) {
-                    // Special handling for WPA3-Personal networks. If the password is
-                    // incorrect, the AP will send association rejection, with status code 1
-                    // (unspecified failure). In SAE networks, the password authentication
-                    // is not related to the 4-way handshake. In this case, we will send an
-                    // authentication failure event up.
-                    mStaIfaceHal.logCallback("SAE incorrect password");
-                    mWifiMonitor.broadcastAuthenticationFailureEvent(
-                            mIfaceName, WifiManager.ERROR_AUTH_FAILURE_WRONG_PSWD, -1);
-                }
+            if (isWrongPwd) {
+                mWifiMonitor.broadcastAuthenticationFailureEvent(
+                        mIfaceName, WifiManager.ERROR_AUTH_FAILURE_WRONG_PSWD, -1);
             }
             mWifiMonitor
                     .broadcastAssociationRejectionEvent(
diff --git a/tests/wifitests/src/com/android/server/wifi/SupplicantStaIfaceHalTest.java b/tests/wifitests/src/com/android/server/wifi/SupplicantStaIfaceHalTest.java
index 843a63c..facb7f9 100644
--- a/tests/wifitests/src/com/android/server/wifi/SupplicantStaIfaceHalTest.java
+++ b/tests/wifitests/src/com/android/server/wifi/SupplicantStaIfaceHalTest.java
@@ -1063,12 +1063,12 @@
      * Tests the handling of incorrect network passwords for WPA3-Personal networks
      */
     @Test
-    public void testAuthRejectionPassword() throws Exception {
+    public void testWpa3AuthRejectionPassword() throws Exception {
         executeAndValidateInitializationSequence();
         assertNotNull(mISupplicantStaIfaceCallback);
 
         executeAndValidateConnectSequenceWithKeyMgmt(SUPPLICANT_NETWORK_ID, false,
-                WifiConfiguration.KeyMgmt.SAE);
+                WifiConfiguration.KeyMgmt.SAE, null);
 
         int statusCode = ISupplicantStaIfaceCallback.StatusCode.UNSPECIFIED_FAILURE;
 
@@ -1081,6 +1081,27 @@
     }
 
     /**
+     * Tests the handling of incorrect network passwords for WEP networks.
+     */
+    @Test
+    public void testWepAuthRejectionPassword() throws Exception {
+        executeAndValidateInitializationSequence();
+        assertNotNull(mISupplicantStaIfaceCallback);
+
+        executeAndValidateConnectSequenceWithKeyMgmt(SUPPLICANT_NETWORK_ID, false,
+                WifiConfiguration.KeyMgmt.NONE, "97CA326539");
+
+        int statusCode = ISupplicantStaIfaceCallback.StatusCode.CHALLENGE_FAIL;
+
+        mISupplicantStaIfaceCallback.onAssociationRejected(
+                NativeUtil.macAddressToByteArray(BSSID), statusCode, false);
+        verify(mWifiMonitor).broadcastAuthenticationFailureEvent(eq(WLAN0_IFACE_NAME),
+                eq(WifiManager.ERROR_AUTH_FAILURE_WRONG_PSWD), eq(-1));
+        verify(mWifiMonitor).broadcastAssociationRejectionEvent(
+                eq(WLAN0_IFACE_NAME), eq(statusCode), eq(false), eq(BSSID));
+    }
+
+    /**
      * Tests the handling of incorrect network passwords, edge case.
      *
      * If the network is removed during 4-way handshake, do not call it a password mismatch.
@@ -2396,7 +2417,7 @@
     private WifiConfiguration executeAndValidateConnectSequence(
             final int newFrameworkNetworkId, final boolean haveExistingNetwork) throws Exception {
         return executeAndValidateConnectSequenceWithKeyMgmt(newFrameworkNetworkId,
-                haveExistingNetwork, WifiConfiguration.KeyMgmt.WPA_PSK);
+                haveExistingNetwork, WifiConfiguration.KeyMgmt.WPA_PSK, null);
     }
 
     /**
@@ -2404,16 +2425,19 @@
      *
      * @param newFrameworkNetworkId Framework Network Id of the new network to connect.
      * @param haveExistingNetwork Removes the existing network.
-     * @param keyMgmt Key management of the new network
+     * @param keyMgmt Key management of the new network.
+     * @param wepKey if configurations are for a WEP network else null.
      * @return the WifiConfiguration object of the new network to connect.
      */
     private WifiConfiguration executeAndValidateConnectSequenceWithKeyMgmt(
             final int newFrameworkNetworkId, final boolean haveExistingNetwork,
-            int keyMgmt) throws Exception {
+            int keyMgmt, String wepKey) throws Exception {
         setupMocksForConnectSequence(haveExistingNetwork);
         WifiConfiguration config = new WifiConfiguration();
         config.networkId = newFrameworkNetworkId;
         config.allowedKeyManagement.set(keyMgmt);
+        config.wepKeys[0] = wepKey;
+        config.wepTxKeyIndex = 0;
         assertTrue(mDut.connectToNetwork(WLAN0_IFACE_NAME, config));
         validateConnectSequence(haveExistingNetwork, 1);
         return config;