blob: 32850212b08a81520c1577cf44f7b5f96003614d [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"
David K. Bainbridgec415efe2021-08-19 13:05:21 +000023 "github.com/opencord/voltha-protos/v5/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 Scandolo9ddb3a92021-04-14 16:16:20 -070049func (s *mockService) Initialize(stream types.Stream) {}
50func (s *mockService) UpdateStream(stream types.Stream) {}
51func (s *mockService) Disable() {}
Matteo Scandolo75ed5b92020-09-03 09:03:16 -070052
Matteo Scandoloe9f5a6b2020-09-16 15:54:38 -070053func createTestService(needsEapol bool, needsDchp bool) (*Service, error) {
54
55 enableContext := context.TODO()
56
Matteo Scandolo8a574812021-05-20 15:18:53 -070057 mac := net.HardwareAddr{0x2e, byte(1), byte(1), byte(1), byte(1), byte(1)}
Matteo Scandolo75ed5b92020-09-03 09:03:16 -070058 onu := createMockOnu(1, 1)
Matteo Scandoloe9f5a6b2020-09-16 15:54:38 -070059 onu.PonPort = &PonPort{}
60 onu.PonPort.Olt = &OltDevice{}
61 onu.PonPort.Olt.enableContext = enableContext
Matteo Scandolo8a574812021-05-20 15:18:53 -070062
63 uni := UniPort{ID: 1, Onu: onu}
64 return NewService(0, "testService", mac, &uni, 900, 900,
Matteo Scandoloe9f5a6b2020-09-16 15:54:38 -070065 needsEapol, needsDchp, false, 64, 0, false,
Matteo Scandolobd875b32020-09-18 17:46:31 -070066 7, 7, 7, 7)
Matteo Scandoloe9f5a6b2020-09-16 15:54:38 -070067}
68
69// test the internalState transitions
70func TestService_InternalState(t *testing.T) {
71 s, err := createTestService(false, false)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -070072
73 assert.Nil(t, err)
74
75 assert.Empty(t, s.PacketCh)
Matteo Scandoloadc72a82020-09-08 18:46:08 -070076 s.Initialize(&mockStream{})
Matteo Scandolo75ed5b92020-09-03 09:03:16 -070077
Matteo Scandoloadc72a82020-09-08 18:46:08 -070078 // check that channels have been created
Matteo Scandolo75ed5b92020-09-03 09:03:16 -070079 assert.NotNil(t, s.PacketCh)
Matteo Scandoloadc72a82020-09-08 18:46:08 -070080 assert.NotNil(t, s.Channel)
81
82 // set EAPOL and DHCP states to something else
83 s.EapolState.SetState("eap_response_success_received")
84 s.DHCPState.SetState("dhcp_ack_received")
Matteo Scandolo75ed5b92020-09-03 09:03:16 -070085
86 s.Disable()
Matteo Scandoloadc72a82020-09-08 18:46:08 -070087 // make sure the EAPOL and DHCP states have been reset after disable
Matteo Scandolo75ed5b92020-09-03 09:03:16 -070088 assert.Equal(t, "created", s.EapolState.Current())
89 assert.Equal(t, "created", s.DHCPState.Current())
Matteo Scandoloadc72a82020-09-08 18:46:08 -070090
91 // make sure the channel have been closed
92 assert.Nil(t, s.Channel)
93 assert.Nil(t, s.PacketCh)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -070094}
95
96// make sure that if the service does not need EAPOL we're not sending any packet
Matteo Scandolo4a036262020-08-17 15:56:13 -070097func TestService_HandleAuth_noEapol(t *testing.T) {
Matteo Scandoloe9f5a6b2020-09-16 15:54:38 -070098 s, err := createTestService(false, false)
Matteo Scandolo4a036262020-08-17 15:56:13 -070099
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700100 assert.Nil(t, err)
Matteo Scandolo4a036262020-08-17 15:56:13 -0700101
102 stream := &mockStream{
103 Calls: make(map[int]*openolt.Indication),
104 channel: make(chan int, 10),
105 }
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700106 s.Initialize(stream)
Matteo Scandolo4a036262020-08-17 15:56:13 -0700107
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700108 s.HandleAuth()
109 time.Sleep(1 * time.Second)
Matteo Scandolo4a036262020-08-17 15:56:13 -0700110
111 // if the service does not need EAPOL we don't expect any packet to be generated
112 assert.Equal(t, stream.CallCount, 0)
113
114 // state should not change
115 assert.Equal(t, s.EapolState.Current(), "created")
116}
117
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700118// make sure that if the service does need EAPOL we're sending any packet
Matteo Scandolo4a036262020-08-17 15:56:13 -0700119func TestService_HandleAuth_withEapol(t *testing.T) {
Matteo Scandoloe9f5a6b2020-09-16 15:54:38 -0700120 s, err := createTestService(true, false)
Matteo Scandolo4a036262020-08-17 15:56:13 -0700121
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700122 assert.Nil(t, err)
Matteo Scandolo4a036262020-08-17 15:56:13 -0700123
124 stream := &mockStream{
125 Calls: make(map[int]*openolt.Indication),
126 }
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700127 s.Initialize(stream)
Matteo Scandolo4a036262020-08-17 15:56:13 -0700128
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700129 s.HandleAuth()
130 time.Sleep(1 * time.Second)
Matteo Scandolo4a036262020-08-17 15:56:13 -0700131
132 // if the service does not need EAPOL we don't expect any packet to be generated
133 assert.Equal(t, stream.CallCount, 1)
134
135 // state should not change
136 assert.Equal(t, s.EapolState.Current(), "eap_start_sent")
137}
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700138
139// make sure that if the service does not need DHCP we're not sending any packet
140func TestService_HandleDhcp_not_needed(t *testing.T) {
Matteo Scandoloe9f5a6b2020-09-16 15:54:38 -0700141 s, err := createTestService(false, false)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700142
143 assert.Nil(t, err)
144
145 stream := &mockStream{
146 Calls: make(map[int]*openolt.Indication),
147 }
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700148 s.Initialize(stream)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700149
Matteo Scandolobd875b32020-09-18 17:46:31 -0700150 s.HandleDhcp(7, 900)
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700151 time.Sleep(1 * time.Second)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700152
153 assert.Equal(t, stream.CallCount, 0)
154
155 // state should not change
156 assert.Equal(t, s.DHCPState.Current(), "created")
157}
158
159// when we receive a DHCP flow we call HandleDhcp an all the ONU Services
160// each service device whether the tag matches it's own configuration
161func TestService_HandleDhcp_different_c_Tag(t *testing.T) {
Matteo Scandoloe9f5a6b2020-09-16 15:54:38 -0700162 s, err := createTestService(false, true)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700163
164 assert.Nil(t, err)
165
166 stream := &mockStream{
167 Calls: make(map[int]*openolt.Indication),
168 }
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700169 s.Initialize(stream)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700170
171 // NOTE that the c_tag is different from the one configured in the service
Matteo Scandolobd875b32020-09-18 17:46:31 -0700172 s.HandleDhcp(7, 800)
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700173 time.Sleep(1 * time.Second)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700174
175 assert.Equal(t, stream.CallCount, 0)
176
177 // state should not change
178 assert.Equal(t, s.DHCPState.Current(), "created")
179}
180
Matteo Scandolobd875b32020-09-18 17:46:31 -0700181// when we receive a DHCP flow we call HandleDhcp an all the ONU Services
182// each service device whether the tag matches it's own configuration
183func TestService_HandleDhcp_different_pbit(t *testing.T) {
184 s, err := createTestService(false, true)
185
186 assert.Nil(t, err)
187
188 stream := &mockStream{
189 Calls: make(map[int]*openolt.Indication),
190 }
191 s.Initialize(stream)
192
193 // NOTE that the c_tag is different from the one configured in the service
194 s.HandleDhcp(5, 900)
195 time.Sleep(1 * time.Second)
196
197 assert.Equal(t, stream.CallCount, 0)
198
199 // state should not change
200 assert.Equal(t, s.DHCPState.Current(), "created")
201}
202
203// if PBIT is 255 it means all of them, so start DHCP if the C_TAG matches
204func TestService_HandleDhcp_pbit_255(t *testing.T) {
205 s, err := createTestService(false, true)
206
207 assert.Nil(t, err)
208
209 stream := &mockStream{
210 Calls: make(map[int]*openolt.Indication),
211 }
212 s.Initialize(stream)
213
214 // NOTE that the c_tag is different from the one configured in the service
215 s.HandleDhcp(255, 900)
216 time.Sleep(1 * time.Second)
217
218 assert.Equal(t, 1, stream.CallCount)
219 assert.Equal(t, s.DHCPState.Current(), "dhcp_discovery_sent")
220}
221
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700222// make sure that if the service does need DHCP we're sending any packet
223func TestService_HandleDhcp_needed(t *testing.T) {
Matteo Scandoloe9f5a6b2020-09-16 15:54:38 -0700224 s, err := createTestService(false, true)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700225
226 assert.Nil(t, err)
227
228 stream := &mockStream{
229 Calls: make(map[int]*openolt.Indication),
230 }
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700231 s.Initialize(stream)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700232
Matteo Scandolobd875b32020-09-18 17:46:31 -0700233 s.HandleDhcp(7, 900)
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700234 time.Sleep(1 * time.Second)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700235
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700236 assert.Equal(t, 1, stream.CallCount)
237 assert.Equal(t, "dhcp_discovery_sent", s.DHCPState.Current())
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700238}
Matteo Scandoloe9f5a6b2020-09-16 15:54:38 -0700239
240// Test that if the EAPOL state machine doesn't complete in 30 seconds we
241// move it to EAPOL failed
242func TestService_EAPOLFailed(t *testing.T) {
Matteo Scandolo76c3ff62020-09-24 18:03:01 -0700243
244 common.Config = &common.GlobalConfig{
245 BBSim: common.BBSimConfig{
246 AuthRetry: false,
247 },
248 }
249
Matteo Scandoloe9f5a6b2020-09-16 15:54:38 -0700250 // override the default wait time
251 eapolWaitTime = 500 * time.Millisecond
252 s, err := createTestService(true, false)
253
254 assert.Nil(t, err)
255
256 stream := &mockStream{
257 Calls: make(map[int]*openolt.Indication),
258 }
259 s.Initialize(stream)
260
261 // set to failed if timeout occurs
262 _ = s.EapolState.Event("start_auth")
263 time.Sleep(1 * time.Second)
264 assert.Equal(t, "auth_failed", s.EapolState.Current())
265
266 // do not set to failed if succeeded
267 s.EapolState.SetState("created")
268 _ = s.EapolState.Event("start_auth")
269 s.EapolState.SetState("eap_response_success_received")
270 time.Sleep(1 * time.Second)
271 assert.Equal(t, "eap_response_success_received", s.EapolState.Current())
272
273}
274
Matteo Scandolo76c3ff62020-09-24 18:03:01 -0700275func TestService_EAPOLRestart(t *testing.T) {
276
277 common.Config = &common.GlobalConfig{
278 BBSim: common.BBSimConfig{
279 AuthRetry: true,
280 },
281 }
282
283 eapolWaitTime = 500 * time.Millisecond
284 s, err := createTestService(true, false)
285
286 assert.Nil(t, err)
287
288 stream := &mockStream{
289 Calls: make(map[int]*openolt.Indication),
290 }
291 s.Initialize(stream)
292
293 // set to failed if timeout occurs
294 _ = s.EapolState.Event("start_auth")
295
296 // after a second EAPOL should have failed and restarted
297 time.Sleep(1 * time.Second)
298 assert.Equal(t, "eap_start_sent", s.EapolState.Current())
299}
300
Matteo Scandoloe9f5a6b2020-09-16 15:54:38 -0700301// Test that if the DHCP state machine doesn't complete in 30 seconds we
302// move it to DHCP failed
303func TestService_DHCPFailed(t *testing.T) {
Matteo Scandolo76c3ff62020-09-24 18:03:01 -0700304
305 common.Config = &common.GlobalConfig{
306 BBSim: common.BBSimConfig{
307 DhcpRetry: false,
308 },
309 }
310
Matteo Scandoloe9f5a6b2020-09-16 15:54:38 -0700311 // override the default wait time
312 dhcpWaitTime = 100 * time.Millisecond
313 s, err := createTestService(false, true)
314
315 assert.Nil(t, err)
316
317 stream := &mockStream{
318 Calls: make(map[int]*openolt.Indication),
319 }
320 s.Initialize(stream)
321
322 // set to failed if timeout occurs
323 _ = s.DHCPState.Event("start_dhcp")
324 time.Sleep(1 * time.Second)
325 assert.Equal(t, "dhcp_failed", s.DHCPState.Current())
326
327 // do not set to failed if succeeded
328 s.DHCPState.SetState("created")
329 _ = s.DHCPState.Event("start_dhcp")
330 s.DHCPState.SetState("dhcp_ack_received")
331 time.Sleep(1 * time.Second)
332 assert.Equal(t, "dhcp_ack_received", s.DHCPState.Current())
333}
Matteo Scandolo76c3ff62020-09-24 18:03:01 -0700334
335func TestService_DHCPRestart(t *testing.T) {
336 common.Config = &common.GlobalConfig{
337 BBSim: common.BBSimConfig{
338 DhcpRetry: true,
339 },
340 }
341
342 // override the default wait time
343 dhcpWaitTime = 100 * time.Millisecond
344 s, err := createTestService(false, true)
345
346 assert.Nil(t, err)
347
348 stream := &mockStream{
349 Calls: make(map[int]*openolt.Indication),
350 }
351 s.Initialize(stream)
352
353 // set to failed if timeout occurs
354 _ = s.DHCPState.Event("start_dhcp")
355 time.Sleep(1 * time.Second)
356 assert.Equal(t, "dhcp_discovery_sent", s.DHCPState.Current())
357}