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" |
| 21 | "github.com/opencord/voltha-protos/v2/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" |
| 25 | ) |
| 26 | |
| 27 | type mockService struct { |
| 28 | Name string |
| 29 | HandleAuthCallCount int |
| 30 | HandleDhcpCallCount int |
| 31 | HandlePacketsCallCount int |
| 32 | } |
| 33 | |
| 34 | func (s *mockService) HandleAuth(stream types.Stream) { |
| 35 | s.HandleAuthCallCount = s.HandleAuthCallCount + 1 |
| 36 | } |
| 37 | |
| 38 | func (s *mockService) HandleDhcp(stream types.Stream, cTag int) { |
| 39 | s.HandleDhcpCallCount = s.HandleDhcpCallCount + 1 |
| 40 | } |
| 41 | |
| 42 | func (s *mockService) HandlePackets(stream types.Stream) { |
| 43 | s.HandlePacketsCallCount = s.HandlePacketsCallCount + 1 |
| 44 | } |
| 45 | |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame^] | 46 | func (s *mockService) Initialize() {} |
| 47 | func (s *mockService) Disable() {} |
| 48 | |
| 49 | // test the internalState transitions |
| 50 | func TestService_InternalState(t *testing.T) { |
| 51 | mac := net.HardwareAddr{0x2e, 0x60, byte(1), byte(1), byte(1), byte(1)} |
| 52 | onu := createMockOnu(1, 1) |
| 53 | s, err := NewService("testService", mac, onu, 900, 900, |
| 54 | false, false, false, 64, 0, false, |
| 55 | 0, 0, 0, 0) |
| 56 | |
| 57 | assert.Nil(t, err) |
| 58 | |
| 59 | assert.Empty(t, s.PacketCh) |
| 60 | s.Initialize() |
| 61 | |
| 62 | assert.NotNil(t, s.PacketCh) |
| 63 | |
| 64 | s.Disable() |
| 65 | assert.Equal(t, "created", s.EapolState.Current()) |
| 66 | assert.Equal(t, "created", s.DHCPState.Current()) |
| 67 | } |
| 68 | |
| 69 | // 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] | 70 | func TestService_HandleAuth_noEapol(t *testing.T) { |
| 71 | mac := net.HardwareAddr{0x2e, 0x60, byte(1), byte(1), byte(1), byte(1)} |
| 72 | onu := createMockOnu(1, 1) |
| 73 | s, err := NewService("testService", mac, onu, 900, 900, |
| 74 | false, false, false, 64, 0, false, |
| 75 | 0, 0, 0, 0) |
| 76 | |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame^] | 77 | assert.Nil(t, err) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 78 | |
| 79 | stream := &mockStream{ |
| 80 | Calls: make(map[int]*openolt.Indication), |
| 81 | channel: make(chan int, 10), |
| 82 | } |
| 83 | |
| 84 | s.HandleAuth(stream) |
| 85 | |
| 86 | // if the service does not need EAPOL we don't expect any packet to be generated |
| 87 | assert.Equal(t, stream.CallCount, 0) |
| 88 | |
| 89 | // state should not change |
| 90 | assert.Equal(t, s.EapolState.Current(), "created") |
| 91 | } |
| 92 | |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame^] | 93 | // 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] | 94 | func TestService_HandleAuth_withEapol(t *testing.T) { |
| 95 | mac := net.HardwareAddr{0x2e, 0x60, byte(1), byte(1), byte(1), byte(1)} |
| 96 | onu := createMockOnu(1, 1) |
| 97 | s, err := NewService("testService", mac, onu, 900, 900, |
| 98 | true, false, false, 64, 0, false, |
| 99 | 0, 0, 0, 0) |
| 100 | |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame^] | 101 | assert.Nil(t, err) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 102 | |
| 103 | stream := &mockStream{ |
| 104 | Calls: make(map[int]*openolt.Indication), |
| 105 | } |
| 106 | |
| 107 | s.HandleAuth(stream) |
| 108 | |
| 109 | // if the service does not need EAPOL we don't expect any packet to be generated |
| 110 | assert.Equal(t, stream.CallCount, 1) |
| 111 | |
| 112 | // state should not change |
| 113 | assert.Equal(t, s.EapolState.Current(), "eap_start_sent") |
| 114 | } |
Matteo Scandolo | 75ed5b9 | 2020-09-03 09:03:16 -0700 | [diff] [blame^] | 115 | |
| 116 | // make sure that if the service does not need DHCP we're not sending any packet |
| 117 | func TestService_HandleDhcp_not_needed(t *testing.T) { |
| 118 | mac := net.HardwareAddr{0x2e, 0x60, byte(1), byte(1), byte(1), byte(1)} |
| 119 | onu := createMockOnu(1, 1) |
| 120 | s, err := NewService("testService", mac, onu, 900, 900, |
| 121 | false, false, false, 64, 0, false, |
| 122 | 0, 0, 0, 0) |
| 123 | |
| 124 | assert.Nil(t, err) |
| 125 | |
| 126 | stream := &mockStream{ |
| 127 | Calls: make(map[int]*openolt.Indication), |
| 128 | } |
| 129 | |
| 130 | s.HandleDhcp(stream, 900) |
| 131 | |
| 132 | assert.Equal(t, stream.CallCount, 0) |
| 133 | |
| 134 | // state should not change |
| 135 | assert.Equal(t, s.DHCPState.Current(), "created") |
| 136 | } |
| 137 | |
| 138 | // when we receive a DHCP flow we call HandleDhcp an all the ONU Services |
| 139 | // each service device whether the tag matches it's own configuration |
| 140 | func TestService_HandleDhcp_different_c_Tag(t *testing.T) { |
| 141 | mac := net.HardwareAddr{0x2e, 0x60, byte(1), byte(1), byte(1), byte(1)} |
| 142 | onu := createMockOnu(1, 1) |
| 143 | s, err := NewService("testService", mac, onu, 900, 900, |
| 144 | false, false, false, 64, 0, false, |
| 145 | 0, 0, 0, 0) |
| 146 | |
| 147 | assert.Nil(t, err) |
| 148 | |
| 149 | stream := &mockStream{ |
| 150 | Calls: make(map[int]*openolt.Indication), |
| 151 | } |
| 152 | |
| 153 | // NOTE that the c_tag is different from the one configured in the service |
| 154 | s.HandleDhcp(stream, 800) |
| 155 | |
| 156 | assert.Equal(t, stream.CallCount, 0) |
| 157 | |
| 158 | // state should not change |
| 159 | assert.Equal(t, s.DHCPState.Current(), "created") |
| 160 | } |
| 161 | |
| 162 | // make sure that if the service does need DHCP we're sending any packet |
| 163 | func TestService_HandleDhcp_needed(t *testing.T) { |
| 164 | mac := net.HardwareAddr{0x2e, 0x60, byte(1), byte(1), byte(1), byte(1)} |
| 165 | onu := createMockOnu(1, 1) |
| 166 | s, err := NewService("testService", mac, onu, 900, 900, |
| 167 | false, true, false, 64, 0, false, |
| 168 | 0, 0, 0, 0) |
| 169 | |
| 170 | assert.Nil(t, err) |
| 171 | |
| 172 | stream := &mockStream{ |
| 173 | Calls: make(map[int]*openolt.Indication), |
| 174 | } |
| 175 | |
| 176 | s.HandleDhcp(stream, 900) |
| 177 | |
| 178 | assert.Equal(t, stream.CallCount, 1) |
| 179 | |
| 180 | // state should not change |
| 181 | assert.Equal(t, s.DHCPState.Current(), "dhcp_discovery_sent") |
| 182 | } |