blob: 3334e8254b1a8135e4ab3e8e7d42391ae4bd2031 [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"
Matteo Scandolo4f4ac792020-10-01 16:33:21 -070025 "github.com/opencord/voltha-protos/v4/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
81 var serialNumber = "BBSM00000001"
82 var mac = net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(ponPortId), byte(onuId)}
83 var portNo uint32 = 16
84
Matteo Scandolo27428702019-10-11 16:21:16 -070085 stream := &mockStreamSuccess{
86 Calls: make(map[int]*openolt.PacketIndication),
87 fail: false,
88 }
Matteo Scandolo075b1892019-10-07 12:11:07 -070089
Matteo Scandolo24a88c42020-09-17 14:55:28 -070090 if err := SendDHCPDiscovery(ponPortId, onuId, "hsia", 900, gemPortId, serialNumber, portNo, dhcpStateMachine, mac, 7, stream); err != nil {
Matteo Scandolo075b1892019-10-07 12:11:07 -070091 t.Errorf("SendDHCPDiscovery returned an error: %v", err)
92 t.Fail()
93 }
94
Matteo Scandolo27428702019-10-11 16:21:16 -070095 assert.Equal(t, stream.CallCount, 1)
96 assert.Equal(t, stream.Calls[1].PortNo, portNo)
97 assert.Equal(t, stream.Calls[1].IntfId, ponPortId)
98 assert.Equal(t, stream.Calls[1].IntfType, "pon")
99 assert.Equal(t, stream.Calls[1].GemportId, uint32(gemPortId))
Matteo Scandolo075b1892019-10-07 12:11:07 -0700100
101 assert.Equal(t, dhcpStateMachine.Current(), "dhcp_discovery_sent")
102}
103
104// TODO test dhcp.HandleNextPacket
105
106func TestUpdateDhcpFailed(t *testing.T) {
107
108 var onuId uint32 = 1
109 var ponPortId uint32 = 0
110 var serialNumber string = "BBSM00000001"
111
112 dhcpStateMachine.SetState("dhcp_started")
Shrey Baid688b4242020-07-10 20:40:10 +0530113 _ = updateDhcpFailed(onuId, ponPortId, serialNumber, dhcpStateMachine)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700114 assert.Equal(t, dhcpStateMachine.Current(), "dhcp_failed")
115
116 dhcpStateMachine.SetState("dhcp_discovery_sent")
Shrey Baid688b4242020-07-10 20:40:10 +0530117 _ = updateDhcpFailed(onuId, ponPortId, serialNumber, dhcpStateMachine)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700118 assert.Equal(t, dhcpStateMachine.Current(), "dhcp_failed")
119
120 dhcpStateMachine.SetState("dhcp_request_sent")
Shrey Baid688b4242020-07-10 20:40:10 +0530121 _ = updateDhcpFailed(onuId, ponPortId, serialNumber, dhcpStateMachine)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700122 assert.Equal(t, dhcpStateMachine.Current(), "dhcp_failed")
123
124 dhcpStateMachine.SetState("dhcp_ack_received")
125 err := updateDhcpFailed(onuId, ponPortId, serialNumber, dhcpStateMachine)
126 if err == nil {
127 t.Errorf("updateDhcpFailed did not return an error")
128 t.Fail()
129 }
130 assert.Equal(t, err.Error(), "event dhcp_failed inappropriate in current state dhcp_ack_received")
131
132}