blob: 219117845fa8849eefbc4fbcd658b9731aa6d144 [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 (
20 "github.com/opencord/bbsim/internal/bbsim/types"
21 "github.com/opencord/voltha-protos/v2/go/openolt"
Matteo Scandolo75ed5b92020-09-03 09:03:16 -070022 "github.com/stretchr/testify/assert"
Matteo Scandolo4a036262020-08-17 15:56:13 -070023 "net"
24 "testing"
Matteo Scandoloadc72a82020-09-08 18:46:08 -070025 "time"
Matteo Scandolo4a036262020-08-17 15:56:13 -070026)
27
28type mockService struct {
29 Name string
30 HandleAuthCallCount int
31 HandleDhcpCallCount int
32 HandlePacketsCallCount int
33}
34
Matteo Scandoloadc72a82020-09-08 18:46:08 -070035func (s *mockService) HandleAuth() {
Matteo Scandolo4a036262020-08-17 15:56:13 -070036 s.HandleAuthCallCount = s.HandleAuthCallCount + 1
37}
38
Matteo Scandoloadc72a82020-09-08 18:46:08 -070039func (s *mockService) HandleDhcp(cTag int) {
Matteo Scandolo4a036262020-08-17 15:56:13 -070040 s.HandleDhcpCallCount = s.HandleDhcpCallCount + 1
41}
42
Matteo Scandoloadc72a82020-09-08 18:46:08 -070043func (s *mockService) HandlePackets() {
Matteo Scandolo4a036262020-08-17 15:56:13 -070044 s.HandlePacketsCallCount = s.HandlePacketsCallCount + 1
45}
46
Matteo Scandoloadc72a82020-09-08 18:46:08 -070047func (s *mockService) Initialize(stream types.Stream) {}
48func (s *mockService) Disable() {}
Matteo Scandolo75ed5b92020-09-03 09:03:16 -070049
50// test the internalState transitions
51func TestService_InternalState(t *testing.T) {
52 mac := net.HardwareAddr{0x2e, 0x60, byte(1), byte(1), byte(1), byte(1)}
53 onu := createMockOnu(1, 1)
54 s, err := NewService("testService", mac, onu, 900, 900,
55 false, false, false, 64, 0, false,
56 0, 0, 0, 0)
57
58 assert.Nil(t, err)
59
60 assert.Empty(t, s.PacketCh)
Matteo Scandoloadc72a82020-09-08 18:46:08 -070061 s.Initialize(&mockStream{})
Matteo Scandolo75ed5b92020-09-03 09:03:16 -070062
Matteo Scandoloadc72a82020-09-08 18:46:08 -070063 // check that channels have been created
Matteo Scandolo75ed5b92020-09-03 09:03:16 -070064 assert.NotNil(t, s.PacketCh)
Matteo Scandoloadc72a82020-09-08 18:46:08 -070065 assert.NotNil(t, s.Channel)
66
67 // set EAPOL and DHCP states to something else
68 s.EapolState.SetState("eap_response_success_received")
69 s.DHCPState.SetState("dhcp_ack_received")
Matteo Scandolo75ed5b92020-09-03 09:03:16 -070070
71 s.Disable()
Matteo Scandoloadc72a82020-09-08 18:46:08 -070072 // make sure the EAPOL and DHCP states have been reset after disable
Matteo Scandolo75ed5b92020-09-03 09:03:16 -070073 assert.Equal(t, "created", s.EapolState.Current())
74 assert.Equal(t, "created", s.DHCPState.Current())
Matteo Scandoloadc72a82020-09-08 18:46:08 -070075
76 // make sure the channel have been closed
77 assert.Nil(t, s.Channel)
78 assert.Nil(t, s.PacketCh)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -070079}
80
81// make sure that if the service does not need EAPOL we're not sending any packet
Matteo Scandolo4a036262020-08-17 15:56:13 -070082func TestService_HandleAuth_noEapol(t *testing.T) {
83 mac := net.HardwareAddr{0x2e, 0x60, byte(1), byte(1), byte(1), byte(1)}
84 onu := createMockOnu(1, 1)
85 s, err := NewService("testService", mac, onu, 900, 900,
86 false, false, false, 64, 0, false,
87 0, 0, 0, 0)
88
Matteo Scandolo75ed5b92020-09-03 09:03:16 -070089 assert.Nil(t, err)
Matteo Scandolo4a036262020-08-17 15:56:13 -070090
91 stream := &mockStream{
92 Calls: make(map[int]*openolt.Indication),
93 channel: make(chan int, 10),
94 }
Matteo Scandoloadc72a82020-09-08 18:46:08 -070095 s.Initialize(stream)
Matteo Scandolo4a036262020-08-17 15:56:13 -070096
Matteo Scandoloadc72a82020-09-08 18:46:08 -070097 s.HandleAuth()
98 time.Sleep(1 * time.Second)
Matteo Scandolo4a036262020-08-17 15:56:13 -070099
100 // if the service does not need EAPOL we don't expect any packet to be generated
101 assert.Equal(t, stream.CallCount, 0)
102
103 // state should not change
104 assert.Equal(t, s.EapolState.Current(), "created")
105}
106
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700107// make sure that if the service does need EAPOL we're sending any packet
Matteo Scandolo4a036262020-08-17 15:56:13 -0700108func TestService_HandleAuth_withEapol(t *testing.T) {
109 mac := net.HardwareAddr{0x2e, 0x60, byte(1), byte(1), byte(1), byte(1)}
110 onu := createMockOnu(1, 1)
111 s, err := NewService("testService", mac, onu, 900, 900,
112 true, false, false, 64, 0, false,
113 0, 0, 0, 0)
114
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700115 assert.Nil(t, err)
Matteo Scandolo4a036262020-08-17 15:56:13 -0700116
117 stream := &mockStream{
118 Calls: make(map[int]*openolt.Indication),
119 }
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700120 s.Initialize(stream)
Matteo Scandolo4a036262020-08-17 15:56:13 -0700121
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700122 s.HandleAuth()
123 time.Sleep(1 * time.Second)
Matteo Scandolo4a036262020-08-17 15:56:13 -0700124
125 // if the service does not need EAPOL we don't expect any packet to be generated
126 assert.Equal(t, stream.CallCount, 1)
127
128 // state should not change
129 assert.Equal(t, s.EapolState.Current(), "eap_start_sent")
130}
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700131
132// make sure that if the service does not need DHCP we're not sending any packet
133func TestService_HandleDhcp_not_needed(t *testing.T) {
134 mac := net.HardwareAddr{0x2e, 0x60, byte(1), byte(1), byte(1), byte(1)}
135 onu := createMockOnu(1, 1)
136 s, err := NewService("testService", mac, onu, 900, 900,
137 false, false, false, 64, 0, false,
138 0, 0, 0, 0)
139
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 Scandoloadc72a82020-09-08 18:46:08 -0700147 s.HandleDhcp(900)
148 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) {
159 mac := net.HardwareAddr{0x2e, 0x60, byte(1), byte(1), byte(1), byte(1)}
160 onu := createMockOnu(1, 1)
161 s, err := NewService("testService", mac, onu, 900, 900,
162 false, false, false, 64, 0, false,
163 0, 0, 0, 0)
164
165 assert.Nil(t, err)
166
167 stream := &mockStream{
168 Calls: make(map[int]*openolt.Indication),
169 }
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700170 s.Initialize(stream)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700171
172 // NOTE that the c_tag is different from the one configured in the service
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700173 s.HandleDhcp(800)
174 time.Sleep(1 * time.Second)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700175
176 assert.Equal(t, stream.CallCount, 0)
177
178 // state should not change
179 assert.Equal(t, s.DHCPState.Current(), "created")
180}
181
182// make sure that if the service does need DHCP we're sending any packet
183func TestService_HandleDhcp_needed(t *testing.T) {
184 mac := net.HardwareAddr{0x2e, 0x60, byte(1), byte(1), byte(1), byte(1)}
185 onu := createMockOnu(1, 1)
186 s, err := NewService("testService", mac, onu, 900, 900,
187 false, true, false, 64, 0, false,
188 0, 0, 0, 0)
189
190 assert.Nil(t, err)
191
192 stream := &mockStream{
193 Calls: make(map[int]*openolt.Indication),
194 }
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700195 s.Initialize(stream)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700196
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700197 s.HandleDhcp(900)
198 time.Sleep(1 * time.Second)
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700199
Matteo Scandoloadc72a82020-09-08 18:46:08 -0700200 assert.Equal(t, 1, stream.CallCount)
201 assert.Equal(t, "dhcp_discovery_sent", s.DHCPState.Current())
Matteo Scandolo75ed5b92020-09-03 09:03:16 -0700202}