1 /*
2  * Copyright (C) 2019 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.internal.net.eap.test.statemachine;
18 
19 import static androidx.test.InstrumentationRegistry.getInstrumentation;
20 
21 import static com.android.internal.net.eap.test.EapTestUtils.getDummyEapSessionConfig;
22 import static com.android.internal.net.eap.test.message.EapTestMessageDefinitions.EAP_SUCCESS_PACKET;
23 
24 import static org.junit.Assert.assertTrue;
25 
26 import android.content.Context;
27 import android.net.eap.test.EapSessionConfig;
28 
29 import com.android.internal.net.eap.test.EapResult;
30 import com.android.internal.net.eap.test.EapResult.EapError;
31 import com.android.internal.net.eap.test.exceptions.EapInvalidRequestException;
32 import com.android.internal.net.eap.test.statemachine.EapStateMachine.CreatedState;
33 import com.android.internal.net.eap.test.statemachine.EapStateMachine.FailureState;
34 import com.android.internal.net.eap.test.statemachine.EapStateMachine.SuccessState;
35 
36 import org.junit.Before;
37 import org.junit.Test;
38 
39 import java.security.SecureRandom;
40 
41 public class EapStateMachineTest {
42     private Context mContext;
43     private EapSessionConfig mEapSessionConfig;
44 
45     @Before
setUp()46     public void setUp() {
47         mContext = getInstrumentation().getContext();
48         mEapSessionConfig = getDummyEapSessionConfig();
49     }
50 
51     @Test
testEapStateMachineStartState()52     public void testEapStateMachineStartState() {
53         EapStateMachine eapStateMachine =
54                 new EapStateMachine(mContext, mEapSessionConfig, new SecureRandom());
55         assertTrue(eapStateMachine.getState() instanceof CreatedState);
56     }
57 
58     @Test
testSuccessStateProcessFails()59     public void testSuccessStateProcessFails() {
60         SuccessState successState =
61                 new EapStateMachine(mContext, mEapSessionConfig, new SecureRandom())
62                 .new SuccessState();
63         EapResult result = successState.process(EAP_SUCCESS_PACKET);
64         assertTrue(result instanceof EapError);
65 
66         EapError eapError = (EapError) result;
67         assertTrue(eapError.cause instanceof EapInvalidRequestException);
68     }
69 
70     @Test
testFailureStateProcessFails()71     public void testFailureStateProcessFails() {
72         FailureState failureState =
73                 new EapStateMachine(mContext, mEapSessionConfig, new SecureRandom())
74                 .new FailureState();
75         EapResult result = failureState.process(EAP_SUCCESS_PACKET);
76         assertTrue(result instanceof EapError);
77 
78         EapError eapError = (EapError) result;
79         assertTrue(eapError.cause instanceof EapInvalidRequestException);
80     }
81 }
82