blob: 936a02dd2293563f9eca1c9781debdd1149b7fde [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
97 // Save current function and restore at the end:
98 old := GetGemPortId
99 defer func() { GetGemPortId = old }()
100
101 GetGemPortId = func(intfId uint32, onuId uint32) (uint16, error) {
102 return 1, nil
103 }
104
Matteo Scandolo27428702019-10-11 16:21:16 -0700105 stream := &mockStream{
106 Calls: make(map[int]*openolt.PacketIndication),
107 fail: true,
108 }
Matteo Scandolo075b1892019-10-07 12:11:07 -0700109
Matteo Scandolo4a036262020-08-17 15:56:13 -0700110 err := SendEapStart(onuId, ponPortId, serialNumber, portNo, macAddress, gemPortId, eapolStateMachine, stream)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700111 if err == nil {
112 t.Errorf("SendEapStart did not return an error")
113 t.Fail()
114 }
115
Matteo Scandolo27428702019-10-11 16:21:16 -0700116 assert.Equal(t, err.Error(), "fake-error")
Matteo Scandolo075b1892019-10-07 12:11:07 -0700117
118 assert.Equal(t, eapolStateMachine.Current(), "auth_failed")
119}
120
121// TODO test eapol.HandleNextPacket
122
123func TestUpdateAuthFailed(t *testing.T) {
124
125 var onuId uint32 = 1
126 var ponPortId uint32 = 0
127 var serialNumber string = "BBSM00000001"
128
129 eapolStateMachine.SetState("auth_started")
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_start_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_identity_sent")
Shrey Baid688b4242020-07-10 20:40:10 +0530138 _ = updateAuthFailed(onuId, ponPortId, serialNumber, eapolStateMachine)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700139 assert.Equal(t, eapolStateMachine.Current(), "auth_failed")
140
141 eapolStateMachine.SetState("eap_response_challenge_sent")
Shrey Baid688b4242020-07-10 20:40:10 +0530142 _ = updateAuthFailed(onuId, ponPortId, serialNumber, eapolStateMachine)
Matteo Scandolo075b1892019-10-07 12:11:07 -0700143 assert.Equal(t, eapolStateMachine.Current(), "auth_failed")
144
145 eapolStateMachine.SetState("eap_response_success_received")
146 err := updateAuthFailed(onuId, ponPortId, serialNumber, eapolStateMachine)
147 if err == nil {
148 t.Errorf("updateAuthFailed did not return an error")
149 t.Fail()
150 }
151 assert.Equal(t, err.Error(), "event auth_failed inappropriate in current state eap_response_success_received")
152
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700153}