blob: 9e37dae6934bd1c65e13a6e61138f8a3e34b7649 [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 Scandolo47e69bb2019-08-28 15:41:12 -070020 "github.com/looplab/fsm"
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070021 bbsim "github.com/opencord/bbsim/internal/bbsim/types"
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070022 "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
31var (
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070032 originalSendEapStart func(onuId uint32, ponPortId uint32, serialNumber string, stream openolt.Openolt_EnableIndicationServer) error
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070033)
34
35type fakeStream struct {
36 calledSend int
37 grpc.ServerStream
38}
39
40func (s fakeStream) Send(flow *openolt.Indication) error {
41 s.calledSend++
42 return nil
43}
44
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070045func setUp() {
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070046 originalSendEapStart = sendEapStart
47}
48
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070049func tearDown() {
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070050 sendEapStart = originalSendEapStart
51}
52
53func TestMain(m *testing.M) {
54 setUp()
55 code := m.Run()
56 tearDown()
57 os.Exit(code)
58}
59
60func TestCreateWPASupplicant(t *testing.T) {
61
62 // mocks
63 mockSendEapStartCalled := 0
64 mockSendEapStartArgs := struct {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070065 onuId uint32
66 ponPortId uint32
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070067 serialNumber *openolt.SerialNumber
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070068 stream openolt.Openolt_EnableIndicationServer
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070069 }{}
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070070 mockSendEapStart := func(onuId uint32, ponPortId uint32, serialNumber string, stream openolt.Openolt_EnableIndicationServer) error {
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070071 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 Scandolo4b3fc7e2019-09-17 16:49:54 -070081 var serialNumber string = "BBSM00000001"
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070082
83 eapolStateMachine := fsm.NewFSM(
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070084 "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 Scandolo47e69bb2019-08-28 15:41:12 -070092 )
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 Scandolo4b3fc7e2019-09-17 16:49:54 -0700102 go func() {
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700103 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 Scandolo4b3fc7e2019-09-17 16:49:54 -0700113}