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