blob: 6aa806039ef432792415a1846e416186cf3c73d8 [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"
22 "github.com/opencord/voltha-protos/go/openolt"
23 "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
66 var serialNumber = "BBSM00000001"
67 var mac = net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(ponPortId), byte(onuId)}
68 var portNo uint32 = 16
69
Matteo Scandolo075b1892019-10-07 12:11:07 -070070 // Save current function and restore at the end:
71 old := GetGemPortId
72 defer func() { GetGemPortId = old }()
73
74 GetGemPortId = func(intfId uint32, onuId uint32) (uint16, error) {
Matteo Scandolo27428702019-10-11 16:21:16 -070075 return gemPortId, nil
Matteo Scandolo075b1892019-10-07 12:11:07 -070076 }
77
Matteo Scandolo27428702019-10-11 16:21:16 -070078 stream := &mockStreamSuccess{
79 Calls: make(map[int]*openolt.PacketIndication),
80 fail: false,
81 }
Matteo Scandolo075b1892019-10-07 12:11:07 -070082
Matteo Scandolo27428702019-10-11 16:21:16 -070083 if err := SendDHCPDiscovery(ponPortId, onuId, serialNumber, portNo, dhcpStateMachine, mac, 1, stream); err != nil {
Matteo Scandolo075b1892019-10-07 12:11:07 -070084 t.Errorf("SendDHCPDiscovery returned an error: %v", err)
85 t.Fail()
86 }
87
Matteo Scandolo27428702019-10-11 16:21:16 -070088 assert.Equal(t, stream.CallCount, 1)
89 assert.Equal(t, stream.Calls[1].PortNo, portNo)
90 assert.Equal(t, stream.Calls[1].IntfId, ponPortId)
91 assert.Equal(t, stream.Calls[1].IntfType, "pon")
92 assert.Equal(t, stream.Calls[1].GemportId, uint32(gemPortId))
Matteo Scandolo075b1892019-10-07 12:11:07 -070093
94 assert.Equal(t, dhcpStateMachine.Current(), "dhcp_discovery_sent")
95}
96
97// TODO test dhcp.HandleNextPacket
98
99func TestUpdateDhcpFailed(t *testing.T) {
100
101 var onuId uint32 = 1
102 var ponPortId uint32 = 0
103 var serialNumber string = "BBSM00000001"
104
105 dhcpStateMachine.SetState("dhcp_started")
106 updateDhcpFailed(onuId, ponPortId, serialNumber, dhcpStateMachine)
107 assert.Equal(t, dhcpStateMachine.Current(), "dhcp_failed")
108
109 dhcpStateMachine.SetState("dhcp_discovery_sent")
110 updateDhcpFailed(onuId, ponPortId, serialNumber, dhcpStateMachine)
111 assert.Equal(t, dhcpStateMachine.Current(), "dhcp_failed")
112
113 dhcpStateMachine.SetState("dhcp_request_sent")
114 updateDhcpFailed(onuId, ponPortId, serialNumber, dhcpStateMachine)
115 assert.Equal(t, dhcpStateMachine.Current(), "dhcp_failed")
116
117 dhcpStateMachine.SetState("dhcp_ack_received")
118 err := updateDhcpFailed(onuId, ponPortId, serialNumber, dhcpStateMachine)
119 if err == nil {
120 t.Errorf("updateDhcpFailed did not return an error")
121 t.Fail()
122 }
123 assert.Equal(t, err.Error(), "event dhcp_failed inappropriate in current state dhcp_ack_received")
124
125}