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 | 618a658 | 2020-09-09 12:21:29 -0700 | [diff] [blame] | 22 | "github.com/opencord/voltha-protos/v3/go/openolt" |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 23 | "github.com/stretchr/testify/assert" |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 24 | "net" |
| 25 | "testing" |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 26 | "time" |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | type mockService struct { |
| 30 | Name string |
| 31 | HandleAuthCallCount int |
| 32 | HandleDhcpCallCount int |
| 33 | HandlePacketsCallCount int |
| 34 | } |
| 35 | |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 36 | func (s *mockService) HandleAuth() { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 37 | s.HandleAuthCallCount = s.HandleAuthCallCount + 1 |
| 38 | } |
| 39 | |
Matteo Scandolo | bd875b3 | 2020-09-18 17:46:31 -0700 | [diff] [blame] | 40 | func (s *mockService) HandleDhcp(pbit uint8, cTag int) { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 41 | s.HandleDhcpCallCount = s.HandleDhcpCallCount + 1 |
| 42 | } |
| 43 | |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 44 | func (s *mockService) HandlePackets() { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 45 | s.HandlePacketsCallCount = s.HandlePacketsCallCount + 1 |
| 46 | } |
| 47 | |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 48 | func (s *mockService) Initialize(stream types.Stream) {} |
| 49 | func (s *mockService) Disable() {} |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 50 | |
Matteo Scandolo | e9f5a6b | 2020-09-16 15:54:38 -0700 | [diff] [blame] | 51 | func createTestService(needsEapol bool, needsDchp bool) (*Service, error) { |
| 52 | |
| 53 | enableContext := context.TODO() |
| 54 | |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 55 | mac := net.HardwareAddr{0x2e, 0x60, byte(1), byte(1), byte(1), byte(1)} |
| 56 | onu := createMockOnu(1, 1) |
Matteo Scandolo | e9f5a6b | 2020-09-16 15:54:38 -0700 | [diff] [blame] | 57 | onu.PonPort = &PonPort{} |
| 58 | onu.PonPort.Olt = &OltDevice{} |
| 59 | onu.PonPort.Olt.enableContext = enableContext |
| 60 | return NewService("testService", mac, onu, 900, 900, |
| 61 | needsEapol, needsDchp, false, 64, 0, false, |
Matteo Scandolo | bd875b3 | 2020-09-18 17:46:31 -0700 | [diff] [blame] | 62 | 7, 7, 7, 7) |
Matteo Scandolo | e9f5a6b | 2020-09-16 15:54:38 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // test the internalState transitions |
| 66 | func TestService_InternalState(t *testing.T) { |
| 67 | s, err := createTestService(false, false) |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 68 | |
| 69 | assert.Nil(t, err) |
| 70 | |
| 71 | assert.Empty(t, s.PacketCh) |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 72 | s.Initialize(&mockStream{}) |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 73 | |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 74 | // check that channels have been created |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 75 | assert.NotNil(t, s.PacketCh) |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 76 | assert.NotNil(t, s.Channel) |
| 77 | |
| 78 | // set EAPOL and DHCP states to something else |
| 79 | s.EapolState.SetState("eap_response_success_received") |
| 80 | s.DHCPState.SetState("dhcp_ack_received") |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 81 | |
| 82 | s.Disable() |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 83 | // make sure the EAPOL and DHCP states have been reset after disable |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 84 | assert.Equal(t, "created", s.EapolState.Current()) |
| 85 | assert.Equal(t, "created", s.DHCPState.Current()) |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 86 | |
| 87 | // make sure the channel have been closed |
| 88 | assert.Nil(t, s.Channel) |
| 89 | assert.Nil(t, s.PacketCh) |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | // 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] | 93 | func TestService_HandleAuth_noEapol(t *testing.T) { |
Matteo Scandolo | e9f5a6b | 2020-09-16 15:54:38 -0700 | [diff] [blame] | 94 | s, err := createTestService(false, false) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 95 | |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 96 | assert.Nil(t, err) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 97 | |
| 98 | stream := &mockStream{ |
| 99 | Calls: make(map[int]*openolt.Indication), |
| 100 | channel: make(chan int, 10), |
| 101 | } |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 102 | s.Initialize(stream) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 103 | |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 104 | s.HandleAuth() |
| 105 | time.Sleep(1 * time.Second) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 106 | |
| 107 | // if the service does not need EAPOL we don't expect any packet to be generated |
| 108 | assert.Equal(t, stream.CallCount, 0) |
| 109 | |
| 110 | // state should not change |
| 111 | assert.Equal(t, s.EapolState.Current(), "created") |
| 112 | } |
| 113 | |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 114 | // 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] | 115 | func TestService_HandleAuth_withEapol(t *testing.T) { |
Matteo Scandolo | e9f5a6b | 2020-09-16 15:54:38 -0700 | [diff] [blame] | 116 | s, err := createTestService(true, false) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 117 | |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 118 | assert.Nil(t, err) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 119 | |
| 120 | stream := &mockStream{ |
| 121 | Calls: make(map[int]*openolt.Indication), |
| 122 | } |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 123 | s.Initialize(stream) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 124 | |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 125 | s.HandleAuth() |
| 126 | time.Sleep(1 * time.Second) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 127 | |
| 128 | // if the service does not need EAPOL we don't expect any packet to be generated |
| 129 | assert.Equal(t, stream.CallCount, 1) |
| 130 | |
| 131 | // state should not change |
| 132 | assert.Equal(t, s.EapolState.Current(), "eap_start_sent") |
| 133 | } |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 134 | |
| 135 | // make sure that if the service does not need DHCP we're not sending any packet |
| 136 | func TestService_HandleDhcp_not_needed(t *testing.T) { |
Matteo Scandolo | e9f5a6b | 2020-09-16 15:54:38 -0700 | [diff] [blame] | 137 | s, err := createTestService(false, false) |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 138 | |
| 139 | assert.Nil(t, err) |
| 140 | |
| 141 | stream := &mockStream{ |
| 142 | Calls: make(map[int]*openolt.Indication), |
| 143 | } |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 144 | s.Initialize(stream) |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 145 | |
Matteo Scandolo | bd875b3 | 2020-09-18 17:46:31 -0700 | [diff] [blame] | 146 | s.HandleDhcp(7, 900) |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 147 | time.Sleep(1 * time.Second) |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 148 | |
| 149 | assert.Equal(t, stream.CallCount, 0) |
| 150 | |
| 151 | // state should not change |
| 152 | assert.Equal(t, s.DHCPState.Current(), "created") |
| 153 | } |
| 154 | |
| 155 | // when we receive a DHCP flow we call HandleDhcp an all the ONU Services |
| 156 | // each service device whether the tag matches it's own configuration |
| 157 | func TestService_HandleDhcp_different_c_Tag(t *testing.T) { |
Matteo Scandolo | e9f5a6b | 2020-09-16 15:54:38 -0700 | [diff] [blame] | 158 | s, err := createTestService(false, true) |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 159 | |
| 160 | assert.Nil(t, err) |
| 161 | |
| 162 | stream := &mockStream{ |
| 163 | Calls: make(map[int]*openolt.Indication), |
| 164 | } |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 165 | s.Initialize(stream) |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 166 | |
| 167 | // 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] | 168 | s.HandleDhcp(7, 800) |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 169 | time.Sleep(1 * time.Second) |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 170 | |
| 171 | assert.Equal(t, stream.CallCount, 0) |
| 172 | |
| 173 | // state should not change |
| 174 | assert.Equal(t, s.DHCPState.Current(), "created") |
| 175 | } |
| 176 | |
Matteo Scandolo | bd875b3 | 2020-09-18 17:46:31 -0700 | [diff] [blame] | 177 | // when we receive a DHCP flow we call HandleDhcp an all the ONU Services |
| 178 | // each service device whether the tag matches it's own configuration |
| 179 | func TestService_HandleDhcp_different_pbit(t *testing.T) { |
| 180 | s, err := createTestService(false, true) |
| 181 | |
| 182 | assert.Nil(t, err) |
| 183 | |
| 184 | stream := &mockStream{ |
| 185 | Calls: make(map[int]*openolt.Indication), |
| 186 | } |
| 187 | s.Initialize(stream) |
| 188 | |
| 189 | // NOTE that the c_tag is different from the one configured in the service |
| 190 | s.HandleDhcp(5, 900) |
| 191 | time.Sleep(1 * time.Second) |
| 192 | |
| 193 | assert.Equal(t, stream.CallCount, 0) |
| 194 | |
| 195 | // state should not change |
| 196 | assert.Equal(t, s.DHCPState.Current(), "created") |
| 197 | } |
| 198 | |
| 199 | // if PBIT is 255 it means all of them, so start DHCP if the C_TAG matches |
| 200 | func TestService_HandleDhcp_pbit_255(t *testing.T) { |
| 201 | s, err := createTestService(false, true) |
| 202 | |
| 203 | assert.Nil(t, err) |
| 204 | |
| 205 | stream := &mockStream{ |
| 206 | Calls: make(map[int]*openolt.Indication), |
| 207 | } |
| 208 | s.Initialize(stream) |
| 209 | |
| 210 | // NOTE that the c_tag is different from the one configured in the service |
| 211 | s.HandleDhcp(255, 900) |
| 212 | time.Sleep(1 * time.Second) |
| 213 | |
| 214 | assert.Equal(t, 1, stream.CallCount) |
| 215 | assert.Equal(t, s.DHCPState.Current(), "dhcp_discovery_sent") |
| 216 | } |
| 217 | |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 218 | // make sure that if the service does need DHCP we're sending any packet |
| 219 | func TestService_HandleDhcp_needed(t *testing.T) { |
Matteo Scandolo | e9f5a6b | 2020-09-16 15:54:38 -0700 | [diff] [blame] | 220 | s, err := createTestService(false, true) |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 221 | |
| 222 | assert.Nil(t, err) |
| 223 | |
| 224 | stream := &mockStream{ |
| 225 | Calls: make(map[int]*openolt.Indication), |
| 226 | } |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 227 | s.Initialize(stream) |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 228 | |
Matteo Scandolo | bd875b3 | 2020-09-18 17:46:31 -0700 | [diff] [blame] | 229 | s.HandleDhcp(7, 900) |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 230 | time.Sleep(1 * time.Second) |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 231 | |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 232 | assert.Equal(t, 1, stream.CallCount) |
| 233 | assert.Equal(t, "dhcp_discovery_sent", s.DHCPState.Current()) |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 234 | } |
Matteo Scandolo | e9f5a6b | 2020-09-16 15:54:38 -0700 | [diff] [blame] | 235 | |
| 236 | // Test that if the EAPOL state machine doesn't complete in 30 seconds we |
| 237 | // move it to EAPOL failed |
| 238 | func TestService_EAPOLFailed(t *testing.T) { |
| 239 | // override the default wait time |
| 240 | eapolWaitTime = 500 * time.Millisecond |
| 241 | s, err := createTestService(true, false) |
| 242 | |
| 243 | assert.Nil(t, err) |
| 244 | |
| 245 | stream := &mockStream{ |
| 246 | Calls: make(map[int]*openolt.Indication), |
| 247 | } |
| 248 | s.Initialize(stream) |
| 249 | |
| 250 | // set to failed if timeout occurs |
| 251 | _ = s.EapolState.Event("start_auth") |
| 252 | time.Sleep(1 * time.Second) |
| 253 | assert.Equal(t, "auth_failed", s.EapolState.Current()) |
| 254 | |
| 255 | // do not set to failed if succeeded |
| 256 | s.EapolState.SetState("created") |
| 257 | _ = s.EapolState.Event("start_auth") |
| 258 | s.EapolState.SetState("eap_response_success_received") |
| 259 | time.Sleep(1 * time.Second) |
| 260 | assert.Equal(t, "eap_response_success_received", s.EapolState.Current()) |
| 261 | |
| 262 | } |
| 263 | |
| 264 | // Test that if the DHCP state machine doesn't complete in 30 seconds we |
| 265 | // move it to DHCP failed |
| 266 | func TestService_DHCPFailed(t *testing.T) { |
| 267 | // override the default wait time |
| 268 | dhcpWaitTime = 100 * time.Millisecond |
| 269 | s, err := createTestService(false, true) |
| 270 | |
| 271 | assert.Nil(t, err) |
| 272 | |
| 273 | stream := &mockStream{ |
| 274 | Calls: make(map[int]*openolt.Indication), |
| 275 | } |
| 276 | s.Initialize(stream) |
| 277 | |
| 278 | // set to failed if timeout occurs |
| 279 | _ = s.DHCPState.Event("start_dhcp") |
| 280 | time.Sleep(1 * time.Second) |
| 281 | assert.Equal(t, "dhcp_failed", s.DHCPState.Current()) |
| 282 | |
| 283 | // do not set to failed if succeeded |
| 284 | s.DHCPState.SetState("created") |
| 285 | _ = s.DHCPState.Event("start_dhcp") |
| 286 | s.DHCPState.SetState("dhcp_ack_received") |
| 287 | time.Sleep(1 * time.Second) |
| 288 | assert.Equal(t, "dhcp_ack_received", s.DHCPState.Current()) |
| 289 | } |