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