blob: 6d708fef9b0652ffabd23b988ad5f64183a34657 [file] [log] [blame]
Ray Milkey4ed93692015-10-07 14:37:17 -07001/*
2 * Copyright 2014 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
18import org.junit.Before;
19import org.junit.Ignore;
20import org.junit.Test;
21import org.onlab.packet.EAP;
22import org.onlab.packet.EAPOL;
23import org.onlab.packet.Ethernet;
24import org.onosproject.core.CoreServiceAdapter;
25import org.onosproject.net.config.Config;
26import org.onosproject.net.config.NetworkConfigRegistryAdapter;
27
28import static org.hamcrest.Matchers.is;
29import static org.hamcrest.Matchers.notNullValue;
30import 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 Hartb92cc512015-11-16 23:05:21 -080037public class AaaIntegrationTest extends AaaTestBase {
Ray Milkey4ed93692015-10-07 14:37:17 -070038
Jonathan Hartb92cc512015-11-16 23:05:21 -080039 private AaaManager aaa;
Ray Milkey4ed93692015-10-07 14:37:17 -070040
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 Hartb92cc512015-11-16 23:05:21 -080049 return (C) new AaaConfig();
Ray Milkey4ed93692015-10-07 14:37:17 -070050 }
51 }
52
53 /**
54 * Sets up the services required by the AAA application.
55 */
56 @Before
57 public void setUp() {
Jonathan Hartb92cc512015-11-16 23:05:21 -080058 aaa = new AaaManager();
Ray Milkey4ed93692015-10-07 14:37:17 -070059 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 Hartb92cc512015-11-16 23:05:21 -0800139 EAPOL successEapol = (EAPOL) successRadiusPacket.getPayload();
140 EAP successEap = (EAP) successEapol.getPayload();
141 assertThat(successEap.getCode(), is(EAP.SUCCESS));
Ray Milkey4ed93692015-10-07 14:37:17 -0700142
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