blob: 0bf4bed6e2956b3cdafcd0bbe3721b3f599ade9c [file] [log] [blame]
Matteo Scandolo99f18462019-10-28 14:14:28 -07001/*
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
17package devices
18
19import (
Matteo Scandolo99f18462019-10-28 14:14:28 -070020 "testing"
Zdravko Bozakov681364d2019-11-10 14:28:46 +010021
22 "gotest.tools/assert"
Matteo Scandolo99f18462019-10-28 14:14:28 -070023)
24
25func Test_Onu_StateMachine_enable(t *testing.T) {
Matteo Scandoloc1147092019-10-29 09:38:33 -070026 onu := createTestOnu()
Zdravko Bozakov681364d2019-11-10 14:28:46 +010027 assert.Equal(t, onu.InternalState.Current(), "initialized")
Matteo Scandolo99f18462019-10-28 14:14:28 -070028 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 Scandolo47ef64b2020-04-20 14:16:07 -070034func 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 Scandolo99f18462019-10-28 14:14:28 -070054func Test_Onu_StateMachine_eapol_start_eap_flow(t *testing.T) {
Matteo Scandoloc1147092019-10-29 09:38:33 -070055 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -070056
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
72func Test_Onu_StateMachine_eapol_start_gem_port(t *testing.T) {
Matteo Scandoloc1147092019-10-29 09:38:33 -070073 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -070074
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
90func Test_Onu_StateMachine_eapol_states(t *testing.T) {
Matteo Scandoloc1147092019-10-29 09:38:33 -070091 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -070092
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 Scandolo5e081b52019-11-21 14:34:25 -0800104
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 Scandolo99f18462019-10-28 14:14:28 -0700113}
114
115func Test_Onu_StateMachine_dhcp_start(t *testing.T) {
Matteo Scandoloc1147092019-10-29 09:38:33 -0700116 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -0700117 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
127func Test_Onu_StateMachine_dhcp_start_error(t *testing.T) {
Matteo Scandoloc1147092019-10-29 09:38:33 -0700128 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -0700129
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
139func Test_Onu_StateMachine_dhcp_states(t *testing.T) {
Matteo Scandoloc1147092019-10-29 09:38:33 -0700140 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -0700141
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 Scandolo5e081b52019-11-21 14:34:25 -0800153
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 Scandolo99f18462019-10-28 14:14:28 -0700163}