Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -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 dhcp |
| 18 | |
| 19 | import ( |
| 20 | "errors" |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 21 | "net" |
| 22 | "testing" |
| 23 | |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 24 | "github.com/looplab/fsm" |
David K. Bainbridge | c415efe | 2021-08-19 13:05:21 +0000 | [diff] [blame^] | 25 | "github.com/opencord/voltha-protos/v5/go/openolt" |
Matteo Scandolo | 24a88c4 | 2020-09-17 14:55:28 -0700 | [diff] [blame] | 26 | "github.com/stretchr/testify/assert" |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 27 | "google.golang.org/grpc" |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | // MOCKS |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 31 | |
| 32 | var dhcpStateMachine = fsm.NewFSM( |
| 33 | "dhcp_started", |
| 34 | fsm.Events{ |
| 35 | {Name: "dhcp_discovery_sent", Src: []string{"dhcp_started"}, Dst: "dhcp_discovery_sent"}, |
| 36 | {Name: "dhcp_request_sent", Src: []string{"dhcp_discovery_sent"}, Dst: "dhcp_request_sent"}, |
| 37 | {Name: "dhcp_ack_received", Src: []string{"dhcp_request_sent"}, Dst: "dhcp_ack_received"}, |
| 38 | {Name: "dhcp_failed", Src: []string{"dhcp_started", "dhcp_discovery_sent", "dhcp_request_sent"}, Dst: "dhcp_failed"}, |
| 39 | }, |
| 40 | fsm.Callbacks{}, |
| 41 | ) |
| 42 | |
| 43 | type mockStreamSuccess struct { |
| 44 | grpc.ServerStream |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 45 | CallCount int |
| 46 | Calls map[int]*openolt.PacketIndication |
| 47 | fail bool |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 50 | func (s *mockStreamSuccess) Send(ind *openolt.Indication) error { |
| 51 | s.CallCount++ |
| 52 | if s.fail { |
| 53 | return errors.New("fake-error") |
| 54 | } |
| 55 | s.Calls[s.CallCount] = ind.GetPktInd() |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 56 | return nil |
| 57 | } |
| 58 | |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 59 | // TESTS |
| 60 | |
Matteo Scandolo | 24a88c4 | 2020-09-17 14:55:28 -0700 | [diff] [blame] | 61 | func TestMacAddressToTxId(t *testing.T) { |
| 62 | mac1 := net.HardwareAddr{0x2e, 0x60, 0x00, 0x0c, 0x0f, 0x02} |
| 63 | mac2 := net.HardwareAddr{0x2e, 0x60, 0x00, 0x0f, 0x0c, 0x02} |
| 64 | mac3 := net.HardwareAddr{0x2e, 0x60, 0x00, 0x0c, 0x13, 0x01} |
| 65 | |
| 66 | xid1 := macAddressToTxId(mac1) |
| 67 | xid2 := macAddressToTxId(mac2) |
| 68 | xid3 := macAddressToTxId(mac3) |
| 69 | |
Matteo Scandolo | 24a88c4 | 2020-09-17 14:55:28 -0700 | [diff] [blame] | 70 | assert.NotEqual(t, xid1, xid2) |
| 71 | assert.NotEqual(t, xid1, xid3) |
| 72 | assert.NotEqual(t, xid2, xid3) |
| 73 | } |
| 74 | |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 75 | func TestSendDHCPDiscovery(t *testing.T) { |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 76 | dhcpStateMachine.SetState("dhcp_started") |
| 77 | |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 78 | var onuId uint32 = 1 |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 79 | var gemPortId uint32 = 1 |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 80 | var ponPortId uint32 = 0 |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 81 | var uniId uint32 = 0 |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 82 | var serialNumber = "BBSM00000001" |
| 83 | var mac = net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(ponPortId), byte(onuId)} |
| 84 | var portNo uint32 = 16 |
| 85 | |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 86 | stream := &mockStreamSuccess{ |
| 87 | Calls: make(map[int]*openolt.PacketIndication), |
| 88 | fail: false, |
| 89 | } |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 90 | |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 91 | if err := SendDHCPDiscovery(ponPortId, onuId, "hsia", 900, gemPortId, serialNumber, portNo, uniId, dhcpStateMachine, mac, 7, stream); err != nil { |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 92 | t.Errorf("SendDHCPDiscovery returned an error: %v", err) |
| 93 | t.Fail() |
| 94 | } |
| 95 | |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 96 | assert.Equal(t, stream.CallCount, 1) |
| 97 | assert.Equal(t, stream.Calls[1].PortNo, portNo) |
| 98 | assert.Equal(t, stream.Calls[1].IntfId, ponPortId) |
| 99 | assert.Equal(t, stream.Calls[1].IntfType, "pon") |
| 100 | assert.Equal(t, stream.Calls[1].GemportId, uint32(gemPortId)) |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 101 | |
| 102 | assert.Equal(t, dhcpStateMachine.Current(), "dhcp_discovery_sent") |
| 103 | } |
| 104 | |
| 105 | // TODO test dhcp.HandleNextPacket |
| 106 | |
| 107 | func TestUpdateDhcpFailed(t *testing.T) { |
| 108 | |
| 109 | var onuId uint32 = 1 |
| 110 | var ponPortId uint32 = 0 |
| 111 | var serialNumber string = "BBSM00000001" |
| 112 | |
| 113 | dhcpStateMachine.SetState("dhcp_started") |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 114 | _ = updateDhcpFailed(onuId, ponPortId, serialNumber, dhcpStateMachine) |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 115 | assert.Equal(t, dhcpStateMachine.Current(), "dhcp_failed") |
| 116 | |
| 117 | dhcpStateMachine.SetState("dhcp_discovery_sent") |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 118 | _ = updateDhcpFailed(onuId, ponPortId, serialNumber, dhcpStateMachine) |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 119 | assert.Equal(t, dhcpStateMachine.Current(), "dhcp_failed") |
| 120 | |
| 121 | dhcpStateMachine.SetState("dhcp_request_sent") |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 122 | _ = updateDhcpFailed(onuId, ponPortId, serialNumber, dhcpStateMachine) |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame] | 123 | assert.Equal(t, dhcpStateMachine.Current(), "dhcp_failed") |
| 124 | |
| 125 | dhcpStateMachine.SetState("dhcp_ack_received") |
| 126 | err := updateDhcpFailed(onuId, ponPortId, serialNumber, dhcpStateMachine) |
| 127 | if err == nil { |
| 128 | t.Errorf("updateDhcpFailed did not return an error") |
| 129 | t.Fail() |
| 130 | } |
| 131 | assert.Equal(t, err.Error(), "event dhcp_failed inappropriate in current state dhcp_ack_received") |
| 132 | |
| 133 | } |