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 ( |
| 20 | "github.com/opencord/bbsim/internal/bbsim/types" |
Matteo Scandolo | 618a658 | 2020-09-09 12:21:29 -0700 | [diff] [blame^] | 21 | "github.com/opencord/voltha-protos/v3/go/openolt" |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 22 | "github.com/stretchr/testify/assert" |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 23 | "net" |
| 24 | "testing" |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 25 | "time" |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | type mockService struct { |
| 29 | Name string |
| 30 | HandleAuthCallCount int |
| 31 | HandleDhcpCallCount int |
| 32 | HandlePacketsCallCount int |
| 33 | } |
| 34 | |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 35 | func (s *mockService) HandleAuth() { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 36 | s.HandleAuthCallCount = s.HandleAuthCallCount + 1 |
| 37 | } |
| 38 | |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 39 | func (s *mockService) HandleDhcp(cTag int) { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 40 | s.HandleDhcpCallCount = s.HandleDhcpCallCount + 1 |
| 41 | } |
| 42 | |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 43 | func (s *mockService) HandlePackets() { |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 44 | s.HandlePacketsCallCount = s.HandlePacketsCallCount + 1 |
| 45 | } |
| 46 | |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 47 | func (s *mockService) Initialize(stream types.Stream) {} |
| 48 | func (s *mockService) Disable() {} |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 49 | |
| 50 | // test the internalState transitions |
| 51 | func 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 Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 61 | s.Initialize(&mockStream{}) |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 62 | |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 63 | // check that channels have been created |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 64 | assert.NotNil(t, s.PacketCh) |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 65 | 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 Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 70 | |
| 71 | s.Disable() |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 72 | // make sure the EAPOL and DHCP states have been reset after disable |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 73 | assert.Equal(t, "created", s.EapolState.Current()) |
| 74 | assert.Equal(t, "created", s.DHCPState.Current()) |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 75 | |
| 76 | // make sure the channel have been closed |
| 77 | assert.Nil(t, s.Channel) |
| 78 | assert.Nil(t, s.PacketCh) |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | // 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] | 82 | func 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 Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 89 | assert.Nil(t, err) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 90 | |
| 91 | stream := &mockStream{ |
| 92 | Calls: make(map[int]*openolt.Indication), |
| 93 | channel: make(chan int, 10), |
| 94 | } |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 95 | s.Initialize(stream) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 96 | |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 97 | s.HandleAuth() |
| 98 | time.Sleep(1 * time.Second) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 99 | |
| 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 Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 107 | // 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] | 108 | func 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 Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 115 | assert.Nil(t, err) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 116 | |
| 117 | stream := &mockStream{ |
| 118 | Calls: make(map[int]*openolt.Indication), |
| 119 | } |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 120 | s.Initialize(stream) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 121 | |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 122 | s.HandleAuth() |
| 123 | time.Sleep(1 * time.Second) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 124 | |
| 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 Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 131 | |
| 132 | // make sure that if the service does not need DHCP we're not sending any packet |
| 133 | func 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 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 | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 147 | s.HandleDhcp(900) |
| 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) { |
| 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 Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 170 | s.Initialize(stream) |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 171 | |
| 172 | // NOTE that the c_tag is different from the one configured in the service |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 173 | s.HandleDhcp(800) |
| 174 | time.Sleep(1 * time.Second) |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 175 | |
| 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 |
| 183 | func 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 Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 195 | s.Initialize(stream) |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 196 | |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 197 | s.HandleDhcp(900) |
| 198 | time.Sleep(1 * time.Second) |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 199 | |
Matteo Scandolo | adc72a8 | 2020-09-08 18:46:08 -0700 | [diff] [blame] | 200 | assert.Equal(t, 1, stream.CallCount) |
| 201 | assert.Equal(t, "dhcp_discovery_sent", s.DHCPState.Current()) |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame] | 202 | } |