[SEBA-897] Unit Test cases for SEBA-34

Change-Id: Ib7d691ebc02a842d81cc7c72ed153cb06dccab57
diff --git a/app/src/main/java/org/opencord/aaa/impl/AaaManager.java b/app/src/main/java/org/opencord/aaa/impl/AaaManager.java
index e3f5822..43d8021 100644
--- a/app/src/main/java/org/opencord/aaa/impl/AaaManager.java
+++ b/app/src/main/java/org/opencord/aaa/impl/AaaManager.java
@@ -1084,6 +1084,7 @@
             aaaSupplicantStatsManager.getMachineStatsDelegate()
                    .notify(new AaaMachineStatisticsEvent(AaaMachineStatisticsEvent.Type.STATS_UPDATE, obj));
             StateMachine removed = stateMachines.remove(sessionId);
+
             if (removed != null) {
                 StateMachine.deleteStateMachineMapping(removed);
             }
diff --git a/app/src/test/java/org/opencord/aaa/impl/AaaStatisticsTest.java b/app/src/test/java/org/opencord/aaa/impl/AaaStatisticsTest.java
index 25b8fc8..adfa05f 100644
--- a/app/src/test/java/org/opencord/aaa/impl/AaaStatisticsTest.java
+++ b/app/src/test/java/org/opencord/aaa/impl/AaaStatisticsTest.java
@@ -52,6 +52,7 @@
 import static org.hamcrest.Matchers.is;
 import static org.hamcrest.Matchers.notNullValue;
 import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertThat;
 import static org.onosproject.net.NetTestTools.connectPoint;
 import static org.slf4j.LoggerFactory.getLogger;
@@ -279,19 +280,90 @@
         assertNotEquals(aaaStatisticsManager.getAaaStats().getEapolStartReqTrans(), ZERO);
         assertNotEquals(aaaStatisticsManager.getAaaStats().getEapolTransRespNotNak(), ZERO);
         assertNotEquals(aaaStatisticsManager.getAaaStats().getEapPktTxauthChooseEap(), ZERO);
+        assertNotEquals(aaaStatisticsManager.getAaaStats().getValidEapolFramesRx(), ZERO);
+        assertNotEquals(aaaStatisticsManager.getAaaStats().getEapolFramesTx(), ZERO);
+        assertNotEquals(aaaStatisticsManager.getAaaStats().getReqEapFramesTx(), ZERO);
+        assertNotEquals(aaaStatisticsManager.getAaaStats().getRequestIdFramesTx(), ZERO);
+        assertEquals(aaaStatisticsManager.getAaaStats().getInvalidBodyLength(), ZERO);
+        assertEquals(aaaStatisticsManager.getAaaStats().getInvalidPktType(), ZERO);
+        assertEquals(aaaStatisticsManager.getAaaStats().getPendingResSupp(), ZERO);
+       // Counts the aaa Statistics count and displays in the log
+       countAaaStatistics();
 
-        assertNotEquals(aaaStatisticsManager.getAaaStats().getAcceptResponsesRx(), ZERO);
-        assertNotEquals(aaaStatisticsManager.getAaaStats().getAccessRequestsTx(), ZERO);
-        assertNotEquals(aaaStatisticsManager.getAaaStats().getChallengeResponsesRx(), ZERO);
-        assertNotEquals(aaaStatisticsManager.getAaaStats().getDroppedResponsesRx(), ZERO);
-        assertNotEquals(aaaStatisticsManager.getAaaStats().getInvalidValidatorsRx(), ZERO);
-
-        // Counts the aaa Statistics count and displays in the log
-        countAaaStatistics();
     }
 
-    /**
-     * Tests the count for defected packets.
+    /** Tests invalid packets reaching AAA.
+     *  And counts the aaa Stats for successful transmission.
+     *   @throws DeserializationException
+     *  if packed deserialization fails.
+     */
+    @Test
+    public void testStatisticsForInvalidPackets() throws Exception {
+
+        //Test Authenticator State Machine Status. Should be Pending
+        // (1) Supplicant start up
+        Ethernet startPacket = constructSupplicantStartPacket();
+        sendPacket(startPacket);
+
+        Ethernet responsePacket = (Ethernet) fetchPacket(0);
+        checkRadiusPacket(aaaManager, responsePacket, EAP.ATTR_IDENTITY);
+
+        // (2) Supplicant identify
+
+        Ethernet identifyPacket = constructSupplicantIdentifyPacket(null, EAP.ATTR_IDENTITY, (byte) 1, null);
+        sendPacket(identifyPacket);
+
+        RADIUS radiusIdentifyPacket = (RADIUS) fetchPacket(1);
+        checkRadiusPacketFromSupplicant(radiusIdentifyPacket);
+
+        assertThat(radiusIdentifyPacket.getCode(), is(RADIUS.RADIUS_CODE_ACCESS_REQUEST));
+        assertThat(new String(radiusIdentifyPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_USERNAME).getValue()),
+                is("testuser"));
+        IpAddress nasIp = IpAddress.valueOf(IpAddress.Version.INET,
+                  radiusIdentifyPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_IP).getValue());
+        assertThat(nasIp.toString(), is(aaaManager.nasIpAddress.getHostAddress()));
+
+        // State machine should have been created by now
+
+        StateMachine stateMachine = aaaManager.getStateMachine(SESSION_ID);
+        assertThat(stateMachine, notNullValue());
+        assertThat(stateMachine.state(), is(StateMachine.STATE_PENDING));
+
+        // (3) RADIUS NAK challenge
+
+       RADIUS radiusCodeAccessChallengePacket = constructRadiusCodeAccessChallengePacket(
+                  RADIUS.RADIUS_CODE_ACCESS_CHALLENGE, EAP.ATTR_NAK, radiusIdentifyPacket.getIdentifier(),
+                  aaaManager.radiusSecret.getBytes());
+        aaaManager.handleRadiusPacket(radiusCodeAccessChallengePacket);
+
+        Ethernet radiusChallengeNakPacket = (Ethernet) fetchPacket(2);
+        checkRadiusPacket(aaaManager, radiusChallengeNakPacket, EAP.ATTR_NAK);
+
+        // (4) Supplicant NAK response
+
+       Ethernet nakRadiusPacket = constructSupplicantIdentifyPacket(stateMachine, EAP.ATTR_NAK,
+           stateMachine.challengeIdentifier(), radiusChallengeNakPacket);
+       sendPacket(nakRadiusPacket);
+
+       //Statistic Should be increased.
+       assertNotEquals(aaaStatisticsManager.getAaaStats().getPendingResSupp(), ZERO);
+
+       //Test if packet with invalid eapol type recieved.
+       // Supplicant ASF Packet
+       Ethernet invalidPacket = constructSupplicantAsfPacket();
+       sendPacket(invalidPacket);
+       //Statistic Should be increased.
+       assertNotEquals(aaaStatisticsManager.getAaaStats().getInvalidPktType(), ZERO);
+       assertNotEquals(aaaStatisticsManager.getAaaStats().getAccessRequestsTx(), ZERO);
+       assertNotEquals(aaaStatisticsManager.getAaaStats().getChallengeResponsesRx(), ZERO);
+       assertNotEquals(aaaStatisticsManager.getAaaStats().getDroppedResponsesRx(), ZERO);
+       assertNotEquals(aaaStatisticsManager.getAaaStats().getInvalidValidatorsRx(), ZERO);
+       // Counts the aaa Statistics count and displays in the log
+       countAaaStatistics();
+    }
+
+
+    /** Tests the count for defected packets.
      *
      * @throws DeserializationException if packed deserialization fails.
      */
@@ -510,6 +582,7 @@
         assertNotEquals(aaaStatisticsManager.getAaaStats().getEapolStartReqTrans(), ZERO);
         assertNotEquals(aaaStatisticsManager.getAaaStats().getEapolTransRespNotNak(), ZERO);
         assertNotEquals(aaaStatisticsManager.getAaaStats().getEapPktTxauthChooseEap(), ZERO);
+        assertNotEquals(aaaStatisticsManager.getAaaStats().getAuthStateIdle(), ZERO);
         // Counts the aaa Statistics count
         countAaaStatistics();
 
diff --git a/app/src/test/java/org/opencord/aaa/impl/AaaTestBase.java b/app/src/test/java/org/opencord/aaa/impl/AaaTestBase.java
index ec4e113..30d2356 100644
--- a/app/src/test/java/org/opencord/aaa/impl/AaaTestBase.java
+++ b/app/src/test/java/org/opencord/aaa/impl/AaaTestBase.java
@@ -453,6 +453,33 @@
     }
 
     /**
+     * Constructs an Ethernet packet containing a EAPOL_ASF Payload.
+     *
+     * @return Ethernet packet
+     */
+    Ethernet constructSupplicantAsfPacket() {
+        Ethernet eth = new Ethernet();
+        eth.setDestinationMACAddress(clientMac.toBytes());
+        eth.setSourceMACAddress(serverMac.toBytes());
+        eth.setEtherType(EthType.EtherType.EAPOL.ethType().toShort());
+        eth.setVlanID((short) 2);
+
+        EAP eap = new EAP(EAPOL.EAPOL_START, (byte) 3, EAPOL.EAPOL_START, null);
+
+        // eapol header
+        EAPOL eapol = new EAPOL();
+        eapol.setEapolType(EAPOL.EAPOL_ASF);
+        eapol.setPacketLength(eap.getLength());
+
+        // eap part
+        eapol.setPayload(eap);
+
+        eth.setPayload(eapol);
+        eth.setPad(true);
+        return eth;
+    }
+
+    /**
      * Checks the contents of a RADIUS packet being sent to the RADIUS server.
      *
      * @param radiusPacket packet to check