blob: 4053ff9c8d6ed9c74a115db2fcaf52b0df371d25 [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 */
Matteo Scandolocf847b82019-04-26 15:00:00 -070017package org.opencord.aaa.impl;
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 Hart5db44532018-07-12 18:13:54 -070035 StateMachine.setDelegate(e -> { });
Jonathan Hart932bedc2018-07-12 13:46:09 -070036 stateMachine = new StateMachine("session0");
Ari Saha89831742015-06-26 10:31:48 -070037 }
38
39 @After
40 public void tearDown() {
41 System.out.println("Tear Down.");
Ray Milkeyf61a24e2015-09-24 16:34:02 -070042 StateMachine.destroyMaps();
Ari Saha89831742015-06-26 10:31:48 -070043 stateMachine = null;
44 }
45
46 @Test
47 /**
48 * Test all the basic inputs from state to state: IDLE -> STARTED -> PENDING -> AUTHORIZED -> IDLE
49 */
50 public void basic() throws StateMachineException {
51 System.out.println("======= BASIC =======.");
alshabib6d527452016-06-01 18:00:47 -070052 assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha89831742015-06-26 10:31:48 -070053
54 stateMachine.start();
alshabib6d527452016-06-01 18:00:47 -070055 assertEquals(stateMachine.state(), StateMachine.STATE_STARTED);
Ari Saha89831742015-06-26 10:31:48 -070056
57 stateMachine.requestAccess();
alshabib6d527452016-06-01 18:00:47 -070058 assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha89831742015-06-26 10:31:48 -070059
60 stateMachine.authorizeAccess();
alshabib6d527452016-06-01 18:00:47 -070061 assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED);
Ari Saha89831742015-06-26 10:31:48 -070062
63 stateMachine.logoff();
alshabib6d527452016-06-01 18:00:47 -070064 assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha89831742015-06-26 10:31:48 -070065 }
66
67 @Test
68 /**
69 * Test all inputs from an IDLE state (starting with the ones that are not impacting the current state)
70 */
71 public void testIdleState() throws StateMachineException {
72 System.out.println("======= IDLE STATE TEST =======.");
73 stateMachine.requestAccess();
alshabib6d527452016-06-01 18:00:47 -070074 assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha89831742015-06-26 10:31:48 -070075
76 stateMachine.authorizeAccess();
alshabib6d527452016-06-01 18:00:47 -070077 assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha89831742015-06-26 10:31:48 -070078
79 stateMachine.denyAccess();
alshabib6d527452016-06-01 18:00:47 -070080 assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha89831742015-06-26 10:31:48 -070081
82 stateMachine.logoff();
alshabib6d527452016-06-01 18:00:47 -070083 assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha89831742015-06-26 10:31:48 -070084
85 stateMachine.start();
alshabib6d527452016-06-01 18:00:47 -070086 assertEquals(stateMachine.state(), StateMachine.STATE_STARTED);
Ari Saha89831742015-06-26 10:31:48 -070087 }
88
89 @Test
90 /**
91 * Test all inputs from an STARTED state (starting with the ones that are not impacting the current state)
92 */
93 public void testStartedState() throws StateMachineException {
94 System.out.println("======= STARTED STATE TEST =======.");
95 stateMachine.start();
96
97 stateMachine.authorizeAccess();
alshabib6d527452016-06-01 18:00:47 -070098 assertEquals(stateMachine.state(), StateMachine.STATE_STARTED);
Ari Saha89831742015-06-26 10:31:48 -070099
100 stateMachine.denyAccess();
alshabib6d527452016-06-01 18:00:47 -0700101 assertEquals(stateMachine.state(), StateMachine.STATE_STARTED);
Ari Saha89831742015-06-26 10:31:48 -0700102
103 stateMachine.logoff();
alshabib6d527452016-06-01 18:00:47 -0700104 assertEquals(stateMachine.state(), StateMachine.STATE_STARTED);
Ari Saha89831742015-06-26 10:31:48 -0700105
106 stateMachine.start();
alshabib6d527452016-06-01 18:00:47 -0700107 assertEquals(stateMachine.state(), StateMachine.STATE_STARTED);
Ari Saha89831742015-06-26 10:31:48 -0700108
109 stateMachine.requestAccess();
alshabib6d527452016-06-01 18:00:47 -0700110 assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha89831742015-06-26 10:31:48 -0700111 }
112
113 @Test
114 /**
115 * Test all inputs from a PENDING state (starting with the ones that are not impacting the current state).
116 * The next valid state for this test is AUTHORIZED
117 */
118 public void testPendingStateToAuthorized() throws StateMachineException {
119 System.out.println("======= PENDING STATE TEST (AUTHORIZED) =======.");
120 stateMachine.start();
121 stateMachine.requestAccess();
122
123 stateMachine.logoff();
alshabib6d527452016-06-01 18:00:47 -0700124 assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha89831742015-06-26 10:31:48 -0700125
126 stateMachine.start();
alshabib6d527452016-06-01 18:00:47 -0700127 assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha89831742015-06-26 10:31:48 -0700128
129 stateMachine.requestAccess();
alshabib6d527452016-06-01 18:00:47 -0700130 assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha89831742015-06-26 10:31:48 -0700131
132 stateMachine.authorizeAccess();
alshabib6d527452016-06-01 18:00:47 -0700133 assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED);
Ari Saha89831742015-06-26 10:31:48 -0700134
135 stateMachine.denyAccess();
alshabib6d527452016-06-01 18:00:47 -0700136 assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED);
Ari Saha89831742015-06-26 10:31:48 -0700137 }
138
139 @Test
140 /**
141 * Test all inputs from an PENDING state (starting with the ones that are not impacting the current state).
142 * The next valid state for this test is UNAUTHORIZED
143 */
144 public void testPendingStateToUnauthorized() throws StateMachineException {
145 System.out.println("======= PENDING STATE TEST (DENIED) =======.");
146 stateMachine.start();
147 stateMachine.requestAccess();
148
149 stateMachine.logoff();
alshabib6d527452016-06-01 18:00:47 -0700150 assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha89831742015-06-26 10:31:48 -0700151
152 stateMachine.start();
alshabib6d527452016-06-01 18:00:47 -0700153 assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha89831742015-06-26 10:31:48 -0700154
155 stateMachine.requestAccess();
alshabib6d527452016-06-01 18:00:47 -0700156 assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha89831742015-06-26 10:31:48 -0700157
158 stateMachine.denyAccess();
alshabib6d527452016-06-01 18:00:47 -0700159 assertEquals(stateMachine.state(), StateMachine.STATE_UNAUTHORIZED);
Ari Saha89831742015-06-26 10:31:48 -0700160
161 stateMachine.authorizeAccess();
alshabib6d527452016-06-01 18:00:47 -0700162 assertEquals(stateMachine.state(), StateMachine.STATE_UNAUTHORIZED);
Ari Saha89831742015-06-26 10:31:48 -0700163 }
164
165 @Test
166 /**
167 * Test all inputs from an AUTHORIZED state (starting with the ones that are not impacting the current state).
168 */
169 public void testAuthorizedState() throws StateMachineException {
170 System.out.println("======= AUTHORIZED STATE TEST =======.");
171 stateMachine.start();
172 stateMachine.requestAccess();
173 stateMachine.authorizeAccess();
174
175 stateMachine.start();
alshabib6d527452016-06-01 18:00:47 -0700176 assertEquals(stateMachine.state(), StateMachine.STATE_STARTED);
Ari Saha89831742015-06-26 10:31:48 -0700177
178 stateMachine.requestAccess();
alshabib6d527452016-06-01 18:00:47 -0700179 assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha89831742015-06-26 10:31:48 -0700180
181 stateMachine.authorizeAccess();
alshabib6d527452016-06-01 18:00:47 -0700182 assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED);
Ari Saha89831742015-06-26 10:31:48 -0700183
184 stateMachine.denyAccess();
alshabib6d527452016-06-01 18:00:47 -0700185 assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED);
Ari Saha89831742015-06-26 10:31:48 -0700186
187 stateMachine.logoff();
alshabib6d527452016-06-01 18:00:47 -0700188 assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha89831742015-06-26 10:31:48 -0700189 }
190
191 @Test
192 /**
193 * Test all inputs from an UNAUTHORIZED state (starting with the ones that are not impacting the current state).
194 */
195 public void testUnauthorizedState() throws StateMachineException {
196 System.out.println("======= UNAUTHORIZED STATE TEST =======.");
197 stateMachine.start();
198 stateMachine.requestAccess();
199 stateMachine.denyAccess();
200
201 stateMachine.start();
ke han04e47f32016-10-28 14:15:43 +0800202 assertEquals(stateMachine.state(), StateMachine.STATE_STARTED);
Ari Saha89831742015-06-26 10:31:48 -0700203
204 stateMachine.requestAccess();
ke han04e47f32016-10-28 14:15:43 +0800205 assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha89831742015-06-26 10:31:48 -0700206
207 stateMachine.authorizeAccess();
ke han04e47f32016-10-28 14:15:43 +0800208 assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED);
Ari Saha89831742015-06-26 10:31:48 -0700209
210 stateMachine.denyAccess();
ke han04e47f32016-10-28 14:15:43 +0800211 assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED);
Ari Saha89831742015-06-26 10:31:48 -0700212
213 stateMachine.logoff();
alshabib6d527452016-06-01 18:00:47 -0700214 assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha89831742015-06-26 10:31:48 -0700215 }
216
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700217 @Test
218 public void testSessionIdLookups() {
219 String sessionId1 = "session1";
220 String sessionId2 = "session2";
221 String sessionId3 = "session3";
222
223 StateMachine machine1ShouldBeNull =
224 StateMachine.lookupStateMachineBySessionId(sessionId1);
225 assertNull(machine1ShouldBeNull);
226 StateMachine machine2ShouldBeNull =
227 StateMachine.lookupStateMachineBySessionId(sessionId2);
228 assertNull(machine2ShouldBeNull);
229
Jonathan Hart932bedc2018-07-12 13:46:09 -0700230 StateMachine stateMachine1 = new StateMachine(sessionId1);
231 StateMachine stateMachine2 = new StateMachine(sessionId2);
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700232
233 assertEquals(stateMachine1,
234 StateMachine.lookupStateMachineBySessionId(sessionId1));
235 assertEquals(stateMachine2,
236 StateMachine.lookupStateMachineBySessionId(sessionId2));
237 assertNull(StateMachine.lookupStateMachineBySessionId(sessionId3));
238 }
239
240 @Test
241 public void testIdentifierLookups() throws StateMachineException {
242 String sessionId1 = "session1";
243 String sessionId2 = "session2";
244
245 StateMachine machine1ShouldBeNull =
246 StateMachine.lookupStateMachineById((byte) 1);
247 assertNull(machine1ShouldBeNull);
248 StateMachine machine2ShouldBeNull =
249 StateMachine.lookupStateMachineById((byte) 2);
250 assertNull(machine2ShouldBeNull);
251
Jonathan Hart932bedc2018-07-12 13:46:09 -0700252 StateMachine stateMachine1 = new StateMachine(sessionId1);
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700253 stateMachine1.start();
Jonathan Hart932bedc2018-07-12 13:46:09 -0700254 StateMachine stateMachine2 = new StateMachine(sessionId2);
Ray Milkeyf61a24e2015-09-24 16:34:02 -0700255 stateMachine2.start();
256
257 assertEquals(stateMachine1,
258 StateMachine.lookupStateMachineById(stateMachine1.identifier()));
259 assertEquals(stateMachine2,
260 StateMachine.lookupStateMachineById(stateMachine2.identifier()));
Ari Saha89831742015-06-26 10:31:48 -0700261 }
David K. Bainbridge62019492017-07-28 17:02:10 -0700262
263 @Test
264 /**
265 * Test state machine deletes
266 */
267 public void testStateMachineReset() throws StateMachineException {
268
269 int count = 256;
270
271 //StateMachine.initializeMaps();
272 StateMachine.lookupStateMachineById((byte) 1);
273
274 // Instantiate a bunch of state machines
275 for (int i = 0; i < count; i += 1) {
276 String mac = String.format("00:00:00:00:00:%02x", i);
Jonathan Hart932bedc2018-07-12 13:46:09 -0700277 StateMachine sm = new StateMachine(mac);
David K. Bainbridge62019492017-07-28 17:02:10 -0700278 sm.start();
279 sm.setSupplicantAddress(MacAddress.valueOf(mac));
280 }
281
282 // Verify all state machines with a "even" MAC exist
283 for (int i = 0; i < count; i += 2) {
284 String mac = String.format("00:00:00:00:00:%02x", i);
285 assertNotNull(StateMachine.lookupStateMachineBySessionId(mac));
286 }
287
288 // Delete all state machines with a "even" MAC
289 for (int i = 0; i < count; i += 2) {
290 String mac = String.format("00:00:00:00:00:%02x", i);
291 StateMachine.deleteByMac(MacAddress.valueOf(mac));
292 }
293
294 // Verify all the delete state machines no long exist
295 for (int i = 0; i < count; i += 2) {
296 String mac = String.format("00:00:00:00:00:%02x", i);
297 assertNull(StateMachine.lookupStateMachineBySessionId(mac));
298 }
299 }
Ari Saha89831742015-06-26 10:31:48 -0700300}