Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package eapol |
| 18 | |
| 19 | import ( |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame^] | 20 | "errors" |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 21 | "github.com/looplab/fsm" |
| 22 | "github.com/opencord/voltha-protos/go/openolt" |
| 23 | "google.golang.org/grpc" |
| 24 | "gotest.tools/assert" |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 25 | "testing" |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 26 | ) |
| 27 | |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame^] | 28 | // MOCKS |
| 29 | var calledSend = 0 |
| 30 | |
| 31 | var 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 Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 41 | ) |
| 42 | |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame^] | 43 | type mockStreamSuccess struct { |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 44 | grpc.ServerStream |
| 45 | } |
| 46 | |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame^] | 47 | func (s mockStreamSuccess) Send(ind *openolt.Indication) error { |
| 48 | calledSend++ |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 49 | return nil |
| 50 | } |
| 51 | |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame^] | 52 | type mockStreamError struct { |
| 53 | grpc.ServerStream |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame^] | 56 | func (s mockStreamError) Send(ind *openolt.Indication) error { |
| 57 | calledSend++ |
| 58 | return errors.New("stream-error") |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame^] | 61 | // TESTS |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 62 | |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame^] | 63 | func TestSendEapStartSuccess(t *testing.T) { |
| 64 | calledSend = 0 |
| 65 | eapolStateMachine.SetState("auth_started") |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 66 | |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame^] | 67 | // 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 Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 73 | } |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 74 | |
| 75 | // params for the function under test |
| 76 | var onuId uint32 = 1 |
| 77 | var ponPortId uint32 = 0 |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 78 | var serialNumber string = "BBSM00000001" |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 79 | |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame^] | 80 | stream := mockStreamSuccess{} |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 81 | |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame^] | 82 | if err := SendEapStart(onuId, ponPortId, serialNumber, eapolStateMachine, stream); err != nil { |
| 83 | t.Errorf("SendEapStart returned an error: %v", err) |
| 84 | t.Fail() |
| 85 | } |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 86 | |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame^] | 87 | assert.Equal(t, calledSend, 1) |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 88 | |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame^] | 89 | assert.Equal(t, eapolStateMachine.Current(), "eap_start_sent") |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 90 | |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame^] | 91 | } |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 92 | |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame^] | 93 | func TestSendEapStartFailNoGemPort(t *testing.T) { |
| 94 | calledSend = 0 |
| 95 | eapolStateMachine.SetState("auth_started") |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 96 | |
Matteo Scandolo | 075b189 | 2019-10-07 12:11:07 -0700 | [diff] [blame^] | 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 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 | |
| 123 | func 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 | |
| 155 | func 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 Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 185 | } |