blob: 1c09f6ef28577b15bce4177a60fce4d8b0cd2e88 [file] [log] [blame]
Ari Saha79d7c252015-06-26 10:31:48 -07001/*
2 *
3 * Copyright 2015 AT&T Foundry
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18package org.onosproject.aaa;
19
20import org.junit.After;
21import org.junit.Assert;
22import org.junit.Before;
23import org.junit.Test;
Ray Milkey4ed93692015-10-07 14:37:17 -070024
25import static org.junit.Assert.assertEquals;
26import static org.junit.Assert.assertNull;
Ari Saha79d7c252015-06-26 10:31:48 -070027
28
29public class StateMachineTest {
30 StateMachine stateMachine = null;
31
32 @Before
33 public void setUp() {
34 System.out.println("Set Up.");
35 StateMachine.bitSet.clear();
Ray Milkey75879ef2015-09-24 16:34:02 -070036 StateMachine.initializeMaps();
Ari Saha79d7c252015-06-26 10:31:48 -070037 stateMachine = new StateMachine("session0", null);
38 }
39
40 @After
41 public void tearDown() {
42 System.out.println("Tear Down.");
43 StateMachine.bitSet.clear();
Ray Milkey75879ef2015-09-24 16:34:02 -070044 StateMachine.destroyMaps();
Ari Saha79d7c252015-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 =======.");
Ray Milkey75879ef2015-09-24 16:34:02 -070054 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha79d7c252015-06-26 10:31:48 -070055
56 stateMachine.start();
Ray Milkey75879ef2015-09-24 16:34:02 -070057 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_STARTED);
Ari Saha79d7c252015-06-26 10:31:48 -070058
59 stateMachine.requestAccess();
Ray Milkey75879ef2015-09-24 16:34:02 -070060 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha79d7c252015-06-26 10:31:48 -070061
62 stateMachine.authorizeAccess();
Ray Milkey75879ef2015-09-24 16:34:02 -070063 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED);
Ari Saha79d7c252015-06-26 10:31:48 -070064
65 stateMachine.logoff();
Ray Milkey75879ef2015-09-24 16:34:02 -070066 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha79d7c252015-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();
Ray Milkey75879ef2015-09-24 16:34:02 -070076 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha79d7c252015-06-26 10:31:48 -070077
78 stateMachine.authorizeAccess();
Ray Milkey75879ef2015-09-24 16:34:02 -070079 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha79d7c252015-06-26 10:31:48 -070080
81 stateMachine.denyAccess();
Ray Milkey75879ef2015-09-24 16:34:02 -070082 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha79d7c252015-06-26 10:31:48 -070083
84 stateMachine.logoff();
Ray Milkey75879ef2015-09-24 16:34:02 -070085 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha79d7c252015-06-26 10:31:48 -070086
87 stateMachine.start();
Ray Milkey75879ef2015-09-24 16:34:02 -070088 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_STARTED);
Ari Saha79d7c252015-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();
Ray Milkey75879ef2015-09-24 16:34:02 -0700100 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_STARTED);
Ari Saha79d7c252015-06-26 10:31:48 -0700101
102 stateMachine.denyAccess();
Ray Milkey75879ef2015-09-24 16:34:02 -0700103 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_STARTED);
Ari Saha79d7c252015-06-26 10:31:48 -0700104
105 stateMachine.logoff();
Ray Milkey75879ef2015-09-24 16:34:02 -0700106 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_STARTED);
Ari Saha79d7c252015-06-26 10:31:48 -0700107
108 stateMachine.start();
Ray Milkey75879ef2015-09-24 16:34:02 -0700109 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_STARTED);
Ari Saha79d7c252015-06-26 10:31:48 -0700110
111 stateMachine.requestAccess();
Ray Milkey75879ef2015-09-24 16:34:02 -0700112 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha79d7c252015-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();
Ray Milkey75879ef2015-09-24 16:34:02 -0700126 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha79d7c252015-06-26 10:31:48 -0700127
128 stateMachine.start();
Ray Milkey75879ef2015-09-24 16:34:02 -0700129 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha79d7c252015-06-26 10:31:48 -0700130
131 stateMachine.requestAccess();
Ray Milkey75879ef2015-09-24 16:34:02 -0700132 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha79d7c252015-06-26 10:31:48 -0700133
134 stateMachine.authorizeAccess();
Ray Milkey75879ef2015-09-24 16:34:02 -0700135 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED);
Ari Saha79d7c252015-06-26 10:31:48 -0700136
137 stateMachine.denyAccess();
Ray Milkey75879ef2015-09-24 16:34:02 -0700138 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED);
Ari Saha79d7c252015-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();
Ray Milkey75879ef2015-09-24 16:34:02 -0700152 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha79d7c252015-06-26 10:31:48 -0700153
154 stateMachine.start();
Ray Milkey75879ef2015-09-24 16:34:02 -0700155 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha79d7c252015-06-26 10:31:48 -0700156
157 stateMachine.requestAccess();
Ray Milkey75879ef2015-09-24 16:34:02 -0700158 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha79d7c252015-06-26 10:31:48 -0700159
160 stateMachine.denyAccess();
Ray Milkey75879ef2015-09-24 16:34:02 -0700161 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_UNAUTHORIZED);
Ari Saha79d7c252015-06-26 10:31:48 -0700162
163 stateMachine.authorizeAccess();
Ray Milkey75879ef2015-09-24 16:34:02 -0700164 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_UNAUTHORIZED);
Ari Saha79d7c252015-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();
Qianqian Hu5f374cd2016-02-15 17:25:22 +0800178 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_STARTED);
Ari Saha79d7c252015-06-26 10:31:48 -0700179
180 stateMachine.requestAccess();
Qianqian Hu5f374cd2016-02-15 17:25:22 +0800181 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha79d7c252015-06-26 10:31:48 -0700182
183 stateMachine.authorizeAccess();
Ray Milkey75879ef2015-09-24 16:34:02 -0700184 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED);
Ari Saha79d7c252015-06-26 10:31:48 -0700185
186 stateMachine.denyAccess();
Ray Milkey75879ef2015-09-24 16:34:02 -0700187 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED);
Ari Saha79d7c252015-06-26 10:31:48 -0700188
189 stateMachine.logoff();
Ray Milkey75879ef2015-09-24 16:34:02 -0700190 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha79d7c252015-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();
Ray Milkey75879ef2015-09-24 16:34:02 -0700204 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_UNAUTHORIZED);
Ari Saha79d7c252015-06-26 10:31:48 -0700205
206 stateMachine.requestAccess();
Ray Milkey75879ef2015-09-24 16:34:02 -0700207 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_UNAUTHORIZED);
Ari Saha79d7c252015-06-26 10:31:48 -0700208
209 stateMachine.authorizeAccess();
Ray Milkey75879ef2015-09-24 16:34:02 -0700210 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_UNAUTHORIZED);
Ari Saha79d7c252015-06-26 10:31:48 -0700211
212 stateMachine.denyAccess();
Ray Milkey75879ef2015-09-24 16:34:02 -0700213 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_UNAUTHORIZED);
Ari Saha79d7c252015-06-26 10:31:48 -0700214
215 stateMachine.logoff();
Ray Milkey75879ef2015-09-24 16:34:02 -0700216 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha79d7c252015-06-26 10:31:48 -0700217 }
218
219
220 @Test
221 public void testIdentifierAvailability() throws StateMachineException {
222 System.out.println("======= IDENTIFIER TEST =======.");
Ray Milkey75879ef2015-09-24 16:34:02 -0700223 byte identifier = stateMachine.identifier();
224 System.out.println("State: " + stateMachine.state());
Ari Saha79d7c252015-06-26 10:31:48 -0700225 System.out.println("Identifier: " + Byte.toUnsignedInt(identifier));
226 Assert.assertEquals(-1, identifier);
227 stateMachine.start();
228
229
230 StateMachine sm247 = null;
231 StateMachine sm3 = null;
232
233
234 //create 255 others state machines
235 for (int i = 1; i <= 255; i++) {
236 StateMachine sm = new StateMachine("session" + i, null);
237 sm.start();
Ray Milkey75879ef2015-09-24 16:34:02 -0700238 byte id = sm.identifier();
Ari Saha79d7c252015-06-26 10:31:48 -0700239 Assert.assertEquals(i, Byte.toUnsignedInt(id));
240 if (i == 3) {
241 sm3 = sm;
242 System.out.println("SM3: " + sm3.toString());
243 }
244 if (i == 247) {
245 sm247 = sm;
246 System.out.println("SM247: " + sm247.toString());
247 }
248 }
249
250 //simulate the state machine for a specific session and logoff so we can free up a spot for an identifier
251 //let's choose identifier 247 then we free up 3
Ray Milkey75879ef2015-09-24 16:34:02 -0700252 Assert.assertNotNull(sm247);
Ari Saha79d7c252015-06-26 10:31:48 -0700253 sm247.requestAccess();
254 sm247.authorizeAccess();
255 sm247.logoff();
Ari Saha79d7c252015-06-26 10:31:48 -0700256
Ray Milkey75879ef2015-09-24 16:34:02 -0700257 Assert.assertNotNull(sm3);
Ari Saha79d7c252015-06-26 10:31:48 -0700258 sm3.requestAccess();
259 sm3.authorizeAccess();
260 sm3.logoff();
Ari Saha79d7c252015-06-26 10:31:48 -0700261
262 StateMachine otherSM3 = new StateMachine("session3b", null);
263 otherSM3.start();
264 otherSM3.requestAccess();
Ray Milkey75879ef2015-09-24 16:34:02 -0700265 byte id3 = otherSM3.identifier();
Ari Saha79d7c252015-06-26 10:31:48 -0700266 Assert.assertEquals(3, Byte.toUnsignedInt(id3));
267
268 StateMachine otherSM247 = new StateMachine("session247b", null);
269 otherSM247.start();
270 otherSM247.requestAccess();
Ray Milkey75879ef2015-09-24 16:34:02 -0700271 byte id247 = otherSM247.identifier();
Ari Saha79d7c252015-06-26 10:31:48 -0700272 Assert.assertEquals(247, Byte.toUnsignedInt(id247));
Ray Milkey75879ef2015-09-24 16:34:02 -0700273 }
Ari Saha79d7c252015-06-26 10:31:48 -0700274
Ray Milkey75879ef2015-09-24 16:34:02 -0700275 @Test
276 public void testSessionIdLookups() {
277 String sessionId1 = "session1";
278 String sessionId2 = "session2";
279 String sessionId3 = "session3";
280
281 StateMachine machine1ShouldBeNull =
282 StateMachine.lookupStateMachineBySessionId(sessionId1);
283 assertNull(machine1ShouldBeNull);
284 StateMachine machine2ShouldBeNull =
285 StateMachine.lookupStateMachineBySessionId(sessionId2);
286 assertNull(machine2ShouldBeNull);
287
288 StateMachine stateMachine1 = new StateMachine(sessionId1, null);
289 StateMachine stateMachine2 = new StateMachine(sessionId2, null);
290
291 assertEquals(stateMachine1,
292 StateMachine.lookupStateMachineBySessionId(sessionId1));
293 assertEquals(stateMachine2,
294 StateMachine.lookupStateMachineBySessionId(sessionId2));
295 assertNull(StateMachine.lookupStateMachineBySessionId(sessionId3));
296 }
297
298 @Test
299 public void testIdentifierLookups() throws StateMachineException {
300 String sessionId1 = "session1";
301 String sessionId2 = "session2";
302
303 StateMachine machine1ShouldBeNull =
304 StateMachine.lookupStateMachineById((byte) 1);
305 assertNull(machine1ShouldBeNull);
306 StateMachine machine2ShouldBeNull =
307 StateMachine.lookupStateMachineById((byte) 2);
308 assertNull(machine2ShouldBeNull);
309
310 StateMachine stateMachine1 = new StateMachine(sessionId1, null);
311 stateMachine1.start();
312 StateMachine stateMachine2 = new StateMachine(sessionId2, null);
313 stateMachine2.start();
314
315 assertEquals(stateMachine1,
316 StateMachine.lookupStateMachineById(stateMachine1.identifier()));
317 assertEquals(stateMachine2,
318 StateMachine.lookupStateMachineById(stateMachine2.identifier()));
Ari Saha79d7c252015-06-26 10:31:48 -0700319 }
320}