-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathmock.go
74 lines (63 loc) · 1.63 KB
/
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
/*
Copyright (c) Edgeless Systems GmbH
SPDX-License-Identifier: BUSL-1.1
*/
package quote
import (
"bytes"
"crypto/sha256"
"errors"
"sync"
)
type entry struct {
message []byte
pp PackageProperties
ip InfrastructureProperties
}
// MockValidator is a mockup quote validator.
type MockValidator struct {
mutex sync.Mutex
valid map[string]entry
}
// NewMockValidator returns a new MockValidator object.
func NewMockValidator() *MockValidator {
return &MockValidator{
valid: make(map[string]entry),
}
}
// Validate implements the Validator interface.
func (m *MockValidator) Validate(quote []byte, message []byte, pp PackageProperties, ip InfrastructureProperties) error {
m.mutex.Lock()
entry, found := m.valid[string(quote)]
m.mutex.Unlock()
if !found {
return errors.New("wrong quote")
}
if !bytes.Equal(entry.message, message) {
return errors.New("wrong message")
}
if !pp.IsCompliant(entry.pp) {
return errors.New("package does not comply")
}
if !ip.IsCompliant(entry.ip) {
return errors.New("infrastructure does not comply")
}
return nil
}
// AddValidQuote adds a valid quote.
func (m *MockValidator) AddValidQuote(quote []byte, message []byte, pp PackageProperties, ip InfrastructureProperties) {
m.mutex.Lock()
m.valid[string(quote)] = entry{message, pp, ip}
m.mutex.Unlock()
}
// MockIssuer is a mockup quote issuer.
type MockIssuer struct{}
// NewMockIssuer returns a new MockIssuer object.
func NewMockIssuer() *MockIssuer {
return &MockIssuer{}
}
// Issue implements the Issuer interface.
func (m *MockIssuer) Issue(message []byte) ([]byte, error) {
quote := sha256.Sum256(message)
return quote[:], nil
}