blob: b2b292b7d8312a4121afeeb880ea2a7717463e05 [file] [log] [blame]
Matteo Scandolo99f18462019-10-28 14:14:28 -07001/*
2 * Copyright 2018-present Open Networking Foundation
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
17package devices
18
19import (
Matteo Scandolo99f18462019-10-28 14:14:28 -070020 "testing"
Zdravko Bozakov681364d2019-11-10 14:28:46 +010021
22 "gotest.tools/assert"
Matteo Scandolo99f18462019-10-28 14:14:28 -070023)
24
25func Test_Onu_StateMachine_enable(t *testing.T) {
Matteo Scandoloc1147092019-10-29 09:38:33 -070026 onu := createTestOnu()
Zdravko Bozakov681364d2019-11-10 14:28:46 +010027 assert.Equal(t, onu.InternalState.Current(), "initialized")
Matteo Scandolo99f18462019-10-28 14:14:28 -070028 onu.InternalState.Event("discover")
29 assert.Equal(t, onu.InternalState.Current(), "discovered")
30 onu.InternalState.Event("enable")
31 assert.Equal(t, onu.InternalState.Current(), "enabled")
32}
33
Matteo Scandolo47ef64b2020-04-20 14:16:07 -070034func Test_Onu_StateMachine_disable(t *testing.T) {
35 onu := createTestOnu()
36 onu.InternalState.SetState("enabled")
37 assert.Equal(t, onu.InternalState.Current(), "enabled")
38
39 onu.PortNo = 16
40 onu.DhcpFlowReceived = true
Matteo Scandolo328c5f02020-06-26 14:16:39 -070041 onu.EapolFlowReceived = true
42 onu.GemPortAdded = true
Matteo Scandolo47ef64b2020-04-20 14:16:07 -070043 onu.Flows = []FlowKey{
Matteo Scandolo5ff80082019-12-20 13:20:57 -080044 FlowKey{ID: 1, Direction: "upstream"},
45 FlowKey{ID: 2, Direction: "downstream"},
Matteo Scandolo47ef64b2020-04-20 14:16:07 -070046 }
47
48 onu.InternalState.Event("disable")
49 assert.Equal(t, onu.InternalState.Current(), "disabled")
50
51 assert.Equal(t, onu.DhcpFlowReceived, false)
Matteo Scandolo328c5f02020-06-26 14:16:39 -070052 assert.Equal(t, onu.EapolFlowReceived, false)
53 assert.Equal(t, onu.GemPortAdded, false)
Matteo Scandolo47ef64b2020-04-20 14:16:07 -070054 assert.Equal(t, onu.PortNo, uint32(0))
55 assert.Equal(t, len(onu.Flows), 0)
56}
57
Matteo Scandolo5ff80082019-12-20 13:20:57 -080058// check that I can go to auth_started only if
59// - the GemPort is set
60// - the eapolFlow is received
61func Test_Onu_StateMachine_eapol_no_flow(t *testing.T) {
Matteo Scandoloc1147092019-10-29 09:38:33 -070062 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -070063
64 onu.InternalState.SetState("enabled")
Matteo Scandolo99f18462019-10-28 14:14:28 -070065 assert.Equal(t, onu.InternalState.Current(), "enabled")
Matteo Scandolo5ff80082019-12-20 13:20:57 -080066
67 // fail as no EapolFlow has been received
68 err := onu.InternalState.Event("start_auth")
69 if err == nil {
70 t.Fatal("can't start EAPOL without EapolFlow")
71 }
72 assert.Equal(t, onu.InternalState.Current(), "enabled")
73 assert.Equal(t, err.Error(), "transition canceled with error: cannot-go-to-auth-started-as-eapol-flow-is-missing")
Matteo Scandolo99f18462019-10-28 14:14:28 -070074}
75
Matteo Scandolo5ff80082019-12-20 13:20:57 -080076func Test_Onu_StateMachine_eapol_no_gem(t *testing.T) {
Matteo Scandoloc1147092019-10-29 09:38:33 -070077 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -070078
79 onu.InternalState.SetState("enabled")
Matteo Scandolo99f18462019-10-28 14:14:28 -070080 assert.Equal(t, onu.InternalState.Current(), "enabled")
Matteo Scandolo5ff80082019-12-20 13:20:57 -080081 // fail has no GemPort has been set
82 onu.EapolFlowReceived = true
83
84 err := onu.InternalState.Event("start_auth")
85 if err == nil {
86 t.Fatal("can't start EAPOL without GemPort")
87 }
88 assert.Equal(t, onu.InternalState.Current(), "enabled")
89 assert.Equal(t, err.Error(), "transition canceled with error: cannot-go-to-auth-started-as-gemport-is-missing")
90
91}
92
93func Test_Onu_StateMachine_eapol_start(t *testing.T) {
94
95 onu := createTestOnu()
96
97 onu.InternalState.SetState("enabled")
98 assert.Equal(t, onu.InternalState.Current(), "enabled")
99
100 // succeed
101 onu.EapolFlowReceived = true
102 onu.GemPortAdded = true
Matteo Scandolo99f18462019-10-28 14:14:28 -0700103 onu.InternalState.Event("start_auth")
104 assert.Equal(t, onu.InternalState.Current(), "auth_started")
105}
106
107func Test_Onu_StateMachine_eapol_states(t *testing.T) {
Matteo Scandoloc1147092019-10-29 09:38:33 -0700108 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -0700109
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800110 onu.GemPortAdded = true
111 onu.EapolFlowReceived = true
112
Matteo Scandolo99f18462019-10-28 14:14:28 -0700113 onu.InternalState.SetState("auth_started")
114
115 assert.Equal(t, onu.InternalState.Current(), "auth_started")
116 onu.InternalState.Event("eap_start_sent")
117 assert.Equal(t, onu.InternalState.Current(), "eap_start_sent")
118 onu.InternalState.Event("eap_response_identity_sent")
119 assert.Equal(t, onu.InternalState.Current(), "eap_response_identity_sent")
120 onu.InternalState.Event("eap_response_challenge_sent")
121 assert.Equal(t, onu.InternalState.Current(), "eap_response_challenge_sent")
122 onu.InternalState.Event("eap_response_success_received")
123 assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received")
Matteo Scandolo5e081b52019-11-21 14:34:25 -0800124
125 // test that we can retrigger EAPOL
126 states := []string{"eap_start_sent", "eap_response_identity_sent", "eap_response_challenge_sent", "eap_response_success_received", "auth_failed", "dhcp_ack_received", "dhcp_failed"}
127 for _, state := range states {
128 onu.InternalState.SetState(state)
129 err := onu.InternalState.Event("start_auth")
130 assert.Equal(t, err, nil)
131 assert.Equal(t, onu.InternalState.Current(), "auth_started")
132 }
Matteo Scandolo99f18462019-10-28 14:14:28 -0700133}
134
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800135// if auth is set to true we can't go from enabled to dhcp_started
136func Test_Onu_StateMachine_dhcp_no_auth(t *testing.T) {
137 onu := createTestOnu()
138
139 onu.InternalState.SetState("enabled")
140 assert.Equal(t, onu.InternalState.Current(), "enabled")
141
142 onu.Auth = true
143
144 err := onu.InternalState.Event("start_dhcp")
145 if err == nil {
146 t.Fail()
147 }
148 assert.Equal(t, onu.InternalState.Current(), "enabled")
149 assert.Equal(t, err.Error(), "transition canceled with error: cannot-go-to-dhcp-started-as-authentication-is-required")
150}
151
152// if the DHCP flow has not been received we can't start authentication
153func Test_Onu_StateMachine_dhcp_no_flow(t *testing.T) {
154 onu := createTestOnu()
155
156 onu.InternalState.SetState("eap_response_success_received")
157 assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received")
158
159 onu.DhcpFlowReceived = false
160
161 err := onu.InternalState.Event("start_dhcp")
162 if err == nil {
163 t.Fail()
164 }
165 assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received")
166 assert.Equal(t, err.Error(), "transition canceled with error: cannot-go-to-dhcp-started-as-dhcp-flow-is-missing")
167}
168
169// if the ONU does not have a GemPort we can't start DHCP
170func Test_Onu_StateMachine_dhcp_no_gem(t *testing.T) {
171 onu := createTestOnu()
172
173 onu.InternalState.SetState("eap_response_success_received")
174 assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received")
175
176 onu.DhcpFlowReceived = true
177 onu.GemPortAdded = false
178
179 err := onu.InternalState.Event("start_dhcp")
180 if err == nil {
181 t.Fail()
182 }
183 assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received")
184 assert.Equal(t, err.Error(), "transition canceled with error: cannot-go-to-dhcp-started-as-gemport-is-missing")
185}
186
Matteo Scandolo99f18462019-10-28 14:14:28 -0700187func Test_Onu_StateMachine_dhcp_start(t *testing.T) {
Matteo Scandoloc1147092019-10-29 09:38:33 -0700188 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -0700189 onu.DhcpFlowReceived = true
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800190 onu.GemPortAdded = true
191 onu.Auth = true
Matteo Scandolo99f18462019-10-28 14:14:28 -0700192
193 onu.InternalState.SetState("eap_response_success_received")
194 assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received")
195
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800196 // default transition
Matteo Scandolo99f18462019-10-28 14:14:28 -0700197 onu.InternalState.Event("start_dhcp")
Matteo Scandolo99f18462019-10-28 14:14:28 -0700198 assert.Equal(t, onu.InternalState.Current(), "dhcp_started")
199}
200
Matteo Scandolo99f18462019-10-28 14:14:28 -0700201func Test_Onu_StateMachine_dhcp_states(t *testing.T) {
Matteo Scandoloc1147092019-10-29 09:38:33 -0700202 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -0700203
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800204 onu.DhcpFlowReceived = true
205 onu.GemPortAdded = true
Matteo Scandolo99f18462019-10-28 14:14:28 -0700206
207 onu.InternalState.SetState("dhcp_started")
208
209 assert.Equal(t, onu.InternalState.Current(), "dhcp_started")
210 onu.InternalState.Event("dhcp_discovery_sent")
211 assert.Equal(t, onu.InternalState.Current(), "dhcp_discovery_sent")
212 onu.InternalState.Event("dhcp_request_sent")
213 assert.Equal(t, onu.InternalState.Current(), "dhcp_request_sent")
214 onu.InternalState.Event("dhcp_ack_received")
215 assert.Equal(t, onu.InternalState.Current(), "dhcp_ack_received")
Matteo Scandolo5e081b52019-11-21 14:34:25 -0800216
217 // test that we can retrigger DHCP
Matteo Scandolo5e081b52019-11-21 14:34:25 -0800218 states := []string{"eap_response_success_received", "dhcp_discovery_sent", "dhcp_request_sent", "dhcp_ack_received", "dhcp_failed"}
219 for _, state := range states {
220 onu.InternalState.SetState(state)
221 err := onu.InternalState.Event("start_dhcp")
222 assert.Equal(t, err, nil)
223 assert.Equal(t, onu.InternalState.Current(), "dhcp_started")
224 }
Matteo Scandolo99f18462019-10-28 14:14:28 -0700225}