blob: 4309e31bdf1f6dc8e9dccb2705972c73831cd775 [file] [log] [blame]
Ari Saha89831742015-06-26 10:31:48 -07001/*
Brian O'Connor4e33be22017-08-03 22:45:46 -07002 * Copyright 2015-present Open Networking Foundation
Ari Saha89831742015-06-26 10:31:48 -07003 *
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 */
Matteo Scandolocf847b82019-04-26 15:00:00 -070016package org.opencord.aaa.impl;
Ari Saha89831742015-06-26 10:31:48 -070017
18import org.junit.After;
19import org.junit.Before;
20import org.junit.Test;
Jonathan Hart5db44532018-07-12 18:13:54 -070021import org.onlab.junit.TestUtils;
Ray Milkey967776a2015-10-07 14:37:17 -070022import org.onlab.packet.BasePacket;
Ray Milkeyea366452015-09-30 10:56:43 -070023import org.onlab.packet.DeserializationException;
24import org.onlab.packet.EAP;
Ray Milkeyea366452015-09-30 10:56:43 -070025import org.onlab.packet.Ethernet;
Ray Milkeyea366452015-09-30 10:56:43 -070026import org.onlab.packet.IpAddress;
Ray Milkeyea366452015-09-30 10:56:43 -070027import org.onlab.packet.RADIUS;
28import org.onlab.packet.RADIUSAttribute;
Ray Milkeyea366452015-09-30 10:56:43 -070029import org.onosproject.core.CoreServiceAdapter;
Jonathan Hart5db44532018-07-12 18:13:54 -070030import org.onosproject.event.DefaultEventSinkRegistry;
31import org.onosproject.event.Event;
32import org.onosproject.event.EventDeliveryService;
33import org.onosproject.event.EventSink;
Ray Milkeyfcb623d2015-10-01 16:48:18 -070034import org.onosproject.net.config.Config;
35import org.onosproject.net.config.NetworkConfigRegistryAdapter;
Amit Ghoshc9ac1e52017-07-28 12:31:18 +010036import org.onosproject.net.packet.InboundPacket;
Jonathan Hart9d1ce802020-01-28 10:45:08 -080037import org.onosproject.store.service.TestStorageService;
Matteo Scandolocf847b82019-04-26 15:00:00 -070038import org.opencord.aaa.AaaConfig;
Ray Milkeyea366452015-09-30 10:56:43 -070039
Jonathan Hart092dfb22015-11-16 23:05:21 -080040import java.net.InetAddress;
41import java.net.UnknownHostException;
Ray Milkeyea366452015-09-30 10:56:43 -070042
Jonathan Hart5db44532018-07-12 18:13:54 -070043import static com.google.common.base.Preconditions.checkState;
Ray Milkeyea366452015-09-30 10:56:43 -070044import static org.hamcrest.Matchers.is;
45import static org.hamcrest.Matchers.notNullValue;
Jonathan Hart612651f2019-11-25 09:21:43 -080046import static org.hamcrest.Matchers.nullValue;
Ray Milkeyea366452015-09-30 10:56:43 -070047import static org.junit.Assert.assertThat;
Ari Saha89831742015-06-26 10:31:48 -070048
49/**
50 * Set of tests of the ONOS application component.
51 */
Jonathan Hart092dfb22015-11-16 23:05:21 -080052public class AaaManagerTest extends AaaTestBase {
Ari Saha89831742015-06-26 10:31:48 -070053
Ray Milkey967776a2015-10-07 14:37:17 -070054 static final String BAD_IP_ADDRESS = "198.51.100.0";
Ari Saha89831742015-06-26 10:31:48 -070055
Jonathan Hart092dfb22015-11-16 23:05:21 -080056 private AaaManager aaaManager;
kartikey dubeye1545422019-05-22 12:53:45 +000057 private AaaStatisticsManager aaaStatisticsManager;
Ray Milkeyea366452015-09-30 10:56:43 -070058
Jonathan Hart092dfb22015-11-16 23:05:21 -080059 class AaaManagerWithoutRadiusServer extends AaaManager {
Amit Ghoshc9ac1e52017-07-28 12:31:18 +010060 protected void sendRadiusPacket(RADIUS radiusPacket, InboundPacket inPkt) {
Ray Milkey967776a2015-10-07 14:37:17 -070061 savePacket(radiusPacket);
62 }
Ray Milkeyea366452015-09-30 10:56:43 -070063 }
Ray Milkeyea366452015-09-30 10:56:43 -070064 /**
Ray Milkey967776a2015-10-07 14:37:17 -070065 * Mocks the AAAConfig class to force usage of an unroutable address for the
66 * RADIUS server.
Ray Milkeyea366452015-09-30 10:56:43 -070067 */
Jonathan Hart092dfb22015-11-16 23:05:21 -080068 static class MockAaaConfig extends AaaConfig {
Ray Milkeyea366452015-09-30 10:56:43 -070069 @Override
Ray Milkey967776a2015-10-07 14:37:17 -070070 public InetAddress radiusIp() {
Ray Milkeyea366452015-09-30 10:56:43 -070071 try {
Ray Milkey967776a2015-10-07 14:37:17 -070072 return InetAddress.getByName(BAD_IP_ADDRESS);
73 } catch (UnknownHostException ex) {
74 // can't happen
75 throw new IllegalStateException(ex);
Ray Milkeyea366452015-09-30 10:56:43 -070076 }
77 }
78 }
79
80 /**
Ray Milkeyfcb623d2015-10-01 16:48:18 -070081 * Mocks the network config registry.
82 */
83 @SuppressWarnings("unchecked")
84 private static final class TestNetworkConfigRegistry
85 extends NetworkConfigRegistryAdapter {
86 @Override
87 public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) {
Jonathan Hart092dfb22015-11-16 23:05:21 -080088 AaaConfig aaaConfig = new MockAaaConfig();
Ray Milkey967776a2015-10-07 14:37:17 -070089 return (C) aaaConfig;
Ray Milkeyfcb623d2015-10-01 16:48:18 -070090 }
91 }
92
Jonathan Hart5db44532018-07-12 18:13:54 -070093 public static class TestEventDispatcher extends DefaultEventSinkRegistry
94 implements EventDeliveryService {
Jonathan Hart5db44532018-07-12 18:13:54 -070095 @Override
96 @SuppressWarnings("unchecked")
97 public synchronized void post(Event event) {
98 EventSink sink = getSink(event.getClass());
99 checkState(sink != null, "No sink for event %s", event);
100 sink.process(event);
101 }
102
103 @Override
104 public void setDispatchTimeLimit(long millis) {
105 }
106
107 @Override
108 public long getDispatchTimeLimit() {
109 return 0;
110 }
111 }
112
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700113 /**
Ray Milkeyea366452015-09-30 10:56:43 -0700114 * Sets up the services required by the AAA application.
115 */
Ari Saha89831742015-06-26 10:31:48 -0700116 @Before
117 public void setUp() {
Jonathan Hart092dfb22015-11-16 23:05:21 -0800118 aaaManager = new AaaManagerWithoutRadiusServer();
119 aaaManager.netCfgService = new TestNetworkConfigRegistry();
120 aaaManager.coreService = new CoreServiceAdapter();
121 aaaManager.packetService = new MockPacketService();
Amit Ghoshf739be52017-09-21 15:49:37 +0100122 aaaManager.deviceService = new TestDeviceService();
Gamze Abaka1cfdb192018-10-25 11:39:19 +0000123 aaaManager.sadisService = new MockSadisService();
kartikey dubeye1545422019-05-22 12:53:45 +0000124 aaaManager.cfgService = new MockCfgService();
Jonathan Hart9d1ce802020-01-28 10:45:08 -0800125 aaaManager.storageService = new TestStorageService();
kartikey dubeye1545422019-05-22 12:53:45 +0000126 aaaStatisticsManager = new AaaStatisticsManager();
Shubham Sharma4900ce62019-06-19 14:18:50 +0000127 aaaManager.radiusOperationalStatusService = new RadiusOperationalStatusManager();
kartikey dubeye1545422019-05-22 12:53:45 +0000128 TestUtils.setField(aaaStatisticsManager, "eventDispatcher", new TestEventDispatcher());
129 aaaStatisticsManager.activate();
130 aaaManager.aaaStatisticsManager = this.aaaStatisticsManager;
Jonathan Hart5db44532018-07-12 18:13:54 -0700131 TestUtils.setField(aaaManager, "eventDispatcher", new TestEventDispatcher());
kartikey dubeye1545422019-05-22 12:53:45 +0000132 aaaManager.activate(new AaaTestBase.MockComponentContext());
Ari Saha89831742015-06-26 10:31:48 -0700133 }
134
Ray Milkeyea366452015-09-30 10:56:43 -0700135 /**
136 * Tears down the AAA application.
137 */
Ari Saha89831742015-06-26 10:31:48 -0700138 @After
139 public void tearDown() {
kartikey dubeye1545422019-05-22 12:53:45 +0000140 aaaManager.deactivate(new AaaTestBase.MockComponentContext());
Ari Saha89831742015-06-26 10:31:48 -0700141 }
142
Ray Milkeyea366452015-09-30 10:56:43 -0700143 /**
144 * Extracts the RADIUS packet from a packet sent by the supplicant.
145 *
Ray Milkey967776a2015-10-07 14:37:17 -0700146 * @param radius RADIUS packet sent by the supplicant
Ray Milkeyea366452015-09-30 10:56:43 -0700147 * @throws DeserializationException if deserialization of the packet contents
148 * fails.
149 */
Jonathan Hart092dfb22015-11-16 23:05:21 -0800150 private void checkRadiusPacketFromSupplicant(RADIUS radius)
Ray Milkeyea366452015-09-30 10:56:43 -0700151 throws DeserializationException {
Ray Milkeyea366452015-09-30 10:56:43 -0700152 assertThat(radius, notNullValue());
Ray Milkeyea366452015-09-30 10:56:43 -0700153
Ray Milkey967776a2015-10-07 14:37:17 -0700154 EAP eap = radius.decapsulateMessage();
Ray Milkeyea366452015-09-30 10:56:43 -0700155 assertThat(eap, notNullValue());
Ray Milkeyea366452015-09-30 10:56:43 -0700156 }
157
158 /**
159 * Fetches the sent packet at the given index. The requested packet
160 * must be the last packet on the list.
161 *
162 * @param index index into sent packets array
163 * @return packet
164 */
Ray Milkey967776a2015-10-07 14:37:17 -0700165 private BasePacket fetchPacket(int index) {
166 BasePacket packet = savedPackets.get(index);
167 assertThat(packet, notNullValue());
168 return packet;
Ray Milkeyea366452015-09-30 10:56:43 -0700169 }
170
171 /**
172 * Tests the authentication path through the AAA application.
173 *
174 * @throws DeserializationException if packed deserialization fails.
175 */
Ari Saha89831742015-06-26 10:31:48 -0700176 @Test
Jonathan Hart612651f2019-11-25 09:21:43 -0800177 public void testAuthentication() throws Exception {
Ray Milkeyea366452015-09-30 10:56:43 -0700178
179 // (1) Supplicant start up
180
181 Ethernet startPacket = constructSupplicantStartPacket();
182 sendPacket(startPacket);
183
Ray Milkey967776a2015-10-07 14:37:17 -0700184 Ethernet responsePacket = (Ethernet) fetchPacket(0);
Jonathan Hart092dfb22015-11-16 23:05:21 -0800185 checkRadiusPacket(aaaManager, responsePacket, EAP.ATTR_IDENTITY);
Ray Milkeyea366452015-09-30 10:56:43 -0700186
187 // (2) Supplicant identify
188
Jonathan Hart612651f2019-11-25 09:21:43 -0800189 Ethernet identifyPacket = constructSupplicantIdentifyPacket(null,
190 EAP.ATTR_IDENTITY, (byte) 3, null);
Ray Milkeyea366452015-09-30 10:56:43 -0700191 sendPacket(identifyPacket);
192
Ray Milkey967776a2015-10-07 14:37:17 -0700193 RADIUS radiusIdentifyPacket = (RADIUS) fetchPacket(1);
Jonathan Hart612651f2019-11-25 09:21:43 -0800194 byte reqId = radiusIdentifyPacket.getIdentifier();
Ray Milkeyea366452015-09-30 10:56:43 -0700195
Jonathan Hart092dfb22015-11-16 23:05:21 -0800196 checkRadiusPacketFromSupplicant(radiusIdentifyPacket);
Ray Milkey967776a2015-10-07 14:37:17 -0700197
198 assertThat(radiusIdentifyPacket.getCode(), is(RADIUS.RADIUS_CODE_ACCESS_REQUEST));
199 assertThat(new String(radiusIdentifyPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_USERNAME).getValue()),
200 is("testuser"));
Ray Milkeyea366452015-09-30 10:56:43 -0700201
202 IpAddress nasIp =
203 IpAddress.valueOf(IpAddress.Version.INET,
Ray Milkey967776a2015-10-07 14:37:17 -0700204 radiusIdentifyPacket.getAttribute(RADIUSAttribute.RADIUS_ATTR_NAS_IP)
Ray Milkeyea366452015-09-30 10:56:43 -0700205 .getValue());
Jonathan Hart092dfb22015-11-16 23:05:21 -0800206 assertThat(nasIp.toString(), is(aaaManager.nasIpAddress.getHostAddress()));
Ray Milkeyea366452015-09-30 10:56:43 -0700207
208 // State machine should have been created by now
209
Jonathan Hart612651f2019-11-25 09:21:43 -0800210 StateMachine stateMachine = aaaManager.getStateMachine(SESSION_ID);
Ray Milkeyea366452015-09-30 10:56:43 -0700211 assertThat(stateMachine, notNullValue());
212 assertThat(stateMachine.state(), is(StateMachine.STATE_PENDING));
213
214 // (3) RADIUS MD5 challenge
215
Ray Milkey967776a2015-10-07 14:37:17 -0700216 RADIUS radiusCodeAccessChallengePacket =
Jonathan Hart612651f2019-11-25 09:21:43 -0800217 constructRadiusCodeAccessChallengePacket(RADIUS.RADIUS_CODE_ACCESS_CHALLENGE, EAP.ATTR_MD5,
218 reqId, aaaManager.radiusSecret.getBytes());
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100219 aaaManager.handleRadiusPacket(radiusCodeAccessChallengePacket);
Ray Milkeyea366452015-09-30 10:56:43 -0700220
Ray Milkey967776a2015-10-07 14:37:17 -0700221 Ethernet radiusChallengeMD5Packet = (Ethernet) fetchPacket(2);
Jonathan Hart092dfb22015-11-16 23:05:21 -0800222 checkRadiusPacket(aaaManager, radiusChallengeMD5Packet, EAP.ATTR_MD5);
Ray Milkeyea366452015-09-30 10:56:43 -0700223
224 // (4) Supplicant MD5 response
225
Ray Milkey967776a2015-10-07 14:37:17 -0700226 Ethernet md5RadiusPacket =
227 constructSupplicantIdentifyPacket(stateMachine,
228 EAP.ATTR_MD5,
229 stateMachine.challengeIdentifier(),
230 radiusChallengeMD5Packet);
Ray Milkeyea366452015-09-30 10:56:43 -0700231 sendPacket(md5RadiusPacket);
Ray Milkey967776a2015-10-07 14:37:17 -0700232
233 RADIUS responseMd5RadiusPacket = (RADIUS) fetchPacket(3);
234
Jonathan Hart092dfb22015-11-16 23:05:21 -0800235 checkRadiusPacketFromSupplicant(responseMd5RadiusPacket);
Jonathan Hart612651f2019-11-25 09:21:43 -0800236 //assertThat(responseMd5RadiusPacket.getIdentifier(), is((byte) 9));
237 reqId = responseMd5RadiusPacket.getIdentifier();
Ray Milkeyea366452015-09-30 10:56:43 -0700238 assertThat(responseMd5RadiusPacket.getCode(), is(RADIUS.RADIUS_CODE_ACCESS_REQUEST));
239
240 // State machine should be in pending state
241
242 assertThat(stateMachine, notNullValue());
243 assertThat(stateMachine.state(), is(StateMachine.STATE_PENDING));
244
Ray Milkey967776a2015-10-07 14:37:17 -0700245 // (5) RADIUS Success
Ray Milkeyea366452015-09-30 10:56:43 -0700246
Ray Milkey967776a2015-10-07 14:37:17 -0700247 RADIUS successPacket =
Jonathan Hart612651f2019-11-25 09:21:43 -0800248 constructRadiusCodeAccessChallengePacket(RADIUS.RADIUS_CODE_ACCESS_ACCEPT,
249 EAP.SUCCESS, reqId, aaaManager.radiusSecret.getBytes());
Amit Ghoshc9ac1e52017-07-28 12:31:18 +0100250 aaaManager.handleRadiusPacket((successPacket));
Ray Milkey967776a2015-10-07 14:37:17 -0700251 Ethernet supplicantSuccessPacket = (Ethernet) fetchPacket(4);
Ray Milkeyea366452015-09-30 10:56:43 -0700252
Jonathan Hart092dfb22015-11-16 23:05:21 -0800253 checkRadiusPacket(aaaManager, supplicantSuccessPacket, EAP.SUCCESS);
Ray Milkeyea366452015-09-30 10:56:43 -0700254
255 // State machine should be in authorized state
256
257 assertThat(stateMachine, notNullValue());
258 assertThat(stateMachine.state(), is(StateMachine.STATE_AUTHORIZED));
Jonathan Hart612651f2019-11-25 09:21:43 -0800259 }
Ray Milkey967776a2015-10-07 14:37:17 -0700260
Jonathan Hart612651f2019-11-25 09:21:43 -0800261 @Test
262 public void testRemoveAuthentication() {
263 Ethernet startPacket = constructSupplicantStartPacket();
264 sendPacket(startPacket);
265
266 StateMachine stateMachine = aaaManager.getStateMachine(SESSION_ID);
267
268 assertThat(stateMachine, notNullValue());
269 assertThat(stateMachine.state(), is(StateMachine.STATE_STARTED));
270
271 aaaManager.removeAuthenticationStateByMac(stateMachine.supplicantAddress());
272
273 assertThat(aaaManager.getStateMachine(SESSION_ID), nullValue());
Ari Saha89831742015-06-26 10:31:48 -0700274 }
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700275
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700276 /**
277 * Tests the default configuration.
278 */
279 @Test
280 public void testConfig() {
Jonathan Hart092dfb22015-11-16 23:05:21 -0800281 assertThat(aaaManager.nasIpAddress.getHostAddress(), is(AaaConfig.DEFAULT_NAS_IP));
282 assertThat(aaaManager.nasMacAddress, is(AaaConfig.DEFAULT_NAS_MAC));
283 assertThat(aaaManager.radiusIpAddress.getHostAddress(), is(BAD_IP_ADDRESS));
284 assertThat(aaaManager.radiusMacAddress, is(AaaConfig.DEFAULT_RADIUS_MAC));
Ray Milkeyfcb623d2015-10-01 16:48:18 -0700285 }
Ari Saha89831742015-06-26 10:31:48 -0700286}