blob: 03e40b7944874e992096ca39f52d84515ab8a2bc [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"
21 "github.com/looplab/fsm"
Matteo Scandolo3de9de02019-11-14 13:40:03 -080022 "github.com/opencord/voltha-protos/v2/go/openolt"
Matteo Scandolo075b1892019-10-07 12:11:07 -070023 "google.golang.org/grpc"
24 "gotest.tools/assert"
25 "net"
26 "testing"
27)
28
29// MOCKS
Matteo Scandolo075b1892019-10-07 12:11:07 -070030
31var dhcpStateMachine = fsm.NewFSM(
32 "dhcp_started",
33 fsm.Events{
34 {Name: "dhcp_discovery_sent", Src: []string{"dhcp_started"}, Dst: "dhcp_discovery_sent"},
35 {Name: "dhcp_request_sent", Src: []string{"dhcp_discovery_sent"}, Dst: "dhcp_request_sent"},
36 {Name: "dhcp_ack_received", Src: []string{"dhcp_request_sent"}, Dst: "dhcp_ack_received"},
37 {Name: "dhcp_failed", Src: []string{"dhcp_started", "dhcp_discovery_sent", "dhcp_request_sent"}, Dst: "dhcp_failed"},
38 },
39 fsm.Callbacks{},
40)
41
42type mockStreamSuccess struct {
43 grpc.ServerStream
Matteo Scandolo27428702019-10-11 16:21:16 -070044 CallCount int
45 Calls map[int]*openolt.PacketIndication
46 fail bool
Matteo Scandolo075b1892019-10-07 12:11:07 -070047}
48
Matteo Scandolo27428702019-10-11 16:21:16 -070049func (s *mockStreamSuccess) Send(ind *openolt.Indication) error {
50 s.CallCount++
51 if s.fail {
52 return errors.New("fake-error")
53 }
54 s.Calls[s.CallCount] = ind.GetPktInd()
Matteo Scandolo075b1892019-10-07 12:11:07 -070055 return nil
56}
57
Matteo Scandolo075b1892019-10-07 12:11:07 -070058// TESTS
59
60func TestSendDHCPDiscovery(t *testing.T) {
Matteo Scandolo075b1892019-10-07 12:11:07 -070061 dhcpStateMachine.SetState("dhcp_started")
62
Matteo Scandolo27428702019-10-11 16:21:16 -070063 var onuId uint32 = 1
64 var gemPortId uint16 = 1
65 var ponPortId uint32 = 0
Matteo Scandolo378b8c92020-04-16 14:34:22 -070066 var oltId int = 1
Matteo Scandolo27428702019-10-11 16:21:16 -070067 var serialNumber = "BBSM00000001"
68 var mac = net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(ponPortId), byte(onuId)}
69 var portNo uint32 = 16
70
Matteo Scandolo075b1892019-10-07 12:11:07 -070071 // Save current function and restore at the end:
72 old := GetGemPortId
73 defer func() { GetGemPortId = old }()
74
75 GetGemPortId = func(intfId uint32, onuId uint32) (uint16, error) {
Matteo Scandolo27428702019-10-11 16:21:16 -070076 return gemPortId, nil
Matteo Scandolo075b1892019-10-07 12:11:07 -070077 }
78
Matteo Scandolo27428702019-10-11 16:21:16 -070079 stream := &mockStreamSuccess{
80 Calls: make(map[int]*openolt.PacketIndication),
81 fail: false,
82 }
Matteo Scandolo075b1892019-10-07 12:11:07 -070083
Matteo Scandolo378b8c92020-04-16 14:34:22 -070084 if err := SendDHCPDiscovery(oltId, ponPortId, onuId, serialNumber, portNo, dhcpStateMachine, mac, 1, stream); err != nil {
Matteo Scandolo075b1892019-10-07 12:11:07 -070085 t.Errorf("SendDHCPDiscovery returned an error: %v", err)
86 t.Fail()
87 }
88
Matteo Scandolo27428702019-10-11 16:21:16 -070089 assert.Equal(t, stream.CallCount, 1)
90 assert.Equal(t, stream.Calls[1].PortNo, portNo)
91 assert.Equal(t, stream.Calls[1].IntfId, ponPortId)
92 assert.Equal(t, stream.Calls[1].IntfType, "pon")
93 assert.Equal(t, stream.Calls[1].GemportId, uint32(gemPortId))
Matteo Scandolo075b1892019-10-07 12:11:07 -070094
95 assert.Equal(t, dhcpStateMachine.Current(), "dhcp_discovery_sent")
96}
97
98// TODO test dhcp.HandleNextPacket
99
100func TestUpdateDhcpFailed(t *testing.T) {
101
102 var onuId uint32 = 1
103 var ponPortId uint32 = 0
104 var serialNumber string = "BBSM00000001"
105
106 dhcpStateMachine.SetState("dhcp_started")
107 updateDhcpFailed(onuId, ponPortId, serialNumber, dhcpStateMachine)
108 assert.Equal(t, dhcpStateMachine.Current(), "dhcp_failed")
109
110 dhcpStateMachine.SetState("dhcp_discovery_sent")
111 updateDhcpFailed(onuId, ponPortId, serialNumber, dhcpStateMachine)
112 assert.Equal(t, dhcpStateMachine.Current(), "dhcp_failed")
113
114 dhcpStateMachine.SetState("dhcp_request_sent")
115 updateDhcpFailed(onuId, ponPortId, serialNumber, dhcpStateMachine)
116 assert.Equal(t, dhcpStateMachine.Current(), "dhcp_failed")
117
118 dhcpStateMachine.SetState("dhcp_ack_received")
119 err := updateDhcpFailed(onuId, ponPortId, serialNumber, dhcpStateMachine)
120 if err == nil {
121 t.Errorf("updateDhcpFailed did not return an error")
122 t.Fail()
123 }
124 assert.Equal(t, err.Error(), "event dhcp_failed inappropriate in current state dhcp_ack_received")
125
126}