OSDN Git Service

673c1f986a00bcef75179128f0451c7949d94137
[bytom/vapor.git] / blockchain / pseudohsm / pseudohsm_test.go
1 package pseudohsm
2
3 import (
4         "fmt"
5         "io/ioutil"
6         "os"
7         "strings"
8         "testing"
9
10         "github.com/vapor/crypto/ed25519"
11         "github.com/vapor/errors"
12 )
13
14 const dirPath = "testdata/pseudo"
15
16 func TestCreateKeyWithUpperCase(t *testing.T) {
17         hsm, _ := New(dirPath)
18
19         alias := "UPPER"
20
21         xpub, _, err := hsm.XCreate(alias, "password", "en")
22         if err != nil {
23                 t.Fatal(err)
24         }
25
26         if xpub.Alias != strings.ToLower(alias) {
27                 t.Fatal("the created key alias should be lowercase")
28         }
29
30         err = hsm.XDelete(xpub.XPub, "password")
31         if err != nil {
32                 t.Fatal(err)
33         }
34 }
35
36 func TestCreateKeyWithWhiteSpaceTrimed(t *testing.T) {
37         hsm, _ := New(dirPath)
38
39         alias := " with space surrounding "
40
41         xpub, _, err := hsm.XCreate(alias, "password", "en")
42         if err != nil {
43                 t.Fatal(err)
44         }
45
46         if xpub.Alias != strings.TrimSpace(alias) {
47                 t.Fatal("the created key alias should be lowercase")
48         }
49
50         if err = hsm.XDelete(xpub.XPub, "password"); err != nil {
51                 t.Fatal(err)
52         }
53 }
54
55 func TestUpdateKeyAlias(t *testing.T) {
56         hsm, _ := New(dirPath)
57         oldAlias := "old_alias"
58         newAlias := "new_alias"
59
60         xpub, _, err := hsm.XCreate(oldAlias, "password", "en")
61         if err != nil {
62                 t.Fatal(err)
63         }
64
65         if xpub.Alias != strings.TrimSpace(oldAlias) {
66                 t.Fatal("the created key alias should be lowercase")
67         }
68
69         if err = hsm.UpdateKeyAlias(xpub.XPub, oldAlias); err != ErrDuplicateKeyAlias {
70                 t.Fatal("got error:", err, "want error:", ErrDuplicateKeyAlias)
71         }
72
73         if err = hsm.UpdateKeyAlias(xpub.XPub, newAlias); err != nil {
74                 t.Fatal(err)
75         }
76
77         if err = hsm.XDelete(xpub.XPub, "password"); err != nil {
78                 t.Fatal(err)
79         }
80 }
81
82 func TestPseudoHSMChainKDKeys(t *testing.T) {
83
84         hsm, _ := New(dirPath)
85         xpub, _, err := hsm.XCreate("bbs", "password", "en")
86
87         if err != nil {
88                 t.Fatal(err)
89         }
90         xpub2, _, err := hsm.XCreate("bytom", "nopassword", "en")
91         if err != nil {
92                 t.Fatal(err)
93         }
94         msg := []byte("In the face of ignorance and resistance I wrote financial systems into existence")
95         sig, err := hsm.XSign(xpub.XPub, nil, msg, "password")
96         if err != nil {
97                 t.Fatal(err)
98         }
99         if !xpub.XPub.Verify(msg, sig) {
100                 t.Error("expected verify to succeed")
101         }
102         if xpub2.XPub.Verify(msg, sig) {
103                 t.Error("expected verify with wrong pubkey to fail")
104         }
105         path := [][]byte{{3, 2, 6, 3, 8, 2, 7}}
106         sig, err = hsm.XSign(xpub2.XPub, path, msg, "nopassword")
107         if err != nil {
108                 t.Fatal(err)
109         }
110         if xpub2.XPub.Verify(msg, sig) {
111                 t.Error("expected verify with underived pubkey of sig from derived privkey to fail")
112         }
113         if !xpub2.XPub.Derive(path).Verify(msg, sig) {
114                 t.Error("expected verify with derived pubkey of sig from derived privkey to succeed")
115         }
116
117         xpubs := hsm.ListKeys()
118         if len(xpubs) != 2 {
119                 t.Error("expected 2 entries in the db")
120         }
121         err = hsm.ResetPassword(xpub2.XPub, "nopassword", "1password")
122         if err != nil {
123                 t.Fatal(err)
124         }
125         err = hsm.XDelete(xpub.XPub, "password")
126         if err != nil {
127                 t.Fatal(err)
128         }
129         err = hsm.XDelete(xpub2.XPub, "1password")
130         if err != nil {
131                 t.Fatal(err)
132         }
133 }
134
135 func TestKeyWithEmptyAlias(t *testing.T) {
136         hsm, _ := New(dirPath)
137         for i := 0; i < 2; i++ {
138                 xpub, _, err := hsm.XCreate(fmt.Sprintf("xx%d", i), "xx", "en")
139                 if errors.Root(err) != nil {
140                         t.Fatal(err)
141                 }
142                 err = hsm.XDelete(xpub.XPub, "xx")
143                 if err != nil {
144                         t.Fatal(err)
145                 }
146         }
147 }
148
149 func TestSignAndVerifyMessage(t *testing.T) {
150         hsm, _ := New(dirPath)
151         xpub, _, err := hsm.XCreate("TESTKEY", "password", "en")
152         if err != nil {
153                 t.Fatal(err)
154         }
155
156         path := [][]byte{{3, 2, 6, 3, 8, 2, 7}}
157         derivedXPub := xpub.XPub.Derive(path)
158
159         msg := "this is a test message"
160         sig, err := hsm.XSign(xpub.XPub, path, []byte(msg), "password")
161         if err != nil {
162                 t.Fatal(err)
163         }
164
165         dePublicKey := derivedXPub.PublicKey()
166         switch pubKey := dePublicKey.(type) {
167         case ed25519.PublicKey:
168                 // derivedXPub verify success
169                 if !ed25519.Verify(pubKey, []byte(msg), sig) {
170                         t.Fatal("right derivedXPub verify sign failed")
171                 }
172         }
173
174         roPublicKey := xpub.XPub.PublicKey()
175         switch pubKey := roPublicKey.(type) {
176         case ed25519.PublicKey:
177                 // rootXPub verify failed
178                 if ed25519.Verify(pubKey, []byte(msg), sig) {
179                         t.Fatal("right rootXPub verify derivedXPub sign succeed")
180                 }
181         }
182
183         err = hsm.XDelete(xpub.XPub, "password")
184         if err != nil {
185                 t.Fatal(err)
186         }
187 }
188
189 func TestImportKeyFromMnemonic(t *testing.T) {
190         dirPath, err := ioutil.TempDir(".", "")
191         if err != nil {
192                 t.Fatal(err)
193         }
194         defer os.RemoveAll(dirPath)
195
196         hsm, _ := New(dirPath)
197         supportLanguage := []string{"zh_CN", "zh_TW", "en", "it", "ja", "ko", "es"}
198         for i, language := range supportLanguage {
199                 key := fmt.Sprintf("TESTKEY%x", i)
200                 xpub, mnemonic, err := hsm.XCreate(key, "password", language)
201                 if err != nil {
202                         t.Fatal(err)
203                 }
204                 importKey := fmt.Sprintf("IMPORTKEY%x", i)
205                 newXPub, err := hsm.ImportKeyFromMnemonic(importKey, "password", *mnemonic, language)
206                 if err != nil {
207                         t.Fatal(err)
208                 }
209                 if xpub.XPub != newXPub.XPub {
210                         t.Fatal("import key from mnemonic failed")
211                 }
212         }
213 }
214
215 func BenchmarkSign(b *testing.B) {
216         b.StopTimer()
217         auth := "nowpasswd"
218
219         hsm, _ := New(dirPath)
220         xpub, _, err := hsm.XCreate("TESTKEY", auth, "en")
221         if err != nil {
222                 b.Fatal(err)
223         }
224
225         msg := []byte("In the face of ignorance and resistance I wrote financial systems into existence")
226
227         b.StartTimer()
228         for i := 0; i < b.N; i++ {
229                 _, err := hsm.XSign(xpub.XPub, nil, msg, auth)
230                 if err != nil {
231                         b.Fatal(err)
232                 }
233         }
234         err = hsm.XDelete(xpub.XPub, auth)
235         if err != nil {
236                 b.Fatal(err)
237         }
238 }