blob: 2d8a06c2a903bc39720d83ecd0a50727c4fc3a15 [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 (
20 bbsim "github.com/opencord/bbsim/internal/bbsim/types"
21 "github.com/looplab/fsm"
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
31var (
32 originalSendEapStart func(onuId uint32, ponPortId uint32, serialNumber *openolt.SerialNumber, stream openolt.Openolt_EnableIndicationServer) error
33)
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
45func setUp() {
46 originalSendEapStart = sendEapStart
47}
48
49
50func tearDown() {
51 sendEapStart = originalSendEapStart
52}
53
54func TestMain(m *testing.M) {
55 setUp()
56 code := m.Run()
57 tearDown()
58 os.Exit(code)
59}
60
61func TestCreateWPASupplicant(t *testing.T) {
62
63 // mocks
64 mockSendEapStartCalled := 0
65 mockSendEapStartArgs := struct {
66 onuId uint32
67 ponPortId uint32
68 serialNumber *openolt.SerialNumber
69 stream openolt.Openolt_EnableIndicationServer
70 }{}
71 mockSendEapStart := func(onuId uint32, ponPortId uint32, serialNumber *openolt.SerialNumber, stream openolt.Openolt_EnableIndicationServer) error {
72 mockSendEapStartCalled++
73 mockSendEapStartArgs.onuId = onuId
74 mockSendEapStartArgs.ponPortId = ponPortId
75 return nil
76 }
77 sendEapStart = mockSendEapStart
78
79 // params for the function under test
80 var onuId uint32 = 1
81 var ponPortId uint32 = 0
82 var serialNumber = new(openolt.SerialNumber)
83
84 serialNumber.VendorId = []byte("BBSM")
85 serialNumber.VendorSpecific = []byte{0, byte(0 % 256), byte(ponPortId), byte(onuId)}
86
87 eapolStateMachine := fsm.NewFSM(
88 "auth_started",
89 fsm.Events{
90 {Name: "eap_start_sent", Src: []string{"auth_started"}, Dst: "eap_start_sent"},
91 {Name: "eap_resonse_identity_sent", Src: []string{"eap_start_sent"}, Dst: "eap_resonse_identity_sent"},
92 {Name: "eap_resonse_challenge_sent", Src: []string{"eap_resonse_identity_sent"}, Dst: "eap_resonse_challenge_sent"},
93 {Name: "eap_resonse_success_received", Src: []string{"eap_resonse_challenge_sent"}, Dst: "eap_resonse_success_received"},
94 },
95 fsm.Callbacks{},
96 )
97
98 pktOutCh := make(chan *bbsim.ByteMsg, 1024)
99
100 stream := fakeStream{}
101
102 wg := sync.WaitGroup{}
103 wg.Add(1)
104
105 go CreateWPASupplicant(onuId, ponPortId, serialNumber, eapolStateMachine, stream, pktOutCh)
106 go func(){
107 time.Sleep(1 * time.Second)
108 close(pktOutCh)
109 wg.Done()
110 }()
111
112 wg.Wait()
113
114 assert.Equal(t, mockSendEapStartCalled, 1)
115 assert.Equal(t, mockSendEapStartArgs.onuId, onuId)
116 assert.Equal(t, mockSendEapStartArgs.ponPortId, ponPortId)
117}