aboutsummaryrefslogtreecommitdiff
path: root/tests/iketests/src/java/com/android/internal/net/eap/message/mschapv2/EapMsChapV2ChallengeResponseTest.java
blob: 3e243a98568d52efd2df71b3cb078d3ed3754604 (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
/*
 * 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.mschapv2;

import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.EAP_MSCHAP_V2_CHALLENGE_RESPONSE;
import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.ID_INT;
import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.NT_RESPONSE_BYTES;
import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.PEER_CHALLENGE_BYTES;
import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.PEER_NAME_BYTES;
import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.SHORT_CHALLENGE_BYTES;
import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2PacketDefinitions.SHORT_NT_RESPONSE;
import static com.android.internal.net.eap.message.mschapv2.EapMsChapV2TypeData.EAP_MSCHAP_V2_RESPONSE;

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

import com.android.internal.net.eap.exceptions.mschapv2.EapMsChapV2ParsingException;
import com.android.internal.net.eap.message.mschapv2.EapMsChapV2TypeData.EapMsChapV2ChallengeResponse;

import org.junit.Test;

public class EapMsChapV2ChallengeResponseTest {
    private static final int FLAGS = 0;
    private static final int INVALID_FLAGS = 0xFF;

    @Test
    public void testConstructor() throws Exception {
        EapMsChapV2ChallengeResponse challengeResponse =
                new EapMsChapV2ChallengeResponse(
                        ID_INT, PEER_CHALLENGE_BYTES, NT_RESPONSE_BYTES, FLAGS, PEER_NAME_BYTES);
        assertEquals(EAP_MSCHAP_V2_RESPONSE, challengeResponse.opCode);
        assertEquals(ID_INT, challengeResponse.msChapV2Id);
        assertEquals(EAP_MSCHAP_V2_CHALLENGE_RESPONSE.length, challengeResponse.msLength);
        assertArrayEquals(PEER_CHALLENGE_BYTES, challengeResponse.peerChallenge);
        assertArrayEquals(NT_RESPONSE_BYTES, challengeResponse.ntResponse);
        assertEquals(FLAGS, challengeResponse.flags);
        assertArrayEquals(PEER_NAME_BYTES, challengeResponse.name);
    }

    @Test
    public void testConstructorInvalidChallenge() {
        try {
            EapMsChapV2ChallengeResponse challengeResponse =
                    new EapMsChapV2ChallengeResponse(
                            ID_INT,
                            SHORT_CHALLENGE_BYTES,
                            NT_RESPONSE_BYTES,
                            FLAGS,
                            PEER_NAME_BYTES);
            fail("Expected EapMsChapV2ParsingException for invalid Peer Challenge length");
        } catch (EapMsChapV2ParsingException expected) {
        }
    }

    @Test
    public void testConstructorInvalidNtResponse() {
        try {
            EapMsChapV2ChallengeResponse challengeResponse =
                    new EapMsChapV2ChallengeResponse(
                            ID_INT,
                            PEER_CHALLENGE_BYTES,
                            SHORT_NT_RESPONSE,
                            FLAGS,
                            PEER_NAME_BYTES);
            fail("Expected EapMsChapV2ParsingException for invalid NT-Response length");
        } catch (EapMsChapV2ParsingException expected) {
        }
    }

    @Test
    public void testConstructorInvalidFlags() {
        try {
            EapMsChapV2ChallengeResponse challengeResponse =
                    new EapMsChapV2ChallengeResponse(
                            ID_INT,
                            PEER_CHALLENGE_BYTES,
                            NT_RESPONSE_BYTES,
                            INVALID_FLAGS,
                            PEER_NAME_BYTES);
            fail("Expected EapMsChapV2ParsingException for non-zero Flags value");
        } catch (EapMsChapV2ParsingException expected) {
        }
    }

    @Test
    public void testEncode() throws Exception {
        EapMsChapV2ChallengeResponse challengeResponse =
                new EapMsChapV2ChallengeResponse(
                        ID_INT, PEER_CHALLENGE_BYTES, NT_RESPONSE_BYTES, FLAGS, PEER_NAME_BYTES);
        byte[] encodedChallengeResponse = challengeResponse.encode();

        assertArrayEquals(EAP_MSCHAP_V2_CHALLENGE_RESPONSE, encodedChallengeResponse);
    }
}