Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 1 | /* |
Amit Ghosh | c9ac1e5 | 2017-07-28 12:31:18 +0100 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 3 | * |
| 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 | */ |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 17 | package org.opencord.aaa; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 18 | |
| 19 | import org.junit.After; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
David K. Bainbridge | 6201949 | 2017-07-28 17:02:10 -0700 | [diff] [blame] | 22 | import org.onlab.packet.MacAddress; |
| 23 | |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 24 | import static org.junit.Assert.assertEquals; |
David K. Bainbridge | 6201949 | 2017-07-28 17:02:10 -0700 | [diff] [blame] | 25 | import static org.junit.Assert.assertNotNull; |
Jonathan Hart | 932bedc | 2018-07-12 13:46:09 -0700 | [diff] [blame] | 26 | import static org.junit.Assert.assertNull; |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 27 | |
| 28 | public class StateMachineTest { |
| 29 | StateMachine stateMachine = null; |
| 30 | |
| 31 | @Before |
| 32 | public void setUp() { |
| 33 | System.out.println("Set Up."); |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 34 | StateMachine.initializeMaps(); |
Jonathan Hart | 932bedc | 2018-07-12 13:46:09 -0700 | [diff] [blame] | 35 | stateMachine = new StateMachine("session0"); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | @After |
| 39 | public void tearDown() { |
| 40 | System.out.println("Tear Down."); |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 41 | StateMachine.destroyMaps(); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 42 | stateMachine = null; |
| 43 | } |
| 44 | |
| 45 | @Test |
| 46 | /** |
| 47 | * Test all the basic inputs from state to state: IDLE -> STARTED -> PENDING -> AUTHORIZED -> IDLE |
| 48 | */ |
| 49 | public void basic() throws StateMachineException { |
| 50 | System.out.println("======= BASIC =======."); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 51 | assertEquals(stateMachine.state(), StateMachine.STATE_IDLE); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 52 | |
| 53 | stateMachine.start(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 54 | assertEquals(stateMachine.state(), StateMachine.STATE_STARTED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 55 | |
| 56 | stateMachine.requestAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 57 | assertEquals(stateMachine.state(), StateMachine.STATE_PENDING); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 58 | |
| 59 | stateMachine.authorizeAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 60 | assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 61 | |
| 62 | stateMachine.logoff(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 63 | assertEquals(stateMachine.state(), StateMachine.STATE_IDLE); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | @Test |
| 67 | /** |
| 68 | * Test all inputs from an IDLE state (starting with the ones that are not impacting the current state) |
| 69 | */ |
| 70 | public void testIdleState() throws StateMachineException { |
| 71 | System.out.println("======= IDLE STATE TEST =======."); |
| 72 | stateMachine.requestAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 73 | assertEquals(stateMachine.state(), StateMachine.STATE_IDLE); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 74 | |
| 75 | stateMachine.authorizeAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 76 | assertEquals(stateMachine.state(), StateMachine.STATE_IDLE); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 77 | |
| 78 | stateMachine.denyAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 79 | assertEquals(stateMachine.state(), StateMachine.STATE_IDLE); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 80 | |
| 81 | stateMachine.logoff(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 82 | assertEquals(stateMachine.state(), StateMachine.STATE_IDLE); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 83 | |
| 84 | stateMachine.start(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 85 | assertEquals(stateMachine.state(), StateMachine.STATE_STARTED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Test |
| 89 | /** |
| 90 | * Test all inputs from an STARTED state (starting with the ones that are not impacting the current state) |
| 91 | */ |
| 92 | public void testStartedState() throws StateMachineException { |
| 93 | System.out.println("======= STARTED STATE TEST =======."); |
| 94 | stateMachine.start(); |
| 95 | |
| 96 | stateMachine.authorizeAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 97 | assertEquals(stateMachine.state(), StateMachine.STATE_STARTED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 98 | |
| 99 | stateMachine.denyAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 100 | assertEquals(stateMachine.state(), StateMachine.STATE_STARTED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 101 | |
| 102 | stateMachine.logoff(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 103 | assertEquals(stateMachine.state(), StateMachine.STATE_STARTED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 104 | |
| 105 | stateMachine.start(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 106 | assertEquals(stateMachine.state(), StateMachine.STATE_STARTED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 107 | |
| 108 | stateMachine.requestAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 109 | assertEquals(stateMachine.state(), StateMachine.STATE_PENDING); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | @Test |
| 113 | /** |
| 114 | * Test all inputs from a PENDING state (starting with the ones that are not impacting the current state). |
| 115 | * The next valid state for this test is AUTHORIZED |
| 116 | */ |
| 117 | public void testPendingStateToAuthorized() throws StateMachineException { |
| 118 | System.out.println("======= PENDING STATE TEST (AUTHORIZED) =======."); |
| 119 | stateMachine.start(); |
| 120 | stateMachine.requestAccess(); |
| 121 | |
| 122 | stateMachine.logoff(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 123 | assertEquals(stateMachine.state(), StateMachine.STATE_PENDING); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 124 | |
| 125 | stateMachine.start(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 126 | assertEquals(stateMachine.state(), StateMachine.STATE_PENDING); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 127 | |
| 128 | stateMachine.requestAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 129 | assertEquals(stateMachine.state(), StateMachine.STATE_PENDING); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 130 | |
| 131 | stateMachine.authorizeAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 132 | assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 133 | |
| 134 | stateMachine.denyAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 135 | assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Test |
| 139 | /** |
| 140 | * Test all inputs from an PENDING state (starting with the ones that are not impacting the current state). |
| 141 | * The next valid state for this test is UNAUTHORIZED |
| 142 | */ |
| 143 | public void testPendingStateToUnauthorized() throws StateMachineException { |
| 144 | System.out.println("======= PENDING STATE TEST (DENIED) =======."); |
| 145 | stateMachine.start(); |
| 146 | stateMachine.requestAccess(); |
| 147 | |
| 148 | stateMachine.logoff(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 149 | assertEquals(stateMachine.state(), StateMachine.STATE_PENDING); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 150 | |
| 151 | stateMachine.start(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 152 | assertEquals(stateMachine.state(), StateMachine.STATE_PENDING); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 153 | |
| 154 | stateMachine.requestAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 155 | assertEquals(stateMachine.state(), StateMachine.STATE_PENDING); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 156 | |
| 157 | stateMachine.denyAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 158 | assertEquals(stateMachine.state(), StateMachine.STATE_UNAUTHORIZED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 159 | |
| 160 | stateMachine.authorizeAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 161 | assertEquals(stateMachine.state(), StateMachine.STATE_UNAUTHORIZED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | @Test |
| 165 | /** |
| 166 | * Test all inputs from an AUTHORIZED state (starting with the ones that are not impacting the current state). |
| 167 | */ |
| 168 | public void testAuthorizedState() throws StateMachineException { |
| 169 | System.out.println("======= AUTHORIZED STATE TEST =======."); |
| 170 | stateMachine.start(); |
| 171 | stateMachine.requestAccess(); |
| 172 | stateMachine.authorizeAccess(); |
| 173 | |
| 174 | stateMachine.start(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 175 | assertEquals(stateMachine.state(), StateMachine.STATE_STARTED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 176 | |
| 177 | stateMachine.requestAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 178 | assertEquals(stateMachine.state(), StateMachine.STATE_PENDING); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 179 | |
| 180 | stateMachine.authorizeAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 181 | assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 182 | |
| 183 | stateMachine.denyAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 184 | assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 185 | |
| 186 | stateMachine.logoff(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 187 | assertEquals(stateMachine.state(), StateMachine.STATE_IDLE); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | @Test |
| 191 | /** |
| 192 | * Test all inputs from an UNAUTHORIZED state (starting with the ones that are not impacting the current state). |
| 193 | */ |
| 194 | public void testUnauthorizedState() throws StateMachineException { |
| 195 | System.out.println("======= UNAUTHORIZED STATE TEST =======."); |
| 196 | stateMachine.start(); |
| 197 | stateMachine.requestAccess(); |
| 198 | stateMachine.denyAccess(); |
| 199 | |
| 200 | stateMachine.start(); |
ke han | 04e47f3 | 2016-10-28 14:15:43 +0800 | [diff] [blame] | 201 | assertEquals(stateMachine.state(), StateMachine.STATE_STARTED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 202 | |
| 203 | stateMachine.requestAccess(); |
ke han | 04e47f3 | 2016-10-28 14:15:43 +0800 | [diff] [blame] | 204 | assertEquals(stateMachine.state(), StateMachine.STATE_PENDING); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 205 | |
| 206 | stateMachine.authorizeAccess(); |
ke han | 04e47f3 | 2016-10-28 14:15:43 +0800 | [diff] [blame] | 207 | assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 208 | |
| 209 | stateMachine.denyAccess(); |
ke han | 04e47f3 | 2016-10-28 14:15:43 +0800 | [diff] [blame] | 210 | assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 211 | |
| 212 | stateMachine.logoff(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 213 | assertEquals(stateMachine.state(), StateMachine.STATE_IDLE); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 216 | @Test |
| 217 | public void testSessionIdLookups() { |
| 218 | String sessionId1 = "session1"; |
| 219 | String sessionId2 = "session2"; |
| 220 | String sessionId3 = "session3"; |
| 221 | |
| 222 | StateMachine machine1ShouldBeNull = |
| 223 | StateMachine.lookupStateMachineBySessionId(sessionId1); |
| 224 | assertNull(machine1ShouldBeNull); |
| 225 | StateMachine machine2ShouldBeNull = |
| 226 | StateMachine.lookupStateMachineBySessionId(sessionId2); |
| 227 | assertNull(machine2ShouldBeNull); |
| 228 | |
Jonathan Hart | 932bedc | 2018-07-12 13:46:09 -0700 | [diff] [blame] | 229 | StateMachine stateMachine1 = new StateMachine(sessionId1); |
| 230 | StateMachine stateMachine2 = new StateMachine(sessionId2); |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 231 | |
| 232 | assertEquals(stateMachine1, |
| 233 | StateMachine.lookupStateMachineBySessionId(sessionId1)); |
| 234 | assertEquals(stateMachine2, |
| 235 | StateMachine.lookupStateMachineBySessionId(sessionId2)); |
| 236 | assertNull(StateMachine.lookupStateMachineBySessionId(sessionId3)); |
| 237 | } |
| 238 | |
| 239 | @Test |
| 240 | public void testIdentifierLookups() throws StateMachineException { |
| 241 | String sessionId1 = "session1"; |
| 242 | String sessionId2 = "session2"; |
| 243 | |
| 244 | StateMachine machine1ShouldBeNull = |
| 245 | StateMachine.lookupStateMachineById((byte) 1); |
| 246 | assertNull(machine1ShouldBeNull); |
| 247 | StateMachine machine2ShouldBeNull = |
| 248 | StateMachine.lookupStateMachineById((byte) 2); |
| 249 | assertNull(machine2ShouldBeNull); |
| 250 | |
Jonathan Hart | 932bedc | 2018-07-12 13:46:09 -0700 | [diff] [blame] | 251 | StateMachine stateMachine1 = new StateMachine(sessionId1); |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 252 | stateMachine1.start(); |
Jonathan Hart | 932bedc | 2018-07-12 13:46:09 -0700 | [diff] [blame] | 253 | StateMachine stateMachine2 = new StateMachine(sessionId2); |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 254 | stateMachine2.start(); |
| 255 | |
| 256 | assertEquals(stateMachine1, |
| 257 | StateMachine.lookupStateMachineById(stateMachine1.identifier())); |
| 258 | assertEquals(stateMachine2, |
| 259 | StateMachine.lookupStateMachineById(stateMachine2.identifier())); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 260 | } |
David K. Bainbridge | 6201949 | 2017-07-28 17:02:10 -0700 | [diff] [blame] | 261 | |
| 262 | @Test |
| 263 | /** |
| 264 | * Test state machine deletes |
| 265 | */ |
| 266 | public void testStateMachineReset() throws StateMachineException { |
| 267 | |
| 268 | int count = 256; |
| 269 | |
| 270 | //StateMachine.initializeMaps(); |
| 271 | StateMachine.lookupStateMachineById((byte) 1); |
| 272 | |
| 273 | // Instantiate a bunch of state machines |
| 274 | for (int i = 0; i < count; i += 1) { |
| 275 | String mac = String.format("00:00:00:00:00:%02x", i); |
Jonathan Hart | 932bedc | 2018-07-12 13:46:09 -0700 | [diff] [blame] | 276 | StateMachine sm = new StateMachine(mac); |
David K. Bainbridge | 6201949 | 2017-07-28 17:02:10 -0700 | [diff] [blame] | 277 | sm.start(); |
| 278 | sm.setSupplicantAddress(MacAddress.valueOf(mac)); |
| 279 | } |
| 280 | |
| 281 | // Verify all state machines with a "even" MAC exist |
| 282 | for (int i = 0; i < count; i += 2) { |
| 283 | String mac = String.format("00:00:00:00:00:%02x", i); |
| 284 | assertNotNull(StateMachine.lookupStateMachineBySessionId(mac)); |
| 285 | } |
| 286 | |
| 287 | // Delete all state machines with a "even" MAC |
| 288 | for (int i = 0; i < count; i += 2) { |
| 289 | String mac = String.format("00:00:00:00:00:%02x", i); |
| 290 | StateMachine.deleteByMac(MacAddress.valueOf(mac)); |
| 291 | } |
| 292 | |
| 293 | // Verify all the delete state machines no long exist |
| 294 | for (int i = 0; i < count; i += 2) { |
| 295 | String mac = String.format("00:00:00:00:00:%02x", i); |
| 296 | assertNull(StateMachine.lookupStateMachineBySessionId(mac)); |
| 297 | } |
| 298 | } |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 299 | } |