Matteo Scandolo | 378b8c9 | 2020-04-16 14:34:22 -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 | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 20 | "github.com/google/gopacket/layers" |
Matteo Scandolo | 378b8c9 | 2020-04-16 14:34:22 -0700 | [diff] [blame] | 21 | "gotest.tools/assert" |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 22 | "net" |
Matteo Scandolo | 378b8c9 | 2020-04-16 14:34:22 -0700 | [diff] [blame] | 23 | "testing" |
| 24 | ) |
| 25 | |
| 26 | func Test_Onu_CreateOnu(t *testing.T) { |
| 27 | |
| 28 | olt := OltDevice{ |
| 29 | ID: 0, |
| 30 | } |
| 31 | pon := PonPort{ |
| 32 | ID: 1, |
| 33 | Olt: &olt, |
| 34 | } |
| 35 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 36 | onu := CreateONU(&olt, &pon, 1, 0, false) |
Matteo Scandolo | 378b8c9 | 2020-04-16 14:34:22 -0700 | [diff] [blame] | 37 | |
| 38 | assert.Equal(t, onu.Sn(), "BBSM00000101") |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 39 | } |
| 40 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 41 | func Test_AddGemPortToService_eapol(t *testing.T) { |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 42 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 43 | hsia := Service{Name: "hsia", NeedsEapol: true, CTag: 900} |
| 44 | voip := Service{Name: "voip", NeedsEapol: false, CTag: 55} |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 45 | |
| 46 | onu := createTestOnu() |
| 47 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 48 | onu.Services = []ServiceIf{&hsia, &voip} |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 49 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 50 | onu.addGemPortToService(1024, uint32(layers.EthernetTypeEAPOL), 0, 0) |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 51 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 52 | assert.Equal(t, hsia.GemPort, uint32(1024)) |
| 53 | assert.Equal(t, voip.GemPort, uint32(0)) |
| 54 | } |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 55 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 56 | func Test_AddGemPortToService_dhcp(t *testing.T) { |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 57 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 58 | hsia := Service{Name: "hsia", NeedsEapol: true} |
| 59 | voip := Service{Name: "voip", NeedsDhcp: true, CTag: 900} |
| 60 | mc := Service{Name: "mc", CTag: 900} |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 61 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 62 | onu := createTestOnu() |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 63 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 64 | onu.Services = []ServiceIf{&hsia, &voip, &mc} |
| 65 | |
| 66 | onu.addGemPortToService(1025, uint32(layers.EthernetTypeIPv4), 900, 0) |
| 67 | |
| 68 | assert.Equal(t, hsia.GemPort, uint32(0)) |
| 69 | assert.Equal(t, voip.GemPort, uint32(1025)) |
| 70 | assert.Equal(t, mc.GemPort, uint32(0)) |
| 71 | } |
| 72 | |
| 73 | func Test_AddGemPortToService_dataplane(t *testing.T) { |
| 74 | |
| 75 | hsia := Service{Name: "hsia", NeedsEapol: true, CTag: 900, STag: 500} |
| 76 | voip := Service{Name: "voip", NeedsDhcp: true, CTag: 900} |
| 77 | |
| 78 | onu := createTestOnu() |
| 79 | |
| 80 | onu.Services = []ServiceIf{&hsia, &voip} |
| 81 | |
| 82 | onu.addGemPortToService(1024, uint32(layers.EthernetTypeLLC), 500, 900) |
| 83 | |
| 84 | assert.Equal(t, hsia.GemPort, uint32(1024)) |
| 85 | assert.Equal(t, voip.GemPort, uint32(0)) |
| 86 | } |
| 87 | |
| 88 | func Test_FindServiceByMacAddress(t *testing.T) { |
| 89 | |
| 90 | mac := net.HardwareAddr{0x2e, 0x60, byte(1), byte(1), byte(1), byte(2)} |
| 91 | |
| 92 | hsia := Service{Name: "hsia", HwAddress: net.HardwareAddr{0x2e, 0x60, byte(1), byte(1), byte(1), byte(1)}} |
| 93 | voip := Service{Name: "voip", HwAddress: mac} |
| 94 | vod := Service{Name: "vod", HwAddress: net.HardwareAddr{0x2e, 0x60, byte(1), byte(1), byte(1), byte(3)}} |
| 95 | |
| 96 | onu := createTestOnu() |
| 97 | |
| 98 | onu.Services = []ServiceIf{&hsia, &voip, &vod} |
| 99 | |
| 100 | service, err := onu.findServiceByMacAddress(mac) |
| 101 | assert.NilError(t, err) |
| 102 | assert.Equal(t, service.HwAddress.String(), mac.String()) |
Matteo Scandolo | 5ff8008 | 2019-12-20 13:20:57 -0800 | [diff] [blame] | 103 | } |