blob: 09f5e975fef6f26912ed5758092f7b5e1fb17d29 [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
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 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
Andrea Campanellac4781e62020-10-08 12:58:45 +0200360 public void invalidateAll() {
361 }
362
363 public void invalidateId(String id) {
364 }
365
Amit Ghoshf739be52017-09-21 15:49:37 +0100366 public SubscriberAndDeviceInformation getfromCache(String id) {
367 return null;
368 }
369 }
Andrea Campanellac4781e62020-10-08 12:58:45 +0200370
Amit Ghoshf739be52017-09-21 15:49:37 +0100371 /**
Ray Milkey967776a2015-10-07 14:37:17 -0700372 * Mocks the DefaultPacketContext.
373 */
374 final class TestPacketContext extends DefaultPacketContext {
375
Shubham Sharmacf5e5032019-11-26 11:09:21 +0000376 TestPacketContext(long time, InboundPacket inPkt,
Andrea Campanellac4781e62020-10-08 12:58:45 +0200377 OutboundPacket outPkt, boolean block) {
Ray Milkey967776a2015-10-07 14:37:17 -0700378 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 Campanellac4781e62020-10-08 12:58:45 +0200408 byte type,
409 byte id,
410 Ethernet radiusChallenge)
Ray Milkey967776a2015-10-07 14:37:17 -0700411 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 Sharma4900ce62019-06-19 14:18:50 +0000468 EAP eap = new EAP(EAPOL.EAPOL_START, (byte) 3, EAPOL.EAPOL_START, null);
Ray Milkey967776a2015-10-07 14:37:17 -0700469
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 Sharma8d7a9822020-01-28 10:04:01 +0000484 * 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 Milkey967776a2015-10-07 14:37:17 -0700511 * Checks the contents of a RADIUS packet being sent to the RADIUS server.
512 *
513 * @param radiusPacket packet to check
Andrea Campanellac4781e62020-10-08 12:58:45 +0200514 * @param code expected code
Ray Milkey967776a2015-10-07 14:37:17 -0700515 */
Jonathan Hart092dfb22015-11-16 23:05:21 -0800516 void checkRadiusPacket(AaaManager aaaManager, Ethernet radiusPacket, byte code) {
Ray Milkey967776a2015-10-07 14:37:17 -0700517
518 assertThat(radiusPacket.getSourceMAC(),
Jonathan Hart092dfb22015-11-16 23:05:21 -0800519 is(MacAddress.valueOf(aaaManager.nasMacAddress)));
Ray Milkey967776a2015-10-07 14:37:17 -0700520 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 Sharma2b3fb692019-12-12 10:19:10 +0000533
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 Hart612651f2019-11-25 09:21:43 -0800561 /**
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 Campanellac4781e62020-10-08 12:58:45 +0200570 byte identifier, byte[] messageAuth) {
Jonathan Hart612651f2019-11-25 09:21:43 -0800571
572 String challenge = "12345678901234567";
573
574 EAP eap = new EAP(challengeType, (byte) 4, challengeType,
Andrea Campanellac4781e62020-10-08 12:58:45 +0200575 challenge.getBytes(Charsets.US_ASCII));
Jonathan Hart612651f2019-11-25 09:21:43 -0800576 //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 Campanellac4781e62020-10-08 12:58:45 +0200584 challenge.getBytes(Charsets.US_ASCII));
Jonathan Hart612651f2019-11-25 09:21:43 -0800585
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 Milkey967776a2015-10-07 14:37:17 -0700591}