-
Notifications
You must be signed in to change notification settings - Fork 2
/
keycrypter_test.go
executable file
·54 lines (42 loc) · 1.19 KB
/
keycrypter_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
package libwallet
import (
"testing"
)
func TestKeyCrypter(t *testing.T) {
key, _ := NewHDPrivateKey(randomBytes(16), Regtest())
key, _ = key.DeriveTo("m/123'/1")
testPassphrase := "asdasdasd"
t.Run("simple encrypt", func(t *testing.T) {
_, err := KeyEncrypt(key, testPassphrase)
if err != nil {
t.Errorf("KeyEncrypt() error = %v", err)
return
}
})
t.Run("encrypt & decrypt", func(t *testing.T) {
encrypted, err := KeyEncrypt(key, testPassphrase)
if err != nil {
t.Fatalf("KeyEncrypt() error = %v", err)
}
decrypted, err := KeyDecrypt(encrypted, testPassphrase, Regtest())
if err != nil {
t.Fatalf("KeyEncrypt() error = %v", err)
}
if decrypted.Key.String() != key.String() {
t.Errorf("KeyEncrypt() expected key %v got %v", key, decrypted.Key)
}
if decrypted.Path != key.Path {
t.Errorf("KeyEncrypt() expected path %v got %v", key.Path, decrypted.Path)
}
})
t.Run("bad passphrase", func(t *testing.T) {
encrypted, err := KeyEncrypt(key, testPassphrase)
if err != nil {
t.Fatalf("KeyEncrypt() error = %v", err)
}
_, err = KeyDecrypt(encrypted, testPassphrase+"foo", Regtest())
if err == nil {
t.Fatalf("expected decryption error")
}
})
}