github.com/true-sqn/fabric@v2.1.1+incompatible/bccsp/factory/pkcs11_test.go (about)

     1  // +build pkcs11
     2  
     3  /*
     4  Copyright IBM Corp. All Rights Reserved.
     5  
     6  SPDX-License-Identifier: Apache-2.0
     7  */
     8  
     9  package factory
    10  
    11  import (
    12  	"os"
    13  	"testing"
    14  
    15  	"github.com/hyperledger/fabric/bccsp/pkcs11"
    16  	"github.com/stretchr/testify/assert"
    17  )
    18  
    19  func TestExportedInitFactories(t *testing.T) {
    20  	// Reset errors from previous negative test runs
    21  	factoriesInitError = initFactories(nil)
    22  
    23  	err := InitFactories(nil)
    24  	assert.NoError(t, err)
    25  }
    26  
    27  func TestInitFactories(t *testing.T) {
    28  	err := initFactories(nil)
    29  	assert.NoError(t, err)
    30  
    31  	err = initFactories(&FactoryOpts{})
    32  	assert.NoError(t, err)
    33  }
    34  
    35  func TestInitFactoriesInvalidArgs(t *testing.T) {
    36  	err := initFactories(&FactoryOpts{
    37  		ProviderName: "SW",
    38  		SwOpts:       &SwOpts{},
    39  	})
    40  	assert.EqualError(t, err, "Failed initializing SW.BCCSP: Could not initialize BCCSP SW [Failed initializing configuration at [0,]: Hash Family not supported []]")
    41  
    42  	err = initFactories(&FactoryOpts{
    43  		ProviderName: "PKCS11",
    44  		Pkcs11Opts:   &pkcs11.PKCS11Opts{},
    45  	})
    46  	assert.EqualError(t, err, "Failed initializing PKCS11.BCCSP: Could not initialize BCCSP PKCS11 [Failed initializing configuration: Hash Family not supported []]")
    47  }
    48  
    49  func TestGetBCCSPFromOpts(t *testing.T) {
    50  	opts := GetDefaultOpts()
    51  	opts.SwOpts.FileKeystore = &FileKeystoreOpts{KeyStorePath: os.TempDir()}
    52  	opts.SwOpts.Ephemeral = false
    53  	csp, err := GetBCCSPFromOpts(opts)
    54  	assert.NoError(t, err)
    55  	assert.NotNil(t, csp)
    56  
    57  	lib, pin, label := pkcs11.FindPKCS11Lib()
    58  	csp, err = GetBCCSPFromOpts(&FactoryOpts{
    59  		ProviderName: "PKCS11",
    60  		Pkcs11Opts: &pkcs11.PKCS11Opts{
    61  			SecLevel:   256,
    62  			HashFamily: "SHA2",
    63  			Ephemeral:  true,
    64  			Library:    lib,
    65  			Pin:        pin,
    66  			Label:      label,
    67  		},
    68  	})
    69  	assert.NoError(t, err)
    70  	assert.NotNil(t, csp)
    71  
    72  	csp, err = GetBCCSPFromOpts(&FactoryOpts{
    73  		ProviderName: "BadName",
    74  	})
    75  	assert.EqualError(t, err, "Could not find BCCSP, no 'BadName' provider")
    76  	assert.Nil(t, csp)
    77  }