github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/bccsp/factory/factory_test.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 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  	"bytes"
    20  	"encoding/json"
    21  	"flag"
    22  	"fmt"
    23  	"os"
    24  	"testing"
    25  
    26  	"github.com/hyperledger/fabric/bccsp/pkcs11"
    27  	"github.com/spf13/viper"
    28  )
    29  
    30  func TestMain(m *testing.M) {
    31  	flag.Parse()
    32  	lib, pin, label := pkcs11.FindPKCS11Lib()
    33  
    34  	var jsonBCCSP, yamlBCCSP *FactoryOpts
    35  	jsonCFG := []byte(
    36  		`{ "default": "SW", "SW":{ "security": 384, "hash": "SHA3" } }`)
    37  
    38  	err := json.Unmarshal(jsonCFG, &jsonBCCSP)
    39  	if err != nil {
    40  		fmt.Printf("Could not parse JSON config [%s]", err)
    41  		os.Exit(-1)
    42  	}
    43  
    44  	yamlCFG := fmt.Sprintf(`
    45  BCCSP:
    46      default: PKCS11
    47      SW:
    48          Hash: SHA3
    49          Security: 256
    50      PKCS11:
    51          Hash: SHA3
    52          Security: 256
    53  
    54          Library: %s
    55          Pin:     '%s'
    56          Label:   %s
    57          `, lib, pin, label)
    58  
    59  	if lib == "" {
    60  		fmt.Printf("Could not find PKCS11 libraries, running without\n")
    61  		yamlCFG = `
    62  BCCSP:
    63      default: SW
    64      SW:
    65          Hash: SHA3
    66          Security: 256`
    67  	}
    68  
    69  	viper.SetConfigType("yaml")
    70  	err = viper.ReadConfig(bytes.NewBuffer([]byte(yamlCFG)))
    71  	if err != nil {
    72  		fmt.Printf("Could not read YAML config [%s]", err)
    73  		os.Exit(-1)
    74  	}
    75  
    76  	err = viper.UnmarshalKey("bccsp", &yamlBCCSP)
    77  	if err != nil {
    78  		fmt.Printf("Could not parse YAML config [%s]", err)
    79  		os.Exit(-1)
    80  	}
    81  
    82  	cfgVariations := []*FactoryOpts{
    83  		{
    84  			ProviderName: "SW",
    85  			SwOpts: &SwOpts{
    86  				HashFamily: "SHA2",
    87  				SecLevel:   256,
    88  
    89  				Ephemeral: true,
    90  			},
    91  		},
    92  		{},
    93  		{
    94  			ProviderName: "SW",
    95  		},
    96  		jsonBCCSP,
    97  		yamlBCCSP,
    98  	}
    99  
   100  	for index, config := range cfgVariations {
   101  		fmt.Printf("Trying configuration [%d]\n", index)
   102  		InitFactories(config)
   103  		InitFactories(nil)
   104  		m.Run()
   105  	}
   106  	os.Exit(0)
   107  }
   108  
   109  func TestGetDefault(t *testing.T) {
   110  	bccsp := GetDefault()
   111  	if bccsp == nil {
   112  		t.Fatal("Failed getting default BCCSP. Nil instance.")
   113  	}
   114  }
   115  
   116  func TestGetBCCSP(t *testing.T) {
   117  	bccsp, err := GetBCCSP("SW")
   118  	if err != nil {
   119  		t.Fatalf("Failed getting default BCCSP [%s]", err)
   120  	}
   121  	if bccsp == nil {
   122  		t.Fatal("Failed Software BCCSP. Nil instance.")
   123  	}
   124  }