blob: 04837e8532cec983b8b2ab313c4c0d72423bb486 [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 Milkey75879ef2015-09-24 16:34:02 -070024import static org.junit.Assert.*;
Ari Saha79d7c252015-06-26 10:31:48 -070025
26
27public class StateMachineTest {
28 StateMachine stateMachine = null;
29
30 @Before
31 public void setUp() {
32 System.out.println("Set Up.");
33 StateMachine.bitSet.clear();
Ray Milkey75879ef2015-09-24 16:34:02 -070034 StateMachine.initializeMaps();
Ari Saha79d7c252015-06-26 10:31:48 -070035 stateMachine = new StateMachine("session0", null);
36 }
37
38 @After
39 public void tearDown() {
40 System.out.println("Tear Down.");
41 StateMachine.bitSet.clear();
Ray Milkey75879ef2015-09-24 16:34:02 -070042 StateMachine.destroyMaps();
Ari Saha79d7c252015-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 =======.");
Ray Milkey75879ef2015-09-24 16:34:02 -070052 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha79d7c252015-06-26 10:31:48 -070053
54 stateMachine.start();
Ray Milkey75879ef2015-09-24 16:34:02 -070055 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_STARTED);
Ari Saha79d7c252015-06-26 10:31:48 -070056
57 stateMachine.requestAccess();
Ray Milkey75879ef2015-09-24 16:34:02 -070058 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha79d7c252015-06-26 10:31:48 -070059
60 stateMachine.authorizeAccess();
Ray Milkey75879ef2015-09-24 16:34:02 -070061 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED);
Ari Saha79d7c252015-06-26 10:31:48 -070062
63 stateMachine.logoff();
Ray Milkey75879ef2015-09-24 16:34:02 -070064 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha79d7c252015-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();
Ray Milkey75879ef2015-09-24 16:34:02 -070074 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha79d7c252015-06-26 10:31:48 -070075
76 stateMachine.authorizeAccess();
Ray Milkey75879ef2015-09-24 16:34:02 -070077 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha79d7c252015-06-26 10:31:48 -070078
79 stateMachine.denyAccess();
Ray Milkey75879ef2015-09-24 16:34:02 -070080 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha79d7c252015-06-26 10:31:48 -070081
82 stateMachine.logoff();
Ray Milkey75879ef2015-09-24 16:34:02 -070083 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha79d7c252015-06-26 10:31:48 -070084
85 stateMachine.start();
Ray Milkey75879ef2015-09-24 16:34:02 -070086 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_STARTED);
Ari Saha79d7c252015-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();
Ray Milkey75879ef2015-09-24 16:34:02 -070098 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_STARTED);
Ari Saha79d7c252015-06-26 10:31:48 -070099
100 stateMachine.denyAccess();
Ray Milkey75879ef2015-09-24 16:34:02 -0700101 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_STARTED);
Ari Saha79d7c252015-06-26 10:31:48 -0700102
103 stateMachine.logoff();
Ray Milkey75879ef2015-09-24 16:34:02 -0700104 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_STARTED);
Ari Saha79d7c252015-06-26 10:31:48 -0700105
106 stateMachine.start();
Ray Milkey75879ef2015-09-24 16:34:02 -0700107 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_STARTED);
Ari Saha79d7c252015-06-26 10:31:48 -0700108
109 stateMachine.requestAccess();
Ray Milkey75879ef2015-09-24 16:34:02 -0700110 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha79d7c252015-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();
Ray Milkey75879ef2015-09-24 16:34:02 -0700124 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha79d7c252015-06-26 10:31:48 -0700125
126 stateMachine.start();
Ray Milkey75879ef2015-09-24 16:34:02 -0700127 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha79d7c252015-06-26 10:31:48 -0700128
129 stateMachine.requestAccess();
Ray Milkey75879ef2015-09-24 16:34:02 -0700130 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha79d7c252015-06-26 10:31:48 -0700131
132 stateMachine.authorizeAccess();
Ray Milkey75879ef2015-09-24 16:34:02 -0700133 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED);
Ari Saha79d7c252015-06-26 10:31:48 -0700134
135 stateMachine.denyAccess();
Ray Milkey75879ef2015-09-24 16:34:02 -0700136 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED);
Ari Saha79d7c252015-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();
Ray Milkey75879ef2015-09-24 16:34:02 -0700150 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha79d7c252015-06-26 10:31:48 -0700151
152 stateMachine.start();
Ray Milkey75879ef2015-09-24 16:34:02 -0700153 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha79d7c252015-06-26 10:31:48 -0700154
155 stateMachine.requestAccess();
Ray Milkey75879ef2015-09-24 16:34:02 -0700156 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_PENDING);
Ari Saha79d7c252015-06-26 10:31:48 -0700157
158 stateMachine.denyAccess();
Ray Milkey75879ef2015-09-24 16:34:02 -0700159 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_UNAUTHORIZED);
Ari Saha79d7c252015-06-26 10:31:48 -0700160
161 stateMachine.authorizeAccess();
Ray Milkey75879ef2015-09-24 16:34:02 -0700162 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_UNAUTHORIZED);
Ari Saha79d7c252015-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();
Ray Milkey75879ef2015-09-24 16:34:02 -0700176 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED);
Ari Saha79d7c252015-06-26 10:31:48 -0700177
178 stateMachine.requestAccess();
Ray Milkey75879ef2015-09-24 16:34:02 -0700179 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED);
Ari Saha79d7c252015-06-26 10:31:48 -0700180
181 stateMachine.authorizeAccess();
Ray Milkey75879ef2015-09-24 16:34:02 -0700182 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED);
Ari Saha79d7c252015-06-26 10:31:48 -0700183
184 stateMachine.denyAccess();
Ray Milkey75879ef2015-09-24 16:34:02 -0700185 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED);
Ari Saha79d7c252015-06-26 10:31:48 -0700186
187 stateMachine.logoff();
Ray Milkey75879ef2015-09-24 16:34:02 -0700188 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha79d7c252015-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();
Ray Milkey75879ef2015-09-24 16:34:02 -0700202 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_UNAUTHORIZED);
Ari Saha79d7c252015-06-26 10:31:48 -0700203
204 stateMachine.requestAccess();
Ray Milkey75879ef2015-09-24 16:34:02 -0700205 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_UNAUTHORIZED);
Ari Saha79d7c252015-06-26 10:31:48 -0700206
207 stateMachine.authorizeAccess();
Ray Milkey75879ef2015-09-24 16:34:02 -0700208 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_UNAUTHORIZED);
Ari Saha79d7c252015-06-26 10:31:48 -0700209
210 stateMachine.denyAccess();
Ray Milkey75879ef2015-09-24 16:34:02 -0700211 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_UNAUTHORIZED);
Ari Saha79d7c252015-06-26 10:31:48 -0700212
213 stateMachine.logoff();
Ray Milkey75879ef2015-09-24 16:34:02 -0700214 Assert.assertEquals(stateMachine.state(), StateMachine.STATE_IDLE);
Ari Saha79d7c252015-06-26 10:31:48 -0700215 }
216
217
218 @Test
219 public void testIdentifierAvailability() throws StateMachineException {
220 System.out.println("======= IDENTIFIER TEST =======.");
Ray Milkey75879ef2015-09-24 16:34:02 -0700221 byte identifier = stateMachine.identifier();
222 System.out.println("State: " + stateMachine.state());
Ari Saha79d7c252015-06-26 10:31:48 -0700223 System.out.println("Identifier: " + Byte.toUnsignedInt(identifier));
224 Assert.assertEquals(-1, identifier);
225 stateMachine.start();
226
227
228 StateMachine sm247 = null;
229 StateMachine sm3 = null;
230
231
232 //create 255 others state machines
233 for (int i = 1; i <= 255; i++) {
234 StateMachine sm = new StateMachine("session" + i, null);
235 sm.start();
Ray Milkey75879ef2015-09-24 16:34:02 -0700236 byte id = sm.identifier();
Ari Saha79d7c252015-06-26 10:31:48 -0700237 Assert.assertEquals(i, Byte.toUnsignedInt(id));
238 if (i == 3) {
239 sm3 = sm;
240 System.out.println("SM3: " + sm3.toString());
241 }
242 if (i == 247) {
243 sm247 = sm;
244 System.out.println("SM247: " + sm247.toString());
245 }
246 }
247
248 //simulate the state machine for a specific session and logoff so we can free up a spot for an identifier
249 //let's choose identifier 247 then we free up 3
Ray Milkey75879ef2015-09-24 16:34:02 -0700250 Assert.assertNotNull(sm247);
Ari Saha79d7c252015-06-26 10:31:48 -0700251 sm247.requestAccess();
252 sm247.authorizeAccess();
253 sm247.logoff();
Ari Saha79d7c252015-06-26 10:31:48 -0700254
Ray Milkey75879ef2015-09-24 16:34:02 -0700255 Assert.assertNotNull(sm3);
Ari Saha79d7c252015-06-26 10:31:48 -0700256 sm3.requestAccess();
257 sm3.authorizeAccess();
258 sm3.logoff();
Ari Saha79d7c252015-06-26 10:31:48 -0700259
260 StateMachine otherSM3 = new StateMachine("session3b", null);
261 otherSM3.start();
262 otherSM3.requestAccess();
Ray Milkey75879ef2015-09-24 16:34:02 -0700263 byte id3 = otherSM3.identifier();
Ari Saha79d7c252015-06-26 10:31:48 -0700264 Assert.assertEquals(3, Byte.toUnsignedInt(id3));
265
266 StateMachine otherSM247 = new StateMachine("session247b", null);
267 otherSM247.start();
268 otherSM247.requestAccess();
Ray Milkey75879ef2015-09-24 16:34:02 -0700269 byte id247 = otherSM247.identifier();
Ari Saha79d7c252015-06-26 10:31:48 -0700270 Assert.assertEquals(247, Byte.toUnsignedInt(id247));
Ray Milkey75879ef2015-09-24 16:34:02 -0700271 }
Ari Saha79d7c252015-06-26 10:31:48 -0700272
Ray Milkey75879ef2015-09-24 16:34:02 -0700273 @Test
274 public void testSessionIdLookups() {
275 String sessionId1 = "session1";
276 String sessionId2 = "session2";
277 String sessionId3 = "session3";
278
279 StateMachine machine1ShouldBeNull =
280 StateMachine.lookupStateMachineBySessionId(sessionId1);
281 assertNull(machine1ShouldBeNull);
282 StateMachine machine2ShouldBeNull =
283 StateMachine.lookupStateMachineBySessionId(sessionId2);
284 assertNull(machine2ShouldBeNull);
285
286 StateMachine stateMachine1 = new StateMachine(sessionId1, null);
287 StateMachine stateMachine2 = new StateMachine(sessionId2, null);
288
289 assertEquals(stateMachine1,
290 StateMachine.lookupStateMachineBySessionId(sessionId1));
291 assertEquals(stateMachine2,
292 StateMachine.lookupStateMachineBySessionId(sessionId2));
293 assertNull(StateMachine.lookupStateMachineBySessionId(sessionId3));
294 }
295
296 @Test
297 public void testIdentifierLookups() throws StateMachineException {
298 String sessionId1 = "session1";
299 String sessionId2 = "session2";
300
301 StateMachine machine1ShouldBeNull =
302 StateMachine.lookupStateMachineById((byte) 1);
303 assertNull(machine1ShouldBeNull);
304 StateMachine machine2ShouldBeNull =
305 StateMachine.lookupStateMachineById((byte) 2);
306 assertNull(machine2ShouldBeNull);
307
308 StateMachine stateMachine1 = new StateMachine(sessionId1, null);
309 stateMachine1.start();
310 StateMachine stateMachine2 = new StateMachine(sessionId2, null);
311 stateMachine2.start();
312
313 assertEquals(stateMachine1,
314 StateMachine.lookupStateMachineById(stateMachine1.identifier()));
315 assertEquals(stateMachine2,
316 StateMachine.lookupStateMachineById(stateMachine2.identifier()));
Ari Saha79d7c252015-06-26 10:31:48 -0700317 }
318}