-
Notifications
You must be signed in to change notification settings - Fork 162
/
handler_test.go
177 lines (157 loc) · 5.42 KB
/
handler_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright 2019 Anapaya Systems
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package keepalive
import (
"context"
"net"
"os"
"sync"
"testing"
"time"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/scionproto/scion/go/beacon_srv/internal/ifstate"
"github.com/scionproto/scion/go/beacon_srv/internal/keepalive/mock_keepalive"
"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/ctrl"
"github.com/scionproto/scion/go/lib/ctrl/ifid"
"github.com/scionproto/scion/go/lib/infra"
"github.com/scionproto/scion/go/lib/log"
"github.com/scionproto/scion/go/lib/snet"
"github.com/scionproto/scion/go/lib/spath"
"github.com/scionproto/scion/go/lib/topology"
"github.com/scionproto/scion/go/lib/xtest"
)
var (
localIF = common.IFIDType(10)
originIF = common.IFIDType(11)
localIA = xtest.MustParseIA("1-ff00:0:110")
originIA = xtest.MustParseIA("1-ff00:0:111")
)
// Disable logging in all tests
func TestMain(m *testing.M) {
log.Root().SetHandler(log.DiscardHandler())
os.Exit(m.Run())
}
func TestNewHandler(t *testing.T) {
t.Log("NewHandler creates a correct handler")
mctrl := gomock.NewController(t)
defer mctrl.Finish()
t.Run("Non-active interface should cause tasks to execute", func(t *testing.T) {
// The wait group ensures all go routines are finished before the test finishes.
wg := &sync.WaitGroup{}
wg.Add(3)
// Make sure the mock is executed exactly once and updates the waitgroup.
set := func(call *gomock.Call) *gomock.Call {
return call.Times(1).Do(func(_ ...interface{}) { wg.Done() })
}
pusher := mock_keepalive.NewMockIfStatePusher(mctrl)
dropper := mock_keepalive.NewMockRevDropper(mctrl)
set(pusher.EXPECT().Push(gomock.Any(), localIF))
set(dropper.EXPECT().DeleteRevocation(gomock.Any(), localIA, localIF)).Return(nil)
set(dropper.EXPECT().DeleteRevocation(gomock.Any(), originIA, originIF)).Return(nil)
handler := NewHandler(localIA, testInterfaces(), StateChangeTasks{
IfStatePusher: pusher,
RevDropper: dropper,
})
req := infra.NewRequest(context.Background(), &ifid.IFID{OrigIfID: originIF}, nil,
&snet.Addr{IA: originIA, Path: testPath(localIF)}, 0)
res := handler.Handle(req)
waitTimeout(wg, t)
assert.Equal(t, res, infra.MetricsResultOk)
})
t.Run("Active interface should cause no tasks to execute", func(t *testing.T) {
intfs := testInterfaces()
intfs.Get(localIF).Activate(42)
handler := NewHandler(localIA, intfs, zeroCallTasks(mctrl))
req := infra.NewRequest(context.Background(), &ifid.IFID{OrigIfID: originIF}, nil,
&snet.Addr{IA: originIA, Path: testPath(localIF)}, 0)
res := handler.Handle(req)
assert.Equal(t, res, infra.MetricsResultOk)
})
t.Run("Invalid requests cause an error", func(t *testing.T) {
handler := NewHandler(localIA, testInterfaces(), zeroCallTasks(mctrl))
tests := []struct {
msg string
req *infra.Request
exp *infra.HandlerResult
}{
{
msg: "Wrong payload type",
req: infra.NewRequest(context.Background(), &ctrl.Pld{}, nil,
&snet.Addr{IA: originIA, Path: testPath(localIF)}, 0),
exp: infra.MetricsErrInternal,
},
{
msg: "Invalid address type",
req: infra.NewRequest(context.Background(), &ifid.IFID{OrigIfID: originIF}, nil,
&net.UnixAddr{}, 0),
exp: infra.MetricsErrInvalid,
},
{
msg: "Invalid path",
req: infra.NewRequest(context.Background(), &ifid.IFID{OrigIfID: originIF}, nil,
&snet.Addr{IA: originIA, Path: &spath.Path{}}, 0),
exp: infra.MetricsErrInvalid,
},
{
msg: "Invalid ConsIngress ifid",
req: infra.NewRequest(context.Background(), &ifid.IFID{OrigIfID: originIF}, nil,
&snet.Addr{IA: originIA, Path: testPath(originIF)}, 0),
exp: infra.MetricsErrInvalid,
},
{
msg: "Invalid IA",
req: infra.NewRequest(context.Background(), &ifid.IFID{OrigIfID: originIF}, nil,
&snet.Addr{IA: localIA, Path: testPath(localIF)}, 0),
exp: infra.MetricsErrInvalid,
},
}
for _, test := range tests {
t.Log(test.msg)
res := handler.Handle(test.req)
assert.Equal(t, res, test.exp)
}
})
}
func testInterfaces() *ifstate.Interfaces {
infoMap := topology.IfInfoMap{localIF: topology.IFInfo{ISD_AS: originIA}}
return ifstate.NewInterfaces(infoMap, ifstate.Config{})
}
func zeroCallTasks(mctrl *gomock.Controller) StateChangeTasks {
return StateChangeTasks{
IfStatePusher: mock_keepalive.NewMockIfStatePusher(mctrl),
RevDropper: mock_keepalive.NewMockRevDropper(mctrl),
}
}
func testPath(ifid common.IFIDType) *spath.Path {
path := &spath.Path{
Raw: make(common.RawBytes, spath.InfoFieldLength+spath.HopFieldLength),
HopOff: spath.InfoFieldLength,
}
(&spath.HopField{ConsIngress: ifid}).Write(path.Raw[spath.InfoFieldLength:])
return path
}
func waitTimeout(wg *sync.WaitGroup, t *testing.T) {
done := make(chan struct{})
go func() {
wg.Wait()
close(done)
}()
select {
case <-time.After(5 * time.Second):
assert.Fail(t, "Timed out")
case <-done:
}
}