summaryrefslogtreecommitdiffstats
path: root/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/eap/ExpandedEAPMethodTest.java
blob: 1c112dfaa3cbcb4e44597143256123654e908d2b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
 * Copyright (C) 2016 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.server.wifi.hotspot2.anqp.eap;

import static org.junit.Assert.assertEquals;

import androidx.test.filters.SmallTest;

import org.junit.Test;

import java.net.ProtocolException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;

/**
 * Unit tests for {@link com.android.server.wifi.hotspot2.anqp.eap.ExpandedEAPMethod}.
 */
@SmallTest
public class ExpandedEAPMethodTest {
    private static final int TEST_VENDOR_ID = 0x123456;
    private static final long TEST_VENDOR_TYPE = 0x23456523;
    private static final byte[] TEST_DATA_BYTES =
            new byte[] {0x12, 0x34, 0x56, 0x23, 0x45, 0x65, 0x23};

    /**
     * Verify that BufferUnderflowException will be thrown when parsing from an empty buffer.
     *
     * @throws Exception
     */
    @Test(expected = BufferUnderflowException.class)
    public void parseEmptyBuffer() throws Exception {
        ExpandedEAPMethod.parse(
                ByteBuffer.wrap(new byte[0]), ExpandedEAPMethod.EXPECTED_LENGTH_VALUE, false);
    }

    /**
     * Verify that ProtocolException will be thrown when the data length is not the expected
     * length.
     *
     * @throws Exception
     */
    @Test(expected = ProtocolException.class)
    public void parseBufferWithInvalidLength() throws Exception {
        ExpandedEAPMethod.parse(ByteBuffer.wrap(TEST_DATA_BYTES),
                ExpandedEAPMethod.EXPECTED_LENGTH_VALUE - 1, false);
    }

    /**
     * Verify that BufferUnderflowException will be thrown when parsing a truncated buffer.
     *
     * @throws Exception
     */
    @Test(expected = BufferUnderflowException.class)
    public void parseBufferWithTruncatedBuffer() throws Exception {
        ExpandedEAPMethod.parse(ByteBuffer.wrap(TEST_DATA_BYTES, 0, TEST_DATA_BYTES.length - 1),
                ExpandedEAPMethod.EXPECTED_LENGTH_VALUE, false);
    }

    /**
     * Verify that an expected ExpandedEAPMethod is returned when parsing the buffer for a
     * non-inner EAP method.
     *
     * @throws Exception
     */
    @Test
    public void parseBufferForNonInnerEAPMethod() throws Exception {
        ExpandedEAPMethod expected = new ExpandedEAPMethod(
                AuthParam.PARAM_TYPE_EXPANDED_EAP_METHOD, TEST_VENDOR_ID, TEST_VENDOR_TYPE);
        ExpandedEAPMethod actual = ExpandedEAPMethod.parse(
                ByteBuffer.wrap(TEST_DATA_BYTES), ExpandedEAPMethod.EXPECTED_LENGTH_VALUE, false);
        assertEquals(expected, actual);
    }

    /**
     * Verify that an expected CredentialType is returned when parsing the buffer for a
     * inner EAP method.
     *
     * @throws Exception
     */
    @Test
    public void parseBufferForTunneledEAPMethod() throws Exception {
        ExpandedEAPMethod expected = new ExpandedEAPMethod(
                AuthParam.PARAM_TYPE_EXPANDED_INNER_EAP_METHOD, TEST_VENDOR_ID, TEST_VENDOR_TYPE);
        ExpandedEAPMethod actual = ExpandedEAPMethod.parse(
                ByteBuffer.wrap(TEST_DATA_BYTES), ExpandedEAPMethod.EXPECTED_LENGTH_VALUE, true);
        assertEquals(expected, actual);
    }
}