blob: 3fca911cf74050dd692876f96528715c60708d21 [file] [log] [blame]
Ray Milkey967776a2015-10-07 14:37:17 -07001/*
Brian O'Connor4e33be22017-08-03 22:45:46 -07002 * Copyright 2015-present Open Networking Foundation
Ray Milkey967776a2015-10-07 14:37:17 -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;
Ray Milkey967776a2015-10-07 14:37:17 -070017
Jonathan Hart612651f2019-11-25 09:21:43 -080018import com.google.common.base.Charsets;
Andrea Campanella7e0e7e32020-02-13 14:39:55 +010019import com.google.common.collect.Lists;
Ray Milkey967776a2015-10-07 14:37:17 -070020import org.onlab.packet.BasePacket;
21import org.onlab.packet.EAP;
22import org.onlab.packet.EAPOL;
23import org.onlab.packet.EthType;
24import org.onlab.packet.Ethernet;
Amit Ghoshf739be52017-09-21 15:49:37 +010025import org.onlab.packet.Ip4Address;
Ray Milkey967776a2015-10-07 14:37:17 -070026import org.onlab.packet.MacAddress;
Jonathan Hart612651f2019-11-25 09:21:43 -080027import org.onlab.packet.RADIUS;
28import org.onlab.packet.RADIUSAttribute;
Amit Ghoshf739be52017-09-21 15:49:37 +010029import org.onlab.packet.VlanId;
kartikey dubeye1545422019-05-22 12:53:45 +000030import org.onosproject.cfg.ComponentConfigService;
31import org.onosproject.cfg.ConfigProperty;
Andrea Campanellac4781e62020-10-08 12:58:45 +020032import org.onosproject.mastership.MastershipServiceAdapter;
Amit Ghoshf739be52017-09-21 15:49:37 +010033import org.onosproject.net.Annotations;
Amit Ghoshf739be52017-09-21 15:49:37 +010034import org.onosproject.net.ConnectPoint;
Andrea Campanellac4781e62020-10-08 12:58:45 +020035import org.onosproject.net.DeviceId;
Amit Ghoshf739be52017-09-21 15:49:37 +010036import org.onosproject.net.Element;
37import org.onosproject.net.Port;
38import org.onosproject.net.PortNumber;
Jonathan Hart612651f2019-11-25 09:21:43 -080039import org.onosproject.net.device.DeviceServiceAdapter;
Ray Milkey967776a2015-10-07 14:37:17 -070040import org.onosproject.net.packet.DefaultInboundPacket;
41import org.onosproject.net.packet.DefaultPacketContext;
42import org.onosproject.net.packet.InboundPacket;
43import org.onosproject.net.packet.OutboundPacket;
44import org.onosproject.net.packet.PacketContext;
45import org.onosproject.net.packet.PacketProcessor;
46import org.onosproject.net.packet.PacketServiceAdapter;
Gamze Abaka1cfdb192018-10-25 11:39:19 +000047import org.opencord.sadis.BandwidthProfileInformation;
48import org.opencord.sadis.BaseInformationService;
49import org.opencord.sadis.SadisService;
Amit Ghoshf739be52017-09-21 15:49:37 +010050import org.opencord.sadis.SubscriberAndDeviceInformation;
Andrea Campanella7e0e7e32020-02-13 14:39:55 +010051import org.opencord.sadis.UniTagInformation;
kartikey dubeye1545422019-05-22 12:53:45 +000052import org.osgi.framework.Bundle;
53import org.osgi.framework.BundleContext;
54import org.osgi.framework.ServiceReference;
55import org.osgi.service.component.ComponentContext;
56import org.osgi.service.component.ComponentInstance;
Amit Ghoshf739be52017-09-21 15:49:37 +010057
Jonathan Hart092dfb22015-11-16 23:05:21 -080058import java.nio.ByteBuffer;
59import java.security.MessageDigest;
kartikey dubeye1545422019-05-22 12:53:45 +000060import java.util.Dictionary;
61import java.util.Hashtable;
Jonathan Hart092dfb22015-11-16 23:05:21 -080062import java.util.LinkedList;
63import java.util.List;
Amit Ghoshf739be52017-09-21 15:49:37 +010064import java.util.Set;
Jonathan Hart092dfb22015-11-16 23:05:21 -080065
Ray Milkey967776a2015-10-07 14:37:17 -070066import static org.hamcrest.Matchers.instanceOf;
67import static org.hamcrest.Matchers.is;
68import static org.hamcrest.Matchers.notNullValue;
69import static org.junit.Assert.assertThat;
70import static org.junit.Assert.fail;
71import static org.onosproject.net.NetTestTools.connectPoint;
72
73/**
74 * Common methods for AAA app testing.
75 */
Jonathan Hart092dfb22015-11-16 23:05:21 -080076public class AaaTestBase {
Ray Milkey967776a2015-10-07 14:37:17 -070077
Matteo Scandolo9510e5d2020-09-24 17:31:52 -070078 //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 Milkey967776a2015-10-07 14:37:17 -070083 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
Andrea Campanellad8023322021-03-22 17:40:39 +010087 static final String SESSION_ID = "of:1/1";
Ray Milkey967776a2015-10-07 14:37:17 -070088
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 Campanellac4781e62020-10-08 12:58:45 +0200122
kartikey dubeye1545422019-05-22 12:53:45 +0000123 class MockComponentContext implements ComponentContext {
124
Andrea Campanellac4781e62020-10-08 12:58:45 +0200125 @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 dubeye1545422019-05-22 12:53:45 +0000131
Andrea Campanellac4781e62020-10-08 12:58:45 +0200132 @Override
133 public Object locateService(String name) {
134 // TODO Auto-generated method stub
135 return null;
136 }
kartikey dubeye1545422019-05-22 12:53:45 +0000137
Andrea Campanellac4781e62020-10-08 12:58:45 +0200138 @Override
139 public Object locateService(String name, ServiceReference reference) {
140 // TODO Auto-generated method stub
141 return null;
142 }
kartikey dubeye1545422019-05-22 12:53:45 +0000143
Andrea Campanellac4781e62020-10-08 12:58:45 +0200144 @Override
145 public Object[] locateServices(String name) {
146 // TODO Auto-generated method stub
147 return null;
148 }
kartikey dubeye1545422019-05-22 12:53:45 +0000149
Andrea Campanellac4781e62020-10-08 12:58:45 +0200150 @Override
151 public BundleContext getBundleContext() {
152 // TODO Auto-generated method stub
153 return null;
154 }
kartikey dubeye1545422019-05-22 12:53:45 +0000155
Andrea Campanellac4781e62020-10-08 12:58:45 +0200156 @Override
157 public Bundle getUsingBundle() {
158 // TODO Auto-generated method stub
159 return null;
160 }
kartikey dubeye1545422019-05-22 12:53:45 +0000161
Andrea Campanellac4781e62020-10-08 12:58:45 +0200162 @Override
163 public ComponentInstance getComponentInstance() {
164 // TODO Auto-generated method stub
165 return null;
166 }
kartikey dubeye1545422019-05-22 12:53:45 +0000167
Andrea Campanellac4781e62020-10-08 12:58:45 +0200168 @Override
169 public void enableComponent(String name) {
170 // TODO Auto-generated method stub
171 }
kartikey dubeye1545422019-05-22 12:53:45 +0000172
Andrea Campanellac4781e62020-10-08 12:58:45 +0200173 @Override
174 public void disableComponent(String name) {
175 // TODO Auto-generated method stub
176 }
kartikey dubeye1545422019-05-22 12:53:45 +0000177
Andrea Campanellac4781e62020-10-08 12:58:45 +0200178 @Override
179 public ServiceReference getServiceReference() {
180 // TODO Auto-generated method stub
181 return null;
182 }
kartikey dubeye1545422019-05-22 12:53:45 +0000183 }
Ray Milkey967776a2015-10-07 14:37:17 -0700184
185 /**
Amit Ghoshf739be52017-09-21 15:49:37 +0100186 * 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 Campanellac4781e62020-10-08 12:58:45 +0200194
195 private class MockPort implements Port {
Amit Ghoshf739be52017-09-21 15:49:37 +0100196
197 @Override
198 public boolean isEnabled() {
199 return true;
200 }
Andrea Campanellac4781e62020-10-08 12:58:45 +0200201
Amit Ghoshf739be52017-09-21 15:49:37 +0100202 public long portSpeed() {
203 return 1000;
204 }
Andrea Campanellac4781e62020-10-08 12:58:45 +0200205
Amit Ghoshf739be52017-09-21 15:49:37 +0100206 public Element element() {
207 return null;
208 }
Andrea Campanellac4781e62020-10-08 12:58:45 +0200209
Amit Ghoshf739be52017-09-21 15:49:37 +0100210 public PortNumber number() {
211 return null;
212 }
Andrea Campanellac4781e62020-10-08 12:58:45 +0200213
Amit Ghoshf739be52017-09-21 15:49:37 +0100214 public Annotations annotations() {
215 return new MockAnnotations();
216 }
Andrea Campanellac4781e62020-10-08 12:58:45 +0200217
Amit Ghoshf739be52017-09-21 15:49:37 +0100218 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 Campanellac4781e62020-10-08 12:58:45 +0200228
Amit Ghoshf739be52017-09-21 15:49:37 +0100229 public Set<String> keys() {
230 return null;
231 }
232 }
233 }
234
235 private class MockSubscriberAndDeviceInformation extends SubscriberAndDeviceInformation {
236
Andrea Campanella7e0e7e32020-02-13 14:39:55 +0100237 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 Ghoshf739be52017-09-21 15:49:37 +0100241 Ip4Address ipAddress) {
Andrea Campanella7e0e7e32020-02-13 14:39:55 +0100242 // 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 Ghoshf739be52017-09-21 15:49:37 +0100254 this.setHardwareIdentifier(hardId);
255 this.setId(id);
256 this.setIPAddress(ipAddress);
Amit Ghoshf739be52017-09-21 15:49:37 +0100257 this.setNasPortId(nasPortId);
258 this.setCircuitId(circuitId);
Andrea Campanella7e0e7e32020-02-13 14:39:55 +0100259 this.setUniTagList(Lists.newArrayList(uniTagInfo));
Amit Ghoshf739be52017-09-21 15:49:37 +0100260 }
261 }
262
Gamze Abaka1cfdb192018-10-25 11:39:19 +0000263 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 dubeye1545422019-05-22 12:53:45 +0000276 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 Campanellac4781e62020-10-08 12:58:45 +0200295 return null;
kartikey dubeye1545422019-05-22 12:53:45 +0000296 }
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 Campanellac4781e62020-10-08 12:58:45 +0200320 return null;
kartikey dubeye1545422019-05-22 12:53:45 +0000321 }
322
Andrea Campanellac4781e62020-10-08 12:58:45 +0200323 }
324
325 final class MockMastershipService extends MastershipServiceAdapter {
326 @Override
327 public boolean isLocalMaster(DeviceId deviceId) {
328 return true;
329 }
330 }
kartikey dubeye1545422019-05-22 12:53:45 +0000331
Gamze Abaka1cfdb192018-10-25 11:39:19 +0000332 final class MockSubService implements BaseInformationService<SubscriberAndDeviceInformation> {
Andrea Campanella7e0e7e32020-02-13 14:39:55 +0100333 private final VlanId uniTagMatch = VlanId.vlanId((short) 35);
Amit Ghoshf739be52017-09-21 15:49:37 +0100334 private final VlanId clientCtag = VlanId.vlanId((short) 999);
335 private final VlanId clientStag = VlanId.vlanId((short) 111);
Andrea Campanella7e0e7e32020-02-13 14:39:55 +0100336 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 Ghoshf739be52017-09-21 15:49:37 +0100341 private final String clientNasPortId = "PON 1/1";
342 private final String clientCircuitId = "CIR-PON 1/1";
343
Andrea Campanella7e0e7e32020-02-13 14:39:55 +0100344
Amit Ghoshf739be52017-09-21 15:49:37 +0100345 MockSubscriberAndDeviceInformation sub =
Andrea Campanella7e0e7e32020-02-13 14:39:55 +0100346 new MockSubscriberAndDeviceInformation(clientNasPortId, uniTagMatch, clientCtag,
347 clientStag, dsPrio, usPrio,
348 techProfileId, dsBpId, usBpId,
349 clientNasPortId, clientCircuitId, null,
350 null);
Andrea Campanellac4781e62020-10-08 12:58:45 +0200351
Amit Ghoshf739be52017-09-21 15:49:37 +0100352 @Override
353 public SubscriberAndDeviceInformation get(String id) {
354
Andrea Campanellac4781e62020-10-08 12:58:45 +0200355 return sub;
Amit Ghoshf739be52017-09-21 15:49:37 +0100356
357 }
358
359 @Override
Ilayda Ozdemir9fdeee72021-02-26 12:24:27 +0000360 public void clearLocalData() {
361
362 }
363
364 @Override
Andrea Campanellac4781e62020-10-08 12:58:45 +0200365 public void invalidateAll() {
366 }
367
368 public void invalidateId(String id) {
369 }
370
Amit Ghoshf739be52017-09-21 15:49:37 +0100371 public SubscriberAndDeviceInformation getfromCache(String id) {
372 return null;
373 }
374 }
Andrea Campanellac4781e62020-10-08 12:58:45 +0200375
Amit Ghoshf739be52017-09-21 15:49:37 +0100376 /**
Ray Milkey967776a2015-10-07 14:37:17 -0700377 * Mocks the DefaultPacketContext.
378 */
379 final class TestPacketContext extends DefaultPacketContext {
380
Shubham Sharmacf5e5032019-11-26 11:09:21 +0000381 TestPacketContext(long time, InboundPacket inPkt,
Andrea Campanellac4781e62020-10-08 12:58:45 +0200382 OutboundPacket outPkt, boolean block) {
Ray Milkey967776a2015-10-07 14:37:17 -0700383 super(time, inPkt, outPkt, block);
384 }
385
386 @Override
387 public void send() {
388 // We don't send anything out.
389 }
390 }
391
392 /**
393 * Sends an Ethernet packet to the process method of the Packet Processor.
394 *
395 * @param reply Ethernet packet
396 */
397 void sendPacket(Ethernet reply) {
398 final ByteBuffer byteBuffer = ByteBuffer.wrap(reply.serialize());
399 InboundPacket inPacket = new DefaultInboundPacket(connectPoint("1", 1),
400 reply,
401 byteBuffer);
402
403 PacketContext context = new TestPacketContext(127L, inPacket, null, false);
404 packetProcessor.process(context);
405 }
406
407 /**
408 * Constructs an Ethernet packet containing identification payload.
409 *
410 * @return Ethernet packet
411 */
412 Ethernet constructSupplicantIdentifyPacket(StateMachine stateMachine,
Andrea Campanellac4781e62020-10-08 12:58:45 +0200413 byte type,
414 byte id,
415 Ethernet radiusChallenge)
Ray Milkey967776a2015-10-07 14:37:17 -0700416 throws Exception {
417 Ethernet eth = new Ethernet();
418 eth.setDestinationMACAddress(clientMac.toBytes());
419 eth.setSourceMACAddress(serverMac.toBytes());
420 eth.setEtherType(EthType.EtherType.EAPOL.ethType().toShort());
421 eth.setVlanID((short) 2);
422
423 String username = "testuser";
424 byte[] data = username.getBytes();
425
426
427 if (type == EAP.ATTR_MD5) {
428 String password = "testpassword";
429 EAPOL eapol = (EAPOL) radiusChallenge.getPayload();
430 EAP eap = (EAP) eapol.getPayload();
431
432 byte[] identifier = new byte[password.length() + eap.getData().length];
433
434 identifier[0] = stateMachine.challengeIdentifier();
435 System.arraycopy(password.getBytes(), 0, identifier, 1, password.length());
436 System.arraycopy(eap.getData(), 1, identifier, 1 + password.length(), 16);
437
438 MessageDigest md = MessageDigest.getInstance("MD5");
439 byte[] hash = md.digest(identifier);
440 data = new byte[17];
441 data[0] = (byte) 16;
442 System.arraycopy(hash, 0, data, 1, 16);
443 }
444 EAP eap = new EAP(EAP.RESPONSE, (byte) 1, type,
445 data);
446 eap.setIdentifier(id);
447
448 // eapol header
449 EAPOL eapol = new EAPOL();
450 eapol.setEapolType(EAPOL.EAPOL_PACKET);
451 eapol.setPacketLength(eap.getLength());
452
453 // eap part
454 eapol.setPayload(eap);
455
456 eth.setPayload(eapol);
457 eth.setPad(true);
458 return eth;
459 }
460
461 /**
462 * Constructs an Ethernet packet containing a EAPOL_START Payload.
463 *
464 * @return Ethernet packet
465 */
466 Ethernet constructSupplicantStartPacket() {
467 Ethernet eth = new Ethernet();
468 eth.setDestinationMACAddress(clientMac.toBytes());
469 eth.setSourceMACAddress(serverMac.toBytes());
470 eth.setEtherType(EthType.EtherType.EAPOL.ethType().toShort());
471 eth.setVlanID((short) 2);
472
Shubham Sharma4900ce62019-06-19 14:18:50 +0000473 EAP eap = new EAP(EAPOL.EAPOL_START, (byte) 3, EAPOL.EAPOL_START, null);
Ray Milkey967776a2015-10-07 14:37:17 -0700474
475 // eapol header
476 EAPOL eapol = new EAPOL();
477 eapol.setEapolType(EAPOL.EAPOL_START);
478 eapol.setPacketLength(eap.getLength());
479
480 // eap part
481 eapol.setPayload(eap);
482
483 eth.setPayload(eapol);
484 eth.setPad(true);
485 return eth;
486 }
487
488 /**
Shubham Sharma8d7a9822020-01-28 10:04:01 +0000489 * Constructs an Ethernet packet containing a EAPOL_ASF Payload.
490 *
491 * @return Ethernet packet
492 */
493 Ethernet constructSupplicantAsfPacket() {
494 Ethernet eth = new Ethernet();
495 eth.setDestinationMACAddress(clientMac.toBytes());
496 eth.setSourceMACAddress(serverMac.toBytes());
497 eth.setEtherType(EthType.EtherType.EAPOL.ethType().toShort());
498 eth.setVlanID((short) 2);
499
500 EAP eap = new EAP(EAPOL.EAPOL_START, (byte) 3, EAPOL.EAPOL_START, null);
501
502 // eapol header
503 EAPOL eapol = new EAPOL();
504 eapol.setEapolType(EAPOL.EAPOL_ASF);
505 eapol.setPacketLength(eap.getLength());
506
507 // eap part
508 eapol.setPayload(eap);
509
510 eth.setPayload(eapol);
511 eth.setPad(true);
512 return eth;
513 }
514
515 /**
Ray Milkey967776a2015-10-07 14:37:17 -0700516 * Checks the contents of a RADIUS packet being sent to the RADIUS server.
517 *
518 * @param radiusPacket packet to check
Andrea Campanellac4781e62020-10-08 12:58:45 +0200519 * @param code expected code
Ray Milkey967776a2015-10-07 14:37:17 -0700520 */
Jonathan Hart092dfb22015-11-16 23:05:21 -0800521 void checkRadiusPacket(AaaManager aaaManager, Ethernet radiusPacket, byte code) {
Ray Milkey967776a2015-10-07 14:37:17 -0700522
523 assertThat(radiusPacket.getSourceMAC(),
Jonathan Hart092dfb22015-11-16 23:05:21 -0800524 is(MacAddress.valueOf(aaaManager.nasMacAddress)));
Ray Milkey967776a2015-10-07 14:37:17 -0700525 assertThat(radiusPacket.getDestinationMAC(), is(serverMac));
526
527 assertThat(radiusPacket.getPayload(), instanceOf(EAPOL.class));
528 EAPOL eapol = (EAPOL) radiusPacket.getPayload();
529 assertThat(eapol, notNullValue());
530
531 assertThat(eapol.getEapolType(), is(EAPOL.EAPOL_PACKET));
532 assertThat(eapol.getPayload(), instanceOf(EAP.class));
533 EAP eap = (EAP) eapol.getPayload();
534 assertThat(eap, notNullValue());
535
536 assertThat(eap.getCode(), is(code));
537 }
Shubham Sharma2b3fb692019-12-12 10:19:10 +0000538
539 /**
540 * Constructs an Ethernet packet containing a EAPOL_LOGOFF Payload.
541 *
542 * @return Ethernet packet
543 */
544 Ethernet constructSupplicantLogoffPacket() {
545 Ethernet eth = new Ethernet();
546 eth.setDestinationMACAddress(clientMac.toBytes());
547 eth.setSourceMACAddress(serverMac.toBytes());
548 eth.setEtherType(EthType.EtherType.EAPOL.ethType().toShort());
549 eth.setVlanID((short) 2);
550
551 EAP eap = new EAP(EAPOL.EAPOL_LOGOFF, (byte) 2, EAPOL.EAPOL_LOGOFF, null);
552
553 // eapol header
554 EAPOL eapol = new EAPOL();
555 eapol.setEapolType(EAPOL.EAPOL_LOGOFF);
556 eapol.setPacketLength(eap.getLength());
557
558 // eap part
559 eapol.setPayload(eap);
560
561 eth.setPayload(eapol);
562 eth.setPad(true);
563 return eth;
564 }
565
Jonathan Hart612651f2019-11-25 09:21:43 -0800566 /**
567 * Constructs an Ethernet packet containing a RADIUS challenge
568 * packet.
569 *
570 * @param challengeCode code to use in challenge packet
571 * @param challengeType type to use in challenge packet
572 * @return Ethernet packet
573 */
574 RADIUS constructRadiusCodeAccessChallengePacket(byte challengeCode, byte challengeType,
Andrea Campanellac4781e62020-10-08 12:58:45 +0200575 byte identifier, byte[] messageAuth) {
Jonathan Hart612651f2019-11-25 09:21:43 -0800576
577 String challenge = "12345678901234567";
578
579 EAP eap = new EAP(challengeType, (byte) 4, challengeType,
Andrea Campanellac4781e62020-10-08 12:58:45 +0200580 challenge.getBytes(Charsets.US_ASCII));
Jonathan Hart612651f2019-11-25 09:21:43 -0800581 //eap.setIdentifier((byte) 4);
582 eap.setIdentifier(identifier);
583
584 RADIUS radius = new RADIUS();
585 radius.setCode(challengeCode);
586 //radius.setIdentifier((byte) 4);
587 radius.setIdentifier(identifier);
588 radius.setAttribute(RADIUSAttribute.RADIUS_ATTR_STATE,
Andrea Campanellac4781e62020-10-08 12:58:45 +0200589 challenge.getBytes(Charsets.US_ASCII));
Jonathan Hart612651f2019-11-25 09:21:43 -0800590
591 radius.setPayload(eap);
592 radius.setAttribute(RADIUSAttribute.RADIUS_ATTR_EAP_MESSAGE, eap.serialize());
593 radius.setAttribute(RADIUSAttribute.RADIUS_ATTR_MESSAGE_AUTH, messageAuth);
594 return radius;
595 }
Ray Milkey967776a2015-10-07 14:37:17 -0700596}