blob: b48b28634e7049ca492f1c7c1fa90028942928a6 [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")
Shrey Baid688b4242020-07-10 20:40:10 +053028 _ = onu.InternalState.Event("discover")
Matteo Scandolo99f18462019-10-28 14:14:28 -070029 assert.Equal(t, onu.InternalState.Current(), "discovered")
Shrey Baid688b4242020-07-10 20:40:10 +053030 _ = onu.InternalState.Event("enable")
Matteo Scandolo99f18462019-10-28 14:14:28 -070031 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
Matteo Scandolo328c5f02020-06-26 14:16:39 -070040 onu.GemPortAdded = true
Matteo Scandolo47ef64b2020-04-20 14:16:07 -070041 onu.Flows = []FlowKey{
Shrey Baid688b4242020-07-10 20:40:10 +053042 {ID: 1, Direction: "upstream"},
43 {ID: 2, Direction: "downstream"},
Matteo Scandolo47ef64b2020-04-20 14:16:07 -070044 }
45
Shrey Baid688b4242020-07-10 20:40:10 +053046 _ = onu.InternalState.Event("disable")
Matteo Scandolo47ef64b2020-04-20 14:16:07 -070047 assert.Equal(t, onu.InternalState.Current(), "disabled")
48
Matteo Scandolo328c5f02020-06-26 14:16:39 -070049 assert.Equal(t, onu.GemPortAdded, false)
Matteo Scandolo47ef64b2020-04-20 14:16:07 -070050 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 Scandolo4a036262020-08-17 15:56:13 -070058 t.Skip("Needs to be moved in the Service struct")
Matteo Scandoloc1147092019-10-29 09:38:33 -070059 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -070060
61 onu.InternalState.SetState("enabled")
Matteo Scandolo99f18462019-10-28 14:14:28 -070062 assert.Equal(t, onu.InternalState.Current(), "enabled")
Matteo Scandolo5ff80082019-12-20 13:20:57 -080063
64 // fail as no EapolFlow has been received
65 err := onu.InternalState.Event("start_auth")
66 if err == nil {
67 t.Fatal("can't start EAPOL without EapolFlow")
68 }
69 assert.Equal(t, onu.InternalState.Current(), "enabled")
70 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 -070071}
72
Matteo Scandolo5ff80082019-12-20 13:20:57 -080073func Test_Onu_StateMachine_eapol_no_gem(t *testing.T) {
Matteo Scandolo4a036262020-08-17 15:56:13 -070074 t.Skip("Needs to be moved in the Service struct")
Matteo Scandoloc1147092019-10-29 09:38:33 -070075 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -070076
77 onu.InternalState.SetState("enabled")
Matteo Scandolo99f18462019-10-28 14:14:28 -070078 assert.Equal(t, onu.InternalState.Current(), "enabled")
Matteo Scandolo5ff80082019-12-20 13:20:57 -080079
Matteo Scandolo4a036262020-08-17 15:56:13 -070080 // fail has no GemPort has been set
Matteo Scandolo5ff80082019-12-20 13:20:57 -080081 err := onu.InternalState.Event("start_auth")
82 if err == nil {
83 t.Fatal("can't start EAPOL without GemPort")
84 }
85 assert.Equal(t, onu.InternalState.Current(), "enabled")
86 assert.Equal(t, err.Error(), "transition canceled with error: cannot-go-to-auth-started-as-gemport-is-missing")
87
88}
89
90func Test_Onu_StateMachine_eapol_start(t *testing.T) {
Matteo Scandolo4a036262020-08-17 15:56:13 -070091 t.Skip("Needs to be moved in the Service struct")
Matteo Scandolo5ff80082019-12-20 13:20:57 -080092 onu := createTestOnu()
93
94 onu.InternalState.SetState("enabled")
95 assert.Equal(t, onu.InternalState.Current(), "enabled")
96
97 // succeed
Matteo Scandolo5ff80082019-12-20 13:20:57 -080098 onu.GemPortAdded = true
Shrey Baid688b4242020-07-10 20:40:10 +053099 _ = onu.InternalState.Event("start_auth")
Matteo Scandolo99f18462019-10-28 14:14:28 -0700100 assert.Equal(t, onu.InternalState.Current(), "auth_started")
101}
102
103func Test_Onu_StateMachine_eapol_states(t *testing.T) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700104 t.Skip("Needs to be moved in the Service struct")
Matteo Scandoloc1147092019-10-29 09:38:33 -0700105 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -0700106
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800107 onu.GemPortAdded = true
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800108
Matteo Scandolo99f18462019-10-28 14:14:28 -0700109 onu.InternalState.SetState("auth_started")
110
111 assert.Equal(t, onu.InternalState.Current(), "auth_started")
Shrey Baid688b4242020-07-10 20:40:10 +0530112 _ = onu.InternalState.Event("eap_start_sent")
Matteo Scandolo99f18462019-10-28 14:14:28 -0700113 assert.Equal(t, onu.InternalState.Current(), "eap_start_sent")
Shrey Baid688b4242020-07-10 20:40:10 +0530114 _ = onu.InternalState.Event("eap_response_identity_sent")
Matteo Scandolo99f18462019-10-28 14:14:28 -0700115 assert.Equal(t, onu.InternalState.Current(), "eap_response_identity_sent")
Shrey Baid688b4242020-07-10 20:40:10 +0530116 _ = onu.InternalState.Event("eap_response_challenge_sent")
Matteo Scandolo99f18462019-10-28 14:14:28 -0700117 assert.Equal(t, onu.InternalState.Current(), "eap_response_challenge_sent")
Shrey Baid688b4242020-07-10 20:40:10 +0530118 _ = onu.InternalState.Event("eap_response_success_received")
Matteo Scandolo99f18462019-10-28 14:14:28 -0700119 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) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700133 t.Skip("Needs to be moved in the Service struct")
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800134 onu := createTestOnu()
135
136 onu.InternalState.SetState("enabled")
137 assert.Equal(t, onu.InternalState.Current(), "enabled")
138
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800139 err := onu.InternalState.Event("start_dhcp")
140 if err == nil {
141 t.Fail()
142 }
143 assert.Equal(t, onu.InternalState.Current(), "enabled")
144 assert.Equal(t, err.Error(), "transition canceled with error: cannot-go-to-dhcp-started-as-authentication-is-required")
145}
146
147// if the DHCP flow has not been received we can't start authentication
148func Test_Onu_StateMachine_dhcp_no_flow(t *testing.T) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700149 t.Skip("Needs to be moved in the Service struct")
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800150 onu := createTestOnu()
151
152 onu.InternalState.SetState("eap_response_success_received")
153 assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received")
154
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800155 err := onu.InternalState.Event("start_dhcp")
156 if err == nil {
157 t.Fail()
158 }
159 assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received")
160 assert.Equal(t, err.Error(), "transition canceled with error: cannot-go-to-dhcp-started-as-dhcp-flow-is-missing")
161}
162
163// if the ONU does not have a GemPort we can't start DHCP
164func Test_Onu_StateMachine_dhcp_no_gem(t *testing.T) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700165 t.Skip("Needs to be moved in the Service struct")
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800166 onu := createTestOnu()
167
168 onu.InternalState.SetState("eap_response_success_received")
169 assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received")
170
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800171 onu.GemPortAdded = false
172
173 err := onu.InternalState.Event("start_dhcp")
174 if err == nil {
175 t.Fail()
176 }
177 assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received")
178 assert.Equal(t, err.Error(), "transition canceled with error: cannot-go-to-dhcp-started-as-gemport-is-missing")
179}
180
Matteo Scandolo99f18462019-10-28 14:14:28 -0700181func Test_Onu_StateMachine_dhcp_start(t *testing.T) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700182 t.Skip("Needs to be moved in the Service struct")
Matteo Scandoloc1147092019-10-29 09:38:33 -0700183 onu := createTestOnu()
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800184 onu.GemPortAdded = true
Matteo Scandolo99f18462019-10-28 14:14:28 -0700185
186 onu.InternalState.SetState("eap_response_success_received")
187 assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received")
188
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800189 // default transition
Shrey Baid688b4242020-07-10 20:40:10 +0530190 _ = onu.InternalState.Event("start_dhcp")
Matteo Scandolo99f18462019-10-28 14:14:28 -0700191 assert.Equal(t, onu.InternalState.Current(), "dhcp_started")
192}
193
Matteo Scandolo99f18462019-10-28 14:14:28 -0700194func Test_Onu_StateMachine_dhcp_states(t *testing.T) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700195 t.Skip("Needs to be moved in the Service struct")
Matteo Scandoloc1147092019-10-29 09:38:33 -0700196 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -0700197
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800198 onu.GemPortAdded = true
Matteo Scandolo99f18462019-10-28 14:14:28 -0700199
200 onu.InternalState.SetState("dhcp_started")
201
202 assert.Equal(t, onu.InternalState.Current(), "dhcp_started")
Shrey Baid688b4242020-07-10 20:40:10 +0530203 _ = onu.InternalState.Event("dhcp_discovery_sent")
Matteo Scandolo99f18462019-10-28 14:14:28 -0700204 assert.Equal(t, onu.InternalState.Current(), "dhcp_discovery_sent")
Shrey Baid688b4242020-07-10 20:40:10 +0530205 _ = onu.InternalState.Event("dhcp_request_sent")
Matteo Scandolo99f18462019-10-28 14:14:28 -0700206 assert.Equal(t, onu.InternalState.Current(), "dhcp_request_sent")
Shrey Baid688b4242020-07-10 20:40:10 +0530207 _ = onu.InternalState.Event("dhcp_ack_received")
Matteo Scandolo99f18462019-10-28 14:14:28 -0700208 assert.Equal(t, onu.InternalState.Current(), "dhcp_ack_received")
Matteo Scandolo5e081b52019-11-21 14:34:25 -0800209
210 // test that we can retrigger DHCP
Matteo Scandolo5e081b52019-11-21 14:34:25 -0800211 states := []string{"eap_response_success_received", "dhcp_discovery_sent", "dhcp_request_sent", "dhcp_ack_received", "dhcp_failed"}
212 for _, state := range states {
213 onu.InternalState.SetState(state)
214 err := onu.InternalState.Event("start_dhcp")
215 assert.Equal(t, err, nil)
216 assert.Equal(t, onu.InternalState.Current(), "dhcp_started")
217 }
Matteo Scandolo99f18462019-10-28 14:14:28 -0700218}