blob: 9f8ff2267ff0cd91aeaadb29bac5d2b75886a575 [file] [log] [blame]
Matteo Scandolo47e69bb2019-08-28 15:41:12 -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 eapol
18
19import (
Matteo Scandolo075b1892019-10-07 12:11:07 -070020 "errors"
Shrey Baid688b4242020-07-10 20:40:10 +053021 "net"
22 "testing"
23
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070024 "github.com/looplab/fsm"
Matteo Scandolo3de9de02019-11-14 13:40:03 -080025 "github.com/opencord/voltha-protos/v2/go/openolt"
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070026 "google.golang.org/grpc"
27 "gotest.tools/assert"
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070028)
29
Matteo Scandolo075b1892019-10-07 12:11:07 -070030// MOCKS
Matteo Scandolo075b1892019-10-07 12:11:07 -070031
32var eapolStateMachine = fsm.NewFSM(
33 "auth_started",
34 fsm.Events{
35 {Name: "eap_start_sent", Src: []string{"auth_started"}, Dst: "eap_start_sent"},
36 {Name: "eap_response_identity_sent", Src: []string{"eap_start_sent"}, Dst: "eap_response_identity_sent"},
37 {Name: "eap_response_challenge_sent", Src: []string{"eap_response_identity_sent"}, Dst: "eap_response_challenge_sent"},
38 {Name: "eap_response_success_received", Src: []string{"eap_response_challenge_sent"}, Dst: "eap_response_success_received"},
39 {Name: "auth_failed", Src: []string{"auth_started", "eap_start_sent", "eap_response_identity_sent", "eap_response_challenge_sent"}, Dst: "auth_failed"},
40 },
41 fsm.Callbacks{},
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070042)
43
Matteo Scandolo27428702019-10-11 16:21:16 -070044// params for the function under test
45var onuId uint32 = 1
46var gemPortId uint16 = 1
47var ponPortId uint32 = 0
48var serialNumber string = "BBSM00000001"
49var macAddress = net.HardwareAddr{0x01, 0x80, 0xC2, 0x00, 0x00, 0x03}
50var portNo uint32 = 16
51
52type mockStream struct {
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070053 grpc.ServerStream
Matteo Scandolo27428702019-10-11 16:21:16 -070054 CallCount int
55 Calls map[int]*openolt.PacketIndication
56 fail bool
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070057}
58
Matteo Scandolo27428702019-10-11 16:21:16 -070059func (s *mockStream) Send(ind *openolt.Indication) error {
60 s.CallCount++
61 if s.fail {
62 return errors.New("fake-error")
63 }
64 s.Calls[s.CallCount] = ind.GetPktInd()
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070065 return nil
66}
67
Matteo Scandolo075b1892019-10-07 12:11:07 -070068// TESTS
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070069
Matteo Scandolo075b1892019-10-07 12:11:07 -070070func TestSendEapStartSuccess(t *testing.T) {
Matteo Scandolo075b1892019-10-07 12:11:07 -070071 eapolStateMachine.SetState("auth_started")
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070072
Matteo Scandolo075b1892019-10-07 12:11:07 -070073 // Save current function and restore at the end:
74 old := GetGemPortId
75 defer func() { GetGemPortId = old }()
76
77 GetGemPortId = func(intfId uint32, onuId uint32) (uint16, error) {
Matteo Scandolo27428702019-10-11 16:21:16 -070078 return gemPortId, nil
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070079 }
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070080
Matteo Scandolo27428702019-10-11 16:21:16 -070081 stream := &mockStream{
82 Calls: make(map[int]*openolt.PacketIndication),
83 fail: false,
84 }
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070085
Matteo Scandolo27428702019-10-11 16:21:16 -070086 if err := SendEapStart(onuId, ponPortId, serialNumber, portNo, macAddress, eapolStateMachine, stream); err != nil {
Matteo Scandolo075b1892019-10-07 12:11:07 -070087 t.Errorf("SendEapStart returned an error: %v", err)
88 t.Fail()
89 }
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070090
Matteo Scandolo27428702019-10-11 16:21:16 -070091 assert.Equal(t, stream.CallCount, 1)
92 assert.Equal(t, stream.Calls[1].PortNo, portNo)
93 assert.Equal(t, stream.Calls[1].IntfId, ponPortId)
94 assert.Equal(t, stream.Calls[1].IntfType, "pon")
95 assert.Equal(t, stream.Calls[1].GemportId, uint32(gemPortId))
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070096
Matteo Scandolo075b1892019-10-07 12:11:07 -070097 assert.Equal(t, eapolStateMachine.Current(), "eap_start_sent")
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070098
Matteo Scandolo075b1892019-10-07 12:11:07 -070099}
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700100
Matteo Scandolo075b1892019-10-07 12:11:07 -0700101func TestSendEapStartFailNoGemPort(t *testing.T) {
Matteo Scandolo075b1892019-10-07 12:11:07 -0700102 eapolStateMachine.SetState("auth_started")
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700103
Matteo Scandolo075b1892019-10-07 12:11:07 -0700104 // Save current function and restore at the end:
105 old := GetGemPortId
106 defer func() { GetGemPortId = old }()
107
108 GetGemPortId = func(intfId uint32, onuId uint32) (uint16, error) {
109 return 0, errors.New("no-gem-port")
110 }
111
Matteo Scandolo86e8ce62019-10-11 12:03:10 -0700112 var macAddress = net.HardwareAddr{0x01, 0x80, 0xC2, 0x00, 0x00, 0x03}
113
Matteo Scandolo27428702019-10-11 16:21:16 -0700114 stream := &mockStream{
115 Calls: make(map[int]*openolt.PacketIndication),
116 fail: false,
117 }
Matteo Scandolo075b1892019-10-07 12:11:07 -0700118
Matteo Scandolo27428702019-10-11 16:21:16 -0700119 err := SendEapStart(onuId, ponPortId, serialNumber, portNo, macAddress, eapolStateMachine, stream)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700120 if err == nil {
121 t.Errorf("SendEapStart did not return an error")
122 t.Fail()
123 }
124
125 assert.Equal(t, err.Error(), "no-gem-port")
126
127 assert.Equal(t, eapolStateMachine.Current(), "auth_failed")
128}
129
130func TestSendEapStartFailStreamError(t *testing.T) {
Matteo Scandolo27428702019-10-11 16:21:16 -0700131
Matteo Scandolo075b1892019-10-07 12:11:07 -0700132 eapolStateMachine.SetState("auth_started")
133
134 // Save current function and restore at the end:
135 old := GetGemPortId
136 defer func() { GetGemPortId = old }()
137
138 GetGemPortId = func(intfId uint32, onuId uint32) (uint16, error) {
139 return 1, nil
140 }
141
Matteo Scandolo27428702019-10-11 16:21:16 -0700142 stream := &mockStream{
143 Calls: make(map[int]*openolt.PacketIndication),
144 fail: true,
145 }
Matteo Scandolo075b1892019-10-07 12:11:07 -0700146
Matteo Scandolo27428702019-10-11 16:21:16 -0700147 err := SendEapStart(onuId, ponPortId, serialNumber, portNo, macAddress, eapolStateMachine, stream)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700148 if err == nil {
149 t.Errorf("SendEapStart did not return an error")
150 t.Fail()
151 }
152
Matteo Scandolo27428702019-10-11 16:21:16 -0700153 assert.Equal(t, err.Error(), "fake-error")
Matteo Scandolo075b1892019-10-07 12:11:07 -0700154
155 assert.Equal(t, eapolStateMachine.Current(), "auth_failed")
156}
157
158// TODO test eapol.HandleNextPacket
159
160func TestUpdateAuthFailed(t *testing.T) {
161
162 var onuId uint32 = 1
163 var ponPortId uint32 = 0
164 var serialNumber string = "BBSM00000001"
165
166 eapolStateMachine.SetState("auth_started")
Shrey Baid688b4242020-07-10 20:40:10 +0530167 _ = updateAuthFailed(onuId, ponPortId, serialNumber, eapolStateMachine)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700168 assert.Equal(t, eapolStateMachine.Current(), "auth_failed")
169
170 eapolStateMachine.SetState("eap_start_sent")
Shrey Baid688b4242020-07-10 20:40:10 +0530171 _ = updateAuthFailed(onuId, ponPortId, serialNumber, eapolStateMachine)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700172 assert.Equal(t, eapolStateMachine.Current(), "auth_failed")
173
174 eapolStateMachine.SetState("eap_response_identity_sent")
Shrey Baid688b4242020-07-10 20:40:10 +0530175 _ = updateAuthFailed(onuId, ponPortId, serialNumber, eapolStateMachine)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700176 assert.Equal(t, eapolStateMachine.Current(), "auth_failed")
177
178 eapolStateMachine.SetState("eap_response_challenge_sent")
Shrey Baid688b4242020-07-10 20:40:10 +0530179 _ = updateAuthFailed(onuId, ponPortId, serialNumber, eapolStateMachine)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700180 assert.Equal(t, eapolStateMachine.Current(), "auth_failed")
181
182 eapolStateMachine.SetState("eap_response_success_received")
183 err := updateAuthFailed(onuId, ponPortId, serialNumber, eapolStateMachine)
184 if err == nil {
185 t.Errorf("updateAuthFailed did not return an error")
186 t.Fail()
187 }
188 assert.Equal(t, err.Error(), "event auth_failed inappropriate in current state eap_response_success_received")
189
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700190}