blob: c8ad84c808970c555527a99230c4b358254bfa2c [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 (
20 "gotest.tools/assert"
21 "testing"
22)
23
24func Test_Onu_StateMachine_enable(t *testing.T) {
Matteo Scandoloc1147092019-10-29 09:38:33 -070025 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -070026
27 assert.Equal(t, onu.InternalState.Current(), "created")
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
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")
84}
85
86func Test_Onu_StateMachine_dhcp_start(t *testing.T) {
Matteo Scandoloc1147092019-10-29 09:38:33 -070087 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -070088 onu.DhcpFlowReceived = true
89
90 onu.InternalState.SetState("eap_response_success_received")
91 assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received")
92
93 onu.InternalState.Event("start_dhcp")
94
95 assert.Equal(t, onu.InternalState.Current(), "dhcp_started")
96}
97
98func Test_Onu_StateMachine_dhcp_start_error(t *testing.T) {
Matteo Scandoloc1147092019-10-29 09:38:33 -070099 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -0700100
101 onu.InternalState.SetState("eap_response_success_received")
102 assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received")
103
104 err := onu.InternalState.Event("start_dhcp")
105
106 assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received")
107 assert.Equal(t, err.Error(), "transition canceled with error: cannot-go-to-dhcp-started-as-dhcp-flow-is-missing")
108}
109
110func Test_Onu_StateMachine_dhcp_states(t *testing.T) {
Matteo Scandoloc1147092019-10-29 09:38:33 -0700111 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -0700112
113 onu.DhcpFlowReceived = false
114
115 onu.InternalState.SetState("dhcp_started")
116
117 assert.Equal(t, onu.InternalState.Current(), "dhcp_started")
118 onu.InternalState.Event("dhcp_discovery_sent")
119 assert.Equal(t, onu.InternalState.Current(), "dhcp_discovery_sent")
120 onu.InternalState.Event("dhcp_request_sent")
121 assert.Equal(t, onu.InternalState.Current(), "dhcp_request_sent")
122 onu.InternalState.Event("dhcp_ack_received")
123 assert.Equal(t, onu.InternalState.Current(), "dhcp_ack_received")
124}