blob: f12a95f039bde5338b4c100fa2e701166656759a [file] [log] [blame]
Ari Saha89831742015-06-26 10:31:48 -07001/*
Amit Ghoshc9ac1e52017-07-28 12:31:18 +01002 * Copyright 2017-present Open Networking Foundation
Ari Saha89831742015-06-26 10:31:48 -07003 *
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 */
alshabib6d527452016-06-01 18:00:47 -070017package org.opencord.aaa;
Ari Saha89831742015-06-26 10:31:48 -070018
19import org.junit.After;
Ari Saha89831742015-06-26 10:31:48 -070020import org.junit.Before;
21import org.junit.Test;
David K. Bainbridge62019492017-07-28 17:02:10 -070022import org.onlab.packet.MacAddress;
23
Ray Milkey967776a2015-10-07 14:37:17 -070024import static org.junit.Assert.assertEquals;
David K. Bainbridge62019492017-07-28 17:02:10 -070025import static org.junit.Assert.assertNotNull;
Jonathan Hart932bedc2018-07-12 13:46:09 -070026import static org.junit.Assert.assertNull;
Ari Saha89831742015-06-26 10:31:48 -070027
28public class StateMachineTest {
29 StateMachine stateMachine = null;
30
31 @Before
32 public void setUp() {
33 System.out.println("Set Up.");
Ray Milkeyf61a24e2015-09-24 16:34:02 -070034 StateMachine.initializeMaps();
Jonathan Hart932bedc2018-07-12 13:46:09 -070035 stateMachine = new StateMachine("session0");
Ari Saha89831742015-06-26 10:31:48 -070036 }
37
38 @After
39 public void tearDown() {
40 System.out.println("Tear Down.");
Ray Milkeyf61a24e2015-09-24 16:34:02 -070041 StateMachine.destroyMaps();
Ari Saha89831742015-06-26 10:31:48 -070042 stateMachine = null;
43 }
44
45 @Test
46 /**
47 * Test all the basic inputs from state to state: IDLE -> STARTED -> PENDING -> AUTHORIZED -> IDLE
48 */
49 public void basic() throws StateMachineException {
50 System.out.println("======= BASIC =======.");
alshabib6d527452016-06-01 18:00:47 -070051 assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha89831742015-06-26 10:31:48 -070052
53 stateMachine.start();
alshabib6d527452016-06-01 18:00:47 -070054 assertEquals(stateMachine.state(), StateMachine.STATE_STARTED);
Ari Saha89831742015-06-26 10:31:48 -070055
56 stateMachine.requestAccess();
alshabib6d527452016-06-01 18:00:47 -070057 assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha89831742015-06-26 10:31:48 -070058
59 stateMachine.authorizeAccess();
alshabib6d527452016-06-01 18:00:47 -070060 assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED);
Ari Saha89831742015-06-26 10:31:48 -070061
62 stateMachine.logoff();
alshabib6d527452016-06-01 18:00:47 -070063 assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha89831742015-06-26 10:31:48 -070064 }
65
66 @Test
67 /**
68 * Test all inputs from an IDLE state (starting with the ones that are not impacting the current state)
69 */
70 public void testIdleState() throws StateMachineException {
71 System.out.println("======= IDLE STATE TEST =======.");
72 stateMachine.requestAccess();
alshabib6d527452016-06-01 18:00:47 -070073 assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha89831742015-06-26 10:31:48 -070074
75 stateMachine.authorizeAccess();
alshabib6d527452016-06-01 18:00:47 -070076 assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha89831742015-06-26 10:31:48 -070077
78 stateMachine.denyAccess();
alshabib6d527452016-06-01 18:00:47 -070079 assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha89831742015-06-26 10:31:48 -070080
81 stateMachine.logoff();
alshabib6d527452016-06-01 18:00:47 -070082 assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha89831742015-06-26 10:31:48 -070083
84 stateMachine.start();
alshabib6d527452016-06-01 18:00:47 -070085 assertEquals(stateMachine.state(), StateMachine.STATE_STARTED);
Ari Saha89831742015-06-26 10:31:48 -070086 }
87
88 @Test
89 /**
90 * Test all inputs from an STARTED state (starting with the ones that are not impacting the current state)
91 */
92 public void testStartedState() throws StateMachineException {
93 System.out.println("======= STARTED STATE TEST =======.");
94 stateMachine.start();
95
96 stateMachine.authorizeAccess();
alshabib6d527452016-06-01 18:00:47 -070097 assertEquals(stateMachine.state(), StateMachine.STATE_STARTED);
Ari Saha89831742015-06-26 10:31:48 -070098
99 stateMachine.denyAccess();
alshabib6d527452016-06-01 18:00:47 -0700100 assertEquals(stateMachine.state(), StateMachine.STATE_STARTED);
Ari Saha89831742015-06-26 10:31:48 -0700101
102 stateMachine.logoff();
alshabib6d527452016-06-01 18:00:47 -0700103 assertEquals(stateMachine.state(), StateMachine.STATE_STARTED);
Ari Saha89831742015-06-26 10:31:48 -0700104
105 stateMachine.start();
alshabib6d527452016-06-01 18:00:47 -0700106 assertEquals(stateMachine.state(), StateMachine.STATE_STARTED);
Ari Saha89831742015-06-26 10:31:48 -0700107
108 stateMachine.requestAccess();
alshabib6d527452016-06-01 18:00:47 -0700109 assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha89831742015-06-26 10:31:48 -0700110 }
111
112 @Test
113 /**
114 * Test all inputs from a PENDING state (starting with the ones that are not impacting the current state).
115 * The next valid state for this test is AUTHORIZED
116 */
117 public void testPendingStateToAuthorized() throws StateMachineException {
118 System.out.println("======= PENDING STATE TEST (AUTHORIZED) =======.");
119 stateMachine.start();
120 stateMachine.requestAccess();
121
122 stateMachine.logoff();
alshabib6d527452016-06-01 18:00:47 -0700123 assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha89831742015-06-26 10:31:48 -0700124
125 stateMachine.start();
alshabib6d527452016-06-01 18:00:47 -0700126 assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha89831742015-06-26 10:31:48 -0700127
128 stateMachine.requestAccess();
alshabib6d527452016-06-01 18:00:47 -0700129 assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha89831742015-06-26 10:31:48 -0700130
131 stateMachine.authorizeAccess();
alshabib6d527452016-06-01 18:00:47 -0700132 assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED);
Ari Saha89831742015-06-26 10:31:48 -0700133
134 stateMachine.denyAccess();
alshabib6d527452016-06-01 18:00:47 -0700135 assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED);
Ari Saha89831742015-06-26 10:31:48 -0700136 }
137
138 @Test
139 /**
140 * Test all inputs from an PENDING state (starting with the ones that are not impacting the current state).
141 * The next valid state for this test is UNAUTHORIZED
142 */
143 public void testPendingStateToUnauthorized() throws StateMachineException {
144 System.out.println("======= PENDING STATE TEST (DENIED) =======.");
145 stateMachine.start();
146 stateMachine.requestAccess();
147
148 stateMachine.logoff();
alshabib6d527452016-06-01 18:00:47 -0700149 assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha89831742015-06-26 10:31:48 -0700150
151 stateMachine.start();
alshabib6d527452016-06-01 18:00:47 -0700152 assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha89831742015-06-26 10:31:48 -0700153
154 stateMachine.requestAccess();
alshabib6d527452016-06-01 18:00:47 -0700155 assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha89831742015-06-26 10:31:48 -0700156
157 stateMachine.denyAccess();
alshabib6d527452016-06-01 18:00:47 -0700158 assertEquals(stateMachine.state(), StateMachine.STATE_UNAUTHORIZED);
Ari Saha89831742015-06-26 10:31:48 -0700159
160 stateMachine.authorizeAccess();
alshabib6d527452016-06-01 18:00:47 -0700161 assertEquals(stateMachine.state(), StateMachine.STATE_UNAUTHORIZED);
Ari Saha89831742015-06-26 10:31:48 -0700162 }
163
164 @Test
165 /**
166 * Test all inputs from an AUTHORIZED state (starting with the ones that are not impacting the current state).
167 */
168 public void testAuthorizedState() throws StateMachineException {
169 System.out.println("======= AUTHORIZED STATE TEST =======.");
170 stateMachine.start();
171 stateMachine.requestAccess();
172 stateMachine.authorizeAccess();
173
174 stateMachine.start();
alshabib6d527452016-06-01 18:00:47 -0700175 assertEquals(stateMachine.state(), StateMachine.STATE_STARTED);
Ari Saha89831742015-06-26 10:31:48 -0700176
177 stateMachine.requestAccess();
alshabib6d527452016-06-01 18:00:47 -0700178 assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha89831742015-06-26 10:31:48 -0700179
180 stateMachine.authorizeAccess();
alshabib6d527452016-06-01 18:00:47 -0700181 assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED);
Ari Saha89831742015-06-26 10:31:48 -0700182
183 stateMachine.denyAccess();
alshabib6d527452016-06-01 18:00:47 -0700184 assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED);
Ari Saha89831742015-06-26 10:31:48 -0700185
186 stateMachine.logoff();
alshabib6d527452016-06-01 18:00:47 -0700187 assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha89831742015-06-26 10:31:48 -0700188 }
189
190 @Test
191 /**
192 * Test all inputs from an UNAUTHORIZED state (starting with the ones that are not impacting the current state).
193 */
194 public void testUnauthorizedState() throws StateMachineException {
195 System.out.println("======= UNAUTHORIZED STATE TEST =======.");
196 stateMachine.start();
197 stateMachine.requestAccess();
198 stateMachine.denyAccess();
199
200 stateMachine.start();
ke han04e47f32016-10-28 14:15:43 +0800201 assertEquals(stateMachine.state(), StateMachine.STATE_STARTED);
Ari Saha89831742015-06-26 10:31:48 -0700202
203 stateMachine.requestAccess();
ke han04e47f32016-10-28 14:15:43 +0800204 assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha89831742015-06-26 10:31:48 -0700205
206 stateMachine.authorizeAccess();
ke han04e47f32016-10-28 14:15:43 +0800207 assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED);
Ari Saha89831742015-06-26 10:31:48 -0700208
209 stateMachine.denyAccess();
ke han04e47f32016-10-28 14:15:43 +0800210 assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED);
Ari Saha89831742015-06-26 10:31:48 -0700211
212 stateMachine.logoff();
alshabib6d527452016-06-01 18:00:47 -0700213 assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha89831742015-06-26 10:31:48 -0700214 }
215
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700216 @Test
217 public void testSessionIdLookups() {
218 String sessionId1 = "session1";
219 String sessionId2 = "session2";
220 String sessionId3 = "session3";
221
222 StateMachine machine1ShouldBeNull =
223 StateMachine.lookupStateMachineBySessionId(sessionId1);
224 assertNull(machine1ShouldBeNull);
225 StateMachine machine2ShouldBeNull =
226 StateMachine.lookupStateMachineBySessionId(sessionId2);
227 assertNull(machine2ShouldBeNull);
228
Jonathan Hart932bedc2018-07-12 13:46:09 -0700229 StateMachine stateMachine1 = new StateMachine(sessionId1);
230 StateMachine stateMachine2 = new StateMachine(sessionId2);
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700231
232 assertEquals(stateMachine1,
233 StateMachine.lookupStateMachineBySessionId(sessionId1));
234 assertEquals(stateMachine2,
235 StateMachine.lookupStateMachineBySessionId(sessionId2));
236 assertNull(StateMachine.lookupStateMachineBySessionId(sessionId3));
237 }
238
239 @Test
240 public void testIdentifierLookups() throws StateMachineException {
241 String sessionId1 = "session1";
242 String sessionId2 = "session2";
243
244 StateMachine machine1ShouldBeNull =
245 StateMachine.lookupStateMachineById((byte) 1);
246 assertNull(machine1ShouldBeNull);
247 StateMachine machine2ShouldBeNull =
248 StateMachine.lookupStateMachineById((byte) 2);
249 assertNull(machine2ShouldBeNull);
250
Jonathan Hart932bedc2018-07-12 13:46:09 -0700251 StateMachine stateMachine1 = new StateMachine(sessionId1);
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700252 stateMachine1.start();
Jonathan Hart932bedc2018-07-12 13:46:09 -0700253 StateMachine stateMachine2 = new StateMachine(sessionId2);
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700254 stateMachine2.start();
255
256 assertEquals(stateMachine1,
257 StateMachine.lookupStateMachineById(stateMachine1.identifier()));
258 assertEquals(stateMachine2,
259 StateMachine.lookupStateMachineById(stateMachine2.identifier()));
Ari Saha89831742015-06-26 10:31:48 -0700260 }
David K. Bainbridge62019492017-07-28 17:02:10 -0700261
262 @Test
263 /**
264 * Test state machine deletes
265 */
266 public void testStateMachineReset() throws StateMachineException {
267
268 int count = 256;
269
270 //StateMachine.initializeMaps();
271 StateMachine.lookupStateMachineById((byte) 1);
272
273 // Instantiate a bunch of state machines
274 for (int i = 0; i < count; i += 1) {
275 String mac = String.format("00:00:00:00:00:%02x", i);
Jonathan Hart932bedc2018-07-12 13:46:09 -0700276 StateMachine sm = new StateMachine(mac);
David K. Bainbridge62019492017-07-28 17:02:10 -0700277 sm.start();
278 sm.setSupplicantAddress(MacAddress.valueOf(mac));
279 }
280
281 // Verify all state machines with a "even" MAC exist
282 for (int i = 0; i < count; i += 2) {
283 String mac = String.format("00:00:00:00:00:%02x", i);
284 assertNotNull(StateMachine.lookupStateMachineBySessionId(mac));
285 }
286
287 // Delete all state machines with a "even" MAC
288 for (int i = 0; i < count; i += 2) {
289 String mac = String.format("00:00:00:00:00:%02x", i);
290 StateMachine.deleteByMac(MacAddress.valueOf(mac));
291 }
292
293 // Verify all the delete state machines no long exist
294 for (int i = 0; i < count; i += 2) {
295 String mac = String.format("00:00:00:00:00:%02x", i);
296 assertNull(StateMachine.lookupStateMachineBySessionId(mac));
297 }
298 }
Ari Saha89831742015-06-26 10:31:48 -0700299}