-
Notifications
You must be signed in to change notification settings - Fork 2
/
shard_test.go
190 lines (174 loc) · 4.38 KB
/
shard_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
package multikey
import (
"crypto/rsa"
"fmt"
"testing"
"github.com/adrianosela/multikey/keys"
"github.com/stretchr/testify/assert"
)
func TestNewShard(t *testing.T) {
tests := []struct {
testName string
shardValue []byte
shardsTotal int
expectErr bool
expectedErr string
}{
{
testName: "positive test",
shardValue: []byte{0x80, 0x80, 0x80, 0x80},
shardsTotal: 5,
expectErr: false,
},
{
testName: "empty value test",
shardValue: []byte{},
shardsTotal: 5,
expectErr: true,
expectedErr: errMsgEmptyValue,
},
}
for _, test := range tests {
s, err := newShard(test.shardValue)
if test.expectErr {
assert.Nil(t, s, test.testName)
assert.EqualError(t, err, test.expectedErr, test.testName)
} else {
assert.Nil(t, err)
assert.Equal(t, s.Value, test.shardValue, test.testName)
}
}
}
func TestEncrypt(t *testing.T) {
badKey := &rsa.PublicKey{}
_, goodKey, err := keys.GenerateRSAKeyPair(2048)
if err != nil {
assert.FailNow(t, "could not generate test key")
}
tests := []struct {
testName string
shard *shard
key *rsa.PublicKey
expectErr bool
expectedErr string
}{
{
testName: "positive test",
shard: &shard{
Value: []byte{0x80, 0x80, 0x80, 0x80},
},
key: goodKey,
expectErr: false,
},
{
testName: "empty value test",
shard: &shard{
Value: []byte{},
},
key: goodKey,
expectErr: true,
expectedErr: errMsgEmptyValue,
},
{
testName: "bad key test",
shard: &shard{
Value: []byte{0x80, 0x80, 0x80, 0x80},
},
key: badKey,
expectErr: true,
expectedErr: fmt.Sprintf("%s: %s: %s", errMsgCouldNotEncrypt, "crypto/rsa", "missing public modulus"),
},
}
for _, test := range tests {
es, err := test.shard.encrypt(test.key)
if test.expectErr {
assert.Nil(t, es, test.testName)
assert.EqualError(t, err, test.expectedErr, test.testName)
} else {
assert.Nil(t, err)
assert.NotEqual(t, es.Value, test.shard.Value, test.testName)
assert.Equal(t, es.KeyID, keys.GetFingerprint(test.key), test.testName)
}
}
}
func TestDecrypt(t *testing.T) {
// a good key pair to encrypt/decrypt successfully
goodPriv, goodPub, err := keys.GenerateRSAKeyPair(2048)
if err != nil {
assert.FailNow(t, "could not generate test key")
}
// a different key to test attempting to decrypt with the wrong key
differentPriv, _, err := keys.GenerateRSAKeyPair(2048)
if err != nil {
assert.FailNow(t, "could not generate different test key")
}
// an invalid key to attempt decrypting with
badPriv := &rsa.PrivateKey{}
mockSecret := "this is a secret"
goodShard := &shard{
Value: []byte(mockSecret),
}
goodEncryptedShard, err := goodShard.encrypt(goodPub)
if err != nil {
assert.FailNow(t, "could not encrypt mock shard")
}
tests := []struct {
testName string
encShard *encryptedShard
key *rsa.PrivateKey
expectErr bool
expectedErr string
}{
{
testName: "positive test",
encShard: goodEncryptedShard,
key: goodPriv,
expectErr: false,
},
{
testName: "incorerct key test",
encShard: goodEncryptedShard,
key: differentPriv,
expectErr: true,
expectedErr: errMsgIncorrectDecryptionKey,
},
{
testName: "bad key test",
encShard: goodEncryptedShard,
key: badPriv,
expectErr: true,
expectedErr: errMsgIncorrectDecryptionKey,
},
{
testName: "bad encoding test",
encShard: &encryptedShard{
Value: "thisisnotbase64",
KeyID: keys.GetFingerprint(goodPub),
},
key: goodPriv,
expectErr: true,
expectedErr: fmt.Sprintf("%s: %s", errMsgCouldNotDecode, "illegal base64 data at input byte 12"),
},
{
testName: "not encrypted test",
encShard: &encryptedShard{
Value: "dGhpc2lzYmFzZTY0", // "thisisbase64" in b64
KeyID: keys.GetFingerprint(goodPub),
},
key: goodPriv,
expectErr: true,
expectedErr: fmt.Sprintf("%s: %s: %s", errMsgCouldNotDecrypt, "crypto/rsa", "decryption error"),
},
}
for _, test := range tests {
s, err := test.encShard.decrypt(test.key)
if test.expectErr {
assert.Nil(t, s, test.testName)
assert.EqualError(t, err, test.expectedErr, test.testName)
} else {
assert.Nil(t, err, test.testName)
assert.NotEqual(t, s.Value, test.encShard.Value, test.testName)
assert.Equal(t, string(s.Value), mockSecret, test.testName)
}
}
}