Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 4e33be2 | 2017-08-03 22:45:46 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 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 | */ |
alshabib | 6d52745 | 2016-06-01 18:00:47 -0700 | [diff] [blame] | 16 | package org.opencord.aaa; |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 17 | |
| 18 | import org.junit.Before; |
| 19 | import org.junit.Ignore; |
| 20 | import org.junit.Test; |
| 21 | import org.onlab.packet.EAP; |
| 22 | import org.onlab.packet.EAPOL; |
| 23 | import org.onlab.packet.Ethernet; |
| 24 | import org.onosproject.core.CoreServiceAdapter; |
| 25 | import org.onosproject.net.config.Config; |
| 26 | import org.onosproject.net.config.NetworkConfigRegistryAdapter; |
| 27 | |
| 28 | import static org.hamcrest.Matchers.is; |
| 29 | import static org.hamcrest.Matchers.notNullValue; |
| 30 | import static org.junit.Assert.assertThat; |
| 31 | |
| 32 | /** |
| 33 | * Set of tests of the ONOS application component. These use an existing RADIUS |
| 34 | * server and sends live packets over the network to it. |
| 35 | */ |
| 36 | @Ignore ("This should not be run as part of the standard build") |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 37 | public class AaaIntegrationTest extends AaaTestBase { |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 38 | |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 39 | private AaaManager aaa; |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Mocks the network config registry. |
| 43 | */ |
| 44 | @SuppressWarnings("unchecked") |
| 45 | static final class TestNetworkConfigRegistry |
| 46 | extends NetworkConfigRegistryAdapter { |
| 47 | @Override |
| 48 | public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) { |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 49 | return (C) new AaaConfig(); |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Sets up the services required by the AAA application. |
| 55 | */ |
| 56 | @Before |
| 57 | public void setUp() { |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 58 | aaa = new AaaManager(); |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 59 | aaa.netCfgService = new TestNetworkConfigRegistry(); |
| 60 | aaa.coreService = new CoreServiceAdapter(); |
| 61 | aaa.packetService = new MockPacketService(); |
| 62 | aaa.activate(); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Fetches the sent packet at the given index. The requested packet |
| 67 | * must be the last packet on the list. |
| 68 | * |
| 69 | * @param index index into sent packets array |
| 70 | * @return packet |
| 71 | */ |
| 72 | private Ethernet fetchPacket(int index) { |
| 73 | for (int iteration = 0; iteration < 20; iteration++) { |
| 74 | if (savedPackets.size() > index) { |
| 75 | return (Ethernet) savedPackets.get(index); |
| 76 | } else { |
| 77 | try { |
| 78 | Thread.sleep(250); |
| 79 | } catch (Exception ex) { |
| 80 | return null; |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | return null; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Tests the authentication path through the AAA application by sending |
| 89 | * packets to the RADIUS server and checking the state machine |
| 90 | * transitions. |
| 91 | * |
| 92 | * @throws Exception when an unhandled error occurs |
| 93 | */ |
| 94 | @Test |
| 95 | public void testAuthentication() throws Exception { |
| 96 | |
| 97 | // (1) Supplicant start up |
| 98 | |
| 99 | Ethernet startPacket = constructSupplicantStartPacket(); |
| 100 | sendPacket(startPacket); |
| 101 | |
| 102 | Ethernet responsePacket = fetchPacket(0); |
| 103 | assertThat(responsePacket, notNullValue()); |
| 104 | checkRadiusPacket(aaa, responsePacket, EAP.REQUEST); |
| 105 | |
| 106 | // (2) Supplicant identify |
| 107 | |
| 108 | Ethernet identifyPacket = constructSupplicantIdentifyPacket(null, EAP.ATTR_IDENTITY, (byte) 1, null); |
| 109 | sendPacket(identifyPacket); |
| 110 | |
| 111 | // State machine should have been created by now |
| 112 | |
| 113 | StateMachine stateMachine = |
| 114 | StateMachine.lookupStateMachineBySessionId(SESSION_ID); |
| 115 | assertThat(stateMachine, notNullValue()); |
| 116 | assertThat(stateMachine.state(), is(StateMachine.STATE_PENDING)); |
| 117 | |
| 118 | // (3) RADIUS MD5 challenge |
| 119 | |
| 120 | Ethernet radiusChallengeMD5Packet = fetchPacket(1); |
| 121 | assertThat(radiusChallengeMD5Packet, notNullValue()); |
| 122 | checkRadiusPacket(aaa, radiusChallengeMD5Packet, EAP.REQUEST); |
| 123 | |
| 124 | |
| 125 | // (4) Supplicant MD5 response |
| 126 | |
| 127 | Ethernet md5RadiusPacket = |
| 128 | constructSupplicantIdentifyPacket(stateMachine, |
| 129 | EAP.ATTR_MD5, |
| 130 | stateMachine.challengeIdentifier(), |
| 131 | radiusChallengeMD5Packet); |
| 132 | sendPacket(md5RadiusPacket); |
| 133 | |
| 134 | |
| 135 | // (5) RADIUS Success |
| 136 | |
| 137 | Ethernet successRadiusPacket = fetchPacket(2); |
| 138 | assertThat(successRadiusPacket, notNullValue()); |
Jonathan Hart | 092dfb2 | 2015-11-16 23:05:21 -0800 | [diff] [blame] | 139 | EAPOL successEapol = (EAPOL) successRadiusPacket.getPayload(); |
| 140 | EAP successEap = (EAP) successEapol.getPayload(); |
| 141 | assertThat(successEap.getCode(), is(EAP.SUCCESS)); |
Ray Milkey | 967776a | 2015-10-07 14:37:17 -0700 | [diff] [blame] | 142 | |
| 143 | // State machine should be in authorized state |
| 144 | |
| 145 | assertThat(stateMachine, notNullValue()); |
| 146 | assertThat(stateMachine.state(), is(StateMachine.STATE_AUTHORIZED)); |
| 147 | |
| 148 | } |
| 149 | |
| 150 | } |
| 151 | |