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 | |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 18 | import com.google.common.base.Charsets; |
Andrea Campanella | 7e0e7e3 | 2020-02-13 14:39:55 +0100 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 20 | import org.onlab.packet.BasePacket; |
| 21 | import org.onlab.packet.EAP; |
| 22 | import org.onlab.packet.EAPOL; |
| 23 | import org.onlab.packet.EthType; |
| 24 | import org.onlab.packet.Ethernet; |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 25 | import org.onlab.packet.Ip4Address; |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 26 | import org.onlab.packet.MacAddress; |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 27 | import org.onlab.packet.RADIUS; |
| 28 | import org.onlab.packet.RADIUSAttribute; |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 29 | import org.onlab.packet.VlanId; |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 30 | import org.onosproject.cfg.ComponentConfigService; |
| 31 | import org.onosproject.cfg.ConfigProperty; |
Andrea Campanella | c4781e6 | 2020-10-08 12:58:45 +0200 | [diff] [blame^] | 32 | import org.onosproject.mastership.MastershipServiceAdapter; |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 33 | import org.onosproject.net.Annotations; |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 34 | import org.onosproject.net.ConnectPoint; |
Andrea Campanella | c4781e6 | 2020-10-08 12:58:45 +0200 | [diff] [blame^] | 35 | import org.onosproject.net.DeviceId; |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 36 | import org.onosproject.net.Element; |
| 37 | import org.onosproject.net.Port; |
| 38 | import org.onosproject.net.PortNumber; |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 39 | import org.onosproject.net.device.DeviceServiceAdapter; |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 40 | import org.onosproject.net.packet.DefaultInboundPacket; |
| 41 | import org.onosproject.net.packet.DefaultPacketContext; |
| 42 | import org.onosproject.net.packet.InboundPacket; |
| 43 | import org.onosproject.net.packet.OutboundPacket; |
| 44 | import org.onosproject.net.packet.PacketContext; |
| 45 | import org.onosproject.net.packet.PacketProcessor; |
| 46 | import org.onosproject.net.packet.PacketServiceAdapter; |
Gamze Abaka | 1cfdb19 | 2018-10-25 11:39:19 +0000 | [diff] [blame] | 47 | import org.opencord.sadis.BandwidthProfileInformation; |
| 48 | import org.opencord.sadis.BaseInformationService; |
| 49 | import org.opencord.sadis.SadisService; |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 50 | import org.opencord.sadis.SubscriberAndDeviceInformation; |
Andrea Campanella | 7e0e7e3 | 2020-02-13 14:39:55 +0100 | [diff] [blame] | 51 | import org.opencord.sadis.UniTagInformation; |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 52 | import org.osgi.framework.Bundle; |
| 53 | import org.osgi.framework.BundleContext; |
| 54 | import org.osgi.framework.ServiceReference; |
| 55 | import org.osgi.service.component.ComponentContext; |
| 56 | import org.osgi.service.component.ComponentInstance; |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 57 | |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 58 | import java.nio.ByteBuffer; |
| 59 | import java.security.MessageDigest; |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 60 | import java.util.Dictionary; |
| 61 | import java.util.Hashtable; |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 62 | import java.util.LinkedList; |
| 63 | import java.util.List; |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 64 | import java.util.Set; |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 65 | |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 66 | import static org.hamcrest.Matchers.instanceOf; |
| 67 | import static org.hamcrest.Matchers.is; |
| 68 | import static org.hamcrest.Matchers.notNullValue; |
| 69 | import static org.junit.Assert.assertThat; |
| 70 | import static org.junit.Assert.fail; |
| 71 | import static org.onosproject.net.NetTestTools.connectPoint; |
| 72 | |
| 73 | /** |
| 74 | * Common methods for AAA app testing. |
| 75 | */ |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 76 | public class AaaTestBase { |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 77 | |
Matteo Scandolo | 9510e5d | 2020-09-24 17:31:52 -0700 | [diff] [blame] | 78 | //Time in ms to wait before checking packets |
| 79 | static final int ASSERTION_DELAY = 250; |
| 80 | //Duration in ms of the assertion for packets |
| 81 | static final int ASSERTION_LENGTH = 500; |
| 82 | |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 83 | MacAddress clientMac = MacAddress.valueOf("1a:1a:1a:1a:1a:1a"); |
| 84 | MacAddress serverMac = MacAddress.valueOf("2a:2a:2a:2a:2a:2a"); |
| 85 | |
| 86 | // Our session id will be the device ID ("of:1") with the port ("1") concatenated |
| 87 | static final String SESSION_ID = "of:11"; |
| 88 | |
| 89 | List<BasePacket> savedPackets = new LinkedList<>(); |
| 90 | PacketProcessor packetProcessor; |
| 91 | |
| 92 | /** |
| 93 | * Saves the given packet onto the saved packets list. |
| 94 | * |
| 95 | * @param packet packet to save |
| 96 | */ |
| 97 | void savePacket(BasePacket packet) { |
| 98 | savedPackets.add(packet); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Keeps a reference to the PacketProcessor and saves the OutboundPackets. |
| 103 | */ |
| 104 | class MockPacketService extends PacketServiceAdapter { |
| 105 | |
| 106 | @Override |
| 107 | public void addProcessor(PacketProcessor processor, int priority) { |
| 108 | packetProcessor = processor; |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public void emit(OutboundPacket packet) { |
| 113 | try { |
| 114 | Ethernet eth = Ethernet.deserializer().deserialize(packet.data().array(), |
| 115 | 0, packet.data().array().length); |
| 116 | savePacket(eth); |
| 117 | } catch (Exception e) { |
| 118 | fail(e.getMessage()); |
| 119 | } |
| 120 | } |
| 121 | } |
Andrea Campanella | c4781e6 | 2020-10-08 12:58:45 +0200 | [diff] [blame^] | 122 | |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 123 | class MockComponentContext implements ComponentContext { |
| 124 | |
Andrea Campanella | c4781e6 | 2020-10-08 12:58:45 +0200 | [diff] [blame^] | 125 | @Override |
| 126 | public Dictionary<String, Object> getProperties() { |
| 127 | Dictionary<String, Object> cfgDict = new Hashtable<String, Object>(); |
| 128 | cfgDict.put("statisticsGenerationEvent", 20); |
| 129 | return cfgDict; |
| 130 | } |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 131 | |
Andrea Campanella | c4781e6 | 2020-10-08 12:58:45 +0200 | [diff] [blame^] | 132 | @Override |
| 133 | public Object locateService(String name) { |
| 134 | // TODO Auto-generated method stub |
| 135 | return null; |
| 136 | } |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 137 | |
Andrea Campanella | c4781e6 | 2020-10-08 12:58:45 +0200 | [diff] [blame^] | 138 | @Override |
| 139 | public Object locateService(String name, ServiceReference reference) { |
| 140 | // TODO Auto-generated method stub |
| 141 | return null; |
| 142 | } |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 143 | |
Andrea Campanella | c4781e6 | 2020-10-08 12:58:45 +0200 | [diff] [blame^] | 144 | @Override |
| 145 | public Object[] locateServices(String name) { |
| 146 | // TODO Auto-generated method stub |
| 147 | return null; |
| 148 | } |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 149 | |
Andrea Campanella | c4781e6 | 2020-10-08 12:58:45 +0200 | [diff] [blame^] | 150 | @Override |
| 151 | public BundleContext getBundleContext() { |
| 152 | // TODO Auto-generated method stub |
| 153 | return null; |
| 154 | } |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 155 | |
Andrea Campanella | c4781e6 | 2020-10-08 12:58:45 +0200 | [diff] [blame^] | 156 | @Override |
| 157 | public Bundle getUsingBundle() { |
| 158 | // TODO Auto-generated method stub |
| 159 | return null; |
| 160 | } |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 161 | |
Andrea Campanella | c4781e6 | 2020-10-08 12:58:45 +0200 | [diff] [blame^] | 162 | @Override |
| 163 | public ComponentInstance getComponentInstance() { |
| 164 | // TODO Auto-generated method stub |
| 165 | return null; |
| 166 | } |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 167 | |
Andrea Campanella | c4781e6 | 2020-10-08 12:58:45 +0200 | [diff] [blame^] | 168 | @Override |
| 169 | public void enableComponent(String name) { |
| 170 | // TODO Auto-generated method stub |
| 171 | } |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 172 | |
Andrea Campanella | c4781e6 | 2020-10-08 12:58:45 +0200 | [diff] [blame^] | 173 | @Override |
| 174 | public void disableComponent(String name) { |
| 175 | // TODO Auto-generated method stub |
| 176 | } |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 177 | |
Andrea Campanella | c4781e6 | 2020-10-08 12:58:45 +0200 | [diff] [blame^] | 178 | @Override |
| 179 | public ServiceReference getServiceReference() { |
| 180 | // TODO Auto-generated method stub |
| 181 | return null; |
| 182 | } |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 183 | } |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 184 | |
| 185 | /** |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 186 | * Mocks the DeviceService. |
| 187 | */ |
| 188 | final class TestDeviceService extends DeviceServiceAdapter { |
| 189 | @Override |
| 190 | public Port getPort(ConnectPoint cp) { |
| 191 | return new MockPort(); |
| 192 | } |
| 193 | } |
Andrea Campanella | c4781e6 | 2020-10-08 12:58:45 +0200 | [diff] [blame^] | 194 | |
| 195 | private class MockPort implements Port { |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 196 | |
| 197 | @Override |
| 198 | public boolean isEnabled() { |
| 199 | return true; |
| 200 | } |
Andrea Campanella | c4781e6 | 2020-10-08 12:58:45 +0200 | [diff] [blame^] | 201 | |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 202 | public long portSpeed() { |
| 203 | return 1000; |
| 204 | } |
Andrea Campanella | c4781e6 | 2020-10-08 12:58:45 +0200 | [diff] [blame^] | 205 | |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 206 | public Element element() { |
| 207 | return null; |
| 208 | } |
Andrea Campanella | c4781e6 | 2020-10-08 12:58:45 +0200 | [diff] [blame^] | 209 | |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 210 | public PortNumber number() { |
| 211 | return null; |
| 212 | } |
Andrea Campanella | c4781e6 | 2020-10-08 12:58:45 +0200 | [diff] [blame^] | 213 | |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 214 | public Annotations annotations() { |
| 215 | return new MockAnnotations(); |
| 216 | } |
Andrea Campanella | c4781e6 | 2020-10-08 12:58:45 +0200 | [diff] [blame^] | 217 | |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 218 | public Type type() { |
| 219 | return Port.Type.FIBER; |
| 220 | } |
| 221 | |
| 222 | private class MockAnnotations implements Annotations { |
| 223 | |
| 224 | @Override |
| 225 | public String value(String val) { |
| 226 | return "PON 1/1"; |
| 227 | } |
Andrea Campanella | c4781e6 | 2020-10-08 12:58:45 +0200 | [diff] [blame^] | 228 | |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 229 | public Set<String> keys() { |
| 230 | return null; |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | private class MockSubscriberAndDeviceInformation extends SubscriberAndDeviceInformation { |
| 236 | |
Andrea Campanella | 7e0e7e3 | 2020-02-13 14:39:55 +0100 | [diff] [blame] | 237 | MockSubscriberAndDeviceInformation(String id, VlanId uniTagMatch, VlanId ctag, |
| 238 | VlanId stag, int dsPonPrio, int upPonPrio, |
| 239 | int techProfileId, String dsBpId, String usBpId, |
| 240 | String nasPortId, String circuitId, MacAddress hardId, |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 241 | Ip4Address ipAddress) { |
Andrea Campanella | 7e0e7e3 | 2020-02-13 14:39:55 +0100 | [diff] [blame] | 242 | // Builds UniTagInformation |
| 243 | UniTagInformation.Builder tagInfoBuilder = new UniTagInformation.Builder(); |
| 244 | UniTagInformation uniTagInfo = tagInfoBuilder.setUniTagMatch(uniTagMatch) |
| 245 | .setPonCTag(ctag) |
| 246 | .setPonSTag(stag) |
| 247 | .setDsPonCTagPriority(dsPonPrio) |
| 248 | .setUsPonSTagPriority(upPonPrio) |
| 249 | .setTechnologyProfileId(techProfileId) |
| 250 | .setDownstreamBandwidthProfile(dsBpId) |
| 251 | .setUpstreamBandwidthProfile(usBpId) |
| 252 | .build(); |
| 253 | |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 254 | this.setHardwareIdentifier(hardId); |
| 255 | this.setId(id); |
| 256 | this.setIPAddress(ipAddress); |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 257 | this.setNasPortId(nasPortId); |
| 258 | this.setCircuitId(circuitId); |
Andrea Campanella | 7e0e7e3 | 2020-02-13 14:39:55 +0100 | [diff] [blame] | 259 | this.setUniTagList(Lists.newArrayList(uniTagInfo)); |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | |
Gamze Abaka | 1cfdb19 | 2018-10-25 11:39:19 +0000 | [diff] [blame] | 263 | final class MockSadisService implements SadisService { |
| 264 | |
| 265 | @Override |
| 266 | public BaseInformationService<SubscriberAndDeviceInformation> getSubscriberInfoService() { |
| 267 | return new MockSubService(); |
| 268 | } |
| 269 | |
| 270 | @Override |
| 271 | public BaseInformationService<BandwidthProfileInformation> getBandwidthProfileService() { |
| 272 | return null; |
| 273 | } |
| 274 | } |
| 275 | |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 276 | final class MockCfgService implements ComponentConfigService { |
| 277 | @Override |
| 278 | public Set<String> getComponentNames() { |
| 279 | // TODO Auto-generated method stub |
| 280 | return null; |
| 281 | } |
| 282 | |
| 283 | @Override |
| 284 | public void registerProperties(Class<?> componentClass) { |
| 285 | // TODO Auto-generated method stub |
| 286 | } |
| 287 | |
| 288 | @Override |
| 289 | public void unregisterProperties(Class<?> componentClass, boolean clear) { |
| 290 | // TODO Auto-generated method stub |
| 291 | } |
| 292 | |
| 293 | @Override |
| 294 | public Set<ConfigProperty> getProperties(String componentName) { |
Andrea Campanella | c4781e6 | 2020-10-08 12:58:45 +0200 | [diff] [blame^] | 295 | return null; |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | @Override |
| 299 | public void setProperty(String componentName, String name, String value) { |
| 300 | // TODO Auto-generated method stub |
| 301 | } |
| 302 | |
| 303 | @Override |
| 304 | public void preSetProperty(String componentName, String name, String value) { |
| 305 | // TODO Auto-generated method stub |
| 306 | } |
| 307 | |
| 308 | @Override |
| 309 | public void preSetProperty(String componentName, String name, String value, boolean override) { |
| 310 | // TODO Auto-generated method stub |
| 311 | } |
| 312 | |
| 313 | @Override |
| 314 | public void unsetProperty(String componentName, String name) { |
| 315 | // TODO Auto-generated method stub |
| 316 | } |
| 317 | |
| 318 | @Override |
| 319 | public ConfigProperty getProperty(String componentName, String attribute) { |
Andrea Campanella | c4781e6 | 2020-10-08 12:58:45 +0200 | [diff] [blame^] | 320 | return null; |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Andrea Campanella | c4781e6 | 2020-10-08 12:58:45 +0200 | [diff] [blame^] | 323 | } |
| 324 | |
| 325 | final class MockMastershipService extends MastershipServiceAdapter { |
| 326 | @Override |
| 327 | public boolean isLocalMaster(DeviceId deviceId) { |
| 328 | return true; |
| 329 | } |
| 330 | } |
kartikey dubey | e154542 | 2019-05-22 12:53:45 +0000 | [diff] [blame] | 331 | |
Gamze Abaka | 1cfdb19 | 2018-10-25 11:39:19 +0000 | [diff] [blame] | 332 | final class MockSubService implements BaseInformationService<SubscriberAndDeviceInformation> { |
Andrea Campanella | 7e0e7e3 | 2020-02-13 14:39:55 +0100 | [diff] [blame] | 333 | private final VlanId uniTagMatch = VlanId.vlanId((short) 35); |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 334 | private final VlanId clientCtag = VlanId.vlanId((short) 999); |
| 335 | private final VlanId clientStag = VlanId.vlanId((short) 111); |
Andrea Campanella | 7e0e7e3 | 2020-02-13 14:39:55 +0100 | [diff] [blame] | 336 | private final int dsPrio = 0; |
| 337 | private final int usPrio = 0; |
| 338 | private final int techProfileId = 64; |
| 339 | private final String usBpId = "HSIA-US"; |
| 340 | private final String dsBpId = "HSIA-DS"; |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 341 | private final String clientNasPortId = "PON 1/1"; |
| 342 | private final String clientCircuitId = "CIR-PON 1/1"; |
| 343 | |
Andrea Campanella | 7e0e7e3 | 2020-02-13 14:39:55 +0100 | [diff] [blame] | 344 | |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 345 | MockSubscriberAndDeviceInformation sub = |
Andrea Campanella | 7e0e7e3 | 2020-02-13 14:39:55 +0100 | [diff] [blame] | 346 | new MockSubscriberAndDeviceInformation(clientNasPortId, uniTagMatch, clientCtag, |
| 347 | clientStag, dsPrio, usPrio, |
| 348 | techProfileId, dsBpId, usBpId, |
| 349 | clientNasPortId, clientCircuitId, null, |
| 350 | null); |
Andrea Campanella | c4781e6 | 2020-10-08 12:58:45 +0200 | [diff] [blame^] | 351 | |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 352 | @Override |
| 353 | public SubscriberAndDeviceInformation get(String id) { |
| 354 | |
Andrea Campanella | c4781e6 | 2020-10-08 12:58:45 +0200 | [diff] [blame^] | 355 | return sub; |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 356 | |
| 357 | } |
| 358 | |
| 359 | @Override |
Andrea Campanella | c4781e6 | 2020-10-08 12:58:45 +0200 | [diff] [blame^] | 360 | public void invalidateAll() { |
| 361 | } |
| 362 | |
| 363 | public void invalidateId(String id) { |
| 364 | } |
| 365 | |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 366 | public SubscriberAndDeviceInformation getfromCache(String id) { |
| 367 | return null; |
| 368 | } |
| 369 | } |
Andrea Campanella | c4781e6 | 2020-10-08 12:58:45 +0200 | [diff] [blame^] | 370 | |
Amit Ghosh | f739be5 | 2017-09-21 15:49:37 +0100 | [diff] [blame] | 371 | /** |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 372 | * Mocks the DefaultPacketContext. |
| 373 | */ |
| 374 | final class TestPacketContext extends DefaultPacketContext { |
| 375 | |
Shubham Sharma | cf5e503 | 2019-11-26 11:09:21 +0000 | [diff] [blame] | 376 | TestPacketContext(long time, InboundPacket inPkt, |
Andrea Campanella | c4781e6 | 2020-10-08 12:58:45 +0200 | [diff] [blame^] | 377 | OutboundPacket outPkt, boolean block) { |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 378 | super(time, inPkt, outPkt, block); |
| 379 | } |
| 380 | |
| 381 | @Override |
| 382 | public void send() { |
| 383 | // We don't send anything out. |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Sends an Ethernet packet to the process method of the Packet Processor. |
| 389 | * |
| 390 | * @param reply Ethernet packet |
| 391 | */ |
| 392 | void sendPacket(Ethernet reply) { |
| 393 | final ByteBuffer byteBuffer = ByteBuffer.wrap(reply.serialize()); |
| 394 | InboundPacket inPacket = new DefaultInboundPacket(connectPoint("1", 1), |
| 395 | reply, |
| 396 | byteBuffer); |
| 397 | |
| 398 | PacketContext context = new TestPacketContext(127L, inPacket, null, false); |
| 399 | packetProcessor.process(context); |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * Constructs an Ethernet packet containing identification payload. |
| 404 | * |
| 405 | * @return Ethernet packet |
| 406 | */ |
| 407 | Ethernet constructSupplicantIdentifyPacket(StateMachine stateMachine, |
Andrea Campanella | c4781e6 | 2020-10-08 12:58:45 +0200 | [diff] [blame^] | 408 | byte type, |
| 409 | byte id, |
| 410 | Ethernet radiusChallenge) |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 411 | throws Exception { |
| 412 | Ethernet eth = new Ethernet(); |
| 413 | eth.setDestinationMACAddress(clientMac.toBytes()); |
| 414 | eth.setSourceMACAddress(serverMac.toBytes()); |
| 415 | eth.setEtherType(EthType.EtherType.EAPOL.ethType().toShort()); |
| 416 | eth.setVlanID((short) 2); |
| 417 | |
| 418 | String username = "testuser"; |
| 419 | byte[] data = username.getBytes(); |
| 420 | |
| 421 | |
| 422 | if (type == EAP.ATTR_MD5) { |
| 423 | String password = "testpassword"; |
| 424 | EAPOL eapol = (EAPOL) radiusChallenge.getPayload(); |
| 425 | EAP eap = (EAP) eapol.getPayload(); |
| 426 | |
| 427 | byte[] identifier = new byte[password.length() + eap.getData().length]; |
| 428 | |
| 429 | identifier[0] = stateMachine.challengeIdentifier(); |
| 430 | System.arraycopy(password.getBytes(), 0, identifier, 1, password.length()); |
| 431 | System.arraycopy(eap.getData(), 1, identifier, 1 + password.length(), 16); |
| 432 | |
| 433 | MessageDigest md = MessageDigest.getInstance("MD5"); |
| 434 | byte[] hash = md.digest(identifier); |
| 435 | data = new byte[17]; |
| 436 | data[0] = (byte) 16; |
| 437 | System.arraycopy(hash, 0, data, 1, 16); |
| 438 | } |
| 439 | EAP eap = new EAP(EAP.RESPONSE, (byte) 1, type, |
| 440 | data); |
| 441 | eap.setIdentifier(id); |
| 442 | |
| 443 | // eapol header |
| 444 | EAPOL eapol = new EAPOL(); |
| 445 | eapol.setEapolType(EAPOL.EAPOL_PACKET); |
| 446 | eapol.setPacketLength(eap.getLength()); |
| 447 | |
| 448 | // eap part |
| 449 | eapol.setPayload(eap); |
| 450 | |
| 451 | eth.setPayload(eapol); |
| 452 | eth.setPad(true); |
| 453 | return eth; |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * Constructs an Ethernet packet containing a EAPOL_START Payload. |
| 458 | * |
| 459 | * @return Ethernet packet |
| 460 | */ |
| 461 | Ethernet constructSupplicantStartPacket() { |
| 462 | Ethernet eth = new Ethernet(); |
| 463 | eth.setDestinationMACAddress(clientMac.toBytes()); |
| 464 | eth.setSourceMACAddress(serverMac.toBytes()); |
| 465 | eth.setEtherType(EthType.EtherType.EAPOL.ethType().toShort()); |
| 466 | eth.setVlanID((short) 2); |
| 467 | |
Shubham Sharma | 4900ce6 | 2019-06-19 14:18:50 +0000 | [diff] [blame] | 468 | 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] | 469 | |
| 470 | // eapol header |
| 471 | EAPOL eapol = new EAPOL(); |
| 472 | eapol.setEapolType(EAPOL.EAPOL_START); |
| 473 | eapol.setPacketLength(eap.getLength()); |
| 474 | |
| 475 | // eap part |
| 476 | eapol.setPayload(eap); |
| 477 | |
| 478 | eth.setPayload(eapol); |
| 479 | eth.setPad(true); |
| 480 | return eth; |
| 481 | } |
| 482 | |
| 483 | /** |
Shubham Sharma | 8d7a982 | 2020-01-28 10:04:01 +0000 | [diff] [blame] | 484 | * Constructs an Ethernet packet containing a EAPOL_ASF Payload. |
| 485 | * |
| 486 | * @return Ethernet packet |
| 487 | */ |
| 488 | Ethernet constructSupplicantAsfPacket() { |
| 489 | Ethernet eth = new Ethernet(); |
| 490 | eth.setDestinationMACAddress(clientMac.toBytes()); |
| 491 | eth.setSourceMACAddress(serverMac.toBytes()); |
| 492 | eth.setEtherType(EthType.EtherType.EAPOL.ethType().toShort()); |
| 493 | eth.setVlanID((short) 2); |
| 494 | |
| 495 | EAP eap = new EAP(EAPOL.EAPOL_START, (byte) 3, EAPOL.EAPOL_START, null); |
| 496 | |
| 497 | // eapol header |
| 498 | EAPOL eapol = new EAPOL(); |
| 499 | eapol.setEapolType(EAPOL.EAPOL_ASF); |
| 500 | eapol.setPacketLength(eap.getLength()); |
| 501 | |
| 502 | // eap part |
| 503 | eapol.setPayload(eap); |
| 504 | |
| 505 | eth.setPayload(eapol); |
| 506 | eth.setPad(true); |
| 507 | return eth; |
| 508 | } |
| 509 | |
| 510 | /** |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 511 | * Checks the contents of a RADIUS packet being sent to the RADIUS server. |
| 512 | * |
| 513 | * @param radiusPacket packet to check |
Andrea Campanella | c4781e6 | 2020-10-08 12:58:45 +0200 | [diff] [blame^] | 514 | * @param code expected code |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 515 | */ |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 516 | void checkRadiusPacket(AaaManager aaaManager, Ethernet radiusPacket, byte code) { |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 517 | |
| 518 | assertThat(radiusPacket.getSourceMAC(), |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 519 | is(MacAddress.valueOf(aaaManager.nasMacAddress))); |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 520 | assertThat(radiusPacket.getDestinationMAC(), is(serverMac)); |
| 521 | |
| 522 | assertThat(radiusPacket.getPayload(), instanceOf(EAPOL.class)); |
| 523 | EAPOL eapol = (EAPOL) radiusPacket.getPayload(); |
| 524 | assertThat(eapol, notNullValue()); |
| 525 | |
| 526 | assertThat(eapol.getEapolType(), is(EAPOL.EAPOL_PACKET)); |
| 527 | assertThat(eapol.getPayload(), instanceOf(EAP.class)); |
| 528 | EAP eap = (EAP) eapol.getPayload(); |
| 529 | assertThat(eap, notNullValue()); |
| 530 | |
| 531 | assertThat(eap.getCode(), is(code)); |
| 532 | } |
Shubham Sharma | 2b3fb69 | 2019-12-12 10:19:10 +0000 | [diff] [blame] | 533 | |
| 534 | /** |
| 535 | * Constructs an Ethernet packet containing a EAPOL_LOGOFF Payload. |
| 536 | * |
| 537 | * @return Ethernet packet |
| 538 | */ |
| 539 | Ethernet constructSupplicantLogoffPacket() { |
| 540 | Ethernet eth = new Ethernet(); |
| 541 | eth.setDestinationMACAddress(clientMac.toBytes()); |
| 542 | eth.setSourceMACAddress(serverMac.toBytes()); |
| 543 | eth.setEtherType(EthType.EtherType.EAPOL.ethType().toShort()); |
| 544 | eth.setVlanID((short) 2); |
| 545 | |
| 546 | EAP eap = new EAP(EAPOL.EAPOL_LOGOFF, (byte) 2, EAPOL.EAPOL_LOGOFF, null); |
| 547 | |
| 548 | // eapol header |
| 549 | EAPOL eapol = new EAPOL(); |
| 550 | eapol.setEapolType(EAPOL.EAPOL_LOGOFF); |
| 551 | eapol.setPacketLength(eap.getLength()); |
| 552 | |
| 553 | // eap part |
| 554 | eapol.setPayload(eap); |
| 555 | |
| 556 | eth.setPayload(eapol); |
| 557 | eth.setPad(true); |
| 558 | return eth; |
| 559 | } |
| 560 | |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 561 | /** |
| 562 | * Constructs an Ethernet packet containing a RADIUS challenge |
| 563 | * packet. |
| 564 | * |
| 565 | * @param challengeCode code to use in challenge packet |
| 566 | * @param challengeType type to use in challenge packet |
| 567 | * @return Ethernet packet |
| 568 | */ |
| 569 | RADIUS constructRadiusCodeAccessChallengePacket(byte challengeCode, byte challengeType, |
Andrea Campanella | c4781e6 | 2020-10-08 12:58:45 +0200 | [diff] [blame^] | 570 | byte identifier, byte[] messageAuth) { |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 571 | |
| 572 | String challenge = "12345678901234567"; |
| 573 | |
| 574 | EAP eap = new EAP(challengeType, (byte) 4, challengeType, |
Andrea Campanella | c4781e6 | 2020-10-08 12:58:45 +0200 | [diff] [blame^] | 575 | challenge.getBytes(Charsets.US_ASCII)); |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 576 | //eap.setIdentifier((byte) 4); |
| 577 | eap.setIdentifier(identifier); |
| 578 | |
| 579 | RADIUS radius = new RADIUS(); |
| 580 | radius.setCode(challengeCode); |
| 581 | //radius.setIdentifier((byte) 4); |
| 582 | radius.setIdentifier(identifier); |
| 583 | radius.setAttribute(RADIUSAttribute.RADIUS_ATTR_STATE, |
Andrea Campanella | c4781e6 | 2020-10-08 12:58:45 +0200 | [diff] [blame^] | 584 | challenge.getBytes(Charsets.US_ASCII)); |
Jonathan Hart | 612651f | 2019-11-25 09:21:43 -0800 | [diff] [blame] | 585 | |
| 586 | radius.setPayload(eap); |
| 587 | radius.setAttribute(RADIUSAttribute.RADIUS_ATTR_EAP_MESSAGE, eap.serialize()); |
| 588 | radius.setAttribute(RADIUSAttribute.RADIUS_ATTR_MESSAGE_AUTH, messageAuth); |
| 589 | return radius; |
| 590 | } |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 591 | } |