-
Notifications
You must be signed in to change notification settings - Fork 4
/
lib_test.go
233 lines (199 loc) · 8.13 KB
/
lib_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
package httpsignatures
import (
"crypto"
"crypto/sha256"
"errors"
"net/http"
"reflect"
"strings"
"testing"
)
const testBodyExample = `{"hello": "world"}`
const testFullHostExample = "https://example.com"
const testHostExamplePath = "https://example.com/foo"
const testHostExampleFullPath = "https://example.com/foo?param=value&pet=dog"
const testHostExample = "example.com"
const testDateExample = "Sun, 05 Jan 2014 21:31:40 GMT"
const testContentTypeHeader = "Content-Type"
const testContentTypeJSON = "application/json"
const testRsaPrivateKey1024 = `-----BEGIN RSA PRIVATE KEY-----
MIICXgIBAAKBgQDCFENGw33yGihy92pDjZQhl0C36rPJj+CvfSC8+q28hxA161QF
NUd13wuCTUcq0Qd2qsBe/2hFyc2DCJJg0h1L78+6Z4UMR7EOcpfdUE9Hf3m/hs+F
UR45uBJeDK1HSFHD8bHKD6kv8FPGfJTotc+2xjJwoYi+1hqp1fIekaxsyQIDAQAB
AoGBAJR8ZkCUvx5kzv+utdl7T5MnordT1TvoXXJGXK7ZZ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Ujm6zsZleLt+EjJyJTKVjN9mqJES6U4KcMYs0p0rulmTYnYUD
UV6qwSI7mh5Q0ndPGiRg4ZgUkVI/JiiPuzXJ7MxF4OijXCzFHw==
-----END RSA PRIVATE KEY-----`
const testECDSAPrivateKey = `-----BEGIN EC PRIVATE KEY-----
MIHcAgEBBEIAitPY7vFYgUY71qlXk8ujTpJqzYz8dkYP/hXxMT6JBsZiqf4vhuZI
VDgkIwCgsUQ5U+x1+25gsY/pOOEJKDBKQ7OgBwYFK4EEACOhgYkDgYYABAFKT4ww
5WDrV2vaOIb5m8OK09wkOqQ8DHlvgKjLznCwM0F54n6nGXyErSeMNWeoag9Is7B9
6QMkXPyfZv4ZUL8MKgFYR9QrgDPXQswDxDQ4OWn06eBw0Tp+3CggnkcbkDhrgEK/
BMxUHKgNNKMfDoisg1AaIKGYjiBQzUju58j0P1LoHQ==
-----END EC PRIVATE KEY-----`
const testED25519PrivateKey = `-----BEGIN ED25519 PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEIH4HU1c/HklO8ZoWBnPH0Nzh1YN7qdUPLpHc0o3a9yJW
-----END ED25519 PRIVATE KEY-----`
const testRsaPublicKey1024 = `-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDCFENGw33yGihy92pDjZQhl0C3
6rPJj+CvfSC8+q28hxA161QFNUd13wuCTUcq0Qd2qsBe/2hFyc2DCJJg0h1L78+6
Z4UMR7EOcpfdUE9Hf3m/hs+FUR45uBJeDK1HSFHD8bHKD6kv8FPGfJTotc+2xjJw
oYi+1hqp1fIekaxsyQIDAQAB
-----END PUBLIC KEY-----`
const testRsaPublicKey2048 = `-----BEGIN PUBLIC KEY-----
MIIBITANBgkqhkiG9w0BAQEFAAOCAQ4AMIIBCQKCAQBmTcYuCg3VSpw/5/z9iL5R
15E08RBM7N6GT5MeSUK6q2VQyM7cC6OmdyK0NZH3OoPliBPEk7+OSSmEgVzUiI5H
VwC/sPtRqeqByrKi+8DUGNZo8fB4Rs7+NoMzqw28gIaYuXcLiLtoTWrobxRPNpqw
z/nmWMWfJKTRBOERJBhLPEatVQ4orBcQjFRXsAS4+6RuvDavJ3t5/iky2JNM6Dv2
FomUlCGFjm1386eSSe8peLLx3hjk+mqkKMSTMqWqdmYAhtPQZpUMdqSodw5SjtuV
kBqs/nXt7vHlOp4mHv9fzB8xUN/hHin8mnsFz8RWgfIv+hxKwpEht6A4++miB94H
AgMBAAE=
-----END PUBLIC KEY-----`
const testECDSAPublicKey = `-----BEGIN PUBLIC KEY-----
MIGbMBAGByqGSM49AgEGBSuBBAAjA4GGAAQBSk+MMOVg61dr2jiG+ZvDitPcJDqk
PAx5b4Coy85wsDNBeeJ+pxl8hK0njDVnqGoPSLOwfekDJFz8n2b+GVC/DCoBWEfU
K4Az10LMA8Q0ODlp9OngcNE6ftwoIJ5HG5A4a4BCvwTMVByoDTSjHw6IrINQGiCh
mI4gUM1I7ufI9D9S6B0=
-----END PUBLIC KEY-----`
const testED25519PublicKey = `-----BEGIN PUBLIC KEY-----
MCowBQYDK2VwAyEAyLq0cid6Kq5GernyCT+jyGaz16dep8LmxEFYeJ0IVSQ=
-----END PUBLIC KEY-----`
var (
testGetDigestRequestFunc = func(b string, h string) *http.Request {
r, _ := http.NewRequest(http.MethodPost, testFullHostExample, strings.NewReader(b))
if len(h) > 0 {
r.Header.Set("Digest", h)
}
return r
}
)
const (
testAlgName = "TEST"
testErrAlgName = "ERR"
testRsaDummyName = "RSA-DUMMY"
testRsaErrName = "RSA-ERR"
testEcdsaDummyName = "ECDSA-DUMMY"
)
type testAlg struct{}
func (a testAlg) Algorithm() string {
return testAlgName
}
func (a testAlg) Create(data []byte) ([]byte, error) {
return []byte{}, nil
}
func (a testAlg) Verify(data []byte, digest []byte) error {
return nil
}
type testErrAlg struct{}
func (a testErrAlg) Algorithm() string {
return testErrAlgName
}
func (a testErrAlg) Create(data []byte) ([]byte, error) {
return []byte{}, errors.New("create hash error")
}
func (a testErrAlg) Verify(data []byte, digest []byte) error {
return errors.New("verify hash error")
}
// RsaDummy RSA-DUMMY Algorithm
type RsaDummy struct{}
// Algorithm Return algorithm name
func (a RsaDummy) Algorithm() string {
return testRsaDummyName
}
// Create Create dummy
func (a RsaDummy) Create(secret Secret, data []byte) ([]byte, error) {
return signatureRsaAlgorithmCreate(testRsaDummyName, sha256.New, crypto.SHA256, secret, data)
}
// Verify Verify dummy
func (a RsaDummy) Verify(secret Secret, data []byte, signature []byte) error {
return signatureRsaAlgorithmVerify(testRsaDummyName, sha256.New, crypto.SHA256, secret, data, signature)
}
// EcdsaDummy ECDSA-DUMMY Algorithm
type EcdsaDummy struct{}
// Algorithm Return algorithm name
func (a EcdsaDummy) Algorithm() string {
return testEcdsaDummyName
}
// Create Create dummy
func (a EcdsaDummy) Create(secret Secret, data []byte) ([]byte, error) {
return signatureEcdsaAlgorithmCreate(testEcdsaDummyName, sha256.New, secret, data)
}
// Verify Verify dummy
func (a EcdsaDummy) Verify(secret Secret, data []byte, signature []byte) error {
return signatureEcdsaAlgorithmVerify(testEcdsaDummyName, sha256.New, secret, data, signature)
}
// TestRsaErr algorithm with errors
type TestRsaErr struct{}
// Algorithm Return algorithm name
func (a TestRsaErr) Algorithm() string {
return testRsaErrName
}
// Create Create dummy
func (a TestRsaErr) Create(secret Secret, data []byte) ([]byte, error) {
return nil, errors.New("create error")
}
// Verify Verify dummy
func (a TestRsaErr) Verify(secret Secret, data []byte, signature []byte) error {
return errors.New("verify error")
}
var testSecretsStorage = NewSimpleSecretsStorage(map[string]Secret{
"Test": {
KeyID: "Test",
PrivateKey: testRsaPrivateKey1024,
PublicKey: testRsaPublicKey1024,
Algorithm: "RSA-SHA256",
},
"NotSupported": {
KeyID: "NotSupported",
Algorithm: testRsaDummyName,
},
"Err": {
KeyID: "Err",
Algorithm: testRsaErrName,
},
})
func assert(t *testing.T, got interface{}, err error, eType string, name string, want interface{}, wantErrMsg string) {
if err != nil && reflect.TypeOf(err).String() != eType {
t.Errorf(name+"\ngot error type %s, expected %s", reflect.TypeOf(err).String(), eType)
}
if err != nil && err.Error() != wantErrMsg {
t.Errorf(name+"\nerror message = `%s`, wantErrMsg = `%s`", err.Error(), wantErrMsg)
}
if !reflect.DeepEqual(got, want) {
t.Errorf(name+"\ngot = %v,\nwant = %v", got, want)
}
}