aboutsummaryrefslogtreecommitdiff
path: root/tests/iketests/src/java/com/android/internal/net/eap/message/EapMessageTest.java
blob: 813a30f7809ed0831e149b31c0fe2e5d33d8d08b (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * Copyright (C) 2019 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.internal.net.eap.message;

import static com.android.internal.net.TestUtils.hexStringToByteArray;
import static com.android.internal.net.eap.message.EapData.EAP_NAK;
import static com.android.internal.net.eap.message.EapData.EAP_TYPE_SIM;
import static com.android.internal.net.eap.message.EapMessage.EAP_CODE_REQUEST;
import static com.android.internal.net.eap.message.EapMessage.EAP_CODE_RESPONSE;
import static com.android.internal.net.eap.message.EapMessage.EAP_CODE_SUCCESS;
import static com.android.internal.net.eap.message.EapTestMessageDefinitions.EAP_REQUEST_SIM_START_PACKET;
import static com.android.internal.net.eap.message.EapTestMessageDefinitions.EAP_REQUEST_SIM_TYPE_DATA;
import static com.android.internal.net.eap.message.EapTestMessageDefinitions.EAP_RESPONSE_NAK_PACKET;
import static com.android.internal.net.eap.message.EapTestMessageDefinitions.EAP_RESPONSE_NOTIFICATION_PACKET;
import static com.android.internal.net.eap.message.EapTestMessageDefinitions.EAP_SUCCESS_PACKET;
import static com.android.internal.net.eap.message.EapTestMessageDefinitions.ID_INT;
import static com.android.internal.net.eap.message.EapTestMessageDefinitions.INCOMPLETE_HEADER_PACKET;
import static com.android.internal.net.eap.message.EapTestMessageDefinitions.INVALID_CODE_PACKET;
import static com.android.internal.net.eap.message.EapTestMessageDefinitions.LONG_SUCCESS_PACKET;
import static com.android.internal.net.eap.message.EapTestMessageDefinitions.REQUEST_MISSING_TYPE_PACKET;
import static com.android.internal.net.eap.message.EapTestMessageDefinitions.REQUEST_UNSUPPORTED_TYPE_PACKET;
import static com.android.internal.net.eap.message.EapTestMessageDefinitions.SHORT_PACKET;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.android.internal.net.eap.EapResult;
import com.android.internal.net.eap.EapResult.EapResponse;
import com.android.internal.net.eap.exceptions.EapInvalidPacketLengthException;
import com.android.internal.net.eap.exceptions.InvalidEapCodeException;
import com.android.internal.net.eap.exceptions.UnsupportedEapTypeException;

import org.junit.Test;

import java.util.Arrays;

public class EapMessageTest {
    @Test
    public void testConstructorRequestWithoutType() throws Exception {
        try {
            new EapMessage(EAP_CODE_REQUEST, ID_INT, null);
            fail("Expected EapInvalidPacketLengthException for an EAP-Request without Type value");
        } catch (EapInvalidPacketLengthException expected) {
        }
    }

    @Test
    public void testDecode() throws Exception {
        EapMessage result = EapMessage.decode(EAP_SUCCESS_PACKET);
        assertEquals(EAP_CODE_SUCCESS, result.eapCode);
        assertEquals(ID_INT, result.eapIdentifier);
        assertEquals(EAP_SUCCESS_PACKET.length, result.eapLength);
        assertNull(result.eapData);

        EapData expectedEapData = new EapData(EAP_TYPE_SIM,
                hexStringToByteArray(EAP_REQUEST_SIM_TYPE_DATA));
        result = EapMessage.decode(EAP_REQUEST_SIM_START_PACKET);
        assertEquals(EAP_CODE_REQUEST, result.eapCode);
        assertEquals(ID_INT, result.eapIdentifier);
        assertEquals(EAP_REQUEST_SIM_START_PACKET.length, result.eapLength);
        assertEquals(expectedEapData, result.eapData);
    }

    @Test
    public void testDecodeInvalidCode() throws Exception {
        try {
            EapMessage.decode(INVALID_CODE_PACKET);
            fail("Expected InvalidEapCodeException");
        } catch (InvalidEapCodeException expected) {
        }
    }

    @Test
    public void testDecodeIncompleteHeader() throws Exception {
        try {
            EapMessage.decode(INCOMPLETE_HEADER_PACKET);
            fail("Expected EapInvalidPacketLengthException");
        } catch (EapInvalidPacketLengthException expected) {
        }
    }

    @Test
    public void testDecodeShortPacket() throws Exception {
        try {
            EapMessage.decode(SHORT_PACKET);
            fail("Expected EapInvalidPacketLengthException");
        } catch (EapInvalidPacketLengthException expected) {
        }
    }

    @Test
    public void testDecodeSuccessIncorrectLength() throws Exception {
        try {
            EapMessage.decode(LONG_SUCCESS_PACKET);
            fail("Expected EapInvalidPacketLengthException");
        } catch (EapInvalidPacketLengthException expected) {
        }
    }

    @Test
    public void testDecodeMissingTypeData() throws Exception {
        try {
            EapMessage.decode(REQUEST_MISSING_TYPE_PACKET);
            fail("Expected EapInvalidPacketLengthException");
        } catch (EapInvalidPacketLengthException expected) {
        }
    }

    @Test
    public void testDecodeUnsupportedEapType() throws Exception {
        try {
            EapMessage.decode(REQUEST_UNSUPPORTED_TYPE_PACKET);
            fail("Expected UnsupportedEapDataTypeException");
        } catch (UnsupportedEapTypeException expected) {
            assertEquals(ID_INT, expected.eapIdentifier);
        }
    }

    @Test
    public void testEncode() throws Exception {
        EapMessage eapMessage = new EapMessage(EAP_CODE_SUCCESS, ID_INT, null);
        byte[] actualPacket = eapMessage.encode();
        assertArrayEquals(EAP_SUCCESS_PACKET, actualPacket);

        EapData nakData = new EapData(EAP_NAK, new byte[] {EAP_TYPE_SIM});
        eapMessage = new EapMessage(EAP_CODE_RESPONSE, ID_INT, nakData);
        actualPacket = eapMessage.encode();
        assertArrayEquals(EAP_RESPONSE_NAK_PACKET, actualPacket);
    }

    @Test
    public void testEncodeDecode() throws Exception {
        EapMessage eapMessage = new EapMessage(EAP_CODE_SUCCESS, ID_INT, null);
        EapMessage result = EapMessage.decode(eapMessage.encode());

        assertEquals(eapMessage.eapCode, result.eapCode);
        assertEquals(eapMessage.eapIdentifier, result.eapIdentifier);
        assertEquals(eapMessage.eapLength, result.eapLength);
        assertEquals(eapMessage.eapData, result.eapData);
    }

    @Test
    public void testDecodeEncode() throws Exception {
        byte[] result = EapMessage.decode(EAP_REQUEST_SIM_START_PACKET).encode();
        assertArrayEquals(EAP_REQUEST_SIM_START_PACKET, result);
    }

    @Test
    public void testGetNakResponse() {
        EapResult nakResponse = EapMessage.getNakResponse(ID_INT, Arrays.asList(EAP_TYPE_SIM));

        assertTrue(nakResponse instanceof EapResponse);
        EapResponse eapResponse = (EapResponse) nakResponse;
        assertArrayEquals(EAP_RESPONSE_NAK_PACKET, eapResponse.packet);
    }

    @Test
    public void testGetNotificationResponse() {
        EapResult notificationResponse = EapMessage.getNotificationResponse(ID_INT);

        assertTrue(notificationResponse instanceof EapResponse);
        EapResponse eapResponse = (EapResponse) notificationResponse;
        assertArrayEquals(EAP_RESPONSE_NOTIFICATION_PACKET, eapResponse.packet);
    }
}