blob: 548a14ab3b5bdfa958bf465d46138bf6802cb666 [file] [log] [blame]
Matteo Scandolo4a036262020-08-17 15:56:13 -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 Scandoloe9f5a6b2020-09-16 15:54:38 -070020 "context"
Matteo Scandolo4a036262020-08-17 15:56:13 -070021 "github.com/opencord/bbsim/internal/bbsim/types"
Matteo Scandolo76c3ff62020-09-24 18:03:01 -070022 "github.com/opencord/bbsim/internal/common"
Matteo Scandolo618a6582020-09-09 12:21:29 -070023 "github.com/opencord/voltha-protos/v3/go/openolt"
Matteo Scandolo75ed5b92020-09-03 09:03:16 -070024 "github.com/stretchr/testify/assert"
Matteo Scandolo4a036262020-08-17 15:56:13 -070025 "net"
26 "testing"
Matteo Scandoloadc72a82020-09-08 18:46:08 -070027 "time"
Matteo Scandolo4a036262020-08-17 15:56:13 -070028)
29
30type mockService struct {
31 Name string
32 HandleAuthCallCount int
33 HandleDhcpCallCount int
34 HandlePacketsCallCount int
35}
36
Matteo Scandoloadc72a82020-09-08 18:46:08 -070037func (s *mockService) HandleAuth() {
Matteo Scandolo4a036262020-08-17 15:56:13 -070038 s.HandleAuthCallCount = s.HandleAuthCallCount + 1
39}
40
Matteo Scandolobd875b32020-09-18 17:46:31 -070041func (s *mockService) HandleDhcp(pbit uint8, cTag int) {
Matteo Scandolo4a036262020-08-17 15:56:13 -070042 s.HandleDhcpCallCount = s.HandleDhcpCallCount + 1
43}
44
Matteo Scandoloadc72a82020-09-08 18:46:08 -070045func (s *mockService) HandlePackets() {
Matteo Scandolo4a036262020-08-17 15:56:13 -070046 s.HandlePacketsCallCount = s.HandlePacketsCallCount + 1
47}
48
Matteo Scandoloadc72a82020-09-08 18:46:08 -070049func (s *mockService) Initialize(stream types.Stream) {}
50func (s *mockService) Disable() {}
Matteo Scandolo75ed5b92020-09-03 09:03:16 -070051
Matteo Scandoloe9f5a6b2020-09-16 15:54:38 -070052func createTestService(needsEapol bool, needsDchp bool) (*Service, error) {
53
54 enableContext := context.TODO()
55
Matteo Scandolo75ed5b92020-09-03 09:03:16 -070056 mac := net.HardwareAddr{0x2e, 0x60, byte(1), byte(1), byte(1), byte(1)}
57 onu := createMockOnu(1, 1)
Matteo Scandoloe9f5a6b2020-09-16 15:54:38 -070058 onu.PonPort = &PonPort{}
59 onu.PonPort.Olt = &OltDevice{}
60 onu.PonPort.Olt.enableContext = enableContext
61 return NewService("testService", mac, onu, 900, 900,
62 needsEapol, needsDchp, false, 64, 0, false,
Matteo Scandolobd875b32020-09-18 17:46:31 -070063 7, 7, 7, 7)
Matteo Scandoloe9f5a6b2020-09-16 15:54:38 -070064}
65
66// test the internalState transitions
67func TestService_InternalState(t *testing.T) {
68 s, err := createTestService(false, false)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -070069
70 assert.Nil(t, err)
71
72 assert.Empty(t, s.PacketCh)
Matteo Scandoloadc72a82020-09-08 18:46:08 -070073 s.Initialize(&mockStream{})
Matteo Scandolo75ed5b92020-09-03 09:03:16 -070074
Matteo Scandoloadc72a82020-09-08 18:46:08 -070075 // check that channels have been created
Matteo Scandolo75ed5b92020-09-03 09:03:16 -070076 assert.NotNil(t, s.PacketCh)
Matteo Scandoloadc72a82020-09-08 18:46:08 -070077 assert.NotNil(t, s.Channel)
78
79 // set EAPOL and DHCP states to something else
80 s.EapolState.SetState("eap_response_success_received")
81 s.DHCPState.SetState("dhcp_ack_received")
Matteo Scandolo75ed5b92020-09-03 09:03:16 -070082
83 s.Disable()
Matteo Scandoloadc72a82020-09-08 18:46:08 -070084 // make sure the EAPOL and DHCP states have been reset after disable
Matteo Scandolo75ed5b92020-09-03 09:03:16 -070085 assert.Equal(t, "created", s.EapolState.Current())
86 assert.Equal(t, "created", s.DHCPState.Current())
Matteo Scandoloadc72a82020-09-08 18:46:08 -070087
88 // make sure the channel have been closed
89 assert.Nil(t, s.Channel)
90 assert.Nil(t, s.PacketCh)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -070091}
92
93// make sure that if the service does not need EAPOL we're not sending any packet
Matteo Scandolo4a036262020-08-17 15:56:13 -070094func TestService_HandleAuth_noEapol(t *testing.T) {
Matteo Scandoloe9f5a6b2020-09-16 15:54:38 -070095 s, err := createTestService(false, false)
Matteo Scandolo4a036262020-08-17 15:56:13 -070096
Matteo Scandolo75ed5b92020-09-03 09:03:16 -070097 assert.Nil(t, err)
Matteo Scandolo4a036262020-08-17 15:56:13 -070098
99 stream := &mockStream{
100 Calls: make(map[int]*openolt.Indication),
101 channel: make(chan int, 10),
102 }
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700103 s.Initialize(stream)
Matteo Scandolo4a036262020-08-17 15:56:13 -0700104
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700105 s.HandleAuth()
106 time.Sleep(1 * time.Second)
Matteo Scandolo4a036262020-08-17 15:56:13 -0700107
108 // if the service does not need EAPOL we don't expect any packet to be generated
109 assert.Equal(t, stream.CallCount, 0)
110
111 // state should not change
112 assert.Equal(t, s.EapolState.Current(), "created")
113}
114
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700115// make sure that if the service does need EAPOL we're sending any packet
Matteo Scandolo4a036262020-08-17 15:56:13 -0700116func TestService_HandleAuth_withEapol(t *testing.T) {
Matteo Scandoloe9f5a6b2020-09-16 15:54:38 -0700117 s, err := createTestService(true, false)
Matteo Scandolo4a036262020-08-17 15:56:13 -0700118
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700119 assert.Nil(t, err)
Matteo Scandolo4a036262020-08-17 15:56:13 -0700120
121 stream := &mockStream{
122 Calls: make(map[int]*openolt.Indication),
123 }
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700124 s.Initialize(stream)
Matteo Scandolo4a036262020-08-17 15:56:13 -0700125
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700126 s.HandleAuth()
127 time.Sleep(1 * time.Second)
Matteo Scandolo4a036262020-08-17 15:56:13 -0700128
129 // if the service does not need EAPOL we don't expect any packet to be generated
130 assert.Equal(t, stream.CallCount, 1)
131
132 // state should not change
133 assert.Equal(t, s.EapolState.Current(), "eap_start_sent")
134}
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700135
136// make sure that if the service does not need DHCP we're not sending any packet
137func TestService_HandleDhcp_not_needed(t *testing.T) {
Matteo Scandoloe9f5a6b2020-09-16 15:54:38 -0700138 s, err := createTestService(false, false)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700139
140 assert.Nil(t, err)
141
142 stream := &mockStream{
143 Calls: make(map[int]*openolt.Indication),
144 }
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700145 s.Initialize(stream)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700146
Matteo Scandolobd875b32020-09-18 17:46:31 -0700147 s.HandleDhcp(7, 900)
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700148 time.Sleep(1 * time.Second)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700149
150 assert.Equal(t, stream.CallCount, 0)
151
152 // state should not change
153 assert.Equal(t, s.DHCPState.Current(), "created")
154}
155
156// when we receive a DHCP flow we call HandleDhcp an all the ONU Services
157// each service device whether the tag matches it's own configuration
158func TestService_HandleDhcp_different_c_Tag(t *testing.T) {
Matteo Scandoloe9f5a6b2020-09-16 15:54:38 -0700159 s, err := createTestService(false, true)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700160
161 assert.Nil(t, err)
162
163 stream := &mockStream{
164 Calls: make(map[int]*openolt.Indication),
165 }
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700166 s.Initialize(stream)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700167
168 // NOTE that the c_tag is different from the one configured in the service
Matteo Scandolobd875b32020-09-18 17:46:31 -0700169 s.HandleDhcp(7, 800)
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700170 time.Sleep(1 * time.Second)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700171
172 assert.Equal(t, stream.CallCount, 0)
173
174 // state should not change
175 assert.Equal(t, s.DHCPState.Current(), "created")
176}
177
Matteo Scandolobd875b32020-09-18 17:46:31 -0700178// when we receive a DHCP flow we call HandleDhcp an all the ONU Services
179// each service device whether the tag matches it's own configuration
180func TestService_HandleDhcp_different_pbit(t *testing.T) {
181 s, err := createTestService(false, true)
182
183 assert.Nil(t, err)
184
185 stream := &mockStream{
186 Calls: make(map[int]*openolt.Indication),
187 }
188 s.Initialize(stream)
189
190 // NOTE that the c_tag is different from the one configured in the service
191 s.HandleDhcp(5, 900)
192 time.Sleep(1 * time.Second)
193
194 assert.Equal(t, stream.CallCount, 0)
195
196 // state should not change
197 assert.Equal(t, s.DHCPState.Current(), "created")
198}
199
200// if PBIT is 255 it means all of them, so start DHCP if the C_TAG matches
201func TestService_HandleDhcp_pbit_255(t *testing.T) {
202 s, err := createTestService(false, true)
203
204 assert.Nil(t, err)
205
206 stream := &mockStream{
207 Calls: make(map[int]*openolt.Indication),
208 }
209 s.Initialize(stream)
210
211 // NOTE that the c_tag is different from the one configured in the service
212 s.HandleDhcp(255, 900)
213 time.Sleep(1 * time.Second)
214
215 assert.Equal(t, 1, stream.CallCount)
216 assert.Equal(t, s.DHCPState.Current(), "dhcp_discovery_sent")
217}
218
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700219// make sure that if the service does need DHCP we're sending any packet
220func TestService_HandleDhcp_needed(t *testing.T) {
Matteo Scandoloe9f5a6b2020-09-16 15:54:38 -0700221 s, err := createTestService(false, true)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700222
223 assert.Nil(t, err)
224
225 stream := &mockStream{
226 Calls: make(map[int]*openolt.Indication),
227 }
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700228 s.Initialize(stream)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700229
Matteo Scandolobd875b32020-09-18 17:46:31 -0700230 s.HandleDhcp(7, 900)
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700231 time.Sleep(1 * time.Second)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700232
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700233 assert.Equal(t, 1, stream.CallCount)
234 assert.Equal(t, "dhcp_discovery_sent", s.DHCPState.Current())
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700235}
Matteo Scandoloe9f5a6b2020-09-16 15:54:38 -0700236
237// Test that if the EAPOL state machine doesn't complete in 30 seconds we
238// move it to EAPOL failed
239func TestService_EAPOLFailed(t *testing.T) {
Matteo Scandolo76c3ff62020-09-24 18:03:01 -0700240
241 common.Config = &common.GlobalConfig{
242 BBSim: common.BBSimConfig{
243 AuthRetry: false,
244 },
245 }
246
Matteo Scandoloe9f5a6b2020-09-16 15:54:38 -0700247 // override the default wait time
248 eapolWaitTime = 500 * time.Millisecond
249 s, err := createTestService(true, false)
250
251 assert.Nil(t, err)
252
253 stream := &mockStream{
254 Calls: make(map[int]*openolt.Indication),
255 }
256 s.Initialize(stream)
257
258 // set to failed if timeout occurs
259 _ = s.EapolState.Event("start_auth")
260 time.Sleep(1 * time.Second)
261 assert.Equal(t, "auth_failed", s.EapolState.Current())
262
263 // do not set to failed if succeeded
264 s.EapolState.SetState("created")
265 _ = s.EapolState.Event("start_auth")
266 s.EapolState.SetState("eap_response_success_received")
267 time.Sleep(1 * time.Second)
268 assert.Equal(t, "eap_response_success_received", s.EapolState.Current())
269
270}
271
Matteo Scandolo76c3ff62020-09-24 18:03:01 -0700272func TestService_EAPOLRestart(t *testing.T) {
273
274 common.Config = &common.GlobalConfig{
275 BBSim: common.BBSimConfig{
276 AuthRetry: true,
277 },
278 }
279
280 eapolWaitTime = 500 * time.Millisecond
281 s, err := createTestService(true, false)
282
283 assert.Nil(t, err)
284
285 stream := &mockStream{
286 Calls: make(map[int]*openolt.Indication),
287 }
288 s.Initialize(stream)
289
290 // set to failed if timeout occurs
291 _ = s.EapolState.Event("start_auth")
292
293 // after a second EAPOL should have failed and restarted
294 time.Sleep(1 * time.Second)
295 assert.Equal(t, "eap_start_sent", s.EapolState.Current())
296}
297
Matteo Scandoloe9f5a6b2020-09-16 15:54:38 -0700298// Test that if the DHCP state machine doesn't complete in 30 seconds we
299// move it to DHCP failed
300func TestService_DHCPFailed(t *testing.T) {
Matteo Scandolo76c3ff62020-09-24 18:03:01 -0700301
302 common.Config = &common.GlobalConfig{
303 BBSim: common.BBSimConfig{
304 DhcpRetry: false,
305 },
306 }
307
Matteo Scandoloe9f5a6b2020-09-16 15:54:38 -0700308 // override the default wait time
309 dhcpWaitTime = 100 * time.Millisecond
310 s, err := createTestService(false, true)
311
312 assert.Nil(t, err)
313
314 stream := &mockStream{
315 Calls: make(map[int]*openolt.Indication),
316 }
317 s.Initialize(stream)
318
319 // set to failed if timeout occurs
320 _ = s.DHCPState.Event("start_dhcp")
321 time.Sleep(1 * time.Second)
322 assert.Equal(t, "dhcp_failed", s.DHCPState.Current())
323
324 // do not set to failed if succeeded
325 s.DHCPState.SetState("created")
326 _ = s.DHCPState.Event("start_dhcp")
327 s.DHCPState.SetState("dhcp_ack_received")
328 time.Sleep(1 * time.Second)
329 assert.Equal(t, "dhcp_ack_received", s.DHCPState.Current())
330}
Matteo Scandolo76c3ff62020-09-24 18:03:01 -0700331
332func TestService_DHCPRestart(t *testing.T) {
333 common.Config = &common.GlobalConfig{
334 BBSim: common.BBSimConfig{
335 DhcpRetry: true,
336 },
337 }
338
339 // override the default wait time
340 dhcpWaitTime = 100 * time.Millisecond
341 s, err := createTestService(false, true)
342
343 assert.Nil(t, err)
344
345 stream := &mockStream{
346 Calls: make(map[int]*openolt.Indication),
347 }
348 s.Initialize(stream)
349
350 // set to failed if timeout occurs
351 _ = s.DHCPState.Event("start_dhcp")
352 time.Sleep(1 * time.Second)
353 assert.Equal(t, "dhcp_discovery_sent", s.DHCPState.Current())
354}