blob: c9b7f245addbb5711cf2f86e0a52cf4a00cd39ba [file] [log] [blame]
Matteo Scandolo075b1892019-10-07 12:11:07 -07001/*
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
17package dhcp
18
19import (
20 "errors"
Shrey Baid688b4242020-07-10 20:40:10 +053021 "net"
22 "testing"
23
Matteo Scandolo075b1892019-10-07 12:11:07 -070024 "github.com/looplab/fsm"
David K. Bainbridgec415efe2021-08-19 13:05:21 +000025 "github.com/opencord/voltha-protos/v5/go/openolt"
Matteo Scandolo24a88c42020-09-17 14:55:28 -070026 "github.com/stretchr/testify/assert"
Matteo Scandolo075b1892019-10-07 12:11:07 -070027 "google.golang.org/grpc"
Matteo Scandolo075b1892019-10-07 12:11:07 -070028)
29
30// MOCKS
Matteo Scandolo075b1892019-10-07 12:11:07 -070031
32var 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
43type mockStreamSuccess struct {
44 grpc.ServerStream
Matteo Scandolo27428702019-10-11 16:21:16 -070045 CallCount int
46 Calls map[int]*openolt.PacketIndication
47 fail bool
Matteo Scandolo075b1892019-10-07 12:11:07 -070048}
49
Matteo Scandolo27428702019-10-11 16:21:16 -070050func (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 Scandolo075b1892019-10-07 12:11:07 -070056 return nil
57}
58
Matteo Scandolo075b1892019-10-07 12:11:07 -070059// TESTS
60
Matteo Scandolo24a88c42020-09-17 14:55:28 -070061func 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 Scandolo24a88c42020-09-17 14:55:28 -070070 assert.NotEqual(t, xid1, xid2)
71 assert.NotEqual(t, xid1, xid3)
72 assert.NotEqual(t, xid2, xid3)
73}
74
Matteo Scandolo075b1892019-10-07 12:11:07 -070075func TestSendDHCPDiscovery(t *testing.T) {
Matteo Scandolo075b1892019-10-07 12:11:07 -070076 dhcpStateMachine.SetState("dhcp_started")
77
Matteo Scandolo27428702019-10-11 16:21:16 -070078 var onuId uint32 = 1
Matteo Scandolo4a036262020-08-17 15:56:13 -070079 var gemPortId uint32 = 1
Matteo Scandolo27428702019-10-11 16:21:16 -070080 var ponPortId uint32 = 0
Matteo Scandolo8a574812021-05-20 15:18:53 -070081 var uniId uint32 = 0
Matteo Scandolo27428702019-10-11 16:21:16 -070082 var serialNumber = "BBSM00000001"
83 var mac = net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(ponPortId), byte(onuId)}
84 var portNo uint32 = 16
85
Matteo Scandolo27428702019-10-11 16:21:16 -070086 stream := &mockStreamSuccess{
87 Calls: make(map[int]*openolt.PacketIndication),
88 fail: false,
89 }
Matteo Scandolo075b1892019-10-07 12:11:07 -070090
Matteo Scandolo8a574812021-05-20 15:18:53 -070091 if err := SendDHCPDiscovery(ponPortId, onuId, "hsia", 900, gemPortId, serialNumber, portNo, uniId, dhcpStateMachine, mac, 7, stream); err != nil {
Matteo Scandolo075b1892019-10-07 12:11:07 -070092 t.Errorf("SendDHCPDiscovery returned an error: %v", err)
93 t.Fail()
94 }
95
Matteo Scandolo27428702019-10-11 16:21:16 -070096 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 Scandolo075b1892019-10-07 12:11:07 -0700101
102 assert.Equal(t, dhcpStateMachine.Current(), "dhcp_discovery_sent")
103}
104
105// TODO test dhcp.HandleNextPacket
106
107func 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 Baid688b4242020-07-10 20:40:10 +0530114 _ = updateDhcpFailed(onuId, ponPortId, serialNumber, dhcpStateMachine)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700115 assert.Equal(t, dhcpStateMachine.Current(), "dhcp_failed")
116
117 dhcpStateMachine.SetState("dhcp_discovery_sent")
Shrey Baid688b4242020-07-10 20:40:10 +0530118 _ = updateDhcpFailed(onuId, ponPortId, serialNumber, dhcpStateMachine)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700119 assert.Equal(t, dhcpStateMachine.Current(), "dhcp_failed")
120
121 dhcpStateMachine.SetState("dhcp_request_sent")
Shrey Baid688b4242020-07-10 20:40:10 +0530122 _ = updateDhcpFailed(onuId, ponPortId, serialNumber, dhcpStateMachine)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700123 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}