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 | 5db4453 | 2018-07-12 18:13:54 -0700 | [diff] [blame^] | 35 | StateMachine.setDelegate(e -> { }); |
Jonathan Hart | 932bedc | 2018-07-12 13:46:09 -0700 | [diff] [blame] | 36 | stateMachine = new StateMachine("session0"); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | @After |
| 40 | public void tearDown() { |
| 41 | System.out.println("Tear Down."); |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 42 | StateMachine.destroyMaps(); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 43 | 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 =======."); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 52 | assertEquals(stateMachine.state(), StateMachine.STATE_IDLE); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 53 | |
| 54 | stateMachine.start(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 55 | assertEquals(stateMachine.state(), StateMachine.STATE_STARTED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 56 | |
| 57 | stateMachine.requestAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 58 | assertEquals(stateMachine.state(), StateMachine.STATE_PENDING); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 59 | |
| 60 | stateMachine.authorizeAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 61 | assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 62 | |
| 63 | stateMachine.logoff(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 64 | assertEquals(stateMachine.state(), StateMachine.STATE_IDLE); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 65 | } |
| 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(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 74 | assertEquals(stateMachine.state(), StateMachine.STATE_IDLE); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 75 | |
| 76 | stateMachine.authorizeAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 77 | assertEquals(stateMachine.state(), StateMachine.STATE_IDLE); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 78 | |
| 79 | stateMachine.denyAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 80 | assertEquals(stateMachine.state(), StateMachine.STATE_IDLE); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 81 | |
| 82 | stateMachine.logoff(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 83 | assertEquals(stateMachine.state(), StateMachine.STATE_IDLE); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 84 | |
| 85 | stateMachine.start(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 86 | assertEquals(stateMachine.state(), StateMachine.STATE_STARTED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 87 | } |
| 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(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 98 | assertEquals(stateMachine.state(), StateMachine.STATE_STARTED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 99 | |
| 100 | stateMachine.denyAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 101 | assertEquals(stateMachine.state(), StateMachine.STATE_STARTED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 102 | |
| 103 | stateMachine.logoff(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 104 | assertEquals(stateMachine.state(), StateMachine.STATE_STARTED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 105 | |
| 106 | stateMachine.start(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 107 | assertEquals(stateMachine.state(), StateMachine.STATE_STARTED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 108 | |
| 109 | stateMachine.requestAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 110 | assertEquals(stateMachine.state(), StateMachine.STATE_PENDING); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 111 | } |
| 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(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 124 | assertEquals(stateMachine.state(), StateMachine.STATE_PENDING); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 125 | |
| 126 | stateMachine.start(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 127 | assertEquals(stateMachine.state(), StateMachine.STATE_PENDING); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 128 | |
| 129 | stateMachine.requestAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 130 | assertEquals(stateMachine.state(), StateMachine.STATE_PENDING); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 131 | |
| 132 | stateMachine.authorizeAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 133 | assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 134 | |
| 135 | stateMachine.denyAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 136 | assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 137 | } |
| 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(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 150 | assertEquals(stateMachine.state(), StateMachine.STATE_PENDING); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 151 | |
| 152 | stateMachine.start(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 153 | assertEquals(stateMachine.state(), StateMachine.STATE_PENDING); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 154 | |
| 155 | stateMachine.requestAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 156 | assertEquals(stateMachine.state(), StateMachine.STATE_PENDING); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 157 | |
| 158 | stateMachine.denyAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 159 | assertEquals(stateMachine.state(), StateMachine.STATE_UNAUTHORIZED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 160 | |
| 161 | stateMachine.authorizeAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 162 | assertEquals(stateMachine.state(), StateMachine.STATE_UNAUTHORIZED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 163 | } |
| 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(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 176 | assertEquals(stateMachine.state(), StateMachine.STATE_STARTED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 177 | |
| 178 | stateMachine.requestAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 179 | assertEquals(stateMachine.state(), StateMachine.STATE_PENDING); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 180 | |
| 181 | stateMachine.authorizeAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 182 | assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 183 | |
| 184 | stateMachine.denyAccess(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 185 | assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 186 | |
| 187 | stateMachine.logoff(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 188 | assertEquals(stateMachine.state(), StateMachine.STATE_IDLE); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 189 | } |
| 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 han | 04e47f3 | 2016-10-28 14:15:43 +0800 | [diff] [blame] | 202 | assertEquals(stateMachine.state(), StateMachine.STATE_STARTED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 203 | |
| 204 | stateMachine.requestAccess(); |
ke han | 04e47f3 | 2016-10-28 14:15:43 +0800 | [diff] [blame] | 205 | assertEquals(stateMachine.state(), StateMachine.STATE_PENDING); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 206 | |
| 207 | stateMachine.authorizeAccess(); |
ke han | 04e47f3 | 2016-10-28 14:15:43 +0800 | [diff] [blame] | 208 | assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 209 | |
| 210 | stateMachine.denyAccess(); |
ke han | 04e47f3 | 2016-10-28 14:15:43 +0800 | [diff] [blame] | 211 | assertEquals(stateMachine.state(), StateMachine.STATE_AUTHORIZED); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 212 | |
| 213 | stateMachine.logoff(); |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 214 | assertEquals(stateMachine.state(), StateMachine.STATE_IDLE); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 217 | @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 Hart | 932bedc | 2018-07-12 13:46:09 -0700 | [diff] [blame] | 230 | StateMachine stateMachine1 = new StateMachine(sessionId1); |
| 231 | StateMachine stateMachine2 = new StateMachine(sessionId2); |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 232 | |
| 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 Hart | 932bedc | 2018-07-12 13:46:09 -0700 | [diff] [blame] | 252 | StateMachine stateMachine1 = new StateMachine(sessionId1); |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 253 | stateMachine1.start(); |
Jonathan Hart | 932bedc | 2018-07-12 13:46:09 -0700 | [diff] [blame] | 254 | StateMachine stateMachine2 = new StateMachine(sessionId2); |
Ray Milkey | f61a24e | 2015-09-24 16:34:02 -0700 | [diff] [blame] | 255 | stateMachine2.start(); |
| 256 | |
| 257 | assertEquals(stateMachine1, |
| 258 | StateMachine.lookupStateMachineById(stateMachine1.identifier())); |
| 259 | assertEquals(stateMachine2, |
| 260 | StateMachine.lookupStateMachineById(stateMachine2.identifier())); |
Ari Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 261 | } |
David K. Bainbridge | 6201949 | 2017-07-28 17:02:10 -0700 | [diff] [blame] | 262 | |
| 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 Hart | 932bedc | 2018-07-12 13:46:09 -0700 | [diff] [blame] | 277 | StateMachine sm = new StateMachine(mac); |
David K. Bainbridge | 6201949 | 2017-07-28 17:02:10 -0700 | [diff] [blame] | 278 | 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 Saha | 8983174 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 300 | } |