-
Notifications
You must be signed in to change notification settings - Fork 21
/
deviceAccess_test.go
263 lines (231 loc) · 7.53 KB
/
deviceAccess_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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// SPDX-FileCopyrightText: 2017 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package main
import (
"context"
"errors"
"testing"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/xmidt-org/webpa-common/v2/device"
"go.uber.org/zap/zaptest"
// nolint:staticcheck
"github.com/xmidt-org/wrp-go/v3"
)
type deviceAccessTestCase struct {
Name string
DeviceID string
MissingDevice bool
MissingDeviceCredential bool
MissingWRPCredential bool
IncompleteCheck bool
InputValueCheck bool
Authorized bool
ExpectedError error
IsFatal bool
BaseLabelPairs map[string]string
}
func testAuthorizeWRP(t *testing.T, testCases []deviceAccessTestCase, strict bool) {
for _, testCase := range testCases {
t.Run(testCase.Name, func(t *testing.T) {
var (
assert = assert.New(t)
mockDeviceRegistry = new(device.MockRegistry)
mockDevice = new(device.MockDevice)
mockBinOp = new(mockBinOp)
testLogger = zaptest.NewLogger(t)
counter = mockCounter{labelPairs: make(map[string]string), labels: []string{reasonLabel, outcomeLabel}}
expectedLabels = getLabelMaps(testCase.ExpectedError, testCase.IsFatal, strict, testCase.BaseLabelPairs)
wrpMsg = &wrp.Message{
PartnerIDs: []string{"comcast", "nbc", "sky"},
Destination: testCase.DeviceID,
}
)
testMetadata := getTestDeviceMetadata()
mockDeviceRegistry.On("Get", device.ID(testCase.DeviceID)).Return(mockDevice, !testCase.MissingDevice).Once()
mockDevice.On("Metadata").Return(testMetadata).Once()
mockBinOp.On("name").Return("mockBinOP")
var checks []*parsedCheck
if testCase.MissingDeviceCredential {
checks = getFirstMissingDeviceCredentialChecks(t, mockBinOp)
} else if testCase.MissingWRPCredential {
checks = getSecondCheckMissingWRPCredentiaChecks(t, mockBinOp)
} else if testCase.InputValueCheck {
checks = getSecondCheckWithInputValueChecks(t, mockBinOp)
} else {
checks = getChecks(t, mockBinOp, testCase.IncompleteCheck, testCase.Authorized)
}
counter.On("With", expectedLabels).Return().Once()
counter.On("Add", 1.).Return().Once()
deviceAccessAuthority := &talariaDeviceAccess{
strict: strict,
wrpMessagesCounter: &counter,
deviceRegistry: mockDeviceRegistry,
sep: ">",
logger: testLogger,
checks: checks,
}
err := deviceAccessAuthority.authorizeWRP(context.Background(), wrpMsg)
if strict || testCase.IsFatal {
assert.Equal(testCase.ExpectedError, err)
} else {
assert.Nil(err)
}
assert.Equal(float64(1), counter.count)
assert.Equal(expectedLabels, counter.labelPairs)
})
}
}
func TestAuthorizeWRP(t *testing.T) {
testCases := []deviceAccessTestCase{
{
Name: "Invalid WRP Destination",
DeviceID: "McD's:1122334455",
BaseLabelPairs: map[string]string{
reasonLabel: invalidWRPDest,
},
ExpectedError: errInvalidWRPDestination,
IsFatal: true,
},
{
Name: "Device not found",
DeviceID: "mac:112233445566",
MissingDevice: true,
BaseLabelPairs: map[string]string{
reasonLabel: deviceNotFound,
},
ExpectedError: errDeviceNotFound,
IsFatal: true,
},
{
Name: "Device Credential Missing",
DeviceID: "mac:112233445566",
BaseLabelPairs: map[string]string{
reasonLabel: missingDeviceCredential,
},
MissingDeviceCredential: true,
ExpectedError: errDeviceCredentialMissing,
},
{
Name: "WRP Credential Missing",
DeviceID: "mac:112233445566",
BaseLabelPairs: map[string]string{
reasonLabel: missingWRPCredential,
},
MissingWRPCredential: true,
ExpectedError: errWRPCredentialsMissing,
},
{
Name: "Second Check doesn't complete",
DeviceID: "mac:112233445566",
BaseLabelPairs: map[string]string{
reasonLabel: incompleteCheck,
},
IncompleteCheck: true,
ExpectedError: errIncompleteCheck,
},
{
Name: "Unauthorized Device Access",
DeviceID: "mac:112233445566",
BaseLabelPairs: map[string]string{
reasonLabel: denied,
},
Authorized: false,
ExpectedError: errDeniedDeviceAccess,
},
{
Name: "Authorized Device Access",
DeviceID: "mac:112233445566",
BaseLabelPairs: map[string]string{
reasonLabel: authorized,
},
Authorized: true,
},
}
t.Run("strict", func(t *testing.T) {
testAuthorizeWRP(t, testCases, true)
})
t.Run("lenient", func(t *testing.T) {
testAuthorizeWRP(t, testCases, false)
})
}
func getLabelMaps(err error, isFatal, strict bool, baseLabelPairs map[string]string) prometheus.Labels {
out := make(map[string]string)
for k, v := range baseLabelPairs {
out[k] = v
}
outcome := accepted
if err != nil && (isFatal || strict) {
outcome = rejected
}
out[outcomeLabel] = outcome
return out
}
func getTestDeviceMetadata() *device.Metadata {
metadata := new(device.Metadata)
claims := map[string]interface{}{
device.PartnerIDClaimKey: "sky",
device.TrustClaimKey: 100,
"nested": map[string]interface{}{"happy": true},
}
metadata.SetClaims(claims)
return metadata
}
func getFirstMissingDeviceCredentialChecks(t *testing.T, m *mockBinOp) []*parsedCheck {
m.AssertNotCalled(t, "evaluate", mock.Anything, mock.Anything)
baseChecks := getBaseChecks(m)
baseChecks[0].deviceCredentialPath = "path>not>found"
return baseChecks
}
func getSecondCheckMissingWRPCredentiaChecks(t *testing.T, m *mockBinOp) []*parsedCheck {
m.On("evaluate", 100, 100).Return(true, error(nil)).Once()
m.AssertNotCalled(t, "evaluate", []string{"comcast", "nbc", "sky"}, "sky")
m.AssertNotCalled(t, "evaluate", true, true)
baseChecks := getBaseChecks(m)
baseChecks[1].wrpCredentialPath = "path>not>found"
return baseChecks
}
func getSecondCheckWithInputValueChecks(t *testing.T, m *mockBinOp) []*parsedCheck {
m.On("evaluate", 100, 100).Return(true, error(nil)).Once()
m.On("evaluate", []string{"universal"}, "sky").Return(true, error(nil)).Once()
m.On("evaluate", true, true).Return(true, error(nil)).Once()
m.AssertNotCalled(t, "evaluate", []string{"comcast", "nbc", "sky"}, "sky")
baseChecks := getBaseChecks(m)
baseChecks[1].inputValue = []string{"universal"}
return baseChecks
}
func getChecks(t *testing.T, m *mockBinOp, secondCheckIncomplete, thirdCheckAuthorized bool) []*parsedCheck {
m.On("evaluate", 100, 100).Return(true, error(nil)).Once()
if secondCheckIncomplete {
m.On("evaluate", []string{"comcast", "nbc", "sky"}, "sky").Return(false, errors.New("Could not complete check")).Once()
m.AssertNotCalled(t, "evaluate", mock.Anything, mock.Anything)
return getBaseChecks(m)
}
m.On("evaluate", []string{"comcast", "nbc", "sky"}, "sky").Return(true, error(nil)).Once()
m.On("evaluate", true, true).Return(thirdCheckAuthorized, error(nil))
return getBaseChecks(m)
}
func getBaseChecks(m *mockBinOp) []*parsedCheck {
return []*parsedCheck{
{
name: "trustedDevice",
deviceCredentialPath: device.TrustClaimKey,
assertion: m,
inputValue: 100,
},
{
name: "partnerID",
deviceCredentialPath: device.PartnerIDClaimKey,
wrpCredentialPath: "PartnerIDs",
assertion: m,
inversed: true,
},
{
name: "happyDevice",
deviceCredentialPath: "nested>happy",
assertion: m,
inputValue: true,
},
}
}