blob: 81bf33035fee17e2d95222a63d0abf54bcec9f33 [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
41 onu.Flows = []FlowKey{
Matteo Scandolo5ff80082019-12-20 13:20:57 -080042 FlowKey{ID: 1, Direction: "upstream"},
43 FlowKey{ID: 2, Direction: "downstream"},
Matteo Scandolo47ef64b2020-04-20 14:16:07 -070044 }
45
46 onu.InternalState.Event("disable")
47 assert.Equal(t, onu.InternalState.Current(), "disabled")
48
49 assert.Equal(t, onu.DhcpFlowReceived, false)
50 assert.Equal(t, onu.PortNo, uint32(0))
51 assert.Equal(t, len(onu.Flows), 0)
52}
53
Matteo Scandolo5ff80082019-12-20 13:20:57 -080054// check that I can go to auth_started only if
55// - the GemPort is set
56// - the eapolFlow is received
57func Test_Onu_StateMachine_eapol_no_flow(t *testing.T) {
Matteo Scandoloc1147092019-10-29 09:38:33 -070058 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -070059
60 onu.InternalState.SetState("enabled")
Matteo Scandolo99f18462019-10-28 14:14:28 -070061 assert.Equal(t, onu.InternalState.Current(), "enabled")
Matteo Scandolo5ff80082019-12-20 13:20:57 -080062
63 // fail as no EapolFlow has been received
64 err := onu.InternalState.Event("start_auth")
65 if err == nil {
66 t.Fatal("can't start EAPOL without EapolFlow")
67 }
68 assert.Equal(t, onu.InternalState.Current(), "enabled")
69 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 -070070}
71
Matteo Scandolo5ff80082019-12-20 13:20:57 -080072func Test_Onu_StateMachine_eapol_no_gem(t *testing.T) {
Matteo Scandoloc1147092019-10-29 09:38:33 -070073 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -070074
75 onu.InternalState.SetState("enabled")
Matteo Scandolo99f18462019-10-28 14:14:28 -070076 assert.Equal(t, onu.InternalState.Current(), "enabled")
Matteo Scandolo5ff80082019-12-20 13:20:57 -080077 // fail has no GemPort has been set
78 onu.EapolFlowReceived = true
79
80 err := onu.InternalState.Event("start_auth")
81 if err == nil {
82 t.Fatal("can't start EAPOL without GemPort")
83 }
84 assert.Equal(t, onu.InternalState.Current(), "enabled")
85 assert.Equal(t, err.Error(), "transition canceled with error: cannot-go-to-auth-started-as-gemport-is-missing")
86
87}
88
89func Test_Onu_StateMachine_eapol_start(t *testing.T) {
90
91 onu := createTestOnu()
92
93 onu.InternalState.SetState("enabled")
94 assert.Equal(t, onu.InternalState.Current(), "enabled")
95
96 // succeed
97 onu.EapolFlowReceived = true
98 onu.GemPortAdded = true
Matteo Scandolo99f18462019-10-28 14:14:28 -070099 onu.InternalState.Event("start_auth")
100 assert.Equal(t, onu.InternalState.Current(), "auth_started")
101}
102
103func Test_Onu_StateMachine_eapol_states(t *testing.T) {
Matteo Scandoloc1147092019-10-29 09:38:33 -0700104 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -0700105
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800106 onu.GemPortAdded = true
107 onu.EapolFlowReceived = true
108
Matteo Scandolo99f18462019-10-28 14:14:28 -0700109 onu.InternalState.SetState("auth_started")
110
111 assert.Equal(t, onu.InternalState.Current(), "auth_started")
112 onu.InternalState.Event("eap_start_sent")
113 assert.Equal(t, onu.InternalState.Current(), "eap_start_sent")
114 onu.InternalState.Event("eap_response_identity_sent")
115 assert.Equal(t, onu.InternalState.Current(), "eap_response_identity_sent")
116 onu.InternalState.Event("eap_response_challenge_sent")
117 assert.Equal(t, onu.InternalState.Current(), "eap_response_challenge_sent")
118 onu.InternalState.Event("eap_response_success_received")
119 assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received")
Matteo Scandolo5e081b52019-11-21 14:34:25 -0800120
121 // test that we can retrigger EAPOL
122 states := []string{"eap_start_sent", "eap_response_identity_sent", "eap_response_challenge_sent", "eap_response_success_received", "auth_failed", "dhcp_ack_received", "dhcp_failed"}
123 for _, state := range states {
124 onu.InternalState.SetState(state)
125 err := onu.InternalState.Event("start_auth")
126 assert.Equal(t, err, nil)
127 assert.Equal(t, onu.InternalState.Current(), "auth_started")
128 }
Matteo Scandolo99f18462019-10-28 14:14:28 -0700129}
130
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800131// if auth is set to true we can't go from enabled to dhcp_started
132func Test_Onu_StateMachine_dhcp_no_auth(t *testing.T) {
133 onu := createTestOnu()
134
135 onu.InternalState.SetState("enabled")
136 assert.Equal(t, onu.InternalState.Current(), "enabled")
137
138 onu.Auth = true
139
140 err := onu.InternalState.Event("start_dhcp")
141 if err == nil {
142 t.Fail()
143 }
144 assert.Equal(t, onu.InternalState.Current(), "enabled")
145 assert.Equal(t, err.Error(), "transition canceled with error: cannot-go-to-dhcp-started-as-authentication-is-required")
146}
147
148// if the DHCP flow has not been received we can't start authentication
149func Test_Onu_StateMachine_dhcp_no_flow(t *testing.T) {
150 onu := createTestOnu()
151
152 onu.InternalState.SetState("eap_response_success_received")
153 assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received")
154
155 onu.DhcpFlowReceived = false
156
157 err := onu.InternalState.Event("start_dhcp")
158 if err == nil {
159 t.Fail()
160 }
161 assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received")
162 assert.Equal(t, err.Error(), "transition canceled with error: cannot-go-to-dhcp-started-as-dhcp-flow-is-missing")
163}
164
165// if the ONU does not have a GemPort we can't start DHCP
166func Test_Onu_StateMachine_dhcp_no_gem(t *testing.T) {
167 onu := createTestOnu()
168
169 onu.InternalState.SetState("eap_response_success_received")
170 assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received")
171
172 onu.DhcpFlowReceived = true
173 onu.GemPortAdded = false
174
175 err := onu.InternalState.Event("start_dhcp")
176 if err == nil {
177 t.Fail()
178 }
179 assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received")
180 assert.Equal(t, err.Error(), "transition canceled with error: cannot-go-to-dhcp-started-as-gemport-is-missing")
181}
182
Matteo Scandolo99f18462019-10-28 14:14:28 -0700183func Test_Onu_StateMachine_dhcp_start(t *testing.T) {
Matteo Scandoloc1147092019-10-29 09:38:33 -0700184 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -0700185 onu.DhcpFlowReceived = true
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800186 onu.GemPortAdded = true
187 onu.Auth = true
Matteo Scandolo99f18462019-10-28 14:14:28 -0700188
189 onu.InternalState.SetState("eap_response_success_received")
190 assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received")
191
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800192 // default transition
Matteo Scandolo99f18462019-10-28 14:14:28 -0700193 onu.InternalState.Event("start_dhcp")
Matteo Scandolo99f18462019-10-28 14:14:28 -0700194 assert.Equal(t, onu.InternalState.Current(), "dhcp_started")
195}
196
Matteo Scandolo99f18462019-10-28 14:14:28 -0700197func Test_Onu_StateMachine_dhcp_states(t *testing.T) {
Matteo Scandoloc1147092019-10-29 09:38:33 -0700198 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -0700199
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800200 onu.DhcpFlowReceived = true
201 onu.GemPortAdded = true
Matteo Scandolo99f18462019-10-28 14:14:28 -0700202
203 onu.InternalState.SetState("dhcp_started")
204
205 assert.Equal(t, onu.InternalState.Current(), "dhcp_started")
206 onu.InternalState.Event("dhcp_discovery_sent")
207 assert.Equal(t, onu.InternalState.Current(), "dhcp_discovery_sent")
208 onu.InternalState.Event("dhcp_request_sent")
209 assert.Equal(t, onu.InternalState.Current(), "dhcp_request_sent")
210 onu.InternalState.Event("dhcp_ack_received")
211 assert.Equal(t, onu.InternalState.Current(), "dhcp_ack_received")
Matteo Scandolo5e081b52019-11-21 14:34:25 -0800212
213 // test that we can retrigger DHCP
Matteo Scandolo5e081b52019-11-21 14:34:25 -0800214 states := []string{"eap_response_success_received", "dhcp_discovery_sent", "dhcp_request_sent", "dhcp_ack_received", "dhcp_failed"}
215 for _, state := range states {
216 onu.InternalState.SetState(state)
217 err := onu.InternalState.Event("start_dhcp")
218 assert.Equal(t, err, nil)
219 assert.Equal(t, onu.InternalState.Current(), "dhcp_started")
220 }
Matteo Scandolo99f18462019-10-28 14:14:28 -0700221}