github.com/yacovm/fabric@v2.0.0-alpha.0.20191128145320-c5d4087dc723+incompatible/bccsp/sw/fileks_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 "encoding/hex" 14 "fmt" 15 "io/ioutil" 16 "os" 17 "path/filepath" 18 "testing" 19 20 "github.com/hyperledger/fabric/bccsp/utils" 21 "github.com/stretchr/testify/assert" 22 ) 23 24 func TestInvalidStoreKey(t *testing.T) { 25 t.Parallel() 26 27 tempDir, err := ioutil.TempDir("", "bccspks") 28 assert.NoError(t, err) 29 defer os.RemoveAll(tempDir) 30 31 ks, err := NewFileBasedKeyStore(nil, filepath.Join(tempDir, "bccspks"), false) 32 if err != nil { 33 fmt.Printf("Failed initiliazing KeyStore [%s]", err) 34 os.Exit(-1) 35 } 36 37 err = ks.StoreKey(nil) 38 if err == nil { 39 t.Fatal("Error should be different from nil in this case") 40 } 41 42 err = ks.StoreKey(&ecdsaPrivateKey{nil}) 43 if err == nil { 44 t.Fatal("Error should be different from nil in this case") 45 } 46 47 err = ks.StoreKey(&ecdsaPublicKey{nil}) 48 if err == nil { 49 t.Fatal("Error should be different from nil in this case") 50 } 51 52 err = ks.StoreKey(&aesPrivateKey{nil, false}) 53 if err == nil { 54 t.Fatal("Error should be different from nil in this case") 55 } 56 57 err = ks.StoreKey(&aesPrivateKey{nil, true}) 58 if err == nil { 59 t.Fatal("Error should be different from nil in this case") 60 } 61 } 62 63 func TestBigKeyFile(t *testing.T) { 64 ksPath, err := ioutil.TempDir("", "bccspks") 65 assert.NoError(t, err) 66 defer os.RemoveAll(ksPath) 67 68 ks, err := NewFileBasedKeyStore(nil, ksPath, false) 69 assert.NoError(t, err) 70 71 // Generate a key for keystore to find 72 privKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) 73 assert.NoError(t, err) 74 75 cspKey := &ecdsaPrivateKey{privKey} 76 ski := cspKey.SKI() 77 rawKey, err := utils.PrivateKeyToPEM(privKey, nil) 78 assert.NoError(t, err) 79 80 // Large padding array, of some values PEM parser will NOOP 81 bigBuff := make([]byte, (1 << 17)) 82 for i := range bigBuff { 83 bigBuff[i] = '\n' 84 } 85 copy(bigBuff, rawKey) 86 87 //>64k, so that total file size will be too big 88 ioutil.WriteFile(filepath.Join(ksPath, "bigfile.pem"), bigBuff, 0666) 89 90 _, err = ks.GetKey(ski) 91 assert.Error(t, err) 92 expected := fmt.Sprintf("Key with SKI %s not found in %s", hex.EncodeToString(ski), ksPath) 93 assert.EqualError(t, err, expected) 94 95 // 1k, so that the key would be found 96 ioutil.WriteFile(filepath.Join(ksPath, "smallerfile.pem"), bigBuff[0:1<<10], 0666) 97 98 _, err = ks.GetKey(ski) 99 assert.NoError(t, err) 100 } 101 102 func TestReInitKeyStore(t *testing.T) { 103 ksPath, err := ioutil.TempDir("", "bccspks") 104 assert.NoError(t, err) 105 defer os.RemoveAll(ksPath) 106 107 ks, err := NewFileBasedKeyStore(nil, ksPath, false) 108 assert.NoError(t, err) 109 fbKs, isFileBased := ks.(*fileBasedKeyStore) 110 assert.True(t, isFileBased) 111 err = fbKs.Init(nil, ksPath, false) 112 assert.EqualError(t, err, "KeyStore already initilized.") 113 }