github.com/true-sqn/fabric@v2.1.1+incompatible/bccsp/factory/pkcs11factory_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  	"testing"
    13  
    14  	"github.com/hyperledger/fabric/bccsp/pkcs11"
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  func TestPKCS11FactoryName(t *testing.T) {
    19  	f := &PKCS11Factory{}
    20  	assert.Equal(t, f.Name(), PKCS11BasedFactoryName)
    21  }
    22  
    23  func TestPKCS11FactoryGetInvalidArgs(t *testing.T) {
    24  	f := &PKCS11Factory{}
    25  
    26  	_, err := f.Get(nil)
    27  	assert.Error(t, err, "Invalid config. It must not be nil.")
    28  
    29  	_, err = f.Get(&FactoryOpts{})
    30  	assert.Error(t, err, "Invalid config. It must not be nil.")
    31  
    32  	opts := &FactoryOpts{
    33  		Pkcs11Opts: &pkcs11.PKCS11Opts{},
    34  	}
    35  	_, err = f.Get(opts)
    36  	assert.Error(t, err, "CSP:500 - Failed initializing configuration at [0,]")
    37  }
    38  
    39  func TestPKCS11FactoryGet(t *testing.T) {
    40  	f := &PKCS11Factory{}
    41  	lib, pin, label := pkcs11.FindPKCS11Lib()
    42  
    43  	opts := &FactoryOpts{
    44  		Pkcs11Opts: &pkcs11.PKCS11Opts{
    45  			SecLevel:   256,
    46  			HashFamily: "SHA2",
    47  			Library:    lib,
    48  			Pin:        pin,
    49  			Label:      label,
    50  		},
    51  	}
    52  	csp, err := f.Get(opts)
    53  	assert.NoError(t, err)
    54  	assert.NotNil(t, csp)
    55  
    56  	opts = &FactoryOpts{
    57  		Pkcs11Opts: &pkcs11.PKCS11Opts{
    58  			SecLevel:   256,
    59  			HashFamily: "SHA2",
    60  			Library:    lib,
    61  			Pin:        pin,
    62  			Label:      label,
    63  		},
    64  	}
    65  	csp, err = f.Get(opts)
    66  	assert.NoError(t, err)
    67  	assert.NotNil(t, csp)
    68  
    69  	opts = &FactoryOpts{
    70  		Pkcs11Opts: &pkcs11.PKCS11Opts{
    71  			SecLevel:   256,
    72  			HashFamily: "SHA2",
    73  			Ephemeral:  true,
    74  			Library:    lib,
    75  			Pin:        pin,
    76  			Label:      label,
    77  		},
    78  	}
    79  	csp, err = f.Get(opts)
    80  	assert.NoError(t, err)
    81  	assert.NotNil(t, csp)
    82  }
    83  
    84  func TestPKCS11FactoryGetEmptyKeyStorePath(t *testing.T) {
    85  	f := &PKCS11Factory{}
    86  	lib, pin, label := pkcs11.FindPKCS11Lib()
    87  
    88  	opts := &FactoryOpts{
    89  		Pkcs11Opts: &pkcs11.PKCS11Opts{
    90  			SecLevel:   256,
    91  			HashFamily: "SHA2",
    92  			Library:    lib,
    93  			Pin:        pin,
    94  			Label:      label,
    95  		},
    96  	}
    97  	csp, err := f.Get(opts)
    98  	assert.NoError(t, err)
    99  	assert.NotNil(t, csp)
   100  
   101  	opts = &FactoryOpts{
   102  		Pkcs11Opts: &pkcs11.PKCS11Opts{
   103  			SecLevel:   256,
   104  			HashFamily: "SHA2",
   105  			Library:    lib,
   106  			Pin:        pin,
   107  			Label:      label,
   108  		},
   109  	}
   110  	csp, err = f.Get(opts)
   111  	assert.NoError(t, err)
   112  	assert.NotNil(t, csp)
   113  }