blob: a3df56fad542631c2a66512e2a4384f0fef42fec [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"
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070021 "github.com/looplab/fsm"
22 "github.com/opencord/voltha-protos/go/openolt"
23 "google.golang.org/grpc"
24 "gotest.tools/assert"
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070025 "testing"
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070026)
27
Matteo Scandolo075b1892019-10-07 12:11:07 -070028// MOCKS
29var calledSend = 0
30
31var eapolStateMachine = fsm.NewFSM(
32 "auth_started",
33 fsm.Events{
34 {Name: "eap_start_sent", Src: []string{"auth_started"}, Dst: "eap_start_sent"},
35 {Name: "eap_response_identity_sent", Src: []string{"eap_start_sent"}, Dst: "eap_response_identity_sent"},
36 {Name: "eap_response_challenge_sent", Src: []string{"eap_response_identity_sent"}, Dst: "eap_response_challenge_sent"},
37 {Name: "eap_response_success_received", Src: []string{"eap_response_challenge_sent"}, Dst: "eap_response_success_received"},
38 {Name: "auth_failed", Src: []string{"auth_started", "eap_start_sent", "eap_response_identity_sent", "eap_response_challenge_sent"}, Dst: "auth_failed"},
39 },
40 fsm.Callbacks{},
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070041)
42
Matteo Scandolo075b1892019-10-07 12:11:07 -070043type mockStreamSuccess struct {
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070044 grpc.ServerStream
45}
46
Matteo Scandolo075b1892019-10-07 12:11:07 -070047func (s mockStreamSuccess) Send(ind *openolt.Indication) error {
48 calledSend++
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070049 return nil
50}
51
Matteo Scandolo075b1892019-10-07 12:11:07 -070052type mockStreamError struct {
53 grpc.ServerStream
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070054}
55
Matteo Scandolo075b1892019-10-07 12:11:07 -070056func (s mockStreamError) Send(ind *openolt.Indication) error {
57 calledSend++
58 return errors.New("stream-error")
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070059}
60
Matteo Scandolo075b1892019-10-07 12:11:07 -070061// TESTS
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070062
Matteo Scandolo075b1892019-10-07 12:11:07 -070063func TestSendEapStartSuccess(t *testing.T) {
64 calledSend = 0
65 eapolStateMachine.SetState("auth_started")
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070066
Matteo Scandolo075b1892019-10-07 12:11:07 -070067 // 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
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070073 }
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070074
75 // params for the function under test
76 var onuId uint32 = 1
77 var ponPortId uint32 = 0
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070078 var serialNumber string = "BBSM00000001"
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070079
Matteo Scandolo075b1892019-10-07 12:11:07 -070080 stream := mockStreamSuccess{}
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070081
Matteo Scandolo075b1892019-10-07 12:11:07 -070082 if err := SendEapStart(onuId, ponPortId, serialNumber, eapolStateMachine, stream); err != nil {
83 t.Errorf("SendEapStart returned an error: %v", err)
84 t.Fail()
85 }
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070086
Matteo Scandolo075b1892019-10-07 12:11:07 -070087 assert.Equal(t, calledSend, 1)
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 TestSendEapStartFailNoGemPort(t *testing.T) {
94 calledSend = 0
95 eapolStateMachine.SetState("auth_started")
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070096
Matteo Scandolo075b1892019-10-07 12:11:07 -070097 // 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 0, errors.New("no-gem-port")
103 }
104
105 // params for the function under test
106 var onuId uint32 = 1
107 var ponPortId uint32 = 0
108 var serialNumber string = "BBSM00000001"
109
110 stream := mockStreamSuccess{}
111
112 err := SendEapStart(onuId, ponPortId, serialNumber, eapolStateMachine, stream)
113 if err == nil {
114 t.Errorf("SendEapStart did not return an error")
115 t.Fail()
116 }
117
118 assert.Equal(t, err.Error(), "no-gem-port")
119
120 assert.Equal(t, eapolStateMachine.Current(), "auth_failed")
121}
122
123func TestSendEapStartFailStreamError(t *testing.T) {
124 calledSend = 0
125 eapolStateMachine.SetState("auth_started")
126
127 // Save current function and restore at the end:
128 old := GetGemPortId
129 defer func() { GetGemPortId = old }()
130
131 GetGemPortId = func(intfId uint32, onuId uint32) (uint16, error) {
132 return 1, nil
133 }
134
135 // params for the function under test
136 var onuId uint32 = 1
137 var ponPortId uint32 = 0
138 var serialNumber string = "BBSM00000001"
139
140 stream := mockStreamError{}
141
142 err := SendEapStart(onuId, ponPortId, serialNumber, eapolStateMachine, stream)
143 if err == nil {
144 t.Errorf("SendEapStart did not return an error")
145 t.Fail()
146 }
147
148 assert.Equal(t, err.Error(), "stream-error")
149
150 assert.Equal(t, eapolStateMachine.Current(), "auth_failed")
151}
152
153// TODO test eapol.HandleNextPacket
154
155func TestUpdateAuthFailed(t *testing.T) {
156
157 var onuId uint32 = 1
158 var ponPortId uint32 = 0
159 var serialNumber string = "BBSM00000001"
160
161 eapolStateMachine.SetState("auth_started")
162 updateAuthFailed(onuId, ponPortId, serialNumber, eapolStateMachine)
163 assert.Equal(t, eapolStateMachine.Current(), "auth_failed")
164
165 eapolStateMachine.SetState("eap_start_sent")
166 updateAuthFailed(onuId, ponPortId, serialNumber, eapolStateMachine)
167 assert.Equal(t, eapolStateMachine.Current(), "auth_failed")
168
169 eapolStateMachine.SetState("eap_response_identity_sent")
170 updateAuthFailed(onuId, ponPortId, serialNumber, eapolStateMachine)
171 assert.Equal(t, eapolStateMachine.Current(), "auth_failed")
172
173 eapolStateMachine.SetState("eap_response_challenge_sent")
174 updateAuthFailed(onuId, ponPortId, serialNumber, eapolStateMachine)
175 assert.Equal(t, eapolStateMachine.Current(), "auth_failed")
176
177 eapolStateMachine.SetState("eap_response_success_received")
178 err := updateAuthFailed(onuId, ponPortId, serialNumber, eapolStateMachine)
179 if err == nil {
180 t.Errorf("updateAuthFailed did not return an error")
181 t.Fail()
182 }
183 assert.Equal(t, err.Error(), "event auth_failed inappropriate in current state eap_response_success_received")
184
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700185}