Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 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 | package org.onosproject.aaa; |
| 17 | |
Jonathan Hart | b92cc51 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 18 | import com.google.common.base.Charsets; |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 19 | import org.junit.After; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
Ray Milkey | 4ed9369 | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 22 | import org.onlab.packet.BasePacket; |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 23 | import org.onlab.packet.DeserializationException; |
| 24 | import org.onlab.packet.EAP; |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 25 | import org.onlab.packet.Ethernet; |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 26 | import org.onlab.packet.IpAddress; |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 27 | import org.onlab.packet.RADIUS; |
| 28 | import org.onlab.packet.RADIUSAttribute; |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 29 | import org.onosproject.core.CoreServiceAdapter; |
Ray Milkey | 57f2e14 | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 30 | import org.onosproject.net.config.Config; |
| 31 | import org.onosproject.net.config.NetworkConfigRegistryAdapter; |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 32 | |
Jonathan Hart | b92cc51 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 33 | import java.net.InetAddress; |
| 34 | import java.net.UnknownHostException; |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 35 | |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 36 | import static org.hamcrest.Matchers.is; |
| 37 | import static org.hamcrest.Matchers.notNullValue; |
| 38 | import static org.junit.Assert.assertThat; |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Set of tests of the ONOS application component. |
| 42 | */ |
Jonathan Hart | b92cc51 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 43 | public class AaaManagerTest extends AaaTestBase { |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 44 | |
Ray Milkey | 4ed9369 | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 45 | static final String BAD_IP_ADDRESS = "198.51.100.0"; |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 46 | |
Jonathan Hart | b92cc51 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 47 | private AaaManager aaaManager; |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 48 | |
Jonathan Hart | b92cc51 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 49 | class AaaManagerWithoutRadiusServer extends AaaManager { |
| 50 | protected void sendRadiusPacket(RADIUS radiusPacket) { |
Ray Milkey | 4ed9369 | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 51 | savePacket(radiusPacket); |
| 52 | } |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /** |
Ray Milkey | 4ed9369 | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 56 | * Mocks the AAAConfig class to force usage of an unroutable address for the |
| 57 | * RADIUS server. |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 58 | */ |
Jonathan Hart | b92cc51 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 59 | static class MockAaaConfig extends AaaConfig { |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 60 | @Override |
Ray Milkey | 4ed9369 | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 61 | public InetAddress radiusIp() { |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 62 | try { |
Ray Milkey | 4ed9369 | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 63 | return InetAddress.getByName(BAD_IP_ADDRESS); |
| 64 | } catch (UnknownHostException ex) { |
| 65 | // can't happen |
| 66 | throw new IllegalStateException(ex); |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
Ray Milkey | 57f2e14 | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 72 | * Mocks the network config registry. |
| 73 | */ |
| 74 | @SuppressWarnings("unchecked") |
| 75 | private static final class TestNetworkConfigRegistry |
| 76 | extends NetworkConfigRegistryAdapter { |
| 77 | @Override |
| 78 | public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) { |
Jonathan Hart | b92cc51 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 79 | AaaConfig aaaConfig = new MockAaaConfig(); |
Ray Milkey | 4ed9369 | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 80 | return (C) aaaConfig; |
Ray Milkey | 57f2e14 | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | /** |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 85 | * Constructs an Ethernet packet containing a RADIUS challenge |
| 86 | * packet. |
| 87 | * |
| 88 | * @param challengeCode code to use in challenge packet |
| 89 | * @param challengeType type to use in challenge packet |
| 90 | * @return Ethernet packet |
| 91 | */ |
Jonathan Hart | b92cc51 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 92 | private RADIUS constructRadiusCodeAccessChallengePacket(byte challengeCode, byte challengeType) { |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 93 | |
Ray Milkey | 4ed9369 | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 94 | String challenge = "12345678901234567"; |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 95 | |
| 96 | EAP eap = new EAP(challengeType, (byte) 1, challengeType, |
| 97 | challenge.getBytes(Charsets.US_ASCII)); |
| 98 | eap.setIdentifier((byte) 1); |
| 99 | |
| 100 | RADIUS radius = new RADIUS(); |
| 101 | radius.setCode(challengeCode); |
| 102 | |
| 103 | radius.setAttribute(RADIUSAttribute.RADIUS_ATTR_STATE, |
| 104 | challenge.getBytes(Charsets.US_ASCII)); |
| 105 | |
| 106 | radius.setPayload(eap); |
| 107 | radius.setAttribute(RADIUSAttribute.RADIUS_ATTR_EAP_MESSAGE, |
| 108 | eap.serialize()); |
| 109 | |
Ray Milkey | 4ed9369 | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 110 | return radius; |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Sets up the services required by the AAA application. |
| 115 | */ |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 116 | @Before |
| 117 | public void setUp() { |
Jonathan Hart | b92cc51 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 118 | aaaManager = new AaaManagerWithoutRadiusServer(); |
| 119 | aaaManager.netCfgService = new TestNetworkConfigRegistry(); |
| 120 | aaaManager.coreService = new CoreServiceAdapter(); |
| 121 | aaaManager.packetService = new MockPacketService(); |
| 122 | aaaManager.activate(); |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 125 | /** |
| 126 | * Tears down the AAA application. |
| 127 | */ |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 128 | @After |
| 129 | public void tearDown() { |
Jonathan Hart | b92cc51 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 130 | aaaManager.deactivate(); |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 133 | /** |
| 134 | * Extracts the RADIUS packet from a packet sent by the supplicant. |
| 135 | * |
Ray Milkey | 4ed9369 | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 136 | * @param radius RADIUS packet sent by the supplicant |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 137 | * @throws DeserializationException if deserialization of the packet contents |
| 138 | * fails. |
| 139 | */ |
Jonathan Hart | b92cc51 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 140 | private void checkRadiusPacketFromSupplicant(RADIUS radius) |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 141 | throws DeserializationException { |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 142 | assertThat(radius, notNullValue()); |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 143 | |
Ray Milkey | 4ed9369 | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 144 | EAP eap = radius.decapsulateMessage(); |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 145 | assertThat(eap, notNullValue()); |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Fetches the sent packet at the given index. The requested packet |
| 150 | * must be the last packet on the list. |
| 151 | * |
| 152 | * @param index index into sent packets array |
| 153 | * @return packet |
| 154 | */ |
Ray Milkey | 4ed9369 | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 155 | private BasePacket fetchPacket(int index) { |
| 156 | BasePacket packet = savedPackets.get(index); |
| 157 | assertThat(packet, notNullValue()); |
| 158 | return packet; |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Tests the authentication path through the AAA application. |
| 163 | * |
| 164 | * @throws DeserializationException if packed deserialization fails. |
| 165 | */ |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 166 | @Test |
Ray Milkey | 4ed9369 | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 167 | public void testAuthentication() throws Exception { |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 168 | |
| 169 | // (1) Supplicant start up |
| 170 | |
| 171 | Ethernet startPacket = constructSupplicantStartPacket(); |
| 172 | sendPacket(startPacket); |
| 173 | |
Ray Milkey | 4ed9369 | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 174 | Ethernet responsePacket = (Ethernet) fetchPacket(0); |
Jonathan Hart | b92cc51 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 175 | checkRadiusPacket(aaaManager, responsePacket, EAP.ATTR_IDENTITY); |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 176 | |
| 177 | // (2) Supplicant identify |
| 178 | |
Ray Milkey | 4ed9369 | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 179 | Ethernet identifyPacket = constructSupplicantIdentifyPacket(null, EAP.ATTR_IDENTITY, (byte) 1, null); |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 180 | sendPacket(identifyPacket); |
| 181 | |
Ray Milkey | 4ed9369 | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 182 | RADIUS radiusIdentifyPacket = (RADIUS) fetchPacket(1); |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 183 | |
Jonathan Hart | b92cc51 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 184 | checkRadiusPacketFromSupplicant(radiusIdentifyPacket); |
Ray Milkey | 4ed9369 | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 185 | |
| 186 | assertThat(radiusIdentifyPacket.getCode(), is(RADIUS.RADIUS_CODE_ACCESS_REQUEST)); |
| 187 | assertThat(new String(radiusIdentifyPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_USERNAME).getValue()), |
| 188 | is("testuser")); |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 189 | |
| 190 | IpAddress nasIp = |
| 191 | IpAddress.valueOf(IpAddress.Version.INET, |
Ray Milkey | 4ed9369 | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 192 | radiusIdentifyPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_IP) |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 193 | .getValue()); |
Jonathan Hart | b92cc51 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 194 | assertThat(nasIp.toString(), is(aaaManager.nasIpAddress.getHostAddress())); |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 195 | |
| 196 | // State machine should have been created by now |
| 197 | |
| 198 | StateMachine stateMachine = |
Ray Milkey | 4ed9369 | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 199 | StateMachine.lookupStateMachineBySessionId(SESSION_ID); |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 200 | assertThat(stateMachine, notNullValue()); |
| 201 | assertThat(stateMachine.state(), is(StateMachine.STATE_PENDING)); |
| 202 | |
| 203 | // (3) RADIUS MD5 challenge |
| 204 | |
Ray Milkey | 4ed9369 | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 205 | RADIUS radiusCodeAccessChallengePacket = |
Jonathan Hart | b92cc51 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 206 | constructRadiusCodeAccessChallengePacket(RADIUS.RADIUS_CODE_ACCESS_CHALLENGE, EAP.ATTR_MD5); |
| 207 | aaaManager.radiusListener.handleRadiusPacket(radiusCodeAccessChallengePacket); |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 208 | |
Ray Milkey | 4ed9369 | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 209 | Ethernet radiusChallengeMD5Packet = (Ethernet) fetchPacket(2); |
Jonathan Hart | b92cc51 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 210 | checkRadiusPacket(aaaManager, radiusChallengeMD5Packet, EAP.ATTR_MD5); |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 211 | |
| 212 | // (4) Supplicant MD5 response |
| 213 | |
Ray Milkey | 4ed9369 | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 214 | Ethernet md5RadiusPacket = |
| 215 | constructSupplicantIdentifyPacket(stateMachine, |
| 216 | EAP.ATTR_MD5, |
| 217 | stateMachine.challengeIdentifier(), |
| 218 | radiusChallengeMD5Packet); |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 219 | sendPacket(md5RadiusPacket); |
Ray Milkey | 4ed9369 | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 220 | |
| 221 | RADIUS responseMd5RadiusPacket = (RADIUS) fetchPacket(3); |
| 222 | |
Jonathan Hart | b92cc51 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 223 | checkRadiusPacketFromSupplicant(responseMd5RadiusPacket); |
Ray Milkey | 4ed9369 | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 224 | assertThat(responseMd5RadiusPacket.getIdentifier(), is((byte) 0)); |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 225 | assertThat(responseMd5RadiusPacket.getCode(), is(RADIUS.RADIUS_CODE_ACCESS_REQUEST)); |
| 226 | |
| 227 | // State machine should be in pending state |
| 228 | |
| 229 | assertThat(stateMachine, notNullValue()); |
| 230 | assertThat(stateMachine.state(), is(StateMachine.STATE_PENDING)); |
| 231 | |
Ray Milkey | 4ed9369 | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 232 | // (5) RADIUS Success |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 233 | |
Ray Milkey | 4ed9369 | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 234 | RADIUS successPacket = |
Jonathan Hart | b92cc51 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 235 | constructRadiusCodeAccessChallengePacket(RADIUS.RADIUS_CODE_ACCESS_ACCEPT, EAP.SUCCESS); |
| 236 | aaaManager.radiusListener.handleRadiusPacket((successPacket)); |
Ray Milkey | 4ed9369 | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 237 | Ethernet supplicantSuccessPacket = (Ethernet) fetchPacket(4); |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 238 | |
Jonathan Hart | b92cc51 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 239 | checkRadiusPacket(aaaManager, supplicantSuccessPacket, EAP.SUCCESS); |
Ray Milkey | 911c519 | 2015-09-30 10:56:43 -0700 | [diff] [blame] | 240 | |
| 241 | // State machine should be in authorized state |
| 242 | |
| 243 | assertThat(stateMachine, notNullValue()); |
| 244 | assertThat(stateMachine.state(), is(StateMachine.STATE_AUTHORIZED)); |
Ray Milkey | 4ed9369 | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 245 | |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 246 | } |
Ray Milkey | 57f2e14 | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 247 | |
Ray Milkey | 57f2e14 | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 248 | /** |
| 249 | * Tests the default configuration. |
| 250 | */ |
| 251 | @Test |
| 252 | public void testConfig() { |
Jonathan Hart | b92cc51 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 253 | assertThat(aaaManager.nasIpAddress.getHostAddress(), is(AaaConfig.DEFAULT_NAS_IP)); |
| 254 | assertThat(aaaManager.nasMacAddress, is(AaaConfig.DEFAULT_NAS_MAC)); |
| 255 | assertThat(aaaManager.radiusIpAddress.getHostAddress(), is(BAD_IP_ADDRESS)); |
| 256 | assertThat(aaaManager.radiusMacAddress, is(AaaConfig.DEFAULT_RADIUS_MAC)); |
Ray Milkey | 57f2e14 | 2015-10-01 16:48:18 -0700 | [diff] [blame] | 257 | } |
Ari Saha | 79d7c25 | 2015-06-26 10:31:48 -0700 | [diff] [blame] | 258 | } |