-
Notifications
You must be signed in to change notification settings - Fork 0
/
hasher_mock.go
512 lines (424 loc) · 16.4 KB
/
hasher_mock.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
package crypt
// Code generated by http://github.com/gojuno/minimock (dev). DO NOT EDIT.
import (
"sync"
mm_atomic "sync/atomic"
mm_time "time"
"github.com/gojuno/minimock/v3"
)
// HasherMock implements Hasher
type HasherMock struct {
t minimock.Tester
funcHash func(plain string, globalSalt []byte) (s1 string, err error)
inspectFuncHash func(plain string, globalSalt []byte)
afterHashCounter uint64
beforeHashCounter uint64
HashMock mHasherMockHash
funcVerify func(hashed string, auditee string, globalSalt []byte) (b1 bool, err error)
inspectFuncVerify func(hashed string, auditee string, globalSalt []byte)
afterVerifyCounter uint64
beforeVerifyCounter uint64
VerifyMock mHasherMockVerify
}
// NewHasherMock returns a mock for Hasher
func NewHasherMock(t minimock.Tester) *HasherMock {
m := &HasherMock{t: t}
if controller, ok := t.(minimock.MockController); ok {
controller.RegisterMocker(m)
}
m.HashMock = mHasherMockHash{mock: m}
m.HashMock.callArgs = []*HasherMockHashParams{}
m.VerifyMock = mHasherMockVerify{mock: m}
m.VerifyMock.callArgs = []*HasherMockVerifyParams{}
return m
}
type mHasherMockHash struct {
mock *HasherMock
defaultExpectation *HasherMockHashExpectation
expectations []*HasherMockHashExpectation
callArgs []*HasherMockHashParams
mutex sync.RWMutex
}
// HasherMockHashExpectation specifies expectation struct of the Hasher.Hash
type HasherMockHashExpectation struct {
mock *HasherMock
params *HasherMockHashParams
results *HasherMockHashResults
Counter uint64
}
// HasherMockHashParams contains parameters of the Hasher.Hash
type HasherMockHashParams struct {
plain string
globalSalt []byte
}
// HasherMockHashResults contains results of the Hasher.Hash
type HasherMockHashResults struct {
s1 string
err error
}
// Expect sets up expected params for Hasher.Hash
func (mmHash *mHasherMockHash) Expect(plain string, globalSalt []byte) *mHasherMockHash {
if mmHash.mock.funcHash != nil {
mmHash.mock.t.Fatalf("HasherMock.Hash mock is already set by Set")
}
if mmHash.defaultExpectation == nil {
mmHash.defaultExpectation = &HasherMockHashExpectation{}
}
mmHash.defaultExpectation.params = &HasherMockHashParams{plain, globalSalt}
for _, e := range mmHash.expectations {
if minimock.Equal(e.params, mmHash.defaultExpectation.params) {
mmHash.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmHash.defaultExpectation.params)
}
}
return mmHash
}
// Inspect accepts an inspector function that has same arguments as the Hasher.Hash
func (mmHash *mHasherMockHash) Inspect(f func(plain string, globalSalt []byte)) *mHasherMockHash {
if mmHash.mock.inspectFuncHash != nil {
mmHash.mock.t.Fatalf("Inspect function is already set for HasherMock.Hash")
}
mmHash.mock.inspectFuncHash = f
return mmHash
}
// Return sets up results that will be returned by Hasher.Hash
func (mmHash *mHasherMockHash) Return(s1 string, err error) *HasherMock {
if mmHash.mock.funcHash != nil {
mmHash.mock.t.Fatalf("HasherMock.Hash mock is already set by Set")
}
if mmHash.defaultExpectation == nil {
mmHash.defaultExpectation = &HasherMockHashExpectation{mock: mmHash.mock}
}
mmHash.defaultExpectation.results = &HasherMockHashResults{s1, err}
return mmHash.mock
}
//Set uses given function f to mock the Hasher.Hash method
func (mmHash *mHasherMockHash) Set(f func(plain string, globalSalt []byte) (s1 string, err error)) *HasherMock {
if mmHash.defaultExpectation != nil {
mmHash.mock.t.Fatalf("Default expectation is already set for the Hasher.Hash method")
}
if len(mmHash.expectations) > 0 {
mmHash.mock.t.Fatalf("Some expectations are already set for the Hasher.Hash method")
}
mmHash.mock.funcHash = f
return mmHash.mock
}
// When sets expectation for the Hasher.Hash which will trigger the result defined by the following
// Then helper
func (mmHash *mHasherMockHash) When(plain string, globalSalt []byte) *HasherMockHashExpectation {
if mmHash.mock.funcHash != nil {
mmHash.mock.t.Fatalf("HasherMock.Hash mock is already set by Set")
}
expectation := &HasherMockHashExpectation{
mock: mmHash.mock,
params: &HasherMockHashParams{plain, globalSalt},
}
mmHash.expectations = append(mmHash.expectations, expectation)
return expectation
}
// Then sets up Hasher.Hash return parameters for the expectation previously defined by the When method
func (e *HasherMockHashExpectation) Then(s1 string, err error) *HasherMock {
e.results = &HasherMockHashResults{s1, err}
return e.mock
}
// Hash implements Hasher
func (mmHash *HasherMock) Hash(plain string, globalSalt []byte) (s1 string, err error) {
mm_atomic.AddUint64(&mmHash.beforeHashCounter, 1)
defer mm_atomic.AddUint64(&mmHash.afterHashCounter, 1)
if mmHash.inspectFuncHash != nil {
mmHash.inspectFuncHash(plain, globalSalt)
}
mm_params := &HasherMockHashParams{plain, globalSalt}
// Record call args
mmHash.HashMock.mutex.Lock()
mmHash.HashMock.callArgs = append(mmHash.HashMock.callArgs, mm_params)
mmHash.HashMock.mutex.Unlock()
for _, e := range mmHash.HashMock.expectations {
if minimock.Equal(e.params, mm_params) {
mm_atomic.AddUint64(&e.Counter, 1)
return e.results.s1, e.results.err
}
}
if mmHash.HashMock.defaultExpectation != nil {
mm_atomic.AddUint64(&mmHash.HashMock.defaultExpectation.Counter, 1)
mm_want := mmHash.HashMock.defaultExpectation.params
mm_got := HasherMockHashParams{plain, globalSalt}
if mm_want != nil && !minimock.Equal(*mm_want, mm_got) {
mmHash.t.Errorf("HasherMock.Hash got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got))
}
mm_results := mmHash.HashMock.defaultExpectation.results
if mm_results == nil {
mmHash.t.Fatal("No results are set for the HasherMock.Hash")
}
return (*mm_results).s1, (*mm_results).err
}
if mmHash.funcHash != nil {
return mmHash.funcHash(plain, globalSalt)
}
mmHash.t.Fatalf("Unexpected call to HasherMock.Hash. %v %v", plain, globalSalt)
return
}
// HashAfterCounter returns a count of finished HasherMock.Hash invocations
func (mmHash *HasherMock) HashAfterCounter() uint64 {
return mm_atomic.LoadUint64(&mmHash.afterHashCounter)
}
// HashBeforeCounter returns a count of HasherMock.Hash invocations
func (mmHash *HasherMock) HashBeforeCounter() uint64 {
return mm_atomic.LoadUint64(&mmHash.beforeHashCounter)
}
// Calls returns a list of arguments used in each call to HasherMock.Hash.
// The list is in the same order as the calls were made (i.e. recent calls have a higher index)
func (mmHash *mHasherMockHash) Calls() []*HasherMockHashParams {
mmHash.mutex.RLock()
argCopy := make([]*HasherMockHashParams, len(mmHash.callArgs))
copy(argCopy, mmHash.callArgs)
mmHash.mutex.RUnlock()
return argCopy
}
// MinimockHashDone returns true if the count of the Hash invocations corresponds
// the number of defined expectations
func (m *HasherMock) MinimockHashDone() bool {
for _, e := range m.HashMock.expectations {
if mm_atomic.LoadUint64(&e.Counter) < 1 {
return false
}
}
// if default expectation was set then invocations count should be greater than zero
if m.HashMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterHashCounter) < 1 {
return false
}
// if func was set then invocations count should be greater than zero
if m.funcHash != nil && mm_atomic.LoadUint64(&m.afterHashCounter) < 1 {
return false
}
return true
}
// MinimockHashInspect logs each unmet expectation
func (m *HasherMock) MinimockHashInspect() {
for _, e := range m.HashMock.expectations {
if mm_atomic.LoadUint64(&e.Counter) < 1 {
m.t.Errorf("Expected call to HasherMock.Hash with params: %#v", *e.params)
}
}
// if default expectation was set then invocations count should be greater than zero
if m.HashMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterHashCounter) < 1 {
if m.HashMock.defaultExpectation.params == nil {
m.t.Error("Expected call to HasherMock.Hash")
} else {
m.t.Errorf("Expected call to HasherMock.Hash with params: %#v", *m.HashMock.defaultExpectation.params)
}
}
// if func was set then invocations count should be greater than zero
if m.funcHash != nil && mm_atomic.LoadUint64(&m.afterHashCounter) < 1 {
m.t.Error("Expected call to HasherMock.Hash")
}
}
type mHasherMockVerify struct {
mock *HasherMock
defaultExpectation *HasherMockVerifyExpectation
expectations []*HasherMockVerifyExpectation
callArgs []*HasherMockVerifyParams
mutex sync.RWMutex
}
// HasherMockVerifyExpectation specifies expectation struct of the Hasher.Verify
type HasherMockVerifyExpectation struct {
mock *HasherMock
params *HasherMockVerifyParams
results *HasherMockVerifyResults
Counter uint64
}
// HasherMockVerifyParams contains parameters of the Hasher.Verify
type HasherMockVerifyParams struct {
hashed string
auditee string
globalSalt []byte
}
// HasherMockVerifyResults contains results of the Hasher.Verify
type HasherMockVerifyResults struct {
b1 bool
err error
}
// Expect sets up expected params for Hasher.Verify
func (mmVerify *mHasherMockVerify) Expect(hashed string, auditee string, globalSalt []byte) *mHasherMockVerify {
if mmVerify.mock.funcVerify != nil {
mmVerify.mock.t.Fatalf("HasherMock.Verify mock is already set by Set")
}
if mmVerify.defaultExpectation == nil {
mmVerify.defaultExpectation = &HasherMockVerifyExpectation{}
}
mmVerify.defaultExpectation.params = &HasherMockVerifyParams{hashed, auditee, globalSalt}
for _, e := range mmVerify.expectations {
if minimock.Equal(e.params, mmVerify.defaultExpectation.params) {
mmVerify.mock.t.Fatalf("Expectation set by When has same params: %#v", *mmVerify.defaultExpectation.params)
}
}
return mmVerify
}
// Inspect accepts an inspector function that has same arguments as the Hasher.Verify
func (mmVerify *mHasherMockVerify) Inspect(f func(hashed string, auditee string, globalSalt []byte)) *mHasherMockVerify {
if mmVerify.mock.inspectFuncVerify != nil {
mmVerify.mock.t.Fatalf("Inspect function is already set for HasherMock.Verify")
}
mmVerify.mock.inspectFuncVerify = f
return mmVerify
}
// Return sets up results that will be returned by Hasher.Verify
func (mmVerify *mHasherMockVerify) Return(b1 bool, err error) *HasherMock {
if mmVerify.mock.funcVerify != nil {
mmVerify.mock.t.Fatalf("HasherMock.Verify mock is already set by Set")
}
if mmVerify.defaultExpectation == nil {
mmVerify.defaultExpectation = &HasherMockVerifyExpectation{mock: mmVerify.mock}
}
mmVerify.defaultExpectation.results = &HasherMockVerifyResults{b1, err}
return mmVerify.mock
}
//Set uses given function f to mock the Hasher.Verify method
func (mmVerify *mHasherMockVerify) Set(f func(hashed string, auditee string, globalSalt []byte) (b1 bool, err error)) *HasherMock {
if mmVerify.defaultExpectation != nil {
mmVerify.mock.t.Fatalf("Default expectation is already set for the Hasher.Verify method")
}
if len(mmVerify.expectations) > 0 {
mmVerify.mock.t.Fatalf("Some expectations are already set for the Hasher.Verify method")
}
mmVerify.mock.funcVerify = f
return mmVerify.mock
}
// When sets expectation for the Hasher.Verify which will trigger the result defined by the following
// Then helper
func (mmVerify *mHasherMockVerify) When(hashed string, auditee string, globalSalt []byte) *HasherMockVerifyExpectation {
if mmVerify.mock.funcVerify != nil {
mmVerify.mock.t.Fatalf("HasherMock.Verify mock is already set by Set")
}
expectation := &HasherMockVerifyExpectation{
mock: mmVerify.mock,
params: &HasherMockVerifyParams{hashed, auditee, globalSalt},
}
mmVerify.expectations = append(mmVerify.expectations, expectation)
return expectation
}
// Then sets up Hasher.Verify return parameters for the expectation previously defined by the When method
func (e *HasherMockVerifyExpectation) Then(b1 bool, err error) *HasherMock {
e.results = &HasherMockVerifyResults{b1, err}
return e.mock
}
// Verify implements Hasher
func (mmVerify *HasherMock) Verify(hashed string, auditee string, globalSalt []byte) (b1 bool, err error) {
mm_atomic.AddUint64(&mmVerify.beforeVerifyCounter, 1)
defer mm_atomic.AddUint64(&mmVerify.afterVerifyCounter, 1)
if mmVerify.inspectFuncVerify != nil {
mmVerify.inspectFuncVerify(hashed, auditee, globalSalt)
}
mm_params := &HasherMockVerifyParams{hashed, auditee, globalSalt}
// Record call args
mmVerify.VerifyMock.mutex.Lock()
mmVerify.VerifyMock.callArgs = append(mmVerify.VerifyMock.callArgs, mm_params)
mmVerify.VerifyMock.mutex.Unlock()
for _, e := range mmVerify.VerifyMock.expectations {
if minimock.Equal(e.params, mm_params) {
mm_atomic.AddUint64(&e.Counter, 1)
return e.results.b1, e.results.err
}
}
if mmVerify.VerifyMock.defaultExpectation != nil {
mm_atomic.AddUint64(&mmVerify.VerifyMock.defaultExpectation.Counter, 1)
mm_want := mmVerify.VerifyMock.defaultExpectation.params
mm_got := HasherMockVerifyParams{hashed, auditee, globalSalt}
if mm_want != nil && !minimock.Equal(*mm_want, mm_got) {
mmVerify.t.Errorf("HasherMock.Verify got unexpected parameters, want: %#v, got: %#v%s\n", *mm_want, mm_got, minimock.Diff(*mm_want, mm_got))
}
mm_results := mmVerify.VerifyMock.defaultExpectation.results
if mm_results == nil {
mmVerify.t.Fatal("No results are set for the HasherMock.Verify")
}
return (*mm_results).b1, (*mm_results).err
}
if mmVerify.funcVerify != nil {
return mmVerify.funcVerify(hashed, auditee, globalSalt)
}
mmVerify.t.Fatalf("Unexpected call to HasherMock.Verify. %v %v %v", hashed, auditee, globalSalt)
return
}
// VerifyAfterCounter returns a count of finished HasherMock.Verify invocations
func (mmVerify *HasherMock) VerifyAfterCounter() uint64 {
return mm_atomic.LoadUint64(&mmVerify.afterVerifyCounter)
}
// VerifyBeforeCounter returns a count of HasherMock.Verify invocations
func (mmVerify *HasherMock) VerifyBeforeCounter() uint64 {
return mm_atomic.LoadUint64(&mmVerify.beforeVerifyCounter)
}
// Calls returns a list of arguments used in each call to HasherMock.Verify.
// The list is in the same order as the calls were made (i.e. recent calls have a higher index)
func (mmVerify *mHasherMockVerify) Calls() []*HasherMockVerifyParams {
mmVerify.mutex.RLock()
argCopy := make([]*HasherMockVerifyParams, len(mmVerify.callArgs))
copy(argCopy, mmVerify.callArgs)
mmVerify.mutex.RUnlock()
return argCopy
}
// MinimockVerifyDone returns true if the count of the Verify invocations corresponds
// the number of defined expectations
func (m *HasherMock) MinimockVerifyDone() bool {
for _, e := range m.VerifyMock.expectations {
if mm_atomic.LoadUint64(&e.Counter) < 1 {
return false
}
}
// if default expectation was set then invocations count should be greater than zero
if m.VerifyMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterVerifyCounter) < 1 {
return false
}
// if func was set then invocations count should be greater than zero
if m.funcVerify != nil && mm_atomic.LoadUint64(&m.afterVerifyCounter) < 1 {
return false
}
return true
}
// MinimockVerifyInspect logs each unmet expectation
func (m *HasherMock) MinimockVerifyInspect() {
for _, e := range m.VerifyMock.expectations {
if mm_atomic.LoadUint64(&e.Counter) < 1 {
m.t.Errorf("Expected call to HasherMock.Verify with params: %#v", *e.params)
}
}
// if default expectation was set then invocations count should be greater than zero
if m.VerifyMock.defaultExpectation != nil && mm_atomic.LoadUint64(&m.afterVerifyCounter) < 1 {
if m.VerifyMock.defaultExpectation.params == nil {
m.t.Error("Expected call to HasherMock.Verify")
} else {
m.t.Errorf("Expected call to HasherMock.Verify with params: %#v", *m.VerifyMock.defaultExpectation.params)
}
}
// if func was set then invocations count should be greater than zero
if m.funcVerify != nil && mm_atomic.LoadUint64(&m.afterVerifyCounter) < 1 {
m.t.Error("Expected call to HasherMock.Verify")
}
}
// MinimockFinish checks that all mocked methods have been called the expected number of times
func (m *HasherMock) MinimockFinish() {
if !m.minimockDone() {
m.MinimockHashInspect()
m.MinimockVerifyInspect()
m.t.FailNow()
}
}
// MinimockWait waits for all mocked methods to be called the expected number of times
func (m *HasherMock) MinimockWait(timeout mm_time.Duration) {
timeoutCh := mm_time.After(timeout)
for {
if m.minimockDone() {
return
}
select {
case <-timeoutCh:
m.MinimockFinish()
return
case <-mm_time.After(10 * mm_time.Millisecond):
}
}
}
func (m *HasherMock) minimockDone() bool {
done := true
return done &&
m.MinimockHashDone() &&
m.MinimockVerifyDone()
}