blob: 1de9eeb98ecbc1300cd12b30c69bdb8935373f76 [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 Scandolod15c0b42021-03-22 11:38:13 -070020 omcilib "github.com/opencord/bbsim/internal/common/omci"
21 me "github.com/opencord/omci-lib-go/generated"
Matteo Scandolo99f18462019-10-28 14:14:28 -070022 "testing"
Zdravko Bozakov681364d2019-11-10 14:28:46 +010023
24 "gotest.tools/assert"
Matteo Scandolo99f18462019-10-28 14:14:28 -070025)
26
27func Test_Onu_StateMachine_enable(t *testing.T) {
Matteo Scandoloc1147092019-10-29 09:38:33 -070028 onu := createTestOnu()
Zdravko Bozakov681364d2019-11-10 14:28:46 +010029 assert.Equal(t, onu.InternalState.Current(), "initialized")
Shrey Baid688b4242020-07-10 20:40:10 +053030 _ = onu.InternalState.Event("discover")
Matteo Scandolo99f18462019-10-28 14:14:28 -070031 assert.Equal(t, onu.InternalState.Current(), "discovered")
Shrey Baid688b4242020-07-10 20:40:10 +053032 _ = onu.InternalState.Event("enable")
Matteo Scandolocedde462021-03-09 17:37:16 -080033 assert.Equal(t, onu.InternalState.Current(), OnuStateEnabled)
Matteo Scandolo99f18462019-10-28 14:14:28 -070034}
35
Matteo Scandolo47ef64b2020-04-20 14:16:07 -070036func Test_Onu_StateMachine_disable(t *testing.T) {
37 onu := createTestOnu()
Matteo Scandolocedde462021-03-09 17:37:16 -080038 onu.InternalState.SetState(OnuStateEnabled)
39 assert.Equal(t, onu.InternalState.Current(), OnuStateEnabled)
Matteo Scandolo47ef64b2020-04-20 14:16:07 -070040
41 onu.PortNo = 16
Matteo Scandolo47ef64b2020-04-20 14:16:07 -070042 onu.Flows = []FlowKey{
Shrey Baid688b4242020-07-10 20:40:10 +053043 {ID: 1, Direction: "upstream"},
44 {ID: 2, Direction: "downstream"},
Matteo Scandolo47ef64b2020-04-20 14:16:07 -070045 }
Matteo Scandolod15c0b42021-03-22 11:38:13 -070046 key := omcilib.OnuAlarmInfoMapKey{
47 MeInstance: 257,
48 MeClassID: me.PhysicalPathTerminationPointEthernetUniClassID,
49 }
50 onu.onuAlarmsInfo[key] = omcilib.OnuAlarmInfo{SequenceNo: 1, AlarmBitMap: [28]byte{}}
51 onu.PonPort.storeOnuId(onu.ID, onu.SerialNumber)
52 onu.PonPort.storeAllocId(1, onu.SerialNumber)
53 onu.PonPort.storeGemPort(1, onu.SerialNumber)
Matteo Scandolo47ef64b2020-04-20 14:16:07 -070054
Matteo Scandolocedde462021-03-09 17:37:16 -080055 _ = onu.InternalState.Event(OnuTxDisable)
56 assert.Equal(t, onu.InternalState.Current(), OnuStateDisabled)
Matteo Scandolo47ef64b2020-04-20 14:16:07 -070057
Matteo Scandolo47ef64b2020-04-20 14:16:07 -070058 assert.Equal(t, onu.PortNo, uint32(0))
Matteo Scandolod15c0b42021-03-22 11:38:13 -070059 assert.Equal(t, len(onu.onuAlarmsInfo), 0)
Matteo Scandolo47ef64b2020-04-20 14:16:07 -070060 assert.Equal(t, len(onu.Flows), 0)
Matteo Scandolod15c0b42021-03-22 11:38:13 -070061 assert.Equal(t, len(onu.PonPort.AllocatedOnuIds), 0)
62 assert.Equal(t, len(onu.PonPort.AllocatedAllocIds), 0)
63 assert.Equal(t, len(onu.PonPort.AllocatedGemPorts), 0)
Matteo Scandolo47ef64b2020-04-20 14:16:07 -070064}
65
Matteo Scandolo5ff80082019-12-20 13:20:57 -080066// check that I can go to auth_started only if
67// - the GemPort is set
68// - the eapolFlow is received
69func Test_Onu_StateMachine_eapol_no_flow(t *testing.T) {
Matteo Scandolo4a036262020-08-17 15:56:13 -070070 t.Skip("Needs to be moved in the Service struct")
Matteo Scandoloc1147092019-10-29 09:38:33 -070071 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -070072
Matteo Scandolocedde462021-03-09 17:37:16 -080073 onu.InternalState.SetState(OnuStateEnabled)
74 assert.Equal(t, onu.InternalState.Current(), OnuStateEnabled)
Matteo Scandolo5ff80082019-12-20 13:20:57 -080075
76 // fail as no EapolFlow has been received
77 err := onu.InternalState.Event("start_auth")
78 if err == nil {
79 t.Fatal("can't start EAPOL without EapolFlow")
80 }
Matteo Scandolocedde462021-03-09 17:37:16 -080081 assert.Equal(t, onu.InternalState.Current(), OnuStateEnabled)
Matteo Scandolo5ff80082019-12-20 13:20:57 -080082 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 -070083}
84
Matteo Scandolo5ff80082019-12-20 13:20:57 -080085func Test_Onu_StateMachine_eapol_no_gem(t *testing.T) {
Matteo Scandolo4a036262020-08-17 15:56:13 -070086 t.Skip("Needs to be moved in the Service struct")
Matteo Scandoloc1147092019-10-29 09:38:33 -070087 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -070088
Matteo Scandolocedde462021-03-09 17:37:16 -080089 onu.InternalState.SetState(OnuStateEnabled)
90 assert.Equal(t, onu.InternalState.Current(), OnuStateEnabled)
Matteo Scandolo5ff80082019-12-20 13:20:57 -080091
Matteo Scandolo4a036262020-08-17 15:56:13 -070092 // fail has no GemPort has been set
Matteo Scandolo5ff80082019-12-20 13:20:57 -080093 err := onu.InternalState.Event("start_auth")
94 if err == nil {
95 t.Fatal("can't start EAPOL without GemPort")
96 }
Matteo Scandolocedde462021-03-09 17:37:16 -080097 assert.Equal(t, onu.InternalState.Current(), OnuStateEnabled)
Matteo Scandolo5ff80082019-12-20 13:20:57 -080098 assert.Equal(t, err.Error(), "transition canceled with error: cannot-go-to-auth-started-as-gemport-is-missing")
99
100}
101
102func Test_Onu_StateMachine_eapol_start(t *testing.T) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700103 t.Skip("Needs to be moved in the Service struct")
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800104 onu := createTestOnu()
105
Matteo Scandolocedde462021-03-09 17:37:16 -0800106 onu.InternalState.SetState(OnuStateEnabled)
107 assert.Equal(t, onu.InternalState.Current(), OnuStateEnabled)
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800108
109 // succeed
Shrey Baid688b4242020-07-10 20:40:10 +0530110 _ = onu.InternalState.Event("start_auth")
Matteo Scandolo99f18462019-10-28 14:14:28 -0700111 assert.Equal(t, onu.InternalState.Current(), "auth_started")
112}
113
114func Test_Onu_StateMachine_eapol_states(t *testing.T) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700115 t.Skip("Needs to be moved in the Service struct")
Matteo Scandoloc1147092019-10-29 09:38:33 -0700116 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -0700117
118 onu.InternalState.SetState("auth_started")
119
120 assert.Equal(t, onu.InternalState.Current(), "auth_started")
Shrey Baid688b4242020-07-10 20:40:10 +0530121 _ = onu.InternalState.Event("eap_start_sent")
Matteo Scandolo99f18462019-10-28 14:14:28 -0700122 assert.Equal(t, onu.InternalState.Current(), "eap_start_sent")
Shrey Baid688b4242020-07-10 20:40:10 +0530123 _ = onu.InternalState.Event("eap_response_identity_sent")
Matteo Scandolo99f18462019-10-28 14:14:28 -0700124 assert.Equal(t, onu.InternalState.Current(), "eap_response_identity_sent")
Shrey Baid688b4242020-07-10 20:40:10 +0530125 _ = onu.InternalState.Event("eap_response_challenge_sent")
Matteo Scandolo99f18462019-10-28 14:14:28 -0700126 assert.Equal(t, onu.InternalState.Current(), "eap_response_challenge_sent")
Shrey Baid688b4242020-07-10 20:40:10 +0530127 _ = onu.InternalState.Event("eap_response_success_received")
Matteo Scandolo99f18462019-10-28 14:14:28 -0700128 assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received")
Matteo Scandolo5e081b52019-11-21 14:34:25 -0800129
130 // test that we can retrigger EAPOL
131 states := []string{"eap_start_sent", "eap_response_identity_sent", "eap_response_challenge_sent", "eap_response_success_received", "auth_failed", "dhcp_ack_received", "dhcp_failed"}
132 for _, state := range states {
133 onu.InternalState.SetState(state)
134 err := onu.InternalState.Event("start_auth")
135 assert.Equal(t, err, nil)
136 assert.Equal(t, onu.InternalState.Current(), "auth_started")
137 }
Matteo Scandolo99f18462019-10-28 14:14:28 -0700138}
139
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800140// if auth is set to true we can't go from enabled to dhcp_started
141func Test_Onu_StateMachine_dhcp_no_auth(t *testing.T) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700142 t.Skip("Needs to be moved in the Service struct")
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800143 onu := createTestOnu()
144
Matteo Scandolocedde462021-03-09 17:37:16 -0800145 onu.InternalState.SetState(OnuStateEnabled)
146 assert.Equal(t, onu.InternalState.Current(), OnuStateEnabled)
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800147
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800148 err := onu.InternalState.Event("start_dhcp")
149 if err == nil {
150 t.Fail()
151 }
Matteo Scandolocedde462021-03-09 17:37:16 -0800152 assert.Equal(t, onu.InternalState.Current(), OnuStateEnabled)
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800153 assert.Equal(t, err.Error(), "transition canceled with error: cannot-go-to-dhcp-started-as-authentication-is-required")
154}
155
156// if the DHCP flow has not been received we can't start authentication
157func Test_Onu_StateMachine_dhcp_no_flow(t *testing.T) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700158 t.Skip("Needs to be moved in the Service struct")
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800159 onu := createTestOnu()
160
161 onu.InternalState.SetState("eap_response_success_received")
162 assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received")
163
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800164 err := onu.InternalState.Event("start_dhcp")
165 if err == nil {
166 t.Fail()
167 }
168 assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received")
169 assert.Equal(t, err.Error(), "transition canceled with error: cannot-go-to-dhcp-started-as-dhcp-flow-is-missing")
170}
171
172// if the ONU does not have a GemPort we can't start DHCP
173func Test_Onu_StateMachine_dhcp_no_gem(t *testing.T) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700174 t.Skip("Needs to be moved in the Service struct")
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800175 onu := createTestOnu()
176
177 onu.InternalState.SetState("eap_response_success_received")
178 assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received")
179
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800180 err := onu.InternalState.Event("start_dhcp")
181 if err == nil {
182 t.Fail()
183 }
184 assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received")
185 assert.Equal(t, err.Error(), "transition canceled with error: cannot-go-to-dhcp-started-as-gemport-is-missing")
186}
187
Matteo Scandolo99f18462019-10-28 14:14:28 -0700188func Test_Onu_StateMachine_dhcp_start(t *testing.T) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700189 t.Skip("Needs to be moved in the Service struct")
Matteo Scandoloc1147092019-10-29 09:38:33 -0700190 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -0700191
192 onu.InternalState.SetState("eap_response_success_received")
193 assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received")
194
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800195 // default transition
Shrey Baid688b4242020-07-10 20:40:10 +0530196 _ = onu.InternalState.Event("start_dhcp")
Matteo Scandolo99f18462019-10-28 14:14:28 -0700197 assert.Equal(t, onu.InternalState.Current(), "dhcp_started")
198}
199
Matteo Scandolo99f18462019-10-28 14:14:28 -0700200func Test_Onu_StateMachine_dhcp_states(t *testing.T) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700201 t.Skip("Needs to be moved in the Service struct")
Matteo Scandoloc1147092019-10-29 09:38:33 -0700202 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -0700203
Matteo Scandolo99f18462019-10-28 14:14:28 -0700204 onu.InternalState.SetState("dhcp_started")
205
206 assert.Equal(t, onu.InternalState.Current(), "dhcp_started")
Shrey Baid688b4242020-07-10 20:40:10 +0530207 _ = onu.InternalState.Event("dhcp_discovery_sent")
Matteo Scandolo99f18462019-10-28 14:14:28 -0700208 assert.Equal(t, onu.InternalState.Current(), "dhcp_discovery_sent")
Shrey Baid688b4242020-07-10 20:40:10 +0530209 _ = onu.InternalState.Event("dhcp_request_sent")
Matteo Scandolo99f18462019-10-28 14:14:28 -0700210 assert.Equal(t, onu.InternalState.Current(), "dhcp_request_sent")
Shrey Baid688b4242020-07-10 20:40:10 +0530211 _ = onu.InternalState.Event("dhcp_ack_received")
Matteo Scandolo99f18462019-10-28 14:14:28 -0700212 assert.Equal(t, onu.InternalState.Current(), "dhcp_ack_received")
Matteo Scandolo5e081b52019-11-21 14:34:25 -0800213
214 // test that we can retrigger DHCP
Matteo Scandolo5e081b52019-11-21 14:34:25 -0800215 states := []string{"eap_response_success_received", "dhcp_discovery_sent", "dhcp_request_sent", "dhcp_ack_received", "dhcp_failed"}
216 for _, state := range states {
217 onu.InternalState.SetState(state)
218 err := onu.InternalState.Event("start_dhcp")
219 assert.Equal(t, err, nil)
220 assert.Equal(t, onu.InternalState.Current(), "dhcp_started")
221 }
Matteo Scandolo99f18462019-10-28 14:14:28 -0700222}