github.com/igggame/nebulas-go@v2.1.0+incompatible/crypto/keystore/test/keystore_test.go (about) 1 // Copyright (C) 2017 go-nebulas authors 2 // 3 // This file is part of the go-nebulas library. 4 // 5 // the go-nebulas library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // the go-nebulas library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 // 15 // You should have received a copy of the GNU General Public License 16 // along with the go-nebulas library. If not, see <http://www.gnu.org/licenses/>. 17 // 18 19 package test 20 21 import ( 22 "testing" 23 24 "time" 25 26 "github.com/nebulasio/go-nebulas/crypto" 27 "github.com/nebulasio/go-nebulas/crypto/keystore" 28 "github.com/stretchr/testify/assert" 29 ) 30 31 func TestKeystore_SetKeyPassphrase(t *testing.T) { 32 priv1, _ := crypto.NewPrivateKey(keystore.SECP256K1, nil) 33 priv2, _ := crypto.NewPrivateKey(keystore.SECP256K1, nil) 34 priv3, _ := crypto.NewPrivateKey(keystore.SECP256K1, nil) 35 36 ks := keystore.NewKeystore() 37 38 tests := []struct { 39 name string 40 passphrase []byte 41 alias string 42 key keystore.PrivateKey 43 }{ 44 { 45 "address1", 46 []byte("passphrase"), 47 "alias1", 48 priv1, 49 }, 50 { 51 "address2", 52 []byte("passphrase"), 53 "alias2", 54 priv2, 55 }, 56 { 57 "address3", 58 []byte("passphrase"), 59 "alias3", 60 priv3, 61 }, 62 } 63 for _, tt := range tests { 64 t.Run(tt.name, func(t *testing.T) { 65 err := ks.SetKey(tt.alias, tt.key, tt.passphrase) 66 assert.Nil(t, err, "set key err") 67 got, err := ks.GetKey(tt.alias, tt.passphrase) 68 assert.Equal(t, tt.key, got, "keystore get err") 69 }) 70 } 71 } 72 73 func TestKeystore_ContainsAlias(t *testing.T) { 74 priv1, _ := crypto.NewPrivateKey(keystore.SECP256K1, nil) 75 priv2, _ := crypto.NewPrivateKey(keystore.SECP256K1, nil) 76 priv3, _ := crypto.NewPrivateKey(keystore.SECP256K1, nil) 77 78 ks := keystore.NewKeystore() 79 80 tests := []struct { 81 name string 82 passphrase []byte 83 alias string 84 key keystore.PrivateKey 85 }{ 86 { 87 "address1", 88 []byte("passphrase"), 89 "alias1", 90 priv1, 91 }, 92 { 93 "address2", 94 []byte("passphrase"), 95 "alias2", 96 priv2, 97 }, 98 { 99 "address3", 100 []byte("passphrase"), 101 "alias3", 102 priv3, 103 }, 104 } 105 for _, tt := range tests { 106 t.Run(tt.name, func(t *testing.T) { 107 err := ks.SetKey(tt.alias, tt.key, tt.passphrase) 108 assert.Nil(t, err, "set key err") 109 got, err := ks.ContainsAlias(tt.alias) 110 assert.Nil(t, err, "contains err") 111 assert.True(t, got, "not contains") 112 }) 113 } 114 } 115 116 func TestKeystore_Unlock(t *testing.T) { 117 priv1, _ := crypto.NewPrivateKey(keystore.SECP256K1, nil) 118 priv2, _ := crypto.NewPrivateKey(keystore.SECP256K1, nil) 119 priv3, _ := crypto.NewPrivateKey(keystore.SECP256K1, nil) 120 121 ks := keystore.NewKeystore() 122 123 tests := []struct { 124 name string 125 passphrase []byte 126 alias string 127 key keystore.PrivateKey 128 duration time.Duration 129 want bool 130 }{ 131 { 132 "address1", 133 []byte("passphrase"), 134 "alias1", 135 priv1, 136 time.Second * 0, 137 false, 138 }, 139 { 140 "address2", 141 []byte("passphrase"), 142 "alias2", 143 priv2, 144 time.Second * 3, 145 true, 146 }, 147 { 148 "address3", 149 []byte("passphrase"), 150 "alias3", 151 priv3, 152 time.Second * 5, 153 true, 154 }, 155 } 156 for _, tt := range tests { 157 t.Run(tt.name, func(t *testing.T) { 158 err := ks.SetKey(tt.alias, tt.key, tt.passphrase) 159 assert.Nil(t, err, "set key err") 160 err = ks.Unlock(tt.alias, tt.passphrase, tt.duration) 161 assert.Nil(t, err, "unlock err") 162 }) 163 } 164 time.Sleep(time.Second * 1) 165 for _, tt := range tests { 166 t.Run(tt.name, func(t *testing.T) { 167 got, _ := ks.GetUnlocked(tt.alias) 168 assert.Equal(t, tt.want, tt.key == got, "get unlock err:%s", tt.alias) 169 }) 170 } 171 } 172 173 func TestKeystore_Delete(t *testing.T) { 174 priv1, _ := crypto.NewPrivateKey(keystore.SECP256K1, nil) 175 priv2, _ := crypto.NewPrivateKey(keystore.SECP256K1, nil) 176 priv3, _ := crypto.NewPrivateKey(keystore.SECP256K1, nil) 177 178 ks := keystore.NewKeystore() 179 180 tests := []struct { 181 name string 182 passphrase []byte 183 alias string 184 key keystore.PrivateKey 185 }{ 186 { 187 "address1", 188 []byte("passphrase"), 189 "alias1", 190 priv1, 191 }, 192 { 193 "address2", 194 []byte("passphrase"), 195 "alias2", 196 priv2, 197 }, 198 { 199 "address3", 200 []byte("passphrase"), 201 "alias3", 202 priv3, 203 }, 204 } 205 for _, tt := range tests { 206 t.Run(tt.name, func(t *testing.T) { 207 err := ks.SetKey(tt.alias, tt.key, tt.passphrase) 208 assert.Nil(t, err, "set key err") 209 err = ks.Delete(tt.alias, tt.passphrase) 210 assert.Nil(t, err, "delete err") 211 got, err := ks.ContainsAlias(tt.alias) 212 assert.NotNil(t, err, "contains err") 213 assert.False(t, got, "not contains") 214 }) 215 } 216 }