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