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
17package com.android.server.wifi.hotspot2.anqp.eap;
18
19import static org.junit.Assert.assertEquals;
20
21import android.test.suitebuilder.annotation.SmallTest;
22
23import org.junit.Test;
24
25import java.net.ProtocolException;
26import java.nio.BufferUnderflowException;
27import java.nio.ByteBuffer;
28
29/**
30 * Unit tests for {@link com.android.server.wifi.hotspot2.anqp.eap.ExpandedEAPMethod}.
31 */
32@SmallTest
33public class ExpandedEAPMethodTest {
34    private static final int TEST_VENDOR_ID = 0x123456;
35    private static final long TEST_VENDOR_TYPE = 0x23456523;
36    private static final byte[] TEST_DATA_BYTES =
37            new byte[] {0x12, 0x34, 0x56, 0x23, 0x45, 0x65, 0x23};
38
39    /**
40     * Verify that BufferUnderflowException will be thrown when parsing from an empty buffer.
41     *
42     * @throws Exception
43     */
44    @Test(expected = BufferUnderflowException.class)
45    public void parseEmptyBuffer() throws Exception {
46        ExpandedEAPMethod.parse(
47                ByteBuffer.wrap(new byte[0]), ExpandedEAPMethod.EXPECTED_LENGTH_VALUE, false);
48    }
49
50    /**
51     * Verify that ProtocolException will be thrown when the data length is not the expected
52     * length.
53     *
54     * @throws Exception
55     */
56    @Test(expected = ProtocolException.class)
57    public void parseBufferWithInvalidLength() throws Exception {
58        ExpandedEAPMethod.parse(ByteBuffer.wrap(TEST_DATA_BYTES),
59                ExpandedEAPMethod.EXPECTED_LENGTH_VALUE - 1, false);
60    }
61
62    /**
63     * Verify that BufferUnderflowException will be thrown when parsing a truncated buffer.
64     *
65     * @throws Exception
66     */
67    @Test(expected = BufferUnderflowException.class)
68    public void parseBufferWithTruncatedBuffer() throws Exception {
69        ExpandedEAPMethod.parse(ByteBuffer.wrap(TEST_DATA_BYTES, 0, TEST_DATA_BYTES.length - 1),
70                ExpandedEAPMethod.EXPECTED_LENGTH_VALUE, false);
71    }
72
73    /**
74     * Verify that an expected ExpandedEAPMethod is returned when parsing the buffer for a
75     * non-inner EAP method.
76     *
77     * @throws Exception
78     */
79    @Test
80    public void parseBufferForNonInnerEAPMethod() throws Exception {
81        ExpandedEAPMethod expected = new ExpandedEAPMethod(
82                AuthParam.PARAM_TYPE_EXPANDED_EAP_METHOD, TEST_VENDOR_ID, TEST_VENDOR_TYPE);
83        ExpandedEAPMethod actual = ExpandedEAPMethod.parse(
84                ByteBuffer.wrap(TEST_DATA_BYTES), ExpandedEAPMethod.EXPECTED_LENGTH_VALUE, false);
85        assertEquals(expected, actual);
86    }
87
88    /**
89     * Verify that an expected CredentialType is returned when parsing the buffer for a
90     * inner EAP method.
91     *
92     * @throws Exception
93     */
94    @Test
95    public void parseBufferForTunneledEAPMethod() throws Exception {
96        ExpandedEAPMethod expected = new ExpandedEAPMethod(
97                AuthParam.PARAM_TYPE_EXPANDED_INNER_EAP_METHOD, TEST_VENDOR_ID, TEST_VENDOR_TYPE);
98        ExpandedEAPMethod actual = ExpandedEAPMethod.parse(
99                ByteBuffer.wrap(TEST_DATA_BYTES), ExpandedEAPMethod.EXPECTED_LENGTH_VALUE, true);
100        assertEquals(expected, actual);
101    }
102}
103