blob: ff9b943cd25bfdea239e9dff5b55067c70850bf0 [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 Scandolo3de9de02019-11-14 13:40:03 -080025 "github.com/opencord/voltha-protos/v2/go/openolt"
Matteo Scandolo075b1892019-10-07 12:11:07 -070026 "google.golang.org/grpc"
27 "gotest.tools/assert"
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
61func TestSendDHCPDiscovery(t *testing.T) {
Matteo Scandolo075b1892019-10-07 12:11:07 -070062 dhcpStateMachine.SetState("dhcp_started")
63
Matteo Scandolo27428702019-10-11 16:21:16 -070064 var onuId uint32 = 1
65 var gemPortId uint16 = 1
66 var ponPortId uint32 = 0
Matteo Scandolo378b8c92020-04-16 14:34:22 -070067 var oltId int = 1
Matteo Scandolo27428702019-10-11 16:21:16 -070068 var serialNumber = "BBSM00000001"
69 var mac = net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(ponPortId), byte(onuId)}
70 var portNo uint32 = 16
71
Matteo Scandolo075b1892019-10-07 12:11:07 -070072 // Save current function and restore at the end:
73 old := GetGemPortId
74 defer func() { GetGemPortId = old }()
75
76 GetGemPortId = func(intfId uint32, onuId uint32) (uint16, error) {
Matteo Scandolo27428702019-10-11 16:21:16 -070077 return gemPortId, nil
Matteo Scandolo075b1892019-10-07 12:11:07 -070078 }
79
Matteo Scandolo27428702019-10-11 16:21:16 -070080 stream := &mockStreamSuccess{
81 Calls: make(map[int]*openolt.PacketIndication),
82 fail: false,
83 }
Matteo Scandolo075b1892019-10-07 12:11:07 -070084
Matteo Scandolo378b8c92020-04-16 14:34:22 -070085 if err := SendDHCPDiscovery(oltId, ponPortId, onuId, serialNumber, portNo, dhcpStateMachine, mac, 1, stream); err != nil {
Matteo Scandolo075b1892019-10-07 12:11:07 -070086 t.Errorf("SendDHCPDiscovery returned an error: %v", err)
87 t.Fail()
88 }
89
Matteo Scandolo27428702019-10-11 16:21:16 -070090 assert.Equal(t, stream.CallCount, 1)
91 assert.Equal(t, stream.Calls[1].PortNo, portNo)
92 assert.Equal(t, stream.Calls[1].IntfId, ponPortId)
93 assert.Equal(t, stream.Calls[1].IntfType, "pon")
94 assert.Equal(t, stream.Calls[1].GemportId, uint32(gemPortId))
Matteo Scandolo075b1892019-10-07 12:11:07 -070095
96 assert.Equal(t, dhcpStateMachine.Current(), "dhcp_discovery_sent")
97}
98
99// TODO test dhcp.HandleNextPacket
100
101func TestUpdateDhcpFailed(t *testing.T) {
102
103 var onuId uint32 = 1
104 var ponPortId uint32 = 0
105 var serialNumber string = "BBSM00000001"
106
107 dhcpStateMachine.SetState("dhcp_started")
Shrey Baid688b4242020-07-10 20:40:10 +0530108 _ = updateDhcpFailed(onuId, ponPortId, serialNumber, dhcpStateMachine)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700109 assert.Equal(t, dhcpStateMachine.Current(), "dhcp_failed")
110
111 dhcpStateMachine.SetState("dhcp_discovery_sent")
Shrey Baid688b4242020-07-10 20:40:10 +0530112 _ = updateDhcpFailed(onuId, ponPortId, serialNumber, dhcpStateMachine)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700113 assert.Equal(t, dhcpStateMachine.Current(), "dhcp_failed")
114
115 dhcpStateMachine.SetState("dhcp_request_sent")
Shrey Baid688b4242020-07-10 20:40:10 +0530116 _ = updateDhcpFailed(onuId, ponPortId, serialNumber, dhcpStateMachine)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700117 assert.Equal(t, dhcpStateMachine.Current(), "dhcp_failed")
118
119 dhcpStateMachine.SetState("dhcp_ack_received")
120 err := updateDhcpFailed(onuId, ponPortId, serialNumber, dhcpStateMachine)
121 if err == nil {
122 t.Errorf("updateDhcpFailed did not return an error")
123 t.Fail()
124 }
125 assert.Equal(t, err.Error(), "event dhcp_failed inappropriate in current state dhcp_ack_received")
126
127}