blob: 0733cc08fa59ec1a0257cb5d1e56476425234025 [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
30var calledSend = 0
31
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
45}
46
47func (s mockStreamSuccess) Send(ind *openolt.Indication) error {
48 calledSend++
49 return nil
50}
51
52type mockStreamError struct {
53 grpc.ServerStream
54}
55
56func (s mockStreamError) Send(ind *openolt.Indication) error {
57 calledSend++
58 return errors.New("stream-error")
59}
60
61// TESTS
62
63func TestSendDHCPDiscovery(t *testing.T) {
64 calledSend = 0
65 dhcpStateMachine.SetState("dhcp_started")
66
67 // Save current function and restore at the end:
68 old := GetGemPortId
69 defer func() { GetGemPortId = old }()
70
71 GetGemPortId = func(intfId uint32, onuId uint32) (uint16, error) {
72 return 1, nil
73 }
74
75 var onuId uint32 = 1
76 var ponPortId uint32 = 0
77 var serialNumber string = "BBSM00000001"
78 var mac = net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(ponPortId), byte(onuId)}
79
80 stream := mockStreamSuccess{}
81
82 if err := SendDHCPDiscovery(ponPortId, onuId, serialNumber, dhcpStateMachine, mac, 1, stream); err != nil {
83 t.Errorf("SendDHCPDiscovery returned an error: %v", err)
84 t.Fail()
85 }
86
87 assert.Equal(t, calledSend, 1)
88
89 assert.Equal(t, dhcpStateMachine.Current(), "dhcp_discovery_sent")
90}
91
92// TODO test dhcp.HandleNextPacket
93
94func TestUpdateDhcpFailed(t *testing.T) {
95
96 var onuId uint32 = 1
97 var ponPortId uint32 = 0
98 var serialNumber string = "BBSM00000001"
99
100 dhcpStateMachine.SetState("dhcp_started")
101 updateDhcpFailed(onuId, ponPortId, serialNumber, dhcpStateMachine)
102 assert.Equal(t, dhcpStateMachine.Current(), "dhcp_failed")
103
104 dhcpStateMachine.SetState("dhcp_discovery_sent")
105 updateDhcpFailed(onuId, ponPortId, serialNumber, dhcpStateMachine)
106 assert.Equal(t, dhcpStateMachine.Current(), "dhcp_failed")
107
108 dhcpStateMachine.SetState("dhcp_request_sent")
109 updateDhcpFailed(onuId, ponPortId, serialNumber, dhcpStateMachine)
110 assert.Equal(t, dhcpStateMachine.Current(), "dhcp_failed")
111
112 dhcpStateMachine.SetState("dhcp_ack_received")
113 err := updateDhcpFailed(onuId, ponPortId, serialNumber, dhcpStateMachine)
114 if err == nil {
115 t.Errorf("updateDhcpFailed did not return an error")
116 t.Fail()
117 }
118 assert.Equal(t, err.Error(), "event dhcp_failed inappropriate in current state dhcp_ack_received")
119
120}