blob: cc50188409c80a4a6e22cf90a2801eab299743a8 [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
Matteo Scandolo4a036262020-08-17 15:56:13 -070065 var gemPortId uint32 = 1
Matteo Scandolo27428702019-10-11 16:21:16 -070066 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 Scandolo27428702019-10-11 16:21:16 -070072 stream := &mockStreamSuccess{
73 Calls: make(map[int]*openolt.PacketIndication),
74 fail: false,
75 }
Matteo Scandolo075b1892019-10-07 12:11:07 -070076
Matteo Scandolo4a036262020-08-17 15:56:13 -070077 if err := SendDHCPDiscovery(oltId, ponPortId, onuId, 900, gemPortId, serialNumber, portNo, dhcpStateMachine, mac, stream); err != nil {
Matteo Scandolo075b1892019-10-07 12:11:07 -070078 t.Errorf("SendDHCPDiscovery returned an error: %v", err)
79 t.Fail()
80 }
81
Matteo Scandolo27428702019-10-11 16:21:16 -070082 assert.Equal(t, stream.CallCount, 1)
83 assert.Equal(t, stream.Calls[1].PortNo, portNo)
84 assert.Equal(t, stream.Calls[1].IntfId, ponPortId)
85 assert.Equal(t, stream.Calls[1].IntfType, "pon")
86 assert.Equal(t, stream.Calls[1].GemportId, uint32(gemPortId))
Matteo Scandolo075b1892019-10-07 12:11:07 -070087
88 assert.Equal(t, dhcpStateMachine.Current(), "dhcp_discovery_sent")
89}
90
91// TODO test dhcp.HandleNextPacket
92
93func TestUpdateDhcpFailed(t *testing.T) {
94
95 var onuId uint32 = 1
96 var ponPortId uint32 = 0
97 var serialNumber string = "BBSM00000001"
98
99 dhcpStateMachine.SetState("dhcp_started")
Shrey Baid688b4242020-07-10 20:40:10 +0530100 _ = updateDhcpFailed(onuId, ponPortId, serialNumber, dhcpStateMachine)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700101 assert.Equal(t, dhcpStateMachine.Current(), "dhcp_failed")
102
103 dhcpStateMachine.SetState("dhcp_discovery_sent")
Shrey Baid688b4242020-07-10 20:40:10 +0530104 _ = updateDhcpFailed(onuId, ponPortId, serialNumber, dhcpStateMachine)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700105 assert.Equal(t, dhcpStateMachine.Current(), "dhcp_failed")
106
107 dhcpStateMachine.SetState("dhcp_request_sent")
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_ack_received")
112 err := updateDhcpFailed(onuId, ponPortId, serialNumber, dhcpStateMachine)
113 if err == nil {
114 t.Errorf("updateDhcpFailed did not return an error")
115 t.Fail()
116 }
117 assert.Equal(t, err.Error(), "event dhcp_failed inappropriate in current state dhcp_ack_received")
118
119}