OSDN Git Service

reorganize error code (#1133)
[bytom/bytom.git] / blockchain / pseudohsm / pseudohsm_test.go
1 package pseudohsm
2
3 import (
4         "fmt"
5         "strings"
6         "testing"
7
8         "github.com/bytom/crypto/ed25519"
9         "github.com/bytom/errors"
10 )
11
12 const dirPath = "testdata/pseudo"
13
14 func TestCreateKeyWithUpperCase(t *testing.T) {
15         hsm, _ := New(dirPath)
16
17         alias := "UPPER"
18
19         xpub, err := hsm.XCreate(alias, "password")
20         if err != nil {
21                 t.Fatal(err)
22         }
23
24         if xpub.Alias != strings.ToLower(alias) {
25                 t.Fatal("the created key alias should be lowercase")
26         }
27
28         err = hsm.XDelete(xpub.XPub, "password")
29         if err != nil {
30                 t.Fatal(err)
31         }
32 }
33
34 func TestCreateKeyWithWhiteSpaceTrimed(t *testing.T) {
35         hsm, _ := New(dirPath)
36
37         alias := " with space surrounding "
38
39         xpub, err := hsm.XCreate(alias, "password")
40         if err != nil {
41                 t.Fatal(err)
42         }
43
44         if xpub.Alias != strings.TrimSpace(alias) {
45                 t.Fatal("the created key alias should be lowercase")
46         }
47
48         err = hsm.XDelete(xpub.XPub, "password")
49         if err != nil {
50                 t.Fatal(err)
51         }
52 }
53
54 func TestPseudoHSMChainKDKeys(t *testing.T) {
55
56         hsm, _ := New(dirPath)
57         xpub, err := hsm.XCreate("bbs", "password")
58
59         if err != nil {
60                 t.Fatal(err)
61         }
62         xpub2, err := hsm.XCreate("bytom", "nopassword")
63         if err != nil {
64                 t.Fatal(err)
65         }
66         msg := []byte("In the face of ignorance and resistance I wrote financial systems into existence")
67         sig, err := hsm.XSign(xpub.XPub, nil, msg, "password")
68         if err != nil {
69                 t.Fatal(err)
70         }
71         if !xpub.XPub.Verify(msg, sig) {
72                 t.Error("expected verify to succeed")
73         }
74         if xpub2.XPub.Verify(msg, sig) {
75                 t.Error("expected verify with wrong pubkey to fail")
76         }
77         path := [][]byte{{3, 2, 6, 3, 8, 2, 7}}
78         sig, err = hsm.XSign(xpub2.XPub, path, msg, "nopassword")
79         if err != nil {
80                 t.Fatal(err)
81         }
82         if xpub2.XPub.Verify(msg, sig) {
83                 t.Error("expected verify with underived pubkey of sig from derived privkey to fail")
84         }
85         if !xpub2.XPub.Derive(path).Verify(msg, sig) {
86                 t.Error("expected verify with derived pubkey of sig from derived privkey to succeed")
87         }
88
89         xpubs := hsm.ListKeys()
90         if len(xpubs) != 2 {
91                 t.Error("expected 2 entries in the db")
92         }
93         err = hsm.ResetPassword(xpub2.XPub, "nopassword", "1password")
94         if err != nil {
95                 t.Fatal(err)
96         }
97         err = hsm.XDelete(xpub.XPub, "password")
98         if err != nil {
99                 t.Fatal(err)
100         }
101         err = hsm.XDelete(xpub2.XPub, "1password")
102         if err != nil {
103                 t.Fatal(err)
104         }
105 }
106
107 func TestKeyWithEmptyAlias(t *testing.T) {
108         hsm, _ := New(dirPath)
109         for i := 0; i < 2; i++ {
110                 xpub, err := hsm.XCreate(fmt.Sprintf("xx%d", i), "xx")
111                 if errors.Root(err) != nil {
112                         t.Fatal(err)
113                 }
114                 err = hsm.XDelete(xpub.XPub, "xx")
115                 if err != nil {
116                         t.Fatal(err)
117                 }
118         }
119 }
120
121 func TestSignAndVerifyMessage(t *testing.T) {
122         hsm, _ := New(dirPath)
123         xpub, err := hsm.XCreate("TESTKEY", "password")
124         if err != nil {
125                 t.Fatal(err)
126         }
127
128         path := [][]byte{{3, 2, 6, 3, 8, 2, 7}}
129         derivedXPub := xpub.XPub.Derive(path)
130
131         msg := "this is a test message"
132         sig, err := hsm.XSign(xpub.XPub, path, []byte(msg), "password")
133         if err != nil {
134                 t.Fatal(err)
135         }
136
137         // derivedXPub verify success
138         if !ed25519.Verify(derivedXPub.PublicKey(), []byte(msg), sig) {
139                 t.Fatal("right derivedXPub verify sign failed")
140         }
141
142         // rootXPub verify failed
143         if ed25519.Verify(xpub.XPub.PublicKey(), []byte(msg), sig) {
144                 t.Fatal("right rootXPub verify derivedXPub sign succeed")
145         }
146
147         err = hsm.XDelete(xpub.XPub, "password")
148         if err != nil {
149                 t.Fatal(err)
150         }
151 }
152
153 func BenchmarkSign(b *testing.B) {
154         b.StopTimer()
155         auth := "nowpasswd"
156
157         hsm, _ := New(dirPath)
158         xpub, err := hsm.XCreate(auth, "")
159         if err != nil {
160                 b.Fatal(err)
161         }
162
163         msg := []byte("In the face of ignorance and resistance I wrote financial systems into existence")
164
165         b.StartTimer()
166         for i := 0; i < b.N; i++ {
167                 _, err := hsm.XSign(xpub.XPub, nil, msg, auth)
168                 if err != nil {
169                         b.Fatal(err)
170                 }
171         }
172         err = hsm.XDelete(xpub.XPub, auth)
173         if err != nil {
174                 b.Fatal(err)
175         }
176 }