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

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package factory
     8  
     9  import (
    10  	"fmt"
    11  	"os"
    12  	"strings"
    13  	"testing"
    14  
    15  	"github.com/hyperledger/fabric/bccsp/pkcs11"
    16  	"github.com/spf13/viper"
    17  	"github.com/stretchr/testify/require"
    18  )
    19  
    20  func TestMain(m *testing.M) {
    21  	var yamlBCCSP *FactoryOpts
    22  
    23  	yamlCFG := `
    24  BCCSP:
    25      default: SW
    26      SW:
    27          Hash: SHA3
    28          Security: 256
    29  `
    30  
    31  	if pkcs11Enabled {
    32  		lib, pin, label := pkcs11.FindPKCS11Lib()
    33  		yamlCFG = fmt.Sprintf(`
    34  BCCSP:
    35      default: PKCS11
    36      SW:
    37          Hash: SHA3
    38          Security: 256
    39      PKCS11:
    40          Hash: SHA3
    41          Security: 256
    42  
    43          Library: %s
    44          Pin:     '%s'
    45          Label:   %s
    46          `, lib, pin, label)
    47  	}
    48  
    49  	viper.SetConfigType("yaml")
    50  	err := viper.ReadConfig(strings.NewReader(yamlCFG))
    51  	if err != nil {
    52  		fmt.Printf("Could not read YAML config [%s]", err)
    53  		os.Exit(-1)
    54  	}
    55  
    56  	err = viper.UnmarshalKey("bccsp", &yamlBCCSP)
    57  	if err != nil {
    58  		fmt.Printf("Could not parse YAML config [%s]", err)
    59  		os.Exit(-1)
    60  	}
    61  
    62  	cfgVariations := []*FactoryOpts{
    63  		{},
    64  		{ProviderName: "SW"},
    65  		{ProviderName: "SW", SwOpts: &SwOpts{HashFamily: "SHA2", SecLevel: 256, Ephemeral: true}},
    66  		yamlBCCSP,
    67  	}
    68  
    69  	for index, config := range cfgVariations {
    70  		fmt.Printf("Trying configuration [%d]\n", index)
    71  		factoriesInitError = initFactories(config)
    72  		if factoriesInitError != nil {
    73  			fmt.Fprintf(os.Stderr, "initFactories failed: %s", factoriesInitError)
    74  			os.Exit(1)
    75  		}
    76  		if rc := m.Run(); rc != 0 {
    77  			os.Exit(rc)
    78  		}
    79  	}
    80  	os.Exit(0)
    81  }
    82  
    83  func TestGetDefault(t *testing.T) {
    84  	bccsp := GetDefault()
    85  	require.NotNil(t, bccsp, "Failed getting default BCCSP. Nil instance.")
    86  }