forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keys_test.go
137 lines (110 loc) · 3.04 KB
/
keys_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
package keystore
import (
"context"
"database/sql"
"encoding/base64"
"encoding/json"
"reflect"
"testing"
"time"
"github.com/stellar/go/support/errors"
)
func TestPutKeys(t *testing.T) {
db := openKeystoreDB(t)
defer db.Close() // drop test db
conn := db.Open()
defer conn.Close() // close db connection
ctx := withUserID(context.Background(), "test-user")
s := &Service{conn.DB, nil}
blob := `[{
"id": "test-id",
"salt": "test-salt",
"encrypterName": "test-encrypter-name",
"encryptedBlob": "test-encryptedblob"
}]`
keysBlob := base64.RawURLEncoding.EncodeToString([]byte(blob))
got, err := s.putKeys(ctx, putKeysRequest{KeysBlob: keysBlob})
if err != nil {
t.Fatal(err)
}
verifyKeysBlob(t, got.KeysBlob, keysBlob)
if got.CreatedAt.Before(time.Now().Add(-time.Hour)) {
t.Errorf("got CreatedAt=%s, want CreatedAt within the last hour", got.CreatedAt)
}
}
func TestGetKeys(t *testing.T) {
db := openKeystoreDB(t)
defer db.Close() // drop test db
conn := db.Open()
defer conn.Close() // close db connection
ctx := withUserID(context.Background(), "test-user")
s := &Service{conn.DB, nil}
blob := `[{
"id": "test-id",
"salt": "test-salt",
"encrypterName": "test-encrypter-name",
"encryptedBlob": "test-encryptedblob"
}]`
keysBlob := base64.RawURLEncoding.EncodeToString([]byte(blob))
_, err := s.putKeys(ctx, putKeysRequest{KeysBlob: keysBlob})
if err != nil {
t.Fatal(err)
}
got, err := s.getKeys(ctx)
if err != nil {
t.Fatal(err)
}
verifyKeysBlob(t, got.KeysBlob, keysBlob)
if got.CreatedAt.Before(time.Now().Add(-time.Hour)) {
t.Errorf("got CreatedAt=%s, want CreatedAt within the last hour", got.CreatedAt)
}
}
func TestDeleteKeys(t *testing.T) {
db := openKeystoreDB(t)
defer db.Close() // drop test db
conn := db.Open()
defer conn.Close() // close db connection
ctx := withUserID(context.Background(), "test-user")
s := &Service{conn.DB, nil}
blob := `[{
"id": "test-id",
"salt": "test-salt",
"encrypterName": "test-encrypter-name",
"encryptedBlob": "test-encryptedblob"
}]`
keysBlob := base64.RawURLEncoding.EncodeToString([]byte(blob))
_, err := s.putKeys(ctx, putKeysRequest{KeysBlob: keysBlob})
if err != nil {
t.Fatal(err)
}
err = s.deleteKeys(ctx)
if err != nil {
t.Fatal(err)
}
_, err = s.getKeys(ctx)
if errors.Cause(err) != sql.ErrNoRows {
t.Errorf("expect the keys blob of the user %s to be deleted", userID(ctx))
}
}
func verifyKeysBlob(t *testing.T, gotKeysBlob, inKeysBlob string) {
var gotEncryptedKeys, inEncryptedKeys []encryptedKeyData
gotKeysData, err := base64.RawURLEncoding.DecodeString(gotKeysBlob)
if err != nil {
t.Fatal(err)
}
err = json.Unmarshal(gotKeysData, &gotEncryptedKeys)
if err != nil {
t.Fatal(err)
}
inKeysData, err := base64.RawURLEncoding.DecodeString(inKeysBlob)
if err != nil {
t.Fatal(err)
}
err = json.Unmarshal(inKeysData, &inEncryptedKeys)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(gotEncryptedKeys, inEncryptedKeys) {
t.Errorf("got keys: %v, want keys: %v\n", gotEncryptedKeys, inEncryptedKeys)
}
}