blob: b076a2e3ef00e64c6f472d1382c62b041f356a90 [file] [log] [blame]
Ray Milkey4ed93692015-10-07 14:37:17 -07001/*
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 */
16package org.onosproject.aaa;
17
Ray Milkey4ed93692015-10-07 14:37:17 -070018import org.onlab.packet.BasePacket;
19import org.onlab.packet.EAP;
20import org.onlab.packet.EAPOL;
21import org.onlab.packet.EthType;
22import org.onlab.packet.Ethernet;
23import org.onlab.packet.MacAddress;
24import org.onosproject.net.packet.DefaultInboundPacket;
25import org.onosproject.net.packet.DefaultPacketContext;
26import org.onosproject.net.packet.InboundPacket;
27import org.onosproject.net.packet.OutboundPacket;
28import org.onosproject.net.packet.PacketContext;
29import org.onosproject.net.packet.PacketProcessor;
30import org.onosproject.net.packet.PacketServiceAdapter;
31
Jonathan Hartb92cc512015-11-16 23:05:21 -080032import java.nio.ByteBuffer;
33import java.security.MessageDigest;
34import java.util.LinkedList;
35import java.util.List;
36
Ray Milkey4ed93692015-10-07 14:37:17 -070037import static org.hamcrest.Matchers.instanceOf;
38import static org.hamcrest.Matchers.is;
39import static org.hamcrest.Matchers.notNullValue;
40import static org.junit.Assert.assertThat;
41import static org.junit.Assert.fail;
42import static org.onosproject.net.NetTestTools.connectPoint;
43
44/**
45 * Common methods for AAA app testing.
46 */
Jonathan Hartb92cc512015-11-16 23:05:21 -080047public class AaaTestBase {
Ray Milkey4ed93692015-10-07 14:37:17 -070048
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 Hartb92cc512015-11-16 23:05:21 -0800207 void checkRadiusPacket(AaaManager aaaManager, Ethernet radiusPacket, byte code) {
Ray Milkey4ed93692015-10-07 14:37:17 -0700208
209 assertThat(radiusPacket.getSourceMAC(),
Jonathan Hartb92cc512015-11-16 23:05:21 -0800210 is(MacAddress.valueOf(aaaManager.nasMacAddress)));
Ray Milkey4ed93692015-10-07 14:37:17 -0700211 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}