blob: 55c4294c0ac56c5b10faaae5a96dee4ca63448c3 [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
Matteo Scandolo8a574812021-05-20 15:18:53 -070048var uniId uint32 = 0
Matteo Scandolo27428702019-10-11 16:21:16 -070049var serialNumber string = "BBSM00000001"
50var macAddress = net.HardwareAddr{0x01, 0x80, 0xC2, 0x00, 0x00, 0x03}
51var portNo uint32 = 16
52
53type mockStream struct {
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070054 grpc.ServerStream
Matteo Scandolo27428702019-10-11 16:21:16 -070055 CallCount int
56 Calls map[int]*openolt.PacketIndication
57 fail bool
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070058}
59
Matteo Scandolo27428702019-10-11 16:21:16 -070060func (s *mockStream) Send(ind *openolt.Indication) error {
61 s.CallCount++
62 if s.fail {
63 return errors.New("fake-error")
64 }
65 s.Calls[s.CallCount] = ind.GetPktInd()
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070066 return nil
67}
68
Matteo Scandolo075b1892019-10-07 12:11:07 -070069// TESTS
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070070
Matteo Scandolo075b1892019-10-07 12:11:07 -070071func TestSendEapStartSuccess(t *testing.T) {
Matteo Scandolo075b1892019-10-07 12:11:07 -070072 eapolStateMachine.SetState("auth_started")
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070073
Matteo Scandolo27428702019-10-11 16:21:16 -070074 stream := &mockStream{
75 Calls: make(map[int]*openolt.PacketIndication),
76 fail: false,
77 }
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070078
Matteo Scandolo8a574812021-05-20 15:18:53 -070079 if err := SendEapStart(onuId, ponPortId, serialNumber, portNo, macAddress, gemPortId, uniId, eapolStateMachine, stream); err != nil {
Matteo Scandolo075b1892019-10-07 12:11:07 -070080 t.Errorf("SendEapStart returned an error: %v", err)
81 t.Fail()
82 }
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070083
Matteo Scandolo27428702019-10-11 16:21:16 -070084 assert.Equal(t, stream.CallCount, 1)
85 assert.Equal(t, stream.Calls[1].PortNo, portNo)
86 assert.Equal(t, stream.Calls[1].IntfId, ponPortId)
87 assert.Equal(t, stream.Calls[1].IntfType, "pon")
88 assert.Equal(t, stream.Calls[1].GemportId, uint32(gemPortId))
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070089
Matteo Scandolo075b1892019-10-07 12:11:07 -070090 assert.Equal(t, eapolStateMachine.Current(), "eap_start_sent")
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070091
Matteo Scandolo075b1892019-10-07 12:11:07 -070092}
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070093
Matteo Scandolo075b1892019-10-07 12:11:07 -070094func TestSendEapStartFailStreamError(t *testing.T) {
Matteo Scandolo27428702019-10-11 16:21:16 -070095
Matteo Scandolo075b1892019-10-07 12:11:07 -070096 eapolStateMachine.SetState("auth_started")
97
Matteo Scandolo27428702019-10-11 16:21:16 -070098 stream := &mockStream{
99 Calls: make(map[int]*openolt.PacketIndication),
100 fail: true,
101 }
Matteo Scandolo075b1892019-10-07 12:11:07 -0700102
Matteo Scandolo8a574812021-05-20 15:18:53 -0700103 err := SendEapStart(onuId, ponPortId, serialNumber, portNo, macAddress, gemPortId, uniId, eapolStateMachine, stream)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700104 if err == nil {
105 t.Errorf("SendEapStart did not return an error")
106 t.Fail()
107 }
108
Matteo Scandolo27428702019-10-11 16:21:16 -0700109 assert.Equal(t, err.Error(), "fake-error")
Matteo Scandolo075b1892019-10-07 12:11:07 -0700110
111 assert.Equal(t, eapolStateMachine.Current(), "auth_failed")
112}
113
114// TODO test eapol.HandleNextPacket
115
116func TestUpdateAuthFailed(t *testing.T) {
117
118 var onuId uint32 = 1
119 var ponPortId uint32 = 0
120 var serialNumber string = "BBSM00000001"
121
122 eapolStateMachine.SetState("auth_started")
Shrey Baid688b4242020-07-10 20:40:10 +0530123 _ = updateAuthFailed(onuId, ponPortId, serialNumber, eapolStateMachine)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700124 assert.Equal(t, eapolStateMachine.Current(), "auth_failed")
125
126 eapolStateMachine.SetState("eap_start_sent")
Shrey Baid688b4242020-07-10 20:40:10 +0530127 _ = updateAuthFailed(onuId, ponPortId, serialNumber, eapolStateMachine)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700128 assert.Equal(t, eapolStateMachine.Current(), "auth_failed")
129
130 eapolStateMachine.SetState("eap_response_identity_sent")
Shrey Baid688b4242020-07-10 20:40:10 +0530131 _ = updateAuthFailed(onuId, ponPortId, serialNumber, eapolStateMachine)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700132 assert.Equal(t, eapolStateMachine.Current(), "auth_failed")
133
134 eapolStateMachine.SetState("eap_response_challenge_sent")
Shrey Baid688b4242020-07-10 20:40:10 +0530135 _ = updateAuthFailed(onuId, ponPortId, serialNumber, eapolStateMachine)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700136 assert.Equal(t, eapolStateMachine.Current(), "auth_failed")
137
138 eapolStateMachine.SetState("eap_response_success_received")
139 err := updateAuthFailed(onuId, ponPortId, serialNumber, eapolStateMachine)
140 if err == nil {
141 t.Errorf("updateAuthFailed did not return an error")
142 t.Fail()
143 }
144 assert.Equal(t, err.Error(), "event auth_failed inappropriate in current state eap_response_success_received")
145
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700146}