blob: 43f9bd1258b5f935d7a0b2d12ad1888083cfa4d6 [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
pier99c6d1d2020-02-13 14:39:55 +010018import com.google.common.collect.Lists;
Ray Milkey967776a2015-10-07 14:37:17 -070019import org.onlab.packet.BasePacket;
20import org.onlab.packet.EAP;
21import org.onlab.packet.EAPOL;
22import org.onlab.packet.EthType;
23import org.onlab.packet.Ethernet;
Amit Ghoshf739be52017-09-21 15:49:37 +010024import org.onlab.packet.Ip4Address;
Ray Milkey967776a2015-10-07 14:37:17 -070025import org.onlab.packet.MacAddress;
Amit Ghoshf739be52017-09-21 15:49:37 +010026import org.onlab.packet.VlanId;
kartikey dubeye1545422019-05-22 12:53:45 +000027import org.onosproject.cfg.ComponentConfigService;
28import org.onosproject.cfg.ConfigProperty;
Amit Ghoshf739be52017-09-21 15:49:37 +010029import org.onosproject.net.Annotations;
30import org.onosproject.net.device.DeviceServiceAdapter;
31import org.onosproject.net.ConnectPoint;
Sonal Kasliwal7eef29f2020-02-12 12:23:22 +000032import org.onosproject.net.DeviceId;
Amit Ghoshf739be52017-09-21 15:49:37 +010033import org.onosproject.net.Element;
34import org.onosproject.net.Port;
35import org.onosproject.net.PortNumber;
Ray Milkey967776a2015-10-07 14:37:17 -070036import org.onosproject.net.packet.DefaultInboundPacket;
37import org.onosproject.net.packet.DefaultPacketContext;
38import org.onosproject.net.packet.InboundPacket;
39import org.onosproject.net.packet.OutboundPacket;
40import org.onosproject.net.packet.PacketContext;
41import org.onosproject.net.packet.PacketProcessor;
42import org.onosproject.net.packet.PacketServiceAdapter;
43
Gamze Abaka1cfdb192018-10-25 11:39:19 +000044import org.opencord.sadis.BandwidthProfileInformation;
45import org.opencord.sadis.BaseInformationService;
46import org.opencord.sadis.SadisService;
Amit Ghoshf739be52017-09-21 15:49:37 +010047import org.opencord.sadis.SubscriberAndDeviceInformation;
pier99c6d1d2020-02-13 14:39:55 +010048import org.opencord.sadis.UniTagInformation;
kartikey dubeye1545422019-05-22 12:53:45 +000049import org.osgi.framework.Bundle;
50import org.osgi.framework.BundleContext;
51import org.osgi.framework.ServiceReference;
52import org.osgi.service.component.ComponentContext;
53import org.osgi.service.component.ComponentInstance;
Amit Ghoshf739be52017-09-21 15:49:37 +010054
Jonathan Hart092dfb22015-11-16 23:05:21 -080055import java.nio.ByteBuffer;
56import java.security.MessageDigest;
kartikey dubeye1545422019-05-22 12:53:45 +000057import java.util.Dictionary;
58import java.util.Hashtable;
Jonathan Hart092dfb22015-11-16 23:05:21 -080059import java.util.LinkedList;
60import java.util.List;
Amit Ghoshf739be52017-09-21 15:49:37 +010061import java.util.Set;
Jonathan Hart092dfb22015-11-16 23:05:21 -080062
Ray Milkey967776a2015-10-07 14:37:17 -070063import static org.hamcrest.Matchers.instanceOf;
64import static org.hamcrest.Matchers.is;
65import static org.hamcrest.Matchers.notNullValue;
66import static org.junit.Assert.assertThat;
67import static org.junit.Assert.fail;
68import static org.onosproject.net.NetTestTools.connectPoint;
69
70/**
71 * Common methods for AAA app testing.
72 */
Jonathan Hart092dfb22015-11-16 23:05:21 -080073public class AaaTestBase {
Ray Milkey967776a2015-10-07 14:37:17 -070074
75 MacAddress clientMac = MacAddress.valueOf("1a:1a:1a:1a:1a:1a");
76 MacAddress serverMac = MacAddress.valueOf("2a:2a:2a:2a:2a:2a");
77
78 // Our session id will be the device ID ("of:1") with the port ("1") concatenated
79 static final String SESSION_ID = "of:11";
Sonal Kasliwal7eef29f2020-02-12 12:23:22 +000080 // Device configuration
81 static final DeviceId DEVICE_ID = DeviceId.deviceId("of:1");
82 // Source ip address of a device.
83 static final Ip4Address SOURCE_IP = Ip4Address.valueOf("10.177.125.4");
84 // Port number
85 static final PortNumber PORT_A = PortNumber.portNumber(1);
Ray Milkey967776a2015-10-07 14:37:17 -070086 List<BasePacket> savedPackets = new LinkedList<>();
87 PacketProcessor packetProcessor;
88
89 /**
90 * Saves the given packet onto the saved packets list.
91 *
92 * @param packet packet to save
93 */
94 void savePacket(BasePacket packet) {
95 savedPackets.add(packet);
96 }
97
98 /**
99 * Keeps a reference to the PacketProcessor and saves the OutboundPackets.
100 */
101 class MockPacketService extends PacketServiceAdapter {
102
103 @Override
104 public void addProcessor(PacketProcessor processor, int priority) {
105 packetProcessor = processor;
106 }
107
108 @Override
109 public void emit(OutboundPacket packet) {
110 try {
111 Ethernet eth = Ethernet.deserializer().deserialize(packet.data().array(),
112 0, packet.data().array().length);
113 savePacket(eth);
114 } catch (Exception e) {
115 fail(e.getMessage());
116 }
117 }
118 }
kartikey dubeye1545422019-05-22 12:53:45 +0000119 class MockComponentContext implements ComponentContext {
120
121 @Override
122 public Dictionary<String, Object> getProperties() {
123 Dictionary<String, Object> cfgDict = new Hashtable<String, Object>();
124 cfgDict.put("statisticsGenerationEvent", 20);
125 return cfgDict;
126 }
127
128 @Override
129 public Object locateService(String name) {
130 // TODO Auto-generated method stub
131 return null;
132 }
133
134 @Override
135 public Object locateService(String name, ServiceReference reference) {
136 // TODO Auto-generated method stub
137 return null;
138 }
139
140 @Override
141 public Object[] locateServices(String name) {
142 // TODO Auto-generated method stub
143 return null;
144 }
145
146 @Override
147 public BundleContext getBundleContext() {
148 // TODO Auto-generated method stub
149 return null;
150 }
151
152 @Override
153 public Bundle getUsingBundle() {
154 // TODO Auto-generated method stub
155 return null;
156 }
157
158 @Override
159 public ComponentInstance getComponentInstance() {
160 // TODO Auto-generated method stub
161 return null;
162 }
163
164 @Override
165 public void enableComponent(String name) {
166 // TODO Auto-generated method stub
167 }
168
169 @Override
170 public void disableComponent(String name) {
171 // TODO Auto-generated method stub
172 }
173
174 @Override
175 public ServiceReference getServiceReference() {
176 // TODO Auto-generated method stub
177 return null;
178 }
179 }
Ray Milkey967776a2015-10-07 14:37:17 -0700180
181 /**
Amit Ghoshf739be52017-09-21 15:49:37 +0100182 * Mocks the DeviceService.
183 */
184 final class TestDeviceService extends DeviceServiceAdapter {
185 @Override
186 public Port getPort(ConnectPoint cp) {
187 return new MockPort();
188 }
189 }
190 private class MockPort implements Port {
191
192 @Override
193 public boolean isEnabled() {
194 return true;
195 }
196 public long portSpeed() {
197 return 1000;
198 }
199 public Element element() {
200 return null;
201 }
202 public PortNumber number() {
203 return null;
204 }
205 public Annotations annotations() {
206 return new MockAnnotations();
207 }
208 public Type type() {
209 return Port.Type.FIBER;
210 }
211
212 private class MockAnnotations implements Annotations {
213
214 @Override
215 public String value(String val) {
216 return "PON 1/1";
217 }
218 public Set<String> keys() {
219 return null;
220 }
221 }
222 }
223
224 private class MockSubscriberAndDeviceInformation extends SubscriberAndDeviceInformation {
225
pier99c6d1d2020-02-13 14:39:55 +0100226 MockSubscriberAndDeviceInformation(String id, VlanId uniTagMatch, VlanId ctag,
227 VlanId stag, int dsPonPrio, int upPonPrio,
228 int techProfileId, String dsBpId, String usBpId,
229 String nasPortId, String circuitId, MacAddress hardId,
Amit Ghoshf739be52017-09-21 15:49:37 +0100230 Ip4Address ipAddress) {
pier99c6d1d2020-02-13 14:39:55 +0100231 // Builds UniTagInformation
232 UniTagInformation.Builder tagInfoBuilder = new UniTagInformation.Builder();
233 UniTagInformation uniTagInfo = tagInfoBuilder.setUniTagMatch(uniTagMatch)
234 .setPonCTag(ctag)
235 .setPonSTag(stag)
236 .setDsPonCTagPriority(dsPonPrio)
237 .setUsPonSTagPriority(upPonPrio)
238 .setTechnologyProfileId(techProfileId)
239 .setDownstreamBandwidthProfile(dsBpId)
240 .setUpstreamBandwidthProfile(usBpId)
241 .build();
242
Amit Ghoshf739be52017-09-21 15:49:37 +0100243 this.setHardwareIdentifier(hardId);
244 this.setId(id);
245 this.setIPAddress(ipAddress);
Amit Ghoshf739be52017-09-21 15:49:37 +0100246 this.setNasPortId(nasPortId);
247 this.setCircuitId(circuitId);
pier99c6d1d2020-02-13 14:39:55 +0100248 this.setUniTagList(Lists.newArrayList(uniTagInfo));
Amit Ghoshf739be52017-09-21 15:49:37 +0100249 }
250 }
251
Gamze Abaka1cfdb192018-10-25 11:39:19 +0000252 final class MockSadisService implements SadisService {
253
254 @Override
255 public BaseInformationService<SubscriberAndDeviceInformation> getSubscriberInfoService() {
256 return new MockSubService();
257 }
258
259 @Override
260 public BaseInformationService<BandwidthProfileInformation> getBandwidthProfileService() {
261 return null;
262 }
263 }
264
kartikey dubeye1545422019-05-22 12:53:45 +0000265 final class MockCfgService implements ComponentConfigService {
266 @Override
267 public Set<String> getComponentNames() {
268 // TODO Auto-generated method stub
269 return null;
270 }
271
272 @Override
273 public void registerProperties(Class<?> componentClass) {
274 // TODO Auto-generated method stub
275 }
276
277 @Override
278 public void unregisterProperties(Class<?> componentClass, boolean clear) {
279 // TODO Auto-generated method stub
280 }
281
282 @Override
283 public Set<ConfigProperty> getProperties(String componentName) {
284 return null;
285 }
286
287 @Override
288 public void setProperty(String componentName, String name, String value) {
289 // TODO Auto-generated method stub
290 }
291
292 @Override
293 public void preSetProperty(String componentName, String name, String value) {
294 // TODO Auto-generated method stub
295 }
296
297 @Override
298 public void preSetProperty(String componentName, String name, String value, boolean override) {
299 // TODO Auto-generated method stub
300 }
301
302 @Override
303 public void unsetProperty(String componentName, String name) {
304 // TODO Auto-generated method stub
305 }
306
307 @Override
308 public ConfigProperty getProperty(String componentName, String attribute) {
309 return null;
310 }
311
312}
313
Gamze Abaka1cfdb192018-10-25 11:39:19 +0000314 final class MockSubService implements BaseInformationService<SubscriberAndDeviceInformation> {
pier99c6d1d2020-02-13 14:39:55 +0100315 private final VlanId uniTagMatch = VlanId.vlanId((short) 35);
Amit Ghoshf739be52017-09-21 15:49:37 +0100316 private final VlanId clientCtag = VlanId.vlanId((short) 999);
317 private final VlanId clientStag = VlanId.vlanId((short) 111);
pier99c6d1d2020-02-13 14:39:55 +0100318 private final int dsPrio = 0;
319 private final int usPrio = 0;
320 private final int techProfileId = 64;
321 private final String usBpId = "HSIA-US";
322 private final String dsBpId = "HSIA-DS";
Amit Ghoshf739be52017-09-21 15:49:37 +0100323 private final String clientNasPortId = "PON 1/1";
324 private final String clientCircuitId = "CIR-PON 1/1";
325
pier99c6d1d2020-02-13 14:39:55 +0100326
Amit Ghoshf739be52017-09-21 15:49:37 +0100327 MockSubscriberAndDeviceInformation sub =
pier99c6d1d2020-02-13 14:39:55 +0100328 new MockSubscriberAndDeviceInformation(clientNasPortId, uniTagMatch, clientCtag,
329 clientStag, dsPrio, usPrio,
330 techProfileId, dsBpId, usBpId,
331 clientNasPortId, clientCircuitId, null,
332 null);
Amit Ghoshf739be52017-09-21 15:49:37 +0100333 @Override
334 public SubscriberAndDeviceInformation get(String id) {
335
336 return sub;
337
338 }
339
340 @Override
341 public void invalidateAll() {}
342 public void invalidateId(String id) {}
343 public SubscriberAndDeviceInformation getfromCache(String id) {
344 return null;
345 }
346 }
347 /**
Ray Milkey967776a2015-10-07 14:37:17 -0700348 * Mocks the DefaultPacketContext.
349 */
350 final class TestPacketContext extends DefaultPacketContext {
351
Shubham Sharma1ad16632019-11-26 11:09:21 +0000352 TestPacketContext(long time, InboundPacket inPkt,
Ray Milkey967776a2015-10-07 14:37:17 -0700353 OutboundPacket outPkt, boolean block) {
354 super(time, inPkt, outPkt, block);
355 }
356
357 @Override
358 public void send() {
359 // We don't send anything out.
360 }
361 }
362
363 /**
364 * Sends an Ethernet packet to the process method of the Packet Processor.
365 *
366 * @param reply Ethernet packet
367 */
368 void sendPacket(Ethernet reply) {
369 final ByteBuffer byteBuffer = ByteBuffer.wrap(reply.serialize());
370 InboundPacket inPacket = new DefaultInboundPacket(connectPoint("1", 1),
371 reply,
372 byteBuffer);
373
374 PacketContext context = new TestPacketContext(127L, inPacket, null, false);
375 packetProcessor.process(context);
376 }
377
378 /**
379 * Constructs an Ethernet packet containing identification payload.
380 *
381 * @return Ethernet packet
382 */
383 Ethernet constructSupplicantIdentifyPacket(StateMachine stateMachine,
384 byte type,
385 byte id,
386 Ethernet radiusChallenge)
387 throws Exception {
388 Ethernet eth = new Ethernet();
389 eth.setDestinationMACAddress(clientMac.toBytes());
390 eth.setSourceMACAddress(serverMac.toBytes());
391 eth.setEtherType(EthType.EtherType.EAPOL.ethType().toShort());
392 eth.setVlanID((short) 2);
393
394 String username = "testuser";
395 byte[] data = username.getBytes();
396
397
398 if (type == EAP.ATTR_MD5) {
399 String password = "testpassword";
400 EAPOL eapol = (EAPOL) radiusChallenge.getPayload();
401 EAP eap = (EAP) eapol.getPayload();
402
403 byte[] identifier = new byte[password.length() + eap.getData().length];
404
405 identifier[0] = stateMachine.challengeIdentifier();
406 System.arraycopy(password.getBytes(), 0, identifier, 1, password.length());
407 System.arraycopy(eap.getData(), 1, identifier, 1 + password.length(), 16);
408
409 MessageDigest md = MessageDigest.getInstance("MD5");
410 byte[] hash = md.digest(identifier);
411 data = new byte[17];
412 data[0] = (byte) 16;
413 System.arraycopy(hash, 0, data, 1, 16);
414 }
415 EAP eap = new EAP(EAP.RESPONSE, (byte) 1, type,
416 data);
417 eap.setIdentifier(id);
418
419 // eapol header
420 EAPOL eapol = new EAPOL();
421 eapol.setEapolType(EAPOL.EAPOL_PACKET);
422 eapol.setPacketLength(eap.getLength());
423
424 // eap part
425 eapol.setPayload(eap);
426
427 eth.setPayload(eapol);
428 eth.setPad(true);
429 return eth;
430 }
431
432 /**
433 * Constructs an Ethernet packet containing a EAPOL_START Payload.
434 *
435 * @return Ethernet packet
436 */
437 Ethernet constructSupplicantStartPacket() {
438 Ethernet eth = new Ethernet();
439 eth.setDestinationMACAddress(clientMac.toBytes());
440 eth.setSourceMACAddress(serverMac.toBytes());
441 eth.setEtherType(EthType.EtherType.EAPOL.ethType().toShort());
442 eth.setVlanID((short) 2);
443
Shubham Sharma048cc262019-06-19 14:18:50 +0000444 EAP eap = new EAP(EAPOL.EAPOL_START, (byte) 3, EAPOL.EAPOL_START, null);
Ray Milkey967776a2015-10-07 14:37:17 -0700445
446 // eapol header
447 EAPOL eapol = new EAPOL();
448 eapol.setEapolType(EAPOL.EAPOL_START);
449 eapol.setPacketLength(eap.getLength());
450
451 // eap part
452 eapol.setPayload(eap);
453
454 eth.setPayload(eapol);
455 eth.setPad(true);
456 return eth;
457 }
458
459 /**
Shubham Sharma001ae112020-01-28 10:04:01 +0000460 * Constructs an Ethernet packet containing a EAPOL_ASF Payload.
461 *
462 * @return Ethernet packet
463 */
464 Ethernet constructSupplicantAsfPacket() {
465 Ethernet eth = new Ethernet();
466 eth.setDestinationMACAddress(clientMac.toBytes());
467 eth.setSourceMACAddress(serverMac.toBytes());
468 eth.setEtherType(EthType.EtherType.EAPOL.ethType().toShort());
469 eth.setVlanID((short) 2);
470
471 EAP eap = new EAP(EAPOL.EAPOL_START, (byte) 3, EAPOL.EAPOL_START, null);
472
473 // eapol header
474 EAPOL eapol = new EAPOL();
475 eapol.setEapolType(EAPOL.EAPOL_ASF);
476 eapol.setPacketLength(eap.getLength());
477
478 // eap part
479 eapol.setPayload(eap);
480
481 eth.setPayload(eapol);
482 eth.setPad(true);
483 return eth;
484 }
485
486 /**
Ray Milkey967776a2015-10-07 14:37:17 -0700487 * Checks the contents of a RADIUS packet being sent to the RADIUS server.
488 *
489 * @param radiusPacket packet to check
490 * @param code expected code
491 */
Jonathan Hart092dfb22015-11-16 23:05:21 -0800492 void checkRadiusPacket(AaaManager aaaManager, Ethernet radiusPacket, byte code) {
Ray Milkey967776a2015-10-07 14:37:17 -0700493
494 assertThat(radiusPacket.getSourceMAC(),
Jonathan Hart092dfb22015-11-16 23:05:21 -0800495 is(MacAddress.valueOf(aaaManager.nasMacAddress)));
Ray Milkey967776a2015-10-07 14:37:17 -0700496 assertThat(radiusPacket.getDestinationMAC(), is(serverMac));
497
498 assertThat(radiusPacket.getPayload(), instanceOf(EAPOL.class));
499 EAPOL eapol = (EAPOL) radiusPacket.getPayload();
500 assertThat(eapol, notNullValue());
501
502 assertThat(eapol.getEapolType(), is(EAPOL.EAPOL_PACKET));
503 assertThat(eapol.getPayload(), instanceOf(EAP.class));
504 EAP eap = (EAP) eapol.getPayload();
505 assertThat(eap, notNullValue());
506
507 assertThat(eap.getCode(), is(code));
508 }
Shubham Sharmac7aa6202019-12-12 10:19:10 +0000509
510 /**
511 * Constructs an Ethernet packet containing a EAPOL_LOGOFF Payload.
512 *
513 * @return Ethernet packet
514 */
515 Ethernet constructSupplicantLogoffPacket() {
516 Ethernet eth = new Ethernet();
517 eth.setDestinationMACAddress(clientMac.toBytes());
518 eth.setSourceMACAddress(serverMac.toBytes());
519 eth.setEtherType(EthType.EtherType.EAPOL.ethType().toShort());
520 eth.setVlanID((short) 2);
521
522 EAP eap = new EAP(EAPOL.EAPOL_LOGOFF, (byte) 2, EAPOL.EAPOL_LOGOFF, null);
523
524 // eapol header
525 EAPOL eapol = new EAPOL();
526 eapol.setEapolType(EAPOL.EAPOL_LOGOFF);
527 eapol.setPacketLength(eap.getLength());
528
529 // eap part
530 eapol.setPayload(eap);
531
532 eth.setPayload(eapol);
533 eth.setPad(true);
534 return eth;
535 }
536
Ray Milkey967776a2015-10-07 14:37:17 -0700537}