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 | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 20 | "testing" |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 21 | |
| 22 | "gotest.tools/assert" |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 23 | ) |
| 24 | |
| 25 | func Test_Onu_StateMachine_enable(t *testing.T) { |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame] | 26 | onu := createTestOnu() |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 27 | assert.Equal(t, onu.InternalState.Current(), "initialized") |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 28 | _ = onu.InternalState.Event("discover") |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 29 | assert.Equal(t, onu.InternalState.Current(), "discovered") |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 30 | _ = onu.InternalState.Event("enable") |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 31 | assert.Equal(t, onu.InternalState.Current(), "enabled") |
| 32 | } |
| 33 | |
Matteo Scandolo | 47ef64b | 2020-04-20 14:16:07 -0700 | [diff] [blame] | 34 | func 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 Scandolo | 328c5f0 | 2020-06-26 14:16:39 -0700 | [diff] [blame] | 40 | onu.GemPortAdded = true |
Matteo Scandolo | 47ef64b | 2020-04-20 14:16:07 -0700 | [diff] [blame] | 41 | onu.Flows = []FlowKey{ |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 42 | {ID: 1, Direction: "upstream"}, |
| 43 | {ID: 2, Direction: "downstream"}, |
Matteo Scandolo | 47ef64b | 2020-04-20 14:16:07 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 46 | _ = onu.InternalState.Event("disable") |
Matteo Scandolo | 47ef64b | 2020-04-20 14:16:07 -0700 | [diff] [blame] | 47 | assert.Equal(t, onu.InternalState.Current(), "disabled") |
| 48 | |
Matteo Scandolo | 328c5f0 | 2020-06-26 14:16:39 -0700 | [diff] [blame] | 49 | assert.Equal(t, onu.GemPortAdded, false) |
Matteo Scandolo | 47ef64b | 2020-04-20 14:16:07 -0700 | [diff] [blame] | 50 | assert.Equal(t, onu.PortNo, uint32(0)) |
| 51 | assert.Equal(t, len(onu.Flows), 0) |
| 52 | } |
| 53 | |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 54 | // check that I can go to auth_started only if |
| 55 | // - the GemPort is set |
| 56 | // - the eapolFlow is received |
| 57 | func Test_Onu_StateMachine_eapol_no_flow(t *testing.T) { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 58 | t.Skip("Needs to be moved in the Service struct") |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame] | 59 | onu := createTestOnu() |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 60 | |
| 61 | onu.InternalState.SetState("enabled") |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 62 | assert.Equal(t, onu.InternalState.Current(), "enabled") |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 63 | |
| 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 Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 73 | func Test_Onu_StateMachine_eapol_no_gem(t *testing.T) { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 74 | t.Skip("Needs to be moved in the Service struct") |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame] | 75 | onu := createTestOnu() |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 76 | |
| 77 | onu.InternalState.SetState("enabled") |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 78 | assert.Equal(t, onu.InternalState.Current(), "enabled") |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 79 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 80 | // fail has no GemPort has been set |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 81 | 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 | |
| 90 | func Test_Onu_StateMachine_eapol_start(t *testing.T) { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 91 | t.Skip("Needs to be moved in the Service struct") |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 92 | onu := createTestOnu() |
| 93 | |
| 94 | onu.InternalState.SetState("enabled") |
| 95 | assert.Equal(t, onu.InternalState.Current(), "enabled") |
| 96 | |
| 97 | // succeed |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 98 | onu.GemPortAdded = true |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 99 | _ = onu.InternalState.Event("start_auth") |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 100 | assert.Equal(t, onu.InternalState.Current(), "auth_started") |
| 101 | } |
| 102 | |
| 103 | func Test_Onu_StateMachine_eapol_states(t *testing.T) { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 104 | t.Skip("Needs to be moved in the Service struct") |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame] | 105 | onu := createTestOnu() |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 106 | |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 107 | onu.GemPortAdded = true |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 108 | |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 109 | onu.InternalState.SetState("auth_started") |
| 110 | |
| 111 | assert.Equal(t, onu.InternalState.Current(), "auth_started") |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 112 | _ = onu.InternalState.Event("eap_start_sent") |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 113 | assert.Equal(t, onu.InternalState.Current(), "eap_start_sent") |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 114 | _ = onu.InternalState.Event("eap_response_identity_sent") |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 115 | assert.Equal(t, onu.InternalState.Current(), "eap_response_identity_sent") |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 116 | _ = onu.InternalState.Event("eap_response_challenge_sent") |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 117 | assert.Equal(t, onu.InternalState.Current(), "eap_response_challenge_sent") |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 118 | _ = onu.InternalState.Event("eap_response_success_received") |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 119 | assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received") |
Matteo Scandolo | 5e081b5 | 2019-11-21 14:34:25 -0800 | [diff] [blame] | 120 | |
| 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 Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 131 | // if auth is set to true we can't go from enabled to dhcp_started |
| 132 | func Test_Onu_StateMachine_dhcp_no_auth(t *testing.T) { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 133 | t.Skip("Needs to be moved in the Service struct") |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 134 | onu := createTestOnu() |
| 135 | |
| 136 | onu.InternalState.SetState("enabled") |
| 137 | assert.Equal(t, onu.InternalState.Current(), "enabled") |
| 138 | |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 139 | 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 |
| 148 | func Test_Onu_StateMachine_dhcp_no_flow(t *testing.T) { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 149 | t.Skip("Needs to be moved in the Service struct") |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 150 | onu := createTestOnu() |
| 151 | |
| 152 | onu.InternalState.SetState("eap_response_success_received") |
| 153 | assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received") |
| 154 | |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 155 | 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 |
| 164 | func Test_Onu_StateMachine_dhcp_no_gem(t *testing.T) { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 165 | t.Skip("Needs to be moved in the Service struct") |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 166 | onu := createTestOnu() |
| 167 | |
| 168 | onu.InternalState.SetState("eap_response_success_received") |
| 169 | assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received") |
| 170 | |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 171 | 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 Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 181 | func Test_Onu_StateMachine_dhcp_start(t *testing.T) { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 182 | t.Skip("Needs to be moved in the Service struct") |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame] | 183 | onu := createTestOnu() |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 184 | onu.GemPortAdded = true |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 185 | |
| 186 | onu.InternalState.SetState("eap_response_success_received") |
| 187 | assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received") |
| 188 | |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 189 | // default transition |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 190 | _ = onu.InternalState.Event("start_dhcp") |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 191 | assert.Equal(t, onu.InternalState.Current(), "dhcp_started") |
| 192 | } |
| 193 | |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 194 | func Test_Onu_StateMachine_dhcp_states(t *testing.T) { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 195 | t.Skip("Needs to be moved in the Service struct") |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame] | 196 | onu := createTestOnu() |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 197 | |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 198 | onu.GemPortAdded = true |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 199 | |
| 200 | onu.InternalState.SetState("dhcp_started") |
| 201 | |
| 202 | assert.Equal(t, onu.InternalState.Current(), "dhcp_started") |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 203 | _ = onu.InternalState.Event("dhcp_discovery_sent") |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 204 | assert.Equal(t, onu.InternalState.Current(), "dhcp_discovery_sent") |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 205 | _ = onu.InternalState.Event("dhcp_request_sent") |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 206 | assert.Equal(t, onu.InternalState.Current(), "dhcp_request_sent") |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 207 | _ = onu.InternalState.Event("dhcp_ack_received") |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 208 | assert.Equal(t, onu.InternalState.Current(), "dhcp_ack_received") |
Matteo Scandolo | 5e081b5 | 2019-11-21 14:34:25 -0800 | [diff] [blame] | 209 | |
| 210 | // test that we can retrigger DHCP |
Matteo Scandolo | 5e081b5 | 2019-11-21 14:34:25 -0800 | [diff] [blame] | 211 | 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 Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 218 | } |