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" |
| 22 | "gotest.tools/assert" |
| 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 | |
| 46 | func TestService_HandleAuth_noEapol(t *testing.T) { |
| 47 | mac := net.HardwareAddr{0x2e, 0x60, byte(1), byte(1), byte(1), byte(1)} |
| 48 | onu := createMockOnu(1, 1) |
| 49 | s, err := NewService("testService", mac, onu, 900, 900, |
| 50 | false, false, false, 64, 0, false, |
| 51 | 0, 0, 0, 0) |
| 52 | |
| 53 | assert.NilError(t, err) |
| 54 | |
| 55 | stream := &mockStream{ |
| 56 | Calls: make(map[int]*openolt.Indication), |
| 57 | channel: make(chan int, 10), |
| 58 | } |
| 59 | |
| 60 | s.HandleAuth(stream) |
| 61 | |
| 62 | // if the service does not need EAPOL we don't expect any packet to be generated |
| 63 | assert.Equal(t, stream.CallCount, 0) |
| 64 | |
| 65 | // state should not change |
| 66 | assert.Equal(t, s.EapolState.Current(), "created") |
| 67 | } |
| 68 | |
| 69 | func TestService_HandleAuth_withEapol(t *testing.T) { |
| 70 | mac := net.HardwareAddr{0x2e, 0x60, byte(1), byte(1), byte(1), byte(1)} |
| 71 | onu := createMockOnu(1, 1) |
| 72 | s, err := NewService("testService", mac, onu, 900, 900, |
| 73 | true, false, false, 64, 0, false, |
| 74 | 0, 0, 0, 0) |
| 75 | |
| 76 | assert.NilError(t, err) |
| 77 | |
| 78 | stream := &mockStream{ |
| 79 | Calls: make(map[int]*openolt.Indication), |
| 80 | } |
| 81 | |
| 82 | s.HandleAuth(stream) |
| 83 | |
| 84 | // if the service does not need EAPOL we don't expect any packet to be generated |
| 85 | assert.Equal(t, stream.CallCount, 1) |
| 86 | |
| 87 | // state should not change |
| 88 | assert.Equal(t, s.EapolState.Current(), "eap_start_sent") |
| 89 | } |