Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package devices |
| 18 | |
| 19 | import ( |
Matteo Scandolo | d15c0b4 | 2021-03-22 11:38:13 -0700 | [diff] [blame] | 20 | omcilib "github.com/opencord/bbsim/internal/common/omci" |
| 21 | me "github.com/opencord/omci-lib-go/generated" |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 22 | "testing" |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 23 | |
| 24 | "gotest.tools/assert" |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | func Test_Onu_StateMachine_enable(t *testing.T) { |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame] | 28 | onu := createTestOnu() |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 29 | assert.Equal(t, onu.InternalState.Current(), "initialized") |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 30 | _ = onu.InternalState.Event("discover") |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 31 | assert.Equal(t, onu.InternalState.Current(), "discovered") |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 32 | _ = onu.InternalState.Event("enable") |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 33 | assert.Equal(t, onu.InternalState.Current(), OnuStateEnabled) |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Matteo Scandolo | 47ef64b | 2020-04-20 14:16:07 -0700 | [diff] [blame] | 36 | func Test_Onu_StateMachine_disable(t *testing.T) { |
| 37 | onu := createTestOnu() |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 38 | onu.InternalState.SetState(OnuStateEnabled) |
| 39 | assert.Equal(t, onu.InternalState.Current(), OnuStateEnabled) |
Matteo Scandolo | 47ef64b | 2020-04-20 14:16:07 -0700 | [diff] [blame] | 40 | |
| 41 | onu.PortNo = 16 |
Matteo Scandolo | 47ef64b | 2020-04-20 14:16:07 -0700 | [diff] [blame] | 42 | onu.Flows = []FlowKey{ |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 43 | {ID: 1, Direction: "upstream"}, |
| 44 | {ID: 2, Direction: "downstream"}, |
Matteo Scandolo | 47ef64b | 2020-04-20 14:16:07 -0700 | [diff] [blame] | 45 | } |
Matteo Scandolo | d15c0b4 | 2021-03-22 11:38:13 -0700 | [diff] [blame] | 46 | 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 Scandolo | 47ef64b | 2020-04-20 14:16:07 -0700 | [diff] [blame] | 54 | |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 55 | _ = onu.InternalState.Event(OnuTxDisable) |
| 56 | assert.Equal(t, onu.InternalState.Current(), OnuStateDisabled) |
Matteo Scandolo | 47ef64b | 2020-04-20 14:16:07 -0700 | [diff] [blame] | 57 | |
Matteo Scandolo | 47ef64b | 2020-04-20 14:16:07 -0700 | [diff] [blame] | 58 | assert.Equal(t, onu.PortNo, uint32(0)) |
Matteo Scandolo | d15c0b4 | 2021-03-22 11:38:13 -0700 | [diff] [blame] | 59 | assert.Equal(t, len(onu.onuAlarmsInfo), 0) |
Matteo Scandolo | 47ef64b | 2020-04-20 14:16:07 -0700 | [diff] [blame] | 60 | assert.Equal(t, len(onu.Flows), 0) |
Matteo Scandolo | d15c0b4 | 2021-03-22 11:38:13 -0700 | [diff] [blame] | 61 | 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 Scandolo | 47ef64b | 2020-04-20 14:16:07 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 66 | // check that I can go to auth_started only if |
| 67 | // - the GemPort is set |
| 68 | // - the eapolFlow is received |
| 69 | func Test_Onu_StateMachine_eapol_no_flow(t *testing.T) { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 70 | t.Skip("Needs to be moved in the Service struct") |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame] | 71 | onu := createTestOnu() |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 72 | |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 73 | onu.InternalState.SetState(OnuStateEnabled) |
| 74 | assert.Equal(t, onu.InternalState.Current(), OnuStateEnabled) |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 75 | |
| 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 Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 81 | assert.Equal(t, onu.InternalState.Current(), OnuStateEnabled) |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 82 | assert.Equal(t, err.Error(), "transition canceled with error: cannot-go-to-auth-started-as-eapol-flow-is-missing") |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 85 | func Test_Onu_StateMachine_eapol_no_gem(t *testing.T) { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 86 | t.Skip("Needs to be moved in the Service struct") |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame] | 87 | onu := createTestOnu() |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 88 | |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 89 | onu.InternalState.SetState(OnuStateEnabled) |
| 90 | assert.Equal(t, onu.InternalState.Current(), OnuStateEnabled) |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 91 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 92 | // fail has no GemPort has been set |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 93 | err := onu.InternalState.Event("start_auth") |
| 94 | if err == nil { |
| 95 | t.Fatal("can't start EAPOL without GemPort") |
| 96 | } |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 97 | assert.Equal(t, onu.InternalState.Current(), OnuStateEnabled) |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 98 | assert.Equal(t, err.Error(), "transition canceled with error: cannot-go-to-auth-started-as-gemport-is-missing") |
| 99 | |
| 100 | } |
| 101 | |
| 102 | func Test_Onu_StateMachine_eapol_start(t *testing.T) { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 103 | t.Skip("Needs to be moved in the Service struct") |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 104 | onu := createTestOnu() |
| 105 | |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 106 | onu.InternalState.SetState(OnuStateEnabled) |
| 107 | assert.Equal(t, onu.InternalState.Current(), OnuStateEnabled) |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 108 | |
| 109 | // succeed |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 110 | _ = onu.InternalState.Event("start_auth") |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 111 | assert.Equal(t, onu.InternalState.Current(), "auth_started") |
| 112 | } |
| 113 | |
| 114 | func Test_Onu_StateMachine_eapol_states(t *testing.T) { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 115 | t.Skip("Needs to be moved in the Service struct") |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame] | 116 | onu := createTestOnu() |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 117 | |
| 118 | onu.InternalState.SetState("auth_started") |
| 119 | |
| 120 | assert.Equal(t, onu.InternalState.Current(), "auth_started") |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 121 | _ = onu.InternalState.Event("eap_start_sent") |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 122 | assert.Equal(t, onu.InternalState.Current(), "eap_start_sent") |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 123 | _ = onu.InternalState.Event("eap_response_identity_sent") |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 124 | assert.Equal(t, onu.InternalState.Current(), "eap_response_identity_sent") |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 125 | _ = onu.InternalState.Event("eap_response_challenge_sent") |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 126 | assert.Equal(t, onu.InternalState.Current(), "eap_response_challenge_sent") |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 127 | _ = onu.InternalState.Event("eap_response_success_received") |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 128 | assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received") |
Matteo Scandolo | 5e081b5 | 2019-11-21 14:34:25 -0800 | [diff] [blame] | 129 | |
| 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 Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 140 | // if auth is set to true we can't go from enabled to dhcp_started |
| 141 | func Test_Onu_StateMachine_dhcp_no_auth(t *testing.T) { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 142 | t.Skip("Needs to be moved in the Service struct") |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 143 | onu := createTestOnu() |
| 144 | |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 145 | onu.InternalState.SetState(OnuStateEnabled) |
| 146 | assert.Equal(t, onu.InternalState.Current(), OnuStateEnabled) |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 147 | |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 148 | err := onu.InternalState.Event("start_dhcp") |
| 149 | if err == nil { |
| 150 | t.Fail() |
| 151 | } |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 152 | assert.Equal(t, onu.InternalState.Current(), OnuStateEnabled) |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 153 | 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 |
| 157 | func Test_Onu_StateMachine_dhcp_no_flow(t *testing.T) { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 158 | t.Skip("Needs to be moved in the Service struct") |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 159 | onu := createTestOnu() |
| 160 | |
| 161 | onu.InternalState.SetState("eap_response_success_received") |
| 162 | assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received") |
| 163 | |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 164 | 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 |
| 173 | func Test_Onu_StateMachine_dhcp_no_gem(t *testing.T) { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 174 | t.Skip("Needs to be moved in the Service struct") |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 175 | onu := createTestOnu() |
| 176 | |
| 177 | onu.InternalState.SetState("eap_response_success_received") |
| 178 | assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received") |
| 179 | |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 180 | 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 Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 188 | func Test_Onu_StateMachine_dhcp_start(t *testing.T) { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 189 | t.Skip("Needs to be moved in the Service struct") |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame] | 190 | onu := createTestOnu() |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 191 | |
| 192 | onu.InternalState.SetState("eap_response_success_received") |
| 193 | assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received") |
| 194 | |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 195 | // default transition |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 196 | _ = onu.InternalState.Event("start_dhcp") |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 197 | assert.Equal(t, onu.InternalState.Current(), "dhcp_started") |
| 198 | } |
| 199 | |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 200 | func Test_Onu_StateMachine_dhcp_states(t *testing.T) { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 201 | t.Skip("Needs to be moved in the Service struct") |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame] | 202 | onu := createTestOnu() |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 203 | |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 204 | onu.InternalState.SetState("dhcp_started") |
| 205 | |
| 206 | assert.Equal(t, onu.InternalState.Current(), "dhcp_started") |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 207 | _ = onu.InternalState.Event("dhcp_discovery_sent") |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 208 | assert.Equal(t, onu.InternalState.Current(), "dhcp_discovery_sent") |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 209 | _ = onu.InternalState.Event("dhcp_request_sent") |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 210 | assert.Equal(t, onu.InternalState.Current(), "dhcp_request_sent") |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 211 | _ = onu.InternalState.Event("dhcp_ack_received") |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 212 | assert.Equal(t, onu.InternalState.Current(), "dhcp_ack_received") |
Matteo Scandolo | 5e081b5 | 2019-11-21 14:34:25 -0800 | [diff] [blame] | 213 | |
| 214 | // test that we can retrigger DHCP |
Matteo Scandolo | 5e081b5 | 2019-11-21 14:34:25 -0800 | [diff] [blame] | 215 | 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 Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 222 | } |