blob: 4ceded978e176316d250bdd5b79324082fcfbf02 [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
34func Test_Onu_StateMachine_eapol_start_eap_flow(t *testing.T) {
Matteo Scandoloc1147092019-10-29 09:38:33 -070035 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -070036
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
52func Test_Onu_StateMachine_eapol_start_gem_port(t *testing.T) {
Matteo Scandoloc1147092019-10-29 09:38:33 -070053 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -070054
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
70func Test_Onu_StateMachine_eapol_states(t *testing.T) {
Matteo Scandoloc1147092019-10-29 09:38:33 -070071 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -070072
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 Scandolo5e081b52019-11-21 14:34:25 -080084
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 Scandolo99f18462019-10-28 14:14:28 -070093}
94
95func Test_Onu_StateMachine_dhcp_start(t *testing.T) {
Matteo Scandoloc1147092019-10-29 09:38:33 -070096 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -070097 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
107func Test_Onu_StateMachine_dhcp_start_error(t *testing.T) {
Matteo Scandoloc1147092019-10-29 09:38:33 -0700108 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -0700109
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
119func Test_Onu_StateMachine_dhcp_states(t *testing.T) {
Matteo Scandoloc1147092019-10-29 09:38:33 -0700120 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -0700121
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 Scandolo5e081b52019-11-21 14:34:25 -0800133
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 Scandolo99f18462019-10-28 14:14:28 -0700143}