github.com/true-sqn/fabric@v2.1.1+incompatible/bccsp/sw/inmemoryks_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package sw 8 9 import ( 10 "crypto/ecdsa" 11 "crypto/elliptic" 12 "crypto/rand" 13 "fmt" 14 "testing" 15 16 "github.com/stretchr/testify/assert" 17 ) 18 19 func TestInvalidStore(t *testing.T) { 20 t.Parallel() 21 22 ks := NewInMemoryKeyStore() 23 24 err := ks.StoreKey(nil) 25 assert.EqualError(t, err, "key is nil") 26 } 27 28 func TestInvalidLoad(t *testing.T) { 29 t.Parallel() 30 31 ks := NewInMemoryKeyStore() 32 33 _, err := ks.GetKey(nil) 34 assert.EqualError(t, err, "ski is nil or empty") 35 } 36 37 func TestNoKeyFound(t *testing.T) { 38 t.Parallel() 39 40 ks := NewInMemoryKeyStore() 41 42 ski := []byte("foo") 43 _, err := ks.GetKey(ski) 44 assert.EqualError(t, err, fmt.Sprintf("no key found for ski %x", ski)) 45 } 46 47 func TestStoreLoad(t *testing.T) { 48 t.Parallel() 49 50 ks := NewInMemoryKeyStore() 51 52 // generate a key for the keystore to find 53 privKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) 54 assert.NoError(t, err) 55 cspKey := &ecdsaPrivateKey{privKey} 56 57 // store key 58 err = ks.StoreKey(cspKey) 59 assert.NoError(t, err) 60 61 // load key 62 key, err := ks.GetKey(cspKey.SKI()) 63 assert.NoError(t, err) 64 65 assert.Equal(t, cspKey, key) 66 } 67 68 func TestReadOnly(t *testing.T) { 69 t.Parallel() 70 ks := NewInMemoryKeyStore() 71 readonly := ks.ReadOnly() 72 assert.Equal(t, false, readonly) 73 } 74 75 func TestStoreExisting(t *testing.T) { 76 t.Parallel() 77 78 ks := NewInMemoryKeyStore() 79 80 // generate a key for the keystore to find 81 privKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) 82 assert.NoError(t, err) 83 cspKey := &ecdsaPrivateKey{privKey} 84 85 // store key 86 err = ks.StoreKey(cspKey) 87 assert.NoError(t, err) 88 89 // store key a second time 90 err = ks.StoreKey(cspKey) 91 assert.EqualError(t, err, fmt.Sprintf("ski %x already exists in the keystore", cspKey.SKI())) 92 }