blob: 3d4ea27e4e32bce3a3470a7102e094f8855a4ae4 [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;
Ray Milkey967776a2015-10-07 14:37:17 -070022
David K. Bainbridge62019492017-07-28 17:02:10 -070023import org.onlab.packet.MacAddress;
24
Ray Milkey967776a2015-10-07 14:37:17 -070025import static org.junit.Assert.assertEquals;
26import static org.junit.Assert.assertNull;
David K. Bainbridge62019492017-07-28 17:02:10 -070027import static org.junit.Assert.assertNotNull;
Ari Saha89831742015-06-26 10:31:48 -070028
Amit Ghoshc9ac1e52017-07-28 12:31:18 +010029import org.onlab.packet.VlanId;
Ari Saha89831742015-06-26 10:31:48 -070030
31public class StateMachineTest {
32 StateMachine stateMachine = null;
33
34 @Before
35 public void setUp() {
36 System.out.println("Set Up.");
Ray Milkeyf61a24e2015-09-24 16:34:02 -070037 StateMachine.initializeMaps();
Amit Ghoshc9ac1e52017-07-28 12:31:18 +010038 stateMachine = new StateMachine("session0", VlanId.vlanId((short) 2));
Ari Saha89831742015-06-26 10:31:48 -070039 }
40
41 @After
42 public void tearDown() {
43 System.out.println("Tear Down.");
Ray Milkeyf61a24e2015-09-24 16:34:02 -070044 StateMachine.destroyMaps();
Ari Saha89831742015-06-26 10:31:48 -070045 stateMachine = null;
46 }
47
48 @Test
49 /**
50 * Test all the basic inputs from state to state: IDLE -> STARTED -> PENDING -> AUTHORIZED -> IDLE
51 */
52 public void basic() throws StateMachineException {
53 System.out.println("======= BASIC =======.");
alshabib6d527452016-06-01 18:00:47 -070054 assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha89831742015-06-26 10:31:48 -070055
56 stateMachine.start();
alshabib6d527452016-06-01 18:00:47 -070057 assertEquals(stateMachine.state(), StateMachine.STATE_STARTED);
Ari Saha89831742015-06-26 10:31:48 -070058
59 stateMachine.requestAccess();
alshabib6d527452016-06-01 18:00:47 -070060 assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha89831742015-06-26 10:31:48 -070061
62 stateMachine.authorizeAccess();
alshabib6d527452016-06-01 18:00:47 -070063 assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED);
Ari Saha89831742015-06-26 10:31:48 -070064
65 stateMachine.logoff();
alshabib6d527452016-06-01 18:00:47 -070066 assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha89831742015-06-26 10:31:48 -070067 }
68
69 @Test
70 /**
71 * Test all inputs from an IDLE state (starting with the ones that are not impacting the current state)
72 */
73 public void testIdleState() throws StateMachineException {
74 System.out.println("======= IDLE STATE TEST =======.");
75 stateMachine.requestAccess();
alshabib6d527452016-06-01 18:00:47 -070076 assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha89831742015-06-26 10:31:48 -070077
78 stateMachine.authorizeAccess();
alshabib6d527452016-06-01 18:00:47 -070079 assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha89831742015-06-26 10:31:48 -070080
81 stateMachine.denyAccess();
alshabib6d527452016-06-01 18:00:47 -070082 assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha89831742015-06-26 10:31:48 -070083
84 stateMachine.logoff();
alshabib6d527452016-06-01 18:00:47 -070085 assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha89831742015-06-26 10:31:48 -070086
87 stateMachine.start();
alshabib6d527452016-06-01 18:00:47 -070088 assertEquals(stateMachine.state(), StateMachine.STATE_STARTED);
Ari Saha89831742015-06-26 10:31:48 -070089 }
90
91 @Test
92 /**
93 * Test all inputs from an STARTED state (starting with the ones that are not impacting the current state)
94 */
95 public void testStartedState() throws StateMachineException {
96 System.out.println("======= STARTED STATE TEST =======.");
97 stateMachine.start();
98
99 stateMachine.authorizeAccess();
alshabib6d527452016-06-01 18:00:47 -0700100 assertEquals(stateMachine.state(), StateMachine.STATE_STARTED);
Ari Saha89831742015-06-26 10:31:48 -0700101
102 stateMachine.denyAccess();
alshabib6d527452016-06-01 18:00:47 -0700103 assertEquals(stateMachine.state(), StateMachine.STATE_STARTED);
Ari Saha89831742015-06-26 10:31:48 -0700104
105 stateMachine.logoff();
alshabib6d527452016-06-01 18:00:47 -0700106 assertEquals(stateMachine.state(), StateMachine.STATE_STARTED);
Ari Saha89831742015-06-26 10:31:48 -0700107
108 stateMachine.start();
alshabib6d527452016-06-01 18:00:47 -0700109 assertEquals(stateMachine.state(), StateMachine.STATE_STARTED);
Ari Saha89831742015-06-26 10:31:48 -0700110
111 stateMachine.requestAccess();
alshabib6d527452016-06-01 18:00:47 -0700112 assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha89831742015-06-26 10:31:48 -0700113 }
114
115 @Test
116 /**
117 * Test all inputs from a PENDING state (starting with the ones that are not impacting the current state).
118 * The next valid state for this test is AUTHORIZED
119 */
120 public void testPendingStateToAuthorized() throws StateMachineException {
121 System.out.println("======= PENDING STATE TEST (AUTHORIZED) =======.");
122 stateMachine.start();
123 stateMachine.requestAccess();
124
125 stateMachine.logoff();
alshabib6d527452016-06-01 18:00:47 -0700126 assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha89831742015-06-26 10:31:48 -0700127
128 stateMachine.start();
alshabib6d527452016-06-01 18:00:47 -0700129 assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha89831742015-06-26 10:31:48 -0700130
131 stateMachine.requestAccess();
alshabib6d527452016-06-01 18:00:47 -0700132 assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha89831742015-06-26 10:31:48 -0700133
134 stateMachine.authorizeAccess();
alshabib6d527452016-06-01 18:00:47 -0700135 assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED);
Ari Saha89831742015-06-26 10:31:48 -0700136
137 stateMachine.denyAccess();
alshabib6d527452016-06-01 18:00:47 -0700138 assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED);
Ari Saha89831742015-06-26 10:31:48 -0700139 }
140
141 @Test
142 /**
143 * Test all inputs from an PENDING state (starting with the ones that are not impacting the current state).
144 * The next valid state for this test is UNAUTHORIZED
145 */
146 public void testPendingStateToUnauthorized() throws StateMachineException {
147 System.out.println("======= PENDING STATE TEST (DENIED) =======.");
148 stateMachine.start();
149 stateMachine.requestAccess();
150
151 stateMachine.logoff();
alshabib6d527452016-06-01 18:00:47 -0700152 assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha89831742015-06-26 10:31:48 -0700153
154 stateMachine.start();
alshabib6d527452016-06-01 18:00:47 -0700155 assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha89831742015-06-26 10:31:48 -0700156
157 stateMachine.requestAccess();
alshabib6d527452016-06-01 18:00:47 -0700158 assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha89831742015-06-26 10:31:48 -0700159
160 stateMachine.denyAccess();
alshabib6d527452016-06-01 18:00:47 -0700161 assertEquals(stateMachine.state(), StateMachine.STATE_UNAUTHORIZED);
Ari Saha89831742015-06-26 10:31:48 -0700162
163 stateMachine.authorizeAccess();
alshabib6d527452016-06-01 18:00:47 -0700164 assertEquals(stateMachine.state(), StateMachine.STATE_UNAUTHORIZED);
Ari Saha89831742015-06-26 10:31:48 -0700165 }
166
167 @Test
168 /**
169 * Test all inputs from an AUTHORIZED state (starting with the ones that are not impacting the current state).
170 */
171 public void testAuthorizedState() throws StateMachineException {
172 System.out.println("======= AUTHORIZED STATE TEST =======.");
173 stateMachine.start();
174 stateMachine.requestAccess();
175 stateMachine.authorizeAccess();
176
177 stateMachine.start();
alshabib6d527452016-06-01 18:00:47 -0700178 assertEquals(stateMachine.state(), StateMachine.STATE_STARTED);
Ari Saha89831742015-06-26 10:31:48 -0700179
180 stateMachine.requestAccess();
alshabib6d527452016-06-01 18:00:47 -0700181 assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha89831742015-06-26 10:31:48 -0700182
183 stateMachine.authorizeAccess();
alshabib6d527452016-06-01 18:00:47 -0700184 assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED);
Ari Saha89831742015-06-26 10:31:48 -0700185
186 stateMachine.denyAccess();
alshabib6d527452016-06-01 18:00:47 -0700187 assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED);
Ari Saha89831742015-06-26 10:31:48 -0700188
189 stateMachine.logoff();
alshabib6d527452016-06-01 18:00:47 -0700190 assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha89831742015-06-26 10:31:48 -0700191 }
192
193 @Test
194 /**
195 * Test all inputs from an UNAUTHORIZED state (starting with the ones that are not impacting the current state).
196 */
197 public void testUnauthorizedState() throws StateMachineException {
198 System.out.println("======= UNAUTHORIZED STATE TEST =======.");
199 stateMachine.start();
200 stateMachine.requestAccess();
201 stateMachine.denyAccess();
202
203 stateMachine.start();
ke han04e47f32016-10-28 14:15:43 +0800204 assertEquals(stateMachine.state(), StateMachine.STATE_STARTED);
Ari Saha89831742015-06-26 10:31:48 -0700205
206 stateMachine.requestAccess();
ke han04e47f32016-10-28 14:15:43 +0800207 assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha89831742015-06-26 10:31:48 -0700208
209 stateMachine.authorizeAccess();
ke han04e47f32016-10-28 14:15:43 +0800210 assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED);
Ari Saha89831742015-06-26 10:31:48 -0700211
212 stateMachine.denyAccess();
ke han04e47f32016-10-28 14:15:43 +0800213 assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED);
Ari Saha89831742015-06-26 10:31:48 -0700214
215 stateMachine.logoff();
alshabib6d527452016-06-01 18:00:47 -0700216 assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha89831742015-06-26 10:31:48 -0700217 }
218
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700219 @Test
220 public void testSessionIdLookups() {
221 String sessionId1 = "session1";
222 String sessionId2 = "session2";
223 String sessionId3 = "session3";
224
225 StateMachine machine1ShouldBeNull =
226 StateMachine.lookupStateMachineBySessionId(sessionId1);
227 assertNull(machine1ShouldBeNull);
228 StateMachine machine2ShouldBeNull =
229 StateMachine.lookupStateMachineBySessionId(sessionId2);
230 assertNull(machine2ShouldBeNull);
231
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100232 StateMachine stateMachine1 = new StateMachine(sessionId1, VlanId.vlanId((short) 2));
233 StateMachine stateMachine2 = new StateMachine(sessionId2, VlanId.vlanId((short) 2));
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700234
235 assertEquals(stateMachine1,
236 StateMachine.lookupStateMachineBySessionId(sessionId1));
237 assertEquals(stateMachine2,
238 StateMachine.lookupStateMachineBySessionId(sessionId2));
239 assertNull(StateMachine.lookupStateMachineBySessionId(sessionId3));
240 }
241
242 @Test
243 public void testIdentifierLookups() throws StateMachineException {
244 String sessionId1 = "session1";
245 String sessionId2 = "session2";
246
247 StateMachine machine1ShouldBeNull =
248 StateMachine.lookupStateMachineById((byte) 1);
249 assertNull(machine1ShouldBeNull);
250 StateMachine machine2ShouldBeNull =
251 StateMachine.lookupStateMachineById((byte) 2);
252 assertNull(machine2ShouldBeNull);
253
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100254 StateMachine stateMachine1 = new StateMachine(sessionId1, VlanId.vlanId((short) 2));
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700255 stateMachine1.start();
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100256 StateMachine stateMachine2 = new StateMachine(sessionId2, VlanId.vlanId((short) 2));
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700257 stateMachine2.start();
258
259 assertEquals(stateMachine1,
260 StateMachine.lookupStateMachineById(stateMachine1.identifier()));
261 assertEquals(stateMachine2,
262 StateMachine.lookupStateMachineById(stateMachine2.identifier()));
Ari Saha89831742015-06-26 10:31:48 -0700263 }
David K. Bainbridge62019492017-07-28 17:02:10 -0700264
265 @Test
266 /**
267 * Test state machine deletes
268 */
269 public void testStateMachineReset() throws StateMachineException {
270
271 int count = 256;
272
273 //StateMachine.initializeMaps();
274 StateMachine.lookupStateMachineById((byte) 1);
275
276 // Instantiate a bunch of state machines
277 for (int i = 0; i < count; i += 1) {
278 String mac = String.format("00:00:00:00:00:%02x", i);
279 StateMachine sm = new StateMachine(mac, VlanId.vlanId((short) i));
280 sm.start();
281 sm.setSupplicantAddress(MacAddress.valueOf(mac));
282 }
283
284 // Verify all state machines with a "even" MAC exist
285 for (int i = 0; i < count; i += 2) {
286 String mac = String.format("00:00:00:00:00:%02x", i);
287 assertNotNull(StateMachine.lookupStateMachineBySessionId(mac));
288 }
289
290 // Delete all state machines with a "even" MAC
291 for (int i = 0; i < count; i += 2) {
292 String mac = String.format("00:00:00:00:00:%02x", i);
293 StateMachine.deleteByMac(MacAddress.valueOf(mac));
294 }
295
296 // Verify all the delete state machines no long exist
297 for (int i = 0; i < count; i += 2) {
298 String mac = String.format("00:00:00:00:00:%02x", i);
299 assertNull(StateMachine.lookupStateMachineBySessionId(mac));
300 }
301 }
Ari Saha89831742015-06-26 10:31:48 -0700302}