Home | History | Annotate | Download | only in eap
      1 /*
      2  * Copyright (C) 2016 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 com.android.server.wifi.hotspot2.anqp.eap;
     18 
     19 import static org.junit.Assert.assertEquals;
     20 
     21 import android.support.test.filters.SmallTest;
     22 
     23 import org.junit.Test;
     24 
     25 import java.net.ProtocolException;
     26 import java.nio.BufferUnderflowException;
     27 import java.nio.ByteBuffer;
     28 
     29 /**
     30  * Unit tests for {@link com.android.server.wifi.hotspot2.anqp.eap.CredentialType}.
     31  */
     32 @SmallTest
     33 public class CredentialTypeTest {
     34     private static final int TEST_TYPE = CredentialType.CREDENTIAL_TYPE_USIM;
     35 
     36     /**
     37      * Helper function for generating the test buffer.
     38      *
     39      * @return {@link ByteBuffer}
     40      */
     41     private ByteBuffer getTestBuffer() {
     42         return ByteBuffer.wrap(new byte[] {(byte) TEST_TYPE});
     43     }
     44 
     45     /**
     46      * Verify that BufferUnderflowException will be thrown when parsing from an empty buffer.
     47      *
     48      * @throws Exception
     49      */
     50     @Test(expected = BufferUnderflowException.class)
     51     public void parseEmptyBuffer() throws Exception {
     52         CredentialType.parse(
     53                 ByteBuffer.wrap(new byte[0]), CredentialType.EXPECTED_LENGTH_VALUE, false);
     54     }
     55 
     56     /**
     57      * Verify that ProtocolException will be thrown when the data length value is not the same
     58      * as the expected
     59      *
     60      * @throws Exception
     61      */
     62     @Test(expected = ProtocolException.class)
     63     public void parseBufferWithInvalidLength() throws Exception {
     64         CredentialType.parse(getTestBuffer(), CredentialType.EXPECTED_LENGTH_VALUE - 1, false);
     65     }
     66 
     67     /**
     68      * Verify that an expected CredentialType is returned when parsing the buffer for a
     69      * non-tunneled EAP method.
     70      *
     71      * @throws Exception
     72      */
     73     @Test
     74     public void parseBufferForNonTunneledEAPMethod() throws Exception {
     75         CredentialType expected =
     76                 new CredentialType(AuthParam.PARAM_TYPE_CREDENTIAL_TYPE, TEST_TYPE);
     77         CredentialType actual = CredentialType.parse(
     78                 getTestBuffer(), CredentialType.EXPECTED_LENGTH_VALUE, false);
     79         assertEquals(expected, actual);
     80     }
     81 
     82     /**
     83      * Verify that an expected CredentialType is returned when parsing the buffer for a
     84      * tunneled EAP method.
     85      *
     86      * @throws Exception
     87      */
     88     @Test
     89     public void parseBufferForTunneledEAPMethod() throws Exception {
     90         CredentialType expected = new CredentialType(
     91                 AuthParam.PARAM_TYPE_TUNNELED_EAP_METHOD_CREDENTIAL_TYPE, TEST_TYPE);
     92         CredentialType actual = CredentialType.parse(
     93                 getTestBuffer(), CredentialType.EXPECTED_LENGTH_VALUE, true);
     94         assertEquals(expected, actual);
     95     }
     96 }
     97