blob: 69f941f036242618b2263f72c7f2b62ac1d4f73a [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 Scandolo4f4ac792020-10-01 16:33:21 -070025 "github.com/opencord/voltha-protos/v4/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
Matteo Scandolo4a036262020-08-17 15:56:13 -070046var gemPortId uint32 = 1
Matteo Scandolo27428702019-10-11 16:21:16 -070047var 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 Scandolo27428702019-10-11 16:21:16 -070073 stream := &mockStream{
74 Calls: make(map[int]*openolt.PacketIndication),
75 fail: false,
76 }
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070077
Matteo Scandolo4a036262020-08-17 15:56:13 -070078 if err := SendEapStart(onuId, ponPortId, serialNumber, portNo, macAddress, gemPortId, eapolStateMachine, stream); err != nil {
Matteo Scandolo075b1892019-10-07 12:11:07 -070079 t.Errorf("SendEapStart returned an error: %v", err)
80 t.Fail()
81 }
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070082
Matteo Scandolo27428702019-10-11 16:21:16 -070083 assert.Equal(t, stream.CallCount, 1)
84 assert.Equal(t, stream.Calls[1].PortNo, portNo)
85 assert.Equal(t, stream.Calls[1].IntfId, ponPortId)
86 assert.Equal(t, stream.Calls[1].IntfType, "pon")
87 assert.Equal(t, stream.Calls[1].GemportId, uint32(gemPortId))
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070088
Matteo Scandolo075b1892019-10-07 12:11:07 -070089 assert.Equal(t, eapolStateMachine.Current(), "eap_start_sent")
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070090
Matteo Scandolo075b1892019-10-07 12:11:07 -070091}
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070092
Matteo Scandolo075b1892019-10-07 12:11:07 -070093func TestSendEapStartFailStreamError(t *testing.T) {
Matteo Scandolo27428702019-10-11 16:21:16 -070094
Matteo Scandolo075b1892019-10-07 12:11:07 -070095 eapolStateMachine.SetState("auth_started")
96
Matteo Scandolo27428702019-10-11 16:21:16 -070097 stream := &mockStream{
98 Calls: make(map[int]*openolt.PacketIndication),
99 fail: true,
100 }
Matteo Scandolo075b1892019-10-07 12:11:07 -0700101
Matteo Scandolo4a036262020-08-17 15:56:13 -0700102 err := SendEapStart(onuId, ponPortId, serialNumber, portNo, macAddress, gemPortId, eapolStateMachine, stream)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700103 if err == nil {
104 t.Errorf("SendEapStart did not return an error")
105 t.Fail()
106 }
107
Matteo Scandolo27428702019-10-11 16:21:16 -0700108 assert.Equal(t, err.Error(), "fake-error")
Matteo Scandolo075b1892019-10-07 12:11:07 -0700109
110 assert.Equal(t, eapolStateMachine.Current(), "auth_failed")
111}
112
113// TODO test eapol.HandleNextPacket
114
115func TestUpdateAuthFailed(t *testing.T) {
116
117 var onuId uint32 = 1
118 var ponPortId uint32 = 0
119 var serialNumber string = "BBSM00000001"
120
121 eapolStateMachine.SetState("auth_started")
Shrey Baid688b4242020-07-10 20:40:10 +0530122 _ = updateAuthFailed(onuId, ponPortId, serialNumber, eapolStateMachine)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700123 assert.Equal(t, eapolStateMachine.Current(), "auth_failed")
124
125 eapolStateMachine.SetState("eap_start_sent")
Shrey Baid688b4242020-07-10 20:40:10 +0530126 _ = updateAuthFailed(onuId, ponPortId, serialNumber, eapolStateMachine)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700127 assert.Equal(t, eapolStateMachine.Current(), "auth_failed")
128
129 eapolStateMachine.SetState("eap_response_identity_sent")
Shrey Baid688b4242020-07-10 20:40:10 +0530130 _ = updateAuthFailed(onuId, ponPortId, serialNumber, eapolStateMachine)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700131 assert.Equal(t, eapolStateMachine.Current(), "auth_failed")
132
133 eapolStateMachine.SetState("eap_response_challenge_sent")
Shrey Baid688b4242020-07-10 20:40:10 +0530134 _ = updateAuthFailed(onuId, ponPortId, serialNumber, eapolStateMachine)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700135 assert.Equal(t, eapolStateMachine.Current(), "auth_failed")
136
137 eapolStateMachine.SetState("eap_response_success_received")
138 err := updateAuthFailed(onuId, ponPortId, serialNumber, eapolStateMachine)
139 if err == nil {
140 t.Errorf("updateAuthFailed did not return an error")
141 t.Fail()
142 }
143 assert.Equal(t, err.Error(), "event auth_failed inappropriate in current state eap_response_success_received")
144
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700145}