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") |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 28 | 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 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 |
| 40 | onu.DhcpFlowReceived = true |
Matteo Scandolo | 328c5f0 | 2020-06-26 14:16:39 -0700 | [diff] [blame^] | 41 | onu.EapolFlowReceived = true |
| 42 | onu.GemPortAdded = true |
Matteo Scandolo | 47ef64b | 2020-04-20 14:16:07 -0700 | [diff] [blame] | 43 | onu.Flows = []FlowKey{ |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 44 | FlowKey{ID: 1, Direction: "upstream"}, |
| 45 | FlowKey{ID: 2, Direction: "downstream"}, |
Matteo Scandolo | 47ef64b | 2020-04-20 14:16:07 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | onu.InternalState.Event("disable") |
| 49 | assert.Equal(t, onu.InternalState.Current(), "disabled") |
| 50 | |
| 51 | assert.Equal(t, onu.DhcpFlowReceived, false) |
Matteo Scandolo | 328c5f0 | 2020-06-26 14:16:39 -0700 | [diff] [blame^] | 52 | assert.Equal(t, onu.EapolFlowReceived, false) |
| 53 | assert.Equal(t, onu.GemPortAdded, false) |
Matteo Scandolo | 47ef64b | 2020-04-20 14:16:07 -0700 | [diff] [blame] | 54 | assert.Equal(t, onu.PortNo, uint32(0)) |
| 55 | assert.Equal(t, len(onu.Flows), 0) |
| 56 | } |
| 57 | |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 58 | // check that I can go to auth_started only if |
| 59 | // - the GemPort is set |
| 60 | // - the eapolFlow is received |
| 61 | func Test_Onu_StateMachine_eapol_no_flow(t *testing.T) { |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame] | 62 | onu := createTestOnu() |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 63 | |
| 64 | onu.InternalState.SetState("enabled") |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 65 | assert.Equal(t, onu.InternalState.Current(), "enabled") |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 66 | |
| 67 | // fail as no EapolFlow has been received |
| 68 | err := onu.InternalState.Event("start_auth") |
| 69 | if err == nil { |
| 70 | t.Fatal("can't start EAPOL without EapolFlow") |
| 71 | } |
| 72 | assert.Equal(t, onu.InternalState.Current(), "enabled") |
| 73 | 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] | 74 | } |
| 75 | |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 76 | func Test_Onu_StateMachine_eapol_no_gem(t *testing.T) { |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame] | 77 | onu := createTestOnu() |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 78 | |
| 79 | onu.InternalState.SetState("enabled") |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 80 | assert.Equal(t, onu.InternalState.Current(), "enabled") |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 81 | // fail has no GemPort has been set |
| 82 | onu.EapolFlowReceived = true |
| 83 | |
| 84 | err := onu.InternalState.Event("start_auth") |
| 85 | if err == nil { |
| 86 | t.Fatal("can't start EAPOL without GemPort") |
| 87 | } |
| 88 | assert.Equal(t, onu.InternalState.Current(), "enabled") |
| 89 | assert.Equal(t, err.Error(), "transition canceled with error: cannot-go-to-auth-started-as-gemport-is-missing") |
| 90 | |
| 91 | } |
| 92 | |
| 93 | func Test_Onu_StateMachine_eapol_start(t *testing.T) { |
| 94 | |
| 95 | onu := createTestOnu() |
| 96 | |
| 97 | onu.InternalState.SetState("enabled") |
| 98 | assert.Equal(t, onu.InternalState.Current(), "enabled") |
| 99 | |
| 100 | // succeed |
| 101 | onu.EapolFlowReceived = true |
| 102 | onu.GemPortAdded = true |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 103 | onu.InternalState.Event("start_auth") |
| 104 | assert.Equal(t, onu.InternalState.Current(), "auth_started") |
| 105 | } |
| 106 | |
| 107 | func Test_Onu_StateMachine_eapol_states(t *testing.T) { |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame] | 108 | onu := createTestOnu() |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 109 | |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 110 | onu.GemPortAdded = true |
| 111 | onu.EapolFlowReceived = true |
| 112 | |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 113 | onu.InternalState.SetState("auth_started") |
| 114 | |
| 115 | assert.Equal(t, onu.InternalState.Current(), "auth_started") |
| 116 | onu.InternalState.Event("eap_start_sent") |
| 117 | assert.Equal(t, onu.InternalState.Current(), "eap_start_sent") |
| 118 | onu.InternalState.Event("eap_response_identity_sent") |
| 119 | assert.Equal(t, onu.InternalState.Current(), "eap_response_identity_sent") |
| 120 | onu.InternalState.Event("eap_response_challenge_sent") |
| 121 | assert.Equal(t, onu.InternalState.Current(), "eap_response_challenge_sent") |
| 122 | onu.InternalState.Event("eap_response_success_received") |
| 123 | assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received") |
Matteo Scandolo | 5e081b5 | 2019-11-21 14:34:25 -0800 | [diff] [blame] | 124 | |
| 125 | // test that we can retrigger EAPOL |
| 126 | states := []string{"eap_start_sent", "eap_response_identity_sent", "eap_response_challenge_sent", "eap_response_success_received", "auth_failed", "dhcp_ack_received", "dhcp_failed"} |
| 127 | for _, state := range states { |
| 128 | onu.InternalState.SetState(state) |
| 129 | err := onu.InternalState.Event("start_auth") |
| 130 | assert.Equal(t, err, nil) |
| 131 | assert.Equal(t, onu.InternalState.Current(), "auth_started") |
| 132 | } |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 135 | // if auth is set to true we can't go from enabled to dhcp_started |
| 136 | func Test_Onu_StateMachine_dhcp_no_auth(t *testing.T) { |
| 137 | onu := createTestOnu() |
| 138 | |
| 139 | onu.InternalState.SetState("enabled") |
| 140 | assert.Equal(t, onu.InternalState.Current(), "enabled") |
| 141 | |
| 142 | onu.Auth = true |
| 143 | |
| 144 | err := onu.InternalState.Event("start_dhcp") |
| 145 | if err == nil { |
| 146 | t.Fail() |
| 147 | } |
| 148 | assert.Equal(t, onu.InternalState.Current(), "enabled") |
| 149 | assert.Equal(t, err.Error(), "transition canceled with error: cannot-go-to-dhcp-started-as-authentication-is-required") |
| 150 | } |
| 151 | |
| 152 | // if the DHCP flow has not been received we can't start authentication |
| 153 | func Test_Onu_StateMachine_dhcp_no_flow(t *testing.T) { |
| 154 | onu := createTestOnu() |
| 155 | |
| 156 | onu.InternalState.SetState("eap_response_success_received") |
| 157 | assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received") |
| 158 | |
| 159 | onu.DhcpFlowReceived = false |
| 160 | |
| 161 | err := onu.InternalState.Event("start_dhcp") |
| 162 | if err == nil { |
| 163 | t.Fail() |
| 164 | } |
| 165 | assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received") |
| 166 | assert.Equal(t, err.Error(), "transition canceled with error: cannot-go-to-dhcp-started-as-dhcp-flow-is-missing") |
| 167 | } |
| 168 | |
| 169 | // if the ONU does not have a GemPort we can't start DHCP |
| 170 | func Test_Onu_StateMachine_dhcp_no_gem(t *testing.T) { |
| 171 | onu := createTestOnu() |
| 172 | |
| 173 | onu.InternalState.SetState("eap_response_success_received") |
| 174 | assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received") |
| 175 | |
| 176 | onu.DhcpFlowReceived = true |
| 177 | onu.GemPortAdded = false |
| 178 | |
| 179 | err := onu.InternalState.Event("start_dhcp") |
| 180 | if err == nil { |
| 181 | t.Fail() |
| 182 | } |
| 183 | assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received") |
| 184 | assert.Equal(t, err.Error(), "transition canceled with error: cannot-go-to-dhcp-started-as-gemport-is-missing") |
| 185 | } |
| 186 | |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 187 | func Test_Onu_StateMachine_dhcp_start(t *testing.T) { |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame] | 188 | onu := createTestOnu() |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 189 | onu.DhcpFlowReceived = true |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 190 | onu.GemPortAdded = true |
| 191 | onu.Auth = true |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 192 | |
| 193 | onu.InternalState.SetState("eap_response_success_received") |
| 194 | assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received") |
| 195 | |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 196 | // default transition |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 197 | onu.InternalState.Event("start_dhcp") |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 198 | assert.Equal(t, onu.InternalState.Current(), "dhcp_started") |
| 199 | } |
| 200 | |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 201 | func Test_Onu_StateMachine_dhcp_states(t *testing.T) { |
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 | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 204 | onu.DhcpFlowReceived = true |
| 205 | onu.GemPortAdded = true |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 206 | |
| 207 | onu.InternalState.SetState("dhcp_started") |
| 208 | |
| 209 | assert.Equal(t, onu.InternalState.Current(), "dhcp_started") |
| 210 | onu.InternalState.Event("dhcp_discovery_sent") |
| 211 | assert.Equal(t, onu.InternalState.Current(), "dhcp_discovery_sent") |
| 212 | onu.InternalState.Event("dhcp_request_sent") |
| 213 | assert.Equal(t, onu.InternalState.Current(), "dhcp_request_sent") |
| 214 | onu.InternalState.Event("dhcp_ack_received") |
| 215 | assert.Equal(t, onu.InternalState.Current(), "dhcp_ack_received") |
Matteo Scandolo | 5e081b5 | 2019-11-21 14:34:25 -0800 | [diff] [blame] | 216 | |
| 217 | // test that we can retrigger DHCP |
Matteo Scandolo | 5e081b5 | 2019-11-21 14:34:25 -0800 | [diff] [blame] | 218 | states := []string{"eap_response_success_received", "dhcp_discovery_sent", "dhcp_request_sent", "dhcp_ack_received", "dhcp_failed"} |
| 219 | for _, state := range states { |
| 220 | onu.InternalState.SetState(state) |
| 221 | err := onu.InternalState.Event("start_dhcp") |
| 222 | assert.Equal(t, err, nil) |
| 223 | assert.Equal(t, onu.InternalState.Current(), "dhcp_started") |
| 224 | } |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 225 | } |