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{ |
| 42 | FlowKey{ID:1, Direction:"upstream"}, |
| 43 | FlowKey{ID:2, Direction:"downstream"}, |
| 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 | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 54 | func Test_Onu_StateMachine_eapol_start_eap_flow(t *testing.T) { |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame] | 55 | onu := createTestOnu() |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 56 | |
| 57 | onu.InternalState.SetState("enabled") |
| 58 | |
| 59 | // TODO we need to add a check so that you can't go from eapol_flow_received |
| 60 | // to auth_started without passing through gem_port_added |
| 61 | // (see start_dhcp for an example) |
| 62 | |
| 63 | assert.Equal(t, onu.InternalState.Current(), "enabled") |
| 64 | onu.InternalState.Event("receive_eapol_flow") |
| 65 | assert.Equal(t, onu.InternalState.Current(), "eapol_flow_received") |
| 66 | onu.InternalState.Event("add_gem_port") |
| 67 | assert.Equal(t, onu.InternalState.Current(), "gem_port_added") |
| 68 | onu.InternalState.Event("start_auth") |
| 69 | assert.Equal(t, onu.InternalState.Current(), "auth_started") |
| 70 | } |
| 71 | |
| 72 | func Test_Onu_StateMachine_eapol_start_gem_port(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") |
| 76 | |
| 77 | // TODO we need to add a check so that you can't go from gem_port_added |
| 78 | // to auth_started without passing through eapol_flow_received |
| 79 | // (see start_dhcp for an example) |
| 80 | |
| 81 | assert.Equal(t, onu.InternalState.Current(), "enabled") |
| 82 | onu.InternalState.Event("add_gem_port") |
| 83 | assert.Equal(t, onu.InternalState.Current(), "gem_port_added") |
| 84 | onu.InternalState.Event("receive_eapol_flow") |
| 85 | assert.Equal(t, onu.InternalState.Current(), "eapol_flow_received") |
| 86 | onu.InternalState.Event("start_auth") |
| 87 | assert.Equal(t, onu.InternalState.Current(), "auth_started") |
| 88 | } |
| 89 | |
| 90 | func Test_Onu_StateMachine_eapol_states(t *testing.T) { |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame] | 91 | onu := createTestOnu() |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 92 | |
| 93 | onu.InternalState.SetState("auth_started") |
| 94 | |
| 95 | assert.Equal(t, onu.InternalState.Current(), "auth_started") |
| 96 | onu.InternalState.Event("eap_start_sent") |
| 97 | assert.Equal(t, onu.InternalState.Current(), "eap_start_sent") |
| 98 | onu.InternalState.Event("eap_response_identity_sent") |
| 99 | assert.Equal(t, onu.InternalState.Current(), "eap_response_identity_sent") |
| 100 | onu.InternalState.Event("eap_response_challenge_sent") |
| 101 | assert.Equal(t, onu.InternalState.Current(), "eap_response_challenge_sent") |
| 102 | onu.InternalState.Event("eap_response_success_received") |
| 103 | assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received") |
Matteo Scandolo | 5e081b5 | 2019-11-21 14:34:25 -0800 | [diff] [blame] | 104 | |
| 105 | // test that we can retrigger EAPOL |
| 106 | states := []string{"eap_start_sent", "eap_response_identity_sent", "eap_response_challenge_sent", "eap_response_success_received", "auth_failed", "dhcp_ack_received", "dhcp_failed"} |
| 107 | for _, state := range states { |
| 108 | onu.InternalState.SetState(state) |
| 109 | err := onu.InternalState.Event("start_auth") |
| 110 | assert.Equal(t, err, nil) |
| 111 | assert.Equal(t, onu.InternalState.Current(), "auth_started") |
| 112 | } |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | func Test_Onu_StateMachine_dhcp_start(t *testing.T) { |
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 | onu.DhcpFlowReceived = true |
| 118 | |
| 119 | onu.InternalState.SetState("eap_response_success_received") |
| 120 | assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received") |
| 121 | |
| 122 | onu.InternalState.Event("start_dhcp") |
| 123 | |
| 124 | assert.Equal(t, onu.InternalState.Current(), "dhcp_started") |
| 125 | } |
| 126 | |
| 127 | func Test_Onu_StateMachine_dhcp_start_error(t *testing.T) { |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame] | 128 | onu := createTestOnu() |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 129 | |
| 130 | onu.InternalState.SetState("eap_response_success_received") |
| 131 | assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received") |
| 132 | |
| 133 | err := onu.InternalState.Event("start_dhcp") |
| 134 | |
| 135 | assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received") |
| 136 | assert.Equal(t, err.Error(), "transition canceled with error: cannot-go-to-dhcp-started-as-dhcp-flow-is-missing") |
| 137 | } |
| 138 | |
| 139 | func Test_Onu_StateMachine_dhcp_states(t *testing.T) { |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame] | 140 | onu := createTestOnu() |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 141 | |
| 142 | onu.DhcpFlowReceived = false |
| 143 | |
| 144 | onu.InternalState.SetState("dhcp_started") |
| 145 | |
| 146 | assert.Equal(t, onu.InternalState.Current(), "dhcp_started") |
| 147 | onu.InternalState.Event("dhcp_discovery_sent") |
| 148 | assert.Equal(t, onu.InternalState.Current(), "dhcp_discovery_sent") |
| 149 | onu.InternalState.Event("dhcp_request_sent") |
| 150 | assert.Equal(t, onu.InternalState.Current(), "dhcp_request_sent") |
| 151 | onu.InternalState.Event("dhcp_ack_received") |
| 152 | assert.Equal(t, onu.InternalState.Current(), "dhcp_ack_received") |
Matteo Scandolo | 5e081b5 | 2019-11-21 14:34:25 -0800 | [diff] [blame] | 153 | |
| 154 | // test that we can retrigger DHCP |
| 155 | onu.DhcpFlowReceived = true |
| 156 | states := []string{"eap_response_success_received", "dhcp_discovery_sent", "dhcp_request_sent", "dhcp_ack_received", "dhcp_failed"} |
| 157 | for _, state := range states { |
| 158 | onu.InternalState.SetState(state) |
| 159 | err := onu.InternalState.Event("start_dhcp") |
| 160 | assert.Equal(t, err, nil) |
| 161 | assert.Equal(t, onu.InternalState.Current(), "dhcp_started") |
| 162 | } |
Matteo Scandolo | 99f1846 | 2019-10-28 14:14:28 -0700 | [diff] [blame] | 163 | } |