Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame^] | 1 | /* |
| 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 | */ |
| 18 | package org.onosproject.aaa; |
| 19 | |
| 20 | import org.junit.After; |
| 21 | import org.junit.Assert; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | |
| 25 | |
| 26 | public class StateMachineTest { |
| 27 | StateMachine stateMachine = null; |
| 28 | |
| 29 | @Before |
| 30 | public void setUp() { |
| 31 | System.out.println("Set Up."); |
| 32 | StateMachine.bitSet.clear(); |
| 33 | stateMachine = new StateMachine("session0", null); |
| 34 | } |
| 35 | |
| 36 | @After |
| 37 | public void tearDown() { |
| 38 | System.out.println("Tear Down."); |
| 39 | StateMachine.bitSet.clear(); |
| 40 | stateMachine = null; |
| 41 | } |
| 42 | |
| 43 | @Test |
| 44 | /** |
| 45 | * Test all the basic inputs from state to state: IDLE -> STARTED -> PENDING -> AUTHORIZED -> IDLE |
| 46 | */ |
| 47 | public void basic() throws StateMachineException { |
| 48 | System.out.println("======= BASIC =======."); |
| 49 | Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_IDLE); |
| 50 | |
| 51 | stateMachine.start(); |
| 52 | Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_STARTED); |
| 53 | |
| 54 | stateMachine.requestAccess(); |
| 55 | Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_PENDING); |
| 56 | |
| 57 | stateMachine.authorizeAccess(); |
| 58 | Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_AUTHORIZED); |
| 59 | |
| 60 | stateMachine.logoff(); |
| 61 | Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_IDLE); |
| 62 | } |
| 63 | |
| 64 | @Test |
| 65 | /** |
| 66 | * Test all inputs from an IDLE state (starting with the ones that are not impacting the current state) |
| 67 | */ |
| 68 | public void testIdleState() throws StateMachineException { |
| 69 | System.out.println("======= IDLE STATE TEST =======."); |
| 70 | stateMachine.requestAccess(); |
| 71 | Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_IDLE); |
| 72 | |
| 73 | stateMachine.authorizeAccess(); |
| 74 | Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_IDLE); |
| 75 | |
| 76 | stateMachine.denyAccess(); |
| 77 | Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_IDLE); |
| 78 | |
| 79 | stateMachine.logoff(); |
| 80 | Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_IDLE); |
| 81 | |
| 82 | stateMachine.start(); |
| 83 | Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_STARTED); |
| 84 | } |
| 85 | |
| 86 | @Test |
| 87 | /** |
| 88 | * Test all inputs from an STARTED state (starting with the ones that are not impacting the current state) |
| 89 | */ |
| 90 | public void testStartedState() throws StateMachineException { |
| 91 | System.out.println("======= STARTED STATE TEST =======."); |
| 92 | stateMachine.start(); |
| 93 | |
| 94 | stateMachine.authorizeAccess(); |
| 95 | Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_STARTED); |
| 96 | |
| 97 | stateMachine.denyAccess(); |
| 98 | Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_STARTED); |
| 99 | |
| 100 | stateMachine.logoff(); |
| 101 | Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_STARTED); |
| 102 | |
| 103 | stateMachine.start(); |
| 104 | Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_STARTED); |
| 105 | |
| 106 | stateMachine.requestAccess(); |
| 107 | Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_PENDING); |
| 108 | } |
| 109 | |
| 110 | @Test |
| 111 | /** |
| 112 | * Test all inputs from a PENDING state (starting with the ones that are not impacting the current state). |
| 113 | * The next valid state for this test is AUTHORIZED |
| 114 | */ |
| 115 | public void testPendingStateToAuthorized() throws StateMachineException { |
| 116 | System.out.println("======= PENDING STATE TEST (AUTHORIZED) =======."); |
| 117 | stateMachine.start(); |
| 118 | stateMachine.requestAccess(); |
| 119 | |
| 120 | stateMachine.logoff(); |
| 121 | Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_PENDING); |
| 122 | |
| 123 | stateMachine.start(); |
| 124 | Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_PENDING); |
| 125 | |
| 126 | stateMachine.requestAccess(); |
| 127 | Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_PENDING); |
| 128 | |
| 129 | stateMachine.authorizeAccess(); |
| 130 | Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_AUTHORIZED); |
| 131 | |
| 132 | stateMachine.denyAccess(); |
| 133 | Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_AUTHORIZED); |
| 134 | } |
| 135 | |
| 136 | @Test |
| 137 | /** |
| 138 | * Test all inputs from an PENDING state (starting with the ones that are not impacting the current state). |
| 139 | * The next valid state for this test is UNAUTHORIZED |
| 140 | */ |
| 141 | public void testPendingStateToUnauthorized() throws StateMachineException { |
| 142 | System.out.println("======= PENDING STATE TEST (DENIED) =======."); |
| 143 | stateMachine.start(); |
| 144 | stateMachine.requestAccess(); |
| 145 | |
| 146 | stateMachine.logoff(); |
| 147 | Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_PENDING); |
| 148 | |
| 149 | stateMachine.start(); |
| 150 | Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_PENDING); |
| 151 | |
| 152 | stateMachine.requestAccess(); |
| 153 | Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_PENDING); |
| 154 | |
| 155 | stateMachine.denyAccess(); |
| 156 | Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_UNAUTHORIZED); |
| 157 | |
| 158 | stateMachine.authorizeAccess(); |
| 159 | Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_UNAUTHORIZED); |
| 160 | } |
| 161 | |
| 162 | @Test |
| 163 | /** |
| 164 | * Test all inputs from an AUTHORIZED state (starting with the ones that are not impacting the current state). |
| 165 | */ |
| 166 | public void testAuthorizedState() throws StateMachineException { |
| 167 | System.out.println("======= AUTHORIZED STATE TEST =======."); |
| 168 | stateMachine.start(); |
| 169 | stateMachine.requestAccess(); |
| 170 | stateMachine.authorizeAccess(); |
| 171 | |
| 172 | stateMachine.start(); |
| 173 | Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_AUTHORIZED); |
| 174 | |
| 175 | stateMachine.requestAccess(); |
| 176 | Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_AUTHORIZED); |
| 177 | |
| 178 | stateMachine.authorizeAccess(); |
| 179 | Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_AUTHORIZED); |
| 180 | |
| 181 | stateMachine.denyAccess(); |
| 182 | Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_AUTHORIZED); |
| 183 | |
| 184 | stateMachine.logoff(); |
| 185 | Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_IDLE); |
| 186 | } |
| 187 | |
| 188 | @Test |
| 189 | /** |
| 190 | * Test all inputs from an UNAUTHORIZED state (starting with the ones that are not impacting the current state). |
| 191 | */ |
| 192 | public void testUnauthorizedState() throws StateMachineException { |
| 193 | System.out.println("======= UNAUTHORIZED STATE TEST =======."); |
| 194 | stateMachine.start(); |
| 195 | stateMachine.requestAccess(); |
| 196 | stateMachine.denyAccess(); |
| 197 | |
| 198 | stateMachine.start(); |
| 199 | Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_UNAUTHORIZED); |
| 200 | |
| 201 | stateMachine.requestAccess(); |
| 202 | Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_UNAUTHORIZED); |
| 203 | |
| 204 | stateMachine.authorizeAccess(); |
| 205 | Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_UNAUTHORIZED); |
| 206 | |
| 207 | stateMachine.denyAccess(); |
| 208 | Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_UNAUTHORIZED); |
| 209 | |
| 210 | stateMachine.logoff(); |
| 211 | Assert.assertEquals(stateMachine.getState(), StateMachine.STATE_IDLE); |
| 212 | } |
| 213 | |
| 214 | |
| 215 | @Test |
| 216 | public void testIdentifierAvailability() throws StateMachineException { |
| 217 | System.out.println("======= IDENTIFIER TEST =======."); |
| 218 | byte identifier = stateMachine.getIdentifier(); |
| 219 | System.out.println("State: " + stateMachine.getState()); |
| 220 | System.out.println("Identifier: " + Byte.toUnsignedInt(identifier)); |
| 221 | Assert.assertEquals(-1, identifier); |
| 222 | stateMachine.start(); |
| 223 | |
| 224 | |
| 225 | StateMachine sm247 = null; |
| 226 | StateMachine sm3 = null; |
| 227 | |
| 228 | |
| 229 | //create 255 others state machines |
| 230 | for (int i = 1; i <= 255; i++) { |
| 231 | StateMachine sm = new StateMachine("session" + i, null); |
| 232 | sm.start(); |
| 233 | byte id = sm.getIdentifier(); |
| 234 | Assert.assertEquals(i, Byte.toUnsignedInt(id)); |
| 235 | if (i == 3) { |
| 236 | sm3 = sm; |
| 237 | System.out.println("SM3: " + sm3.toString()); |
| 238 | } |
| 239 | if (i == 247) { |
| 240 | sm247 = sm; |
| 241 | System.out.println("SM247: " + sm247.toString()); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | //simulate the state machine for a specific session and logoff so we can free up a spot for an identifier |
| 246 | //let's choose identifier 247 then we free up 3 |
| 247 | sm247.requestAccess(); |
| 248 | sm247.authorizeAccess(); |
| 249 | sm247.logoff(); |
| 250 | sm247 = null; |
| 251 | |
| 252 | sm3.requestAccess(); |
| 253 | sm3.authorizeAccess(); |
| 254 | sm3.logoff(); |
| 255 | sm3 = null; |
| 256 | |
| 257 | StateMachine otherSM3 = new StateMachine("session3b", null); |
| 258 | otherSM3.start(); |
| 259 | otherSM3.requestAccess(); |
| 260 | byte id3 = otherSM3.getIdentifier(); |
| 261 | Assert.assertEquals(3, Byte.toUnsignedInt(id3)); |
| 262 | |
| 263 | StateMachine otherSM247 = new StateMachine("session247b", null); |
| 264 | otherSM247.start(); |
| 265 | otherSM247.requestAccess(); |
| 266 | byte id247 = otherSM247.getIdentifier(); |
| 267 | Assert.assertEquals(247, Byte.toUnsignedInt(id247)); |
| 268 | |
| 269 | } |
| 270 | } |