Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 4e33be2 | 2017-08-03 22:45:46 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -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 | */ |
Matteo Scandolo | cf847b8 | 2019-04-26 15:00:00 -0700 | [diff] [blame] | 16 | package org.opencord.aaa.impl; |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 17 | |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 18 | import org.onlab.packet.BasePacket; |
| 19 | import org.onlab.packet.EAP; |
| 20 | import org.onlab.packet.EAPOL; |
| 21 | import org.onlab.packet.EthType; |
| 22 | import org.onlab.packet.Ethernet; |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 23 | import org.onlab.packet.Ip4Address; |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 24 | import org.onlab.packet.MacAddress; |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 25 | import org.onlab.packet.VlanId; |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 26 | import org.onosproject.cfg.ComponentConfigService; |
| 27 | import org.onosproject.cfg.ConfigProperty; |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 28 | import org.onosproject.net.Annotations; |
| 29 | import org.onosproject.net.device.DeviceServiceAdapter; |
| 30 | import org.onosproject.net.ConnectPoint; |
| 31 | import org.onosproject.net.Element; |
| 32 | import org.onosproject.net.Port; |
| 33 | import org.onosproject.net.PortNumber; |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 34 | import org.onosproject.net.packet.DefaultInboundPacket; |
| 35 | import org.onosproject.net.packet.DefaultPacketContext; |
| 36 | import org.onosproject.net.packet.InboundPacket; |
| 37 | import org.onosproject.net.packet.OutboundPacket; |
| 38 | import org.onosproject.net.packet.PacketContext; |
| 39 | import org.onosproject.net.packet.PacketProcessor; |
| 40 | import org.onosproject.net.packet.PacketServiceAdapter; |
| 41 | |
Gamze Abaka | 1cfdb19 | 2018-10-25 11:39:19 +0000 | [diff] [blame] | 42 | import org.opencord.sadis.BandwidthProfileInformation; |
| 43 | import org.opencord.sadis.BaseInformationService; |
| 44 | import org.opencord.sadis.SadisService; |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 45 | import org.opencord.sadis.SubscriberAndDeviceInformation; |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 46 | import org.osgi.framework.Bundle; |
| 47 | import org.osgi.framework.BundleContext; |
| 48 | import org.osgi.framework.ServiceReference; |
| 49 | import org.osgi.service.component.ComponentContext; |
| 50 | import org.osgi.service.component.ComponentInstance; |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 51 | |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 52 | import java.nio.ByteBuffer; |
| 53 | import java.security.MessageDigest; |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 54 | import java.util.Dictionary; |
| 55 | import java.util.Hashtable; |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 56 | import java.util.LinkedList; |
| 57 | import java.util.List; |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 58 | import java.util.Set; |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 59 | |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 60 | import static org.hamcrest.Matchers.instanceOf; |
| 61 | import static org.hamcrest.Matchers.is; |
| 62 | import static org.hamcrest.Matchers.notNullValue; |
| 63 | import static org.junit.Assert.assertThat; |
| 64 | import static org.junit.Assert.fail; |
| 65 | import static org.onosproject.net.NetTestTools.connectPoint; |
| 66 | |
| 67 | /** |
| 68 | * Common methods for AAA app testing. |
| 69 | */ |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 70 | public class AaaTestBase { |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 71 | |
| 72 | MacAddress clientMac = MacAddress.valueOf("1a:1a:1a:1a:1a:1a"); |
| 73 | MacAddress serverMac = MacAddress.valueOf("2a:2a:2a:2a:2a:2a"); |
| 74 | |
| 75 | // Our session id will be the device ID ("of:1") with the port ("1") concatenated |
| 76 | static final String SESSION_ID = "of:11"; |
| 77 | |
| 78 | List<BasePacket> savedPackets = new LinkedList<>(); |
| 79 | PacketProcessor packetProcessor; |
| 80 | |
| 81 | /** |
| 82 | * Saves the given packet onto the saved packets list. |
| 83 | * |
| 84 | * @param packet packet to save |
| 85 | */ |
| 86 | void savePacket(BasePacket packet) { |
| 87 | savedPackets.add(packet); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Keeps a reference to the PacketProcessor and saves the OutboundPackets. |
| 92 | */ |
| 93 | class MockPacketService extends PacketServiceAdapter { |
| 94 | |
| 95 | @Override |
| 96 | public void addProcessor(PacketProcessor processor, int priority) { |
| 97 | packetProcessor = processor; |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void emit(OutboundPacket packet) { |
| 102 | try { |
| 103 | Ethernet eth = Ethernet.deserializer().deserialize(packet.data().array(), |
| 104 | 0, packet.data().array().length); |
| 105 | savePacket(eth); |
| 106 | } catch (Exception e) { |
| 107 | fail(e.getMessage()); |
| 108 | } |
| 109 | } |
| 110 | } |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 111 | class MockComponentContext implements ComponentContext { |
| 112 | |
| 113 | @Override |
| 114 | public Dictionary<String, Object> getProperties() { |
| 115 | Dictionary<String, Object> cfgDict = new Hashtable<String, Object>(); |
| 116 | cfgDict.put("statisticsGenerationEvent", 20); |
| 117 | return cfgDict; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public Object locateService(String name) { |
| 122 | // TODO Auto-generated method stub |
| 123 | return null; |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public Object locateService(String name, ServiceReference reference) { |
| 128 | // TODO Auto-generated method stub |
| 129 | return null; |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public Object[] locateServices(String name) { |
| 134 | // TODO Auto-generated method stub |
| 135 | return null; |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public BundleContext getBundleContext() { |
| 140 | // TODO Auto-generated method stub |
| 141 | return null; |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public Bundle getUsingBundle() { |
| 146 | // TODO Auto-generated method stub |
| 147 | return null; |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public ComponentInstance getComponentInstance() { |
| 152 | // TODO Auto-generated method stub |
| 153 | return null; |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public void enableComponent(String name) { |
| 158 | // TODO Auto-generated method stub |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public void disableComponent(String name) { |
| 163 | // TODO Auto-generated method stub |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public ServiceReference getServiceReference() { |
| 168 | // TODO Auto-generated method stub |
| 169 | return null; |
| 170 | } |
| 171 | } |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 172 | |
| 173 | /** |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 174 | * Mocks the DeviceService. |
| 175 | */ |
| 176 | final class TestDeviceService extends DeviceServiceAdapter { |
| 177 | @Override |
| 178 | public Port getPort(ConnectPoint cp) { |
| 179 | return new MockPort(); |
| 180 | } |
| 181 | } |
| 182 | private class MockPort implements Port { |
| 183 | |
| 184 | @Override |
| 185 | public boolean isEnabled() { |
| 186 | return true; |
| 187 | } |
| 188 | public long portSpeed() { |
| 189 | return 1000; |
| 190 | } |
| 191 | public Element element() { |
| 192 | return null; |
| 193 | } |
| 194 | public PortNumber number() { |
| 195 | return null; |
| 196 | } |
| 197 | public Annotations annotations() { |
| 198 | return new MockAnnotations(); |
| 199 | } |
| 200 | public Type type() { |
| 201 | return Port.Type.FIBER; |
| 202 | } |
| 203 | |
| 204 | private class MockAnnotations implements Annotations { |
| 205 | |
| 206 | @Override |
| 207 | public String value(String val) { |
| 208 | return "PON 1/1"; |
| 209 | } |
| 210 | public Set<String> keys() { |
| 211 | return null; |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | private class MockSubscriberAndDeviceInformation extends SubscriberAndDeviceInformation { |
| 217 | |
| 218 | MockSubscriberAndDeviceInformation(String id, VlanId ctag, |
| 219 | VlanId stag, String nasPortId, |
| 220 | String circuitId, MacAddress hardId, |
| 221 | Ip4Address ipAddress) { |
| 222 | this.setCTag(ctag); |
| 223 | this.setHardwareIdentifier(hardId); |
| 224 | this.setId(id); |
| 225 | this.setIPAddress(ipAddress); |
| 226 | this.setSTag(stag); |
| 227 | this.setNasPortId(nasPortId); |
| 228 | this.setCircuitId(circuitId); |
| 229 | } |
| 230 | } |
| 231 | |
Gamze Abaka | 1cfdb19 | 2018-10-25 11:39:19 +0000 | [diff] [blame] | 232 | final class MockSadisService implements SadisService { |
| 233 | |
| 234 | @Override |
| 235 | public BaseInformationService<SubscriberAndDeviceInformation> getSubscriberInfoService() { |
| 236 | return new MockSubService(); |
| 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public BaseInformationService<BandwidthProfileInformation> getBandwidthProfileService() { |
| 241 | return null; |
| 242 | } |
| 243 | } |
| 244 | |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 245 | final class MockCfgService implements ComponentConfigService { |
| 246 | @Override |
| 247 | public Set<String> getComponentNames() { |
| 248 | // TODO Auto-generated method stub |
| 249 | return null; |
| 250 | } |
| 251 | |
| 252 | @Override |
| 253 | public void registerProperties(Class<?> componentClass) { |
| 254 | // TODO Auto-generated method stub |
| 255 | } |
| 256 | |
| 257 | @Override |
| 258 | public void unregisterProperties(Class<?> componentClass, boolean clear) { |
| 259 | // TODO Auto-generated method stub |
| 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public Set<ConfigProperty> getProperties(String componentName) { |
| 264 | return null; |
| 265 | } |
| 266 | |
| 267 | @Override |
| 268 | public void setProperty(String componentName, String name, String value) { |
| 269 | // TODO Auto-generated method stub |
| 270 | } |
| 271 | |
| 272 | @Override |
| 273 | public void preSetProperty(String componentName, String name, String value) { |
| 274 | // TODO Auto-generated method stub |
| 275 | } |
| 276 | |
| 277 | @Override |
| 278 | public void preSetProperty(String componentName, String name, String value, boolean override) { |
| 279 | // TODO Auto-generated method stub |
| 280 | } |
| 281 | |
| 282 | @Override |
| 283 | public void unsetProperty(String componentName, String name) { |
| 284 | // TODO Auto-generated method stub |
| 285 | } |
| 286 | |
| 287 | @Override |
| 288 | public ConfigProperty getProperty(String componentName, String attribute) { |
| 289 | return null; |
| 290 | } |
| 291 | |
| 292 | } |
| 293 | |
Gamze Abaka | 1cfdb19 | 2018-10-25 11:39:19 +0000 | [diff] [blame] | 294 | final class MockSubService implements BaseInformationService<SubscriberAndDeviceInformation> { |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 295 | private final VlanId clientCtag = VlanId.vlanId((short) 999); |
| 296 | private final VlanId clientStag = VlanId.vlanId((short) 111); |
| 297 | private final String clientNasPortId = "PON 1/1"; |
| 298 | private final String clientCircuitId = "CIR-PON 1/1"; |
| 299 | |
| 300 | MockSubscriberAndDeviceInformation sub = |
| 301 | new MockSubscriberAndDeviceInformation(clientNasPortId, clientCtag, |
| 302 | clientStag, clientNasPortId, clientCircuitId, null, null); |
| 303 | @Override |
| 304 | public SubscriberAndDeviceInformation get(String id) { |
| 305 | |
| 306 | return sub; |
| 307 | |
| 308 | } |
| 309 | |
| 310 | @Override |
| 311 | public void invalidateAll() {} |
| 312 | public void invalidateId(String id) {} |
| 313 | public SubscriberAndDeviceInformation getfromCache(String id) { |
| 314 | return null; |
| 315 | } |
| 316 | } |
| 317 | /** |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 318 | * Mocks the DefaultPacketContext. |
| 319 | */ |
| 320 | final class TestPacketContext extends DefaultPacketContext { |
| 321 | |
Shubham Sharma | cf5e503 | 2019-11-26 11:09:21 +0000 | [diff] [blame] | 322 | TestPacketContext(long time, InboundPacket inPkt, |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 323 | OutboundPacket outPkt, boolean block) { |
| 324 | super(time, inPkt, outPkt, block); |
| 325 | } |
| 326 | |
| 327 | @Override |
| 328 | public void send() { |
| 329 | // We don't send anything out. |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Sends an Ethernet packet to the process method of the Packet Processor. |
| 335 | * |
| 336 | * @param reply Ethernet packet |
| 337 | */ |
| 338 | void sendPacket(Ethernet reply) { |
| 339 | final ByteBuffer byteBuffer = ByteBuffer.wrap(reply.serialize()); |
| 340 | InboundPacket inPacket = new DefaultInboundPacket(connectPoint("1", 1), |
| 341 | reply, |
| 342 | byteBuffer); |
| 343 | |
| 344 | PacketContext context = new TestPacketContext(127L, inPacket, null, false); |
| 345 | packetProcessor.process(context); |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Constructs an Ethernet packet containing identification payload. |
| 350 | * |
| 351 | * @return Ethernet packet |
| 352 | */ |
| 353 | Ethernet constructSupplicantIdentifyPacket(StateMachine stateMachine, |
| 354 | byte type, |
| 355 | byte id, |
| 356 | Ethernet radiusChallenge) |
| 357 | throws Exception { |
| 358 | Ethernet eth = new Ethernet(); |
| 359 | eth.setDestinationMACAddress(clientMac.toBytes()); |
| 360 | eth.setSourceMACAddress(serverMac.toBytes()); |
| 361 | eth.setEtherType(EthType.EtherType.EAPOL.ethType().toShort()); |
| 362 | eth.setVlanID((short) 2); |
| 363 | |
| 364 | String username = "testuser"; |
| 365 | byte[] data = username.getBytes(); |
| 366 | |
| 367 | |
| 368 | if (type == EAP.ATTR_MD5) { |
| 369 | String password = "testpassword"; |
| 370 | EAPOL eapol = (EAPOL) radiusChallenge.getPayload(); |
| 371 | EAP eap = (EAP) eapol.getPayload(); |
| 372 | |
| 373 | byte[] identifier = new byte[password.length() + eap.getData().length]; |
| 374 | |
| 375 | identifier[0] = stateMachine.challengeIdentifier(); |
| 376 | System.arraycopy(password.getBytes(), 0, identifier, 1, password.length()); |
| 377 | System.arraycopy(eap.getData(), 1, identifier, 1 + password.length(), 16); |
| 378 | |
| 379 | MessageDigest md = MessageDigest.getInstance("MD5"); |
| 380 | byte[] hash = md.digest(identifier); |
| 381 | data = new byte[17]; |
| 382 | data[0] = (byte) 16; |
| 383 | System.arraycopy(hash, 0, data, 1, 16); |
| 384 | } |
| 385 | EAP eap = new EAP(EAP.RESPONSE, (byte) 1, type, |
| 386 | data); |
| 387 | eap.setIdentifier(id); |
| 388 | |
| 389 | // eapol header |
| 390 | EAPOL eapol = new EAPOL(); |
| 391 | eapol.setEapolType(EAPOL.EAPOL_PACKET); |
| 392 | eapol.setPacketLength(eap.getLength()); |
| 393 | |
| 394 | // eap part |
| 395 | eapol.setPayload(eap); |
| 396 | |
| 397 | eth.setPayload(eapol); |
| 398 | eth.setPad(true); |
| 399 | return eth; |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * Constructs an Ethernet packet containing a EAPOL_START Payload. |
| 404 | * |
| 405 | * @return Ethernet packet |
| 406 | */ |
| 407 | Ethernet constructSupplicantStartPacket() { |
| 408 | Ethernet eth = new Ethernet(); |
| 409 | eth.setDestinationMACAddress(clientMac.toBytes()); |
| 410 | eth.setSourceMACAddress(serverMac.toBytes()); |
| 411 | eth.setEtherType(EthType.EtherType.EAPOL.ethType().toShort()); |
| 412 | eth.setVlanID((short) 2); |
| 413 | |
Shubham Sharma | 4900ce6 | 2019-06-19 14:18:50 +0000 | [diff] [blame^] | 414 | EAP eap = new EAP(EAPOL.EAPOL_START, (byte) 3, EAPOL.EAPOL_START, null); |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 415 | |
| 416 | // eapol header |
| 417 | EAPOL eapol = new EAPOL(); |
| 418 | eapol.setEapolType(EAPOL.EAPOL_START); |
| 419 | eapol.setPacketLength(eap.getLength()); |
| 420 | |
| 421 | // eap part |
| 422 | eapol.setPayload(eap); |
| 423 | |
| 424 | eth.setPayload(eapol); |
| 425 | eth.setPad(true); |
| 426 | return eth; |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * Checks the contents of a RADIUS packet being sent to the RADIUS server. |
| 431 | * |
| 432 | * @param radiusPacket packet to check |
| 433 | * @param code expected code |
| 434 | */ |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 435 | void checkRadiusPacket(AaaManager aaaManager, Ethernet radiusPacket, byte code) { |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 436 | |
| 437 | assertThat(radiusPacket.getSourceMAC(), |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 438 | is(MacAddress.valueOf(aaaManager.nasMacAddress))); |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 439 | assertThat(radiusPacket.getDestinationMAC(), is(serverMac)); |
| 440 | |
| 441 | assertThat(radiusPacket.getPayload(), instanceOf(EAPOL.class)); |
| 442 | EAPOL eapol = (EAPOL) radiusPacket.getPayload(); |
| 443 | assertThat(eapol, notNullValue()); |
| 444 | |
| 445 | assertThat(eapol.getEapolType(), is(EAPOL.EAPOL_PACKET)); |
| 446 | assertThat(eapol.getPayload(), instanceOf(EAP.class)); |
| 447 | EAP eap = (EAP) eapol.getPayload(); |
| 448 | assertThat(eap, notNullValue()); |
| 449 | |
| 450 | assertThat(eap.getCode(), is(code)); |
| 451 | } |
| 452 | } |