Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package devices |
| 18 | |
| 19 | import ( |
Matteo Scandolo | e9f5a6b | 2020-09-16 15:54:38 -0700 | [diff] [blame] | 20 | "context" |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 21 | "github.com/opencord/bbsim/internal/bbsim/types" |
Matteo Scandolo | 76c3ff6 | 2020-09-24 18:03:01 -0700 | [diff] [blame] | 22 | "github.com/opencord/bbsim/internal/common" |
Matteo Scandolo | 4f4ac79 | 2020-10-01 16:33:21 -0700 | [diff] [blame] | 23 | "github.com/opencord/voltha-protos/v4/go/openolt" |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 24 | "github.com/stretchr/testify/assert" |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 25 | "net" |
| 26 | "testing" |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 27 | "time" |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | type mockService struct { |
| 31 | Name string |
| 32 | HandleAuthCallCount int |
| 33 | HandleDhcpCallCount int |
| 34 | HandlePacketsCallCount int |
| 35 | } |
| 36 | |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 37 | func (s *mockService) HandleAuth() { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 38 | s.HandleAuthCallCount = s.HandleAuthCallCount + 1 |
| 39 | } |
| 40 | |
Matteo Scandolo | bd875b3 | 2020-09-18 17:46:31 -0700 | [diff] [blame] | 41 | func (s *mockService) HandleDhcp(pbit uint8, cTag int) { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 42 | s.HandleDhcpCallCount = s.HandleDhcpCallCount + 1 |
| 43 | } |
| 44 | |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 45 | func (s *mockService) HandlePackets() { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 46 | s.HandlePacketsCallCount = s.HandlePacketsCallCount + 1 |
| 47 | } |
| 48 | |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 49 | func (s *mockService) Initialize(stream types.Stream) {} |
| 50 | func (s *mockService) Disable() {} |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 51 | |
Matteo Scandolo | e9f5a6b | 2020-09-16 15:54:38 -0700 | [diff] [blame] | 52 | func createTestService(needsEapol bool, needsDchp bool) (*Service, error) { |
| 53 | |
| 54 | enableContext := context.TODO() |
| 55 | |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 56 | mac := net.HardwareAddr{0x2e, 0x60, byte(1), byte(1), byte(1), byte(1)} |
| 57 | onu := createMockOnu(1, 1) |
Matteo Scandolo | e9f5a6b | 2020-09-16 15:54:38 -0700 | [diff] [blame] | 58 | 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 Scandolo | bd875b3 | 2020-09-18 17:46:31 -0700 | [diff] [blame] | 63 | 7, 7, 7, 7) |
Matteo Scandolo | e9f5a6b | 2020-09-16 15:54:38 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | // test the internalState transitions |
| 67 | func TestService_InternalState(t *testing.T) { |
| 68 | s, err := createTestService(false, false) |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 69 | |
| 70 | assert.Nil(t, err) |
| 71 | |
| 72 | assert.Empty(t, s.PacketCh) |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 73 | s.Initialize(&mockStream{}) |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 74 | |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 75 | // check that channels have been created |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 76 | assert.NotNil(t, s.PacketCh) |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 77 | 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 Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 82 | |
| 83 | s.Disable() |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 84 | // make sure the EAPOL and DHCP states have been reset after disable |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 85 | assert.Equal(t, "created", s.EapolState.Current()) |
| 86 | assert.Equal(t, "created", s.DHCPState.Current()) |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 87 | |
| 88 | // make sure the channel have been closed |
| 89 | assert.Nil(t, s.Channel) |
| 90 | assert.Nil(t, s.PacketCh) |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | // make sure that if the service does not need EAPOL we're not sending any packet |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 94 | func TestService_HandleAuth_noEapol(t *testing.T) { |
Matteo Scandolo | e9f5a6b | 2020-09-16 15:54:38 -0700 | [diff] [blame] | 95 | s, err := createTestService(false, false) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 96 | |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 97 | assert.Nil(t, err) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 98 | |
| 99 | stream := &mockStream{ |
| 100 | Calls: make(map[int]*openolt.Indication), |
| 101 | channel: make(chan int, 10), |
| 102 | } |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 103 | s.Initialize(stream) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 104 | |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 105 | s.HandleAuth() |
| 106 | time.Sleep(1 * time.Second) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 107 | |
| 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 Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 115 | // make sure that if the service does need EAPOL we're sending any packet |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 116 | func TestService_HandleAuth_withEapol(t *testing.T) { |
Matteo Scandolo | e9f5a6b | 2020-09-16 15:54:38 -0700 | [diff] [blame] | 117 | s, err := createTestService(true, false) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 118 | |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 119 | assert.Nil(t, err) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 120 | |
| 121 | stream := &mockStream{ |
| 122 | Calls: make(map[int]*openolt.Indication), |
| 123 | } |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 124 | s.Initialize(stream) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 125 | |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 126 | s.HandleAuth() |
| 127 | time.Sleep(1 * time.Second) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 128 | |
| 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 Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 135 | |
| 136 | // make sure that if the service does not need DHCP we're not sending any packet |
| 137 | func TestService_HandleDhcp_not_needed(t *testing.T) { |
Matteo Scandolo | e9f5a6b | 2020-09-16 15:54:38 -0700 | [diff] [blame] | 138 | s, err := createTestService(false, false) |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 139 | |
| 140 | assert.Nil(t, err) |
| 141 | |
| 142 | stream := &mockStream{ |
| 143 | Calls: make(map[int]*openolt.Indication), |
| 144 | } |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 145 | s.Initialize(stream) |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 146 | |
Matteo Scandolo | bd875b3 | 2020-09-18 17:46:31 -0700 | [diff] [blame] | 147 | s.HandleDhcp(7, 900) |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 148 | time.Sleep(1 * time.Second) |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 149 | |
| 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 |
| 158 | func TestService_HandleDhcp_different_c_Tag(t *testing.T) { |
Matteo Scandolo | e9f5a6b | 2020-09-16 15:54:38 -0700 | [diff] [blame] | 159 | s, err := createTestService(false, true) |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 160 | |
| 161 | assert.Nil(t, err) |
| 162 | |
| 163 | stream := &mockStream{ |
| 164 | Calls: make(map[int]*openolt.Indication), |
| 165 | } |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 166 | s.Initialize(stream) |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 167 | |
| 168 | // NOTE that the c_tag is different from the one configured in the service |
Matteo Scandolo | bd875b3 | 2020-09-18 17:46:31 -0700 | [diff] [blame] | 169 | s.HandleDhcp(7, 800) |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 170 | time.Sleep(1 * time.Second) |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 171 | |
| 172 | assert.Equal(t, stream.CallCount, 0) |
| 173 | |
| 174 | // state should not change |
| 175 | assert.Equal(t, s.DHCPState.Current(), "created") |
| 176 | } |
| 177 | |
Matteo Scandolo | bd875b3 | 2020-09-18 17:46:31 -0700 | [diff] [blame] | 178 | // 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 |
| 180 | func 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 |
| 201 | func 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 Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 219 | // make sure that if the service does need DHCP we're sending any packet |
| 220 | func TestService_HandleDhcp_needed(t *testing.T) { |
Matteo Scandolo | e9f5a6b | 2020-09-16 15:54:38 -0700 | [diff] [blame] | 221 | s, err := createTestService(false, true) |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 222 | |
| 223 | assert.Nil(t, err) |
| 224 | |
| 225 | stream := &mockStream{ |
| 226 | Calls: make(map[int]*openolt.Indication), |
| 227 | } |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 228 | s.Initialize(stream) |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 229 | |
Matteo Scandolo | bd875b3 | 2020-09-18 17:46:31 -0700 | [diff] [blame] | 230 | s.HandleDhcp(7, 900) |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 231 | time.Sleep(1 * time.Second) |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 232 | |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 233 | assert.Equal(t, 1, stream.CallCount) |
| 234 | assert.Equal(t, "dhcp_discovery_sent", s.DHCPState.Current()) |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 235 | } |
Matteo Scandolo | e9f5a6b | 2020-09-16 15:54:38 -0700 | [diff] [blame] | 236 | |
| 237 | // Test that if the EAPOL state machine doesn't complete in 30 seconds we |
| 238 | // move it to EAPOL failed |
| 239 | func TestService_EAPOLFailed(t *testing.T) { |
Matteo Scandolo | 76c3ff6 | 2020-09-24 18:03:01 -0700 | [diff] [blame] | 240 | |
| 241 | common.Config = &common.GlobalConfig{ |
| 242 | BBSim: common.BBSimConfig{ |
| 243 | AuthRetry: false, |
| 244 | }, |
| 245 | } |
| 246 | |
Matteo Scandolo | e9f5a6b | 2020-09-16 15:54:38 -0700 | [diff] [blame] | 247 | // 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 Scandolo | 76c3ff6 | 2020-09-24 18:03:01 -0700 | [diff] [blame] | 272 | func 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 Scandolo | e9f5a6b | 2020-09-16 15:54:38 -0700 | [diff] [blame] | 298 | // Test that if the DHCP state machine doesn't complete in 30 seconds we |
| 299 | // move it to DHCP failed |
| 300 | func TestService_DHCPFailed(t *testing.T) { |
Matteo Scandolo | 76c3ff6 | 2020-09-24 18:03:01 -0700 | [diff] [blame] | 301 | |
| 302 | common.Config = &common.GlobalConfig{ |
| 303 | BBSim: common.BBSimConfig{ |
| 304 | DhcpRetry: false, |
| 305 | }, |
| 306 | } |
| 307 | |
Matteo Scandolo | e9f5a6b | 2020-09-16 15:54:38 -0700 | [diff] [blame] | 308 | // 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 Scandolo | 76c3ff6 | 2020-09-24 18:03:01 -0700 | [diff] [blame] | 331 | |
| 332 | func 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 | } |