OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / tendermint / go-crypto / nano / keys_test.go
1 package nano
2
3 import (
4         "encoding/hex"
5         "os"
6         "testing"
7
8         "github.com/stretchr/testify/assert"
9         "github.com/stretchr/testify/require"
10
11         crypto "github.com/tendermint/go-crypto"
12 )
13
14 func TestLedgerKeys(t *testing.T) {
15         assert, require := assert.New(t), require.New(t)
16
17         cases := []struct {
18                 msg, pubkey, sig string
19                 valid            bool
20         }{
21                 0: {
22                         msg:    "F00D",
23                         pubkey: "8E8754F012C2FDB492183D41437FD837CB81D8BBE731924E2E0DAF43FD3F2C93",
24                         sig:    "787DC03E9E4EE05983E30BAE0DEFB8DB0671DBC2F5874AC93F8D8CA4018F7A42D6F9A9BCEADB422AC8E27CEE9CA205A0B88D22CD686F0A43EB806E8190A3C400",
25                         valid:  true,
26                 },
27                 1: {
28                         msg:    "DEADBEEF",
29                         pubkey: "0C45ADC887A5463F668533443C829ED13EA8E2E890C778957DC28DB9D2AD5A6C",
30                         sig:    "00ED74EED8FDAC7988A14BF6BC222120CBAC249D569AF4C2ADABFC86B792F97DF73C4919BE4B6B0ACB53547273BF29FBF0A9E0992FFAB6CB6C9B09311FC86A00",
31                         valid:  true,
32                 },
33                 2: {
34                         msg:    "1234567890AA",
35                         pubkey: "598FC1F0C76363D14D7480736DEEF390D85863360F075792A6975EFA149FD7EA",
36                         sig:    "59AAB7D7BDC4F936B6415DE672A8B77FA6B8B3451CD95B3A631F31F9A05DAEEE5E7E4F89B64DDEBB5F63DC042CA13B8FCB8185F82AD7FD5636FFDA6B0DC9570B",
37                         valid:  true,
38                 },
39                 3: {
40                         msg:    "1234432112344321",
41                         pubkey: "359E0636E780457294CCA5D2D84DB190C3EDBD6879729C10D3963DEA1D5D8120",
42                         sig:    "616B44EC7A65E7C719C170D669A47DE80C6AC0BB13FBCC89230976F9CC14D4CF9ECF26D4AFBB9FFF625599F1FF6F78EDA15E9F6B6BDCE07CFE9D8C407AC45208",
43                         valid:  true,
44                 },
45                 4: {
46                         msg:    "12344321123443",
47                         pubkey: "359E0636E780457294CCA5D2D84DB190C3EDBD6879729C10D3963DEA1D5D8120",
48                         sig:    "616B44EC7A65E7C719C170D669A47DE80C6AC0BB13FBCC89230976F9CC14D4CF9ECF26D4AFBB9FFF625599F1FF6F78EDA15E9F6B6BDCE07CFE9D8C407AC45208",
49                         valid:  false,
50                 },
51                 5: {
52                         msg:    "1234432112344321",
53                         pubkey: "459E0636E780457294CCA5D2D84DB190C3EDBD6879729C10D3963DEA1D5D8120",
54                         sig:    "616B44EC7A65E7C719C170D669A47DE80C6AC0BB13FBCC89230976F9CC14D4CF9ECF26D4AFBB9FFF625599F1FF6F78EDA15E9F6B6BDCE07CFE9D8C407AC45208",
55                         valid:  false,
56                 },
57                 6: {
58                         msg:    "1234432112344321",
59                         pubkey: "359E0636E780457294CCA5D2D84DB190C3EDBD6879729C10D3963DEA1D5D8120",
60                         sig:    "716B44EC7A65E7C719C170D669A47DE80C6AC0BB13FBCC89230976F9CC14D4CF9ECF26D4AFBB9FFF625599F1FF6F78EDA15E9F6B6BDCE07CFE9D8C407AC45208",
61                         valid:  false,
62                 },
63         }
64
65         for i, tc := range cases {
66                 bmsg, err := hex.DecodeString(tc.msg)
67                 require.NoError(err, "%d", i)
68
69                 priv := NewMockKey(tc.msg, tc.pubkey, tc.sig)
70                 pub := priv.PubKey()
71                 sig := priv.Sign(bmsg)
72
73                 valid := pub.VerifyBytes(bmsg, sig)
74                 assert.Equal(tc.valid, valid, "%d", i)
75         }
76 }
77
78 func TestRealLedger(t *testing.T) {
79         assert, require := assert.New(t), require.New(t)
80
81         if os.Getenv("WITH_LEDGER") == "" {
82                 t.Skip("Set WITH_LEDGER to run code on real ledger")
83         }
84         msg := []byte("kuhehfeohg")
85
86         priv, err := NewPrivKeyLedgerEd25519Ed25519()
87         require.Nil(err, "%+v", err)
88         pub := priv.PubKey()
89         sig := priv.Sign(msg)
90
91         valid := pub.VerifyBytes(msg, sig)
92         assert.True(valid)
93
94         // now, let's serialize the key and make sure it still works
95         bs := priv.Bytes()
96         priv2, err := crypto.PrivKeyFromBytes(bs)
97         require.Nil(err, "%+v", err)
98
99         // make sure we get the same pubkey when we load from disk
100         pub2 := priv2.PubKey()
101         require.Equal(pub, pub2)
102
103         // signing with the loaded key should match the original pubkey
104         sig = priv2.Sign(msg)
105         valid = pub.VerifyBytes(msg, sig)
106         assert.True(valid)
107
108         // make sure pubkeys serialize properly as well
109         bs = pub.Bytes()
110         bpub, err := crypto.PubKeyFromBytes(bs)
111         require.NoError(err)
112         assert.Equal(pub, bpub)
113 }
114
115 // TestRealLedgerErrorHandling calls. These tests assume
116 // the ledger is not plugged in....
117 func TestRealLedgerErrorHandling(t *testing.T) {
118         require := require.New(t)
119
120         if os.Getenv("WITH_LEDGER") != "" {
121                 t.Skip("Skipping on WITH_LEDGER as it tests unplugged cases")
122         }
123
124         // first, try to generate a key, must return an error
125         // (no panic)
126         _, err := NewPrivKeyLedgerEd25519Ed25519()
127         require.Error(err)
128
129         led := PrivKeyLedgerEd25519{} // empty
130         // or with some pub key
131         ed := crypto.GenPrivKeyEd25519()
132         led2 := PrivKeyLedgerEd25519{CachedPubKey: ed.PubKey()}
133
134         // loading these should return errors
135         bs := led.Bytes()
136         _, err = crypto.PrivKeyFromBytes(bs)
137         require.Error(err)
138
139         bs = led2.Bytes()
140         _, err = crypto.PrivKeyFromBytes(bs)
141         require.Error(err)
142 }