Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | */ |
| 16 | package org.onosproject.aaa; |
| 17 | |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 18 | import org.onlab.packet.BasePacket; |
| 19 | import org.onlab.packet.EAP; |
| 20 | import org.onlab.packet.EAPOL; |
| 21 | import org.onlab.packet.EthType; |
| 22 | import org.onlab.packet.Ethernet; |
| 23 | import org.onlab.packet.MacAddress; |
| 24 | import org.onosproject.net.packet.DefaultInboundPacket; |
| 25 | import org.onosproject.net.packet.DefaultPacketContext; |
| 26 | import org.onosproject.net.packet.InboundPacket; |
| 27 | import org.onosproject.net.packet.OutboundPacket; |
| 28 | import org.onosproject.net.packet.PacketContext; |
| 29 | import org.onosproject.net.packet.PacketProcessor; |
| 30 | import org.onosproject.net.packet.PacketServiceAdapter; |
| 31 | |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 32 | import java.nio.ByteBuffer; |
| 33 | import java.security.MessageDigest; |
| 34 | import java.util.LinkedList; |
| 35 | import java.util.List; |
| 36 | |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 37 | import static org.hamcrest.Matchers.instanceOf; |
| 38 | import static org.hamcrest.Matchers.is; |
| 39 | import static org.hamcrest.Matchers.notNullValue; |
| 40 | import static org.junit.Assert.assertThat; |
| 41 | import static org.junit.Assert.fail; |
| 42 | import static org.onosproject.net.NetTestTools.connectPoint; |
| 43 | |
| 44 | /** |
| 45 | * Common methods for AAA app testing. |
| 46 | */ |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 47 | public class AaaTestBase { |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 48 | |
| 49 | MacAddress clientMac = MacAddress.valueOf("1a:1a:1a:1a:1a:1a"); |
| 50 | MacAddress serverMac = MacAddress.valueOf("2a:2a:2a:2a:2a:2a"); |
| 51 | |
| 52 | // Our session id will be the device ID ("of:1") with the port ("1") concatenated |
| 53 | static final String SESSION_ID = "of:11"; |
| 54 | |
| 55 | List<BasePacket> savedPackets = new LinkedList<>(); |
| 56 | PacketProcessor packetProcessor; |
| 57 | |
| 58 | /** |
| 59 | * Saves the given packet onto the saved packets list. |
| 60 | * |
| 61 | * @param packet packet to save |
| 62 | */ |
| 63 | void savePacket(BasePacket packet) { |
| 64 | savedPackets.add(packet); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Keeps a reference to the PacketProcessor and saves the OutboundPackets. |
| 69 | */ |
| 70 | class MockPacketService extends PacketServiceAdapter { |
| 71 | |
| 72 | @Override |
| 73 | public void addProcessor(PacketProcessor processor, int priority) { |
| 74 | packetProcessor = processor; |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public void emit(OutboundPacket packet) { |
| 79 | try { |
| 80 | Ethernet eth = Ethernet.deserializer().deserialize(packet.data().array(), |
| 81 | 0, packet.data().array().length); |
| 82 | savePacket(eth); |
| 83 | } catch (Exception e) { |
| 84 | fail(e.getMessage()); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Mocks the DefaultPacketContext. |
| 91 | */ |
| 92 | final class TestPacketContext extends DefaultPacketContext { |
| 93 | |
| 94 | private TestPacketContext(long time, InboundPacket inPkt, |
| 95 | OutboundPacket outPkt, boolean block) { |
| 96 | super(time, inPkt, outPkt, block); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public void send() { |
| 101 | // We don't send anything out. |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Sends an Ethernet packet to the process method of the Packet Processor. |
| 107 | * |
| 108 | * @param reply Ethernet packet |
| 109 | */ |
| 110 | void sendPacket(Ethernet reply) { |
| 111 | final ByteBuffer byteBuffer = ByteBuffer.wrap(reply.serialize()); |
| 112 | InboundPacket inPacket = new DefaultInboundPacket(connectPoint("1", 1), |
| 113 | reply, |
| 114 | byteBuffer); |
| 115 | |
| 116 | PacketContext context = new TestPacketContext(127L, inPacket, null, false); |
| 117 | packetProcessor.process(context); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Constructs an Ethernet packet containing identification payload. |
| 122 | * |
| 123 | * @return Ethernet packet |
| 124 | */ |
| 125 | Ethernet constructSupplicantIdentifyPacket(StateMachine stateMachine, |
| 126 | byte type, |
| 127 | byte id, |
| 128 | Ethernet radiusChallenge) |
| 129 | throws Exception { |
| 130 | Ethernet eth = new Ethernet(); |
| 131 | eth.setDestinationMACAddress(clientMac.toBytes()); |
| 132 | eth.setSourceMACAddress(serverMac.toBytes()); |
| 133 | eth.setEtherType(EthType.EtherType.EAPOL.ethType().toShort()); |
| 134 | eth.setVlanID((short) 2); |
| 135 | |
| 136 | String username = "testuser"; |
| 137 | byte[] data = username.getBytes(); |
| 138 | |
| 139 | |
| 140 | if (type == EAP.ATTR_MD5) { |
| 141 | String password = "testpassword"; |
| 142 | EAPOL eapol = (EAPOL) radiusChallenge.getPayload(); |
| 143 | EAP eap = (EAP) eapol.getPayload(); |
| 144 | |
| 145 | byte[] identifier = new byte[password.length() + eap.getData().length]; |
| 146 | |
| 147 | identifier[0] = stateMachine.challengeIdentifier(); |
| 148 | System.arraycopy(password.getBytes(), 0, identifier, 1, password.length()); |
| 149 | System.arraycopy(eap.getData(), 1, identifier, 1 + password.length(), 16); |
| 150 | |
| 151 | MessageDigest md = MessageDigest.getInstance("MD5"); |
| 152 | byte[] hash = md.digest(identifier); |
| 153 | data = new byte[17]; |
| 154 | data[0] = (byte) 16; |
| 155 | System.arraycopy(hash, 0, data, 1, 16); |
| 156 | } |
| 157 | EAP eap = new EAP(EAP.RESPONSE, (byte) 1, type, |
| 158 | data); |
| 159 | eap.setIdentifier(id); |
| 160 | |
| 161 | // eapol header |
| 162 | EAPOL eapol = new EAPOL(); |
| 163 | eapol.setEapolType(EAPOL.EAPOL_PACKET); |
| 164 | eapol.setPacketLength(eap.getLength()); |
| 165 | |
| 166 | // eap part |
| 167 | eapol.setPayload(eap); |
| 168 | |
| 169 | eth.setPayload(eapol); |
| 170 | eth.setPad(true); |
| 171 | return eth; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Constructs an Ethernet packet containing a EAPOL_START Payload. |
| 176 | * |
| 177 | * @return Ethernet packet |
| 178 | */ |
| 179 | Ethernet constructSupplicantStartPacket() { |
| 180 | Ethernet eth = new Ethernet(); |
| 181 | eth.setDestinationMACAddress(clientMac.toBytes()); |
| 182 | eth.setSourceMACAddress(serverMac.toBytes()); |
| 183 | eth.setEtherType(EthType.EtherType.EAPOL.ethType().toShort()); |
| 184 | eth.setVlanID((short) 2); |
| 185 | |
| 186 | EAP eap = new EAP(EAPOL.EAPOL_START, (byte) 2, EAPOL.EAPOL_START, null); |
| 187 | |
| 188 | // eapol header |
| 189 | EAPOL eapol = new EAPOL(); |
| 190 | eapol.setEapolType(EAPOL.EAPOL_START); |
| 191 | eapol.setPacketLength(eap.getLength()); |
| 192 | |
| 193 | // eap part |
| 194 | eapol.setPayload(eap); |
| 195 | |
| 196 | eth.setPayload(eapol); |
| 197 | eth.setPad(true); |
| 198 | return eth; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Checks the contents of a RADIUS packet being sent to the RADIUS server. |
| 203 | * |
| 204 | * @param radiusPacket packet to check |
| 205 | * @param code expected code |
| 206 | */ |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 207 | void checkRadiusPacket(AaaManager aaaManager, Ethernet radiusPacket, byte code) { |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 208 | |
| 209 | assertThat(radiusPacket.getSourceMAC(), |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 210 | is(MacAddress.valueOf(aaaManager.nasMacAddress))); |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 211 | assertThat(radiusPacket.getDestinationMAC(), is(serverMac)); |
| 212 | |
| 213 | assertThat(radiusPacket.getPayload(), instanceOf(EAPOL.class)); |
| 214 | EAPOL eapol = (EAPOL) radiusPacket.getPayload(); |
| 215 | assertThat(eapol, notNullValue()); |
| 216 | |
| 217 | assertThat(eapol.getEapolType(), is(EAPOL.EAPOL_PACKET)); |
| 218 | assertThat(eapol.getPayload(), instanceOf(EAP.class)); |
| 219 | EAP eap = (EAP) eapol.getPayload(); |
| 220 | assertThat(eap, notNullValue()); |
| 221 | |
| 222 | assertThat(eap.getCode(), is(code)); |
| 223 | } |
| 224 | } |