github.com/kaituanwang/hyperledger@v2.0.1+incompatible/bccsp/factory/pkcs11_test.go (about) 1 // +build pkcs11 2 3 /* 4 Copyright IBM Corp. 2017 All Rights Reserved. 5 6 Licensed under the Apache License, Version 2.0 (the "License"); 7 you may not use this file except in compliance with the License. 8 You may obtain a copy of the License at 9 10 http://www.apache.org/licenses/LICENSE-2.0 11 12 Unless required by applicable law or agreed to in writing, software 13 distributed under the License is distributed on an "AS IS" BASIS, 14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 See the License for the specific language governing permissions and 16 limitations under the License. 17 */ 18 package factory 19 20 import ( 21 "os" 22 "testing" 23 24 "github.com/hyperledger/fabric/bccsp/pkcs11" 25 "github.com/stretchr/testify/assert" 26 ) 27 28 func TestInitFactories(t *testing.T) { 29 // Reset errors from previous negative test runs 30 factoriesInitError = nil 31 32 err := InitFactories(nil) 33 assert.NoError(t, err) 34 } 35 36 func TestSetFactories(t *testing.T) { 37 err := setFactories(nil) 38 assert.NoError(t, err) 39 40 err = setFactories(&FactoryOpts{}) 41 assert.NoError(t, err) 42 } 43 44 func TestSetFactoriesInvalidArgs(t *testing.T) { 45 err := setFactories(&FactoryOpts{ 46 ProviderName: "SW", 47 SwOpts: &SwOpts{}, 48 }) 49 assert.Error(t, err) 50 assert.Contains(t, err.Error(), "Failed initializing SW.BCCSP") 51 52 err = setFactories(&FactoryOpts{ 53 ProviderName: "PKCS11", 54 Pkcs11Opts: &pkcs11.PKCS11Opts{}, 55 }) 56 assert.Error(t, err) 57 assert.Contains(t, err.Error(), "Failed initializing PKCS11.BCCSP") 58 } 59 60 func TestGetBCCSPFromOpts(t *testing.T) { 61 opts := GetDefaultOpts() 62 opts.SwOpts.FileKeystore = &FileKeystoreOpts{KeyStorePath: os.TempDir()} 63 opts.SwOpts.Ephemeral = false 64 csp, err := GetBCCSPFromOpts(opts) 65 assert.NoError(t, err) 66 assert.NotNil(t, csp) 67 68 lib, pin, label := pkcs11.FindPKCS11Lib() 69 csp, err = GetBCCSPFromOpts(&FactoryOpts{ 70 ProviderName: "PKCS11", 71 Pkcs11Opts: &pkcs11.PKCS11Opts{ 72 SecLevel: 256, 73 HashFamily: "SHA2", 74 Ephemeral: true, 75 Library: lib, 76 Pin: pin, 77 Label: label, 78 }, 79 }) 80 assert.NoError(t, err) 81 assert.NotNil(t, csp) 82 83 csp, err = GetBCCSPFromOpts(&FactoryOpts{ 84 ProviderName: "BadName", 85 }) 86 assert.Error(t, err) 87 assert.Contains(t, err.Error(), "Could not find BCCSP, no 'BadName' provider") 88 assert.Nil(t, csp) 89 }