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