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 | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 20 | "github.com/looplab/fsm" |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 21 | bbsim "github.com/opencord/bbsim/internal/bbsim/types" |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 22 | "github.com/opencord/voltha-protos/go/openolt" |
| 23 | "google.golang.org/grpc" |
| 24 | "gotest.tools/assert" |
| 25 | "os" |
| 26 | "sync" |
| 27 | "testing" |
| 28 | "time" |
| 29 | ) |
| 30 | |
| 31 | var ( |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 32 | originalSendEapStart func(onuId uint32, ponPortId uint32, serialNumber string, stream openolt.Openolt_EnableIndicationServer) error |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 33 | ) |
| 34 | |
| 35 | type fakeStream struct { |
| 36 | calledSend int |
| 37 | grpc.ServerStream |
| 38 | } |
| 39 | |
| 40 | func (s fakeStream) Send(flow *openolt.Indication) error { |
| 41 | s.calledSend++ |
| 42 | return nil |
| 43 | } |
| 44 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 45 | func setUp() { |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 46 | originalSendEapStart = sendEapStart |
| 47 | } |
| 48 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 49 | func tearDown() { |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 50 | sendEapStart = originalSendEapStart |
| 51 | } |
| 52 | |
| 53 | func TestMain(m *testing.M) { |
| 54 | setUp() |
| 55 | code := m.Run() |
| 56 | tearDown() |
| 57 | os.Exit(code) |
| 58 | } |
| 59 | |
| 60 | func TestCreateWPASupplicant(t *testing.T) { |
| 61 | |
| 62 | // mocks |
| 63 | mockSendEapStartCalled := 0 |
| 64 | mockSendEapStartArgs := struct { |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 65 | onuId uint32 |
| 66 | ponPortId uint32 |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 67 | serialNumber *openolt.SerialNumber |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 68 | stream openolt.Openolt_EnableIndicationServer |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 69 | }{} |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 70 | mockSendEapStart := func(onuId uint32, ponPortId uint32, serialNumber string, stream openolt.Openolt_EnableIndicationServer) error { |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 71 | mockSendEapStartCalled++ |
| 72 | mockSendEapStartArgs.onuId = onuId |
| 73 | mockSendEapStartArgs.ponPortId = ponPortId |
| 74 | return nil |
| 75 | } |
| 76 | sendEapStart = mockSendEapStart |
| 77 | |
| 78 | // params for the function under test |
| 79 | var onuId uint32 = 1 |
| 80 | var ponPortId uint32 = 0 |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 81 | var serialNumber string = "BBSM00000001" |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 82 | |
| 83 | eapolStateMachine := fsm.NewFSM( |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 84 | "auth_started", |
| 85 | fsm.Events{ |
| 86 | {Name: "eap_start_sent", Src: []string{"auth_started"}, Dst: "eap_start_sent"}, |
| 87 | {Name: "eap_response_identity_sent", Src: []string{"eap_start_sent"}, Dst: "eap_response_identity_sent"}, |
| 88 | {Name: "eap_response_challenge_sent", Src: []string{"eap_response_identity_sent"}, Dst: "eap_response_challenge_sent"}, |
| 89 | {Name: "eap_response_success_received", Src: []string{"eap_response_challenge_sent"}, Dst: "eap_response_success_received"}, |
| 90 | }, |
| 91 | fsm.Callbacks{}, |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 92 | ) |
| 93 | |
| 94 | pktOutCh := make(chan *bbsim.ByteMsg, 1024) |
| 95 | |
| 96 | stream := fakeStream{} |
| 97 | |
| 98 | wg := sync.WaitGroup{} |
| 99 | wg.Add(1) |
| 100 | |
| 101 | go CreateWPASupplicant(onuId, ponPortId, serialNumber, eapolStateMachine, stream, pktOutCh) |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 102 | go func() { |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 103 | time.Sleep(1 * time.Second) |
| 104 | close(pktOutCh) |
| 105 | wg.Done() |
| 106 | }() |
| 107 | |
| 108 | wg.Wait() |
| 109 | |
| 110 | assert.Equal(t, mockSendEapStartCalled, 1) |
| 111 | assert.Equal(t, mockSendEapStartArgs.onuId, onuId) |
| 112 | assert.Equal(t, mockSendEapStartArgs.ponPortId, ponPortId) |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 113 | } |