blob: 239869b78c3536858fad93ae774b006b00c42f12 [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) {
25 olt := OltDevice{
26 ID: 0,
27 }
28 pon := PonPort{
29 ID: 1,
30 }
31 onu := CreateONU(olt, pon, 1, 900, 900)
32
33 assert.Equal(t, onu.InternalState.Current(), "created")
34 onu.InternalState.Event("discover")
35 assert.Equal(t, onu.InternalState.Current(), "discovered")
36 onu.InternalState.Event("enable")
37 assert.Equal(t, onu.InternalState.Current(), "enabled")
38}
39
40func Test_Onu_StateMachine_eapol_start_eap_flow(t *testing.T) {
41 olt := OltDevice{
42 ID: 0,
43 }
44 pon := PonPort{
45 ID: 1,
46 }
47 onu := CreateONU(olt, pon, 1, 900, 900)
48
49 onu.InternalState.SetState("enabled")
50
51 // TODO we need to add a check so that you can't go from eapol_flow_received
52 // to auth_started without passing through gem_port_added
53 // (see start_dhcp for an example)
54
55 assert.Equal(t, onu.InternalState.Current(), "enabled")
56 onu.InternalState.Event("receive_eapol_flow")
57 assert.Equal(t, onu.InternalState.Current(), "eapol_flow_received")
58 onu.InternalState.Event("add_gem_port")
59 assert.Equal(t, onu.InternalState.Current(), "gem_port_added")
60 onu.InternalState.Event("start_auth")
61 assert.Equal(t, onu.InternalState.Current(), "auth_started")
62}
63
64func Test_Onu_StateMachine_eapol_start_gem_port(t *testing.T) {
65 olt := OltDevice{
66 ID: 0,
67 }
68 pon := PonPort{
69 ID: 1,
70 }
71 onu := CreateONU(olt, pon, 1, 900, 900)
72
73 onu.InternalState.SetState("enabled")
74
75 // TODO we need to add a check so that you can't go from gem_port_added
76 // to auth_started without passing through eapol_flow_received
77 // (see start_dhcp for an example)
78
79 assert.Equal(t, onu.InternalState.Current(), "enabled")
80 onu.InternalState.Event("add_gem_port")
81 assert.Equal(t, onu.InternalState.Current(), "gem_port_added")
82 onu.InternalState.Event("receive_eapol_flow")
83 assert.Equal(t, onu.InternalState.Current(), "eapol_flow_received")
84 onu.InternalState.Event("start_auth")
85 assert.Equal(t, onu.InternalState.Current(), "auth_started")
86}
87
88func Test_Onu_StateMachine_eapol_states(t *testing.T) {
89 olt := OltDevice{
90 ID: 0,
91 }
92 pon := PonPort{
93 ID: 1,
94 }
95 onu := CreateONU(olt, pon, 1, 900, 900)
96
97 onu.InternalState.SetState("auth_started")
98
99 assert.Equal(t, onu.InternalState.Current(), "auth_started")
100 onu.InternalState.Event("eap_start_sent")
101 assert.Equal(t, onu.InternalState.Current(), "eap_start_sent")
102 onu.InternalState.Event("eap_response_identity_sent")
103 assert.Equal(t, onu.InternalState.Current(), "eap_response_identity_sent")
104 onu.InternalState.Event("eap_response_challenge_sent")
105 assert.Equal(t, onu.InternalState.Current(), "eap_response_challenge_sent")
106 onu.InternalState.Event("eap_response_success_received")
107 assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received")
108}
109
110func Test_Onu_StateMachine_dhcp_start(t *testing.T) {
111 olt := OltDevice{
112 ID: 0,
113 }
114 pon := PonPort{
115 ID: 1,
116 }
117 onu := CreateONU(olt, pon, 1, 900, 900)
118 onu.DhcpFlowReceived = true
119
120 onu.InternalState.SetState("eap_response_success_received")
121 assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received")
122
123 onu.InternalState.Event("start_dhcp")
124
125 assert.Equal(t, onu.InternalState.Current(), "dhcp_started")
126}
127
128func Test_Onu_StateMachine_dhcp_start_error(t *testing.T) {
129 olt := OltDevice{
130 ID: 0,
131 }
132 pon := PonPort{
133 ID: 1,
134 }
135 onu := CreateONU(olt, pon, 1, 900, 900)
136
137 onu.InternalState.SetState("eap_response_success_received")
138 assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received")
139
140 err := onu.InternalState.Event("start_dhcp")
141
142 assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received")
143 assert.Equal(t, err.Error(), "transition canceled with error: cannot-go-to-dhcp-started-as-dhcp-flow-is-missing")
144}
145
146func Test_Onu_StateMachine_dhcp_states(t *testing.T) {
147 olt := OltDevice{
148 ID: 0,
149 }
150 pon := PonPort{
151 ID: 1,
152 }
153 onu := CreateONU(olt, pon, 1, 900, 900)
154
155 onu.DhcpFlowReceived = false
156
157 onu.InternalState.SetState("dhcp_started")
158
159 assert.Equal(t, onu.InternalState.Current(), "dhcp_started")
160 onu.InternalState.Event("dhcp_discovery_sent")
161 assert.Equal(t, onu.InternalState.Current(), "dhcp_discovery_sent")
162 onu.InternalState.Event("dhcp_request_sent")
163 assert.Equal(t, onu.InternalState.Current(), "dhcp_request_sent")
164 onu.InternalState.Event("dhcp_ack_received")
165 assert.Equal(t, onu.InternalState.Current(), "dhcp_ack_received")
166}