blob: 7b598a01345b9953623851fe5845c7960dcae05d [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"
Matteo Scandolo24a88c42020-09-17 14:55:28 -070021 "fmt"
Shrey Baid688b4242020-07-10 20:40:10 +053022 "net"
23 "testing"
24
Matteo Scandolo075b1892019-10-07 12:11:07 -070025 "github.com/looplab/fsm"
Matteo Scandolo4f4ac792020-10-01 16:33:21 -070026 "github.com/opencord/voltha-protos/v4/go/openolt"
Matteo Scandolo24a88c42020-09-17 14:55:28 -070027 "github.com/stretchr/testify/assert"
Matteo Scandolo075b1892019-10-07 12:11:07 -070028 "google.golang.org/grpc"
Matteo Scandolo075b1892019-10-07 12:11:07 -070029)
30
31// MOCKS
Matteo Scandolo075b1892019-10-07 12:11:07 -070032
33var 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
44type mockStreamSuccess struct {
45 grpc.ServerStream
Matteo Scandolo27428702019-10-11 16:21:16 -070046 CallCount int
47 Calls map[int]*openolt.PacketIndication
48 fail bool
Matteo Scandolo075b1892019-10-07 12:11:07 -070049}
50
Matteo Scandolo27428702019-10-11 16:21:16 -070051func (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 Scandolo075b1892019-10-07 12:11:07 -070057 return nil
58}
59
Matteo Scandolo075b1892019-10-07 12:11:07 -070060// TESTS
61
Matteo Scandolo24a88c42020-09-17 14:55:28 -070062func 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 Scandolo075b1892019-10-07 12:11:07 -070080func TestSendDHCPDiscovery(t *testing.T) {
Matteo Scandolo075b1892019-10-07 12:11:07 -070081 dhcpStateMachine.SetState("dhcp_started")
82
Matteo Scandolo27428702019-10-11 16:21:16 -070083 var onuId uint32 = 1
Matteo Scandolo4a036262020-08-17 15:56:13 -070084 var gemPortId uint32 = 1
Matteo Scandolo27428702019-10-11 16:21:16 -070085 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 Scandolo27428702019-10-11 16:21:16 -070090 stream := &mockStreamSuccess{
91 Calls: make(map[int]*openolt.PacketIndication),
92 fail: false,
93 }
Matteo Scandolo075b1892019-10-07 12:11:07 -070094
Matteo Scandolo24a88c42020-09-17 14:55:28 -070095 if err := SendDHCPDiscovery(ponPortId, onuId, "hsia", 900, gemPortId, serialNumber, portNo, dhcpStateMachine, mac, 7, stream); err != nil {
Matteo Scandolo075b1892019-10-07 12:11:07 -070096 t.Errorf("SendDHCPDiscovery returned an error: %v", err)
97 t.Fail()
98 }
99
Matteo Scandolo27428702019-10-11 16:21:16 -0700100 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 Scandolo075b1892019-10-07 12:11:07 -0700105
106 assert.Equal(t, dhcpStateMachine.Current(), "dhcp_discovery_sent")
107}
108
109// TODO test dhcp.HandleNextPacket
110
111func 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 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_discovery_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_request_sent")
Shrey Baid688b4242020-07-10 20:40:10 +0530126 _ = updateDhcpFailed(onuId, ponPortId, serialNumber, dhcpStateMachine)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700127 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}