-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmocks_test.go
More file actions
169 lines (141 loc) · 4.37 KB
/
mocks_test.go
File metadata and controls
169 lines (141 loc) · 4.37 KB
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
package encryption_test
import (
"context"
"testing"
"time"
"github.com/0xsequence/nitrocontrol/enclave"
"github.com/0xsequence/nitrocontrol/encryption/data"
"github.com/aws/aws-sdk-go-v2/service/kms"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
type MockKMS struct {
mock.Mock
}
func (m *MockKMS) Decrypt(ctx context.Context, params *kms.DecryptInput, optFns ...func(*kms.Options)) (*kms.DecryptOutput, error) {
args := m.Called(ctx, params, optFns)
if args.Get(0) == nil {
return nil, args.Error(1)
}
return args.Get(0).(*kms.DecryptOutput), args.Error(1)
}
func (m *MockKMS) GenerateDataKey(ctx context.Context, params *kms.GenerateDataKeyInput, optFns ...func(*kms.Options)) (*kms.GenerateDataKeyOutput, error) {
args := m.Called(ctx, params, optFns)
if args.Get(0) == nil {
return nil, args.Error(1)
}
return args.Get(0).(*kms.GenerateDataKeyOutput), args.Error(1)
}
type MockKeysTable struct {
mock.Mock
}
func (m *MockKeysTable) Get(ctx context.Context, generation int, keyIndex int) (*data.CipherKey, bool, error) {
args := m.Called(ctx, generation, keyIndex)
if args.Get(0) == nil {
return nil, args.Bool(1), args.Error(2)
}
return args.Get(0).(*data.CipherKey), args.Bool(1), args.Error(2)
}
func (m *MockKeysTable) GetLatestByKeyRef(ctx context.Context, keyRef string, consistentRead bool) (*data.CipherKey, bool, error) {
args := m.Called(ctx, keyRef, consistentRead)
if args.Get(0) == nil {
return nil, args.Bool(1), args.Error(2)
}
return args.Get(0).(*data.CipherKey), args.Bool(1), args.Error(2)
}
func (m *MockKeysTable) Create(ctx context.Context, key *data.CipherKey) (bool, error) {
args := m.Called(ctx, key)
return args.Bool(0), args.Error(1)
}
func (m *MockKeysTable) Deactivate(ctx context.Context, keyRef string, generation int, now time.Time, attestation []byte) error {
args := m.Called(ctx, keyRef, generation, now, attestation)
return args.Error(0)
}
func (m *MockKeysTable) Delete(ctx context.Context, keyRef string, generation int) error {
args := m.Called(ctx, keyRef, generation)
return args.Error(0)
}
func (m *MockKeysTable) ScanInactive(ctx context.Context, cursor *string) ([]*data.CipherKey, *string, error) {
args := m.Called(ctx, cursor)
var (
keys []*data.CipherKey
nextCursor *string
)
if args.Get(0) != nil {
keys = args.Get(0).([]*data.CipherKey)
}
if args.Get(1) != nil {
nextCursor = args.Get(1).(*string)
}
return keys, nextCursor, args.Error(2)
}
type MockEncryptedDataTable struct {
mock.Mock
}
func (m *MockEncryptedDataTable) TableARN() string {
return m.Called().String(0)
}
func (m *MockEncryptedDataTable) ReferencesCipherKeyRef(ctx context.Context, keyRef string) (bool, error) {
args := m.Called(ctx, keyRef)
return args.Bool(0), args.Error(1)
}
type MockRemoteKey struct {
mock.Mock
}
func (m *MockRemoteKey) RemoteKeyID() string {
return m.Called().String(0)
}
func (m *MockRemoteKey) Encrypt(ctx context.Context, att *enclave.Attestation, plaintext []byte) (string, error) {
args := m.Called(ctx, att, plaintext)
if args.Get(0) == "" {
return "", args.Error(1)
}
return args.String(0), args.Error(1)
}
func (m *MockRemoteKey) Decrypt(ctx context.Context, att *enclave.Attestation, ciphertext string) ([]byte, error) {
args := m.Called(ctx, att, ciphertext)
if args.Get(0) == nil {
return nil, args.Error(1)
}
return args.Get(0).([]byte), args.Error(1)
}
type constantReader struct {
value byte
}
func (r *constantReader) Read(p []byte) (n int, err error) {
for i := range p {
p[i] = r.value
}
return len(p), nil
}
func newCipherKey(t *testing.T, enc *enclave.Enclave, options ...func(*data.CipherKey)) (*data.CipherKey, []byte) {
keyIndex := 4
key := &data.CipherKey{
Generation: 0,
KeyIndex: &keyIndex,
KeyRef: "cipherKey4",
EncryptedShares: map[string]string{
"remoteKey1": "encryptedShare1",
"remoteKey2": "encryptedShare2",
},
CreatedAt: time.Now(),
}
for _, option := range options {
option(key)
}
hash, err := key.Hash()
require.NoError(t, err)
att, err := enc.GetAttestation(context.Background(), nil, hash)
require.NoError(t, err)
defer func() {
if err := att.Close(); err != nil {
t.Log("failed to close attestation", err)
}
}()
key.Attestation = att.Document()
privateKey := [32]byte{}
for i := range privateKey {
privateKey[i] = 0x55 // different from the mocked source of randomness
}
return key, privateKey[:]
}