github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/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/spf13/viper"
    27  )
    28  
    29  func TestMain(m *testing.M) {
    30  	flag.Parse()
    31  	lib, pin, label, enable := findPKCS11Lib()
    32  
    33  	var jsonBCCSP, yamlBCCSP *FactoryOpts
    34  	jsonCFG := []byte(
    35  		`{ "default": "SW", "SW":{ "security": 384, "hash": "SHA3" } }`)
    36  
    37  	err := json.Unmarshal(jsonCFG, &jsonBCCSP)
    38  	if err != nil {
    39  		fmt.Printf("Could not parse JSON config [%s]", err)
    40  		os.Exit(-1)
    41  	}
    42  
    43  	yamlCFG := fmt.Sprintf(`
    44  BCCSP:
    45      default: PKCS11
    46      SW:
    47          Hash: SHA3
    48          Security: 256
    49      PKCS11:
    50          Hash: SHA3
    51          Security: 256
    52          
    53          Library: %s
    54          Pin:     '%s'
    55          Label:   %s
    56          `, lib, pin, label)
    57  
    58  	if !enable {
    59  		fmt.Printf("Could not find PKCS11 libraries, running without\n")
    60  		yamlCFG = `
    61  BCCSP:
    62      default: SW
    63      SW:
    64          Hash: SHA3
    65          Security: 256`
    66  	}
    67  
    68  	viper.SetConfigType("yaml")
    69  	err = viper.ReadConfig(bytes.NewBuffer([]byte(yamlCFG)))
    70  	if err != nil {
    71  		fmt.Printf("Could not read YAML config [%s]", err)
    72  		os.Exit(-1)
    73  	}
    74  
    75  	err = viper.UnmarshalKey("bccsp", &yamlBCCSP)
    76  	if err != nil {
    77  		fmt.Printf("Could not parse YAML config [%s]", err)
    78  		os.Exit(-1)
    79  	}
    80  
    81  	cfgVariations := []*FactoryOpts{
    82  		{
    83  			ProviderName: "SW",
    84  			SwOpts: &SwOpts{
    85  				HashFamily: "SHA2",
    86  				SecLevel:   256,
    87  
    88  				Ephemeral: true,
    89  			},
    90  		},
    91  		{},
    92  		{
    93  			ProviderName: "SW",
    94  		},
    95  		jsonBCCSP,
    96  		yamlBCCSP,
    97  	}
    98  
    99  	for index, config := range cfgVariations {
   100  		fmt.Printf("Trying configuration [%d]\n", index)
   101  		InitFactories(config)
   102  		InitFactories(nil)
   103  		m.Run()
   104  	}
   105  	os.Exit(0)
   106  }
   107  
   108  func TestGetDefault(t *testing.T) {
   109  	bccsp := GetDefault()
   110  	if bccsp == nil {
   111  		t.Fatal("Failed getting default BCCSP. Nil instance.")
   112  	}
   113  }
   114  
   115  func TestGetBCCSP(t *testing.T) {
   116  	bccsp, err := GetBCCSP("SW")
   117  	if err != nil {
   118  		t.Fatalf("Failed getting default BCCSP [%s]", err)
   119  	}
   120  	if bccsp == nil {
   121  		t.Fatal("Failed Software BCCSP. Nil instance.")
   122  	}
   123  }
   124  
   125  func findPKCS11Lib() (lib, pin, label string, enablePKCS11tests bool) {
   126  	//FIXME: Till we workout the configuration piece, look for the libraries in the familiar places
   127  	lib = os.Getenv("PKCS11_LIB")
   128  	if lib == "" {
   129  		pin = "98765432"
   130  		label = "ForFabric"
   131  		possibilities := []string{
   132  			"/usr/lib/softhsm/libsofthsm2.so",                            //Debian
   133  			"/usr/lib/x86_64-linux-gnu/softhsm/libsofthsm2.so",           //Ubuntu
   134  			"/usr/lib/s390x-linux-gnu/softhsm/libsofthsm2.so",            //Ubuntu
   135  			"/usr/lib/powerpc64le-linux-gnu/softhsm/libsofthsm2.so",      //Power
   136  			"/usr/local/Cellar/softhsm/2.1.0/lib/softhsm/libsofthsm2.so", //MacOS
   137  		}
   138  		for _, path := range possibilities {
   139  			if _, err := os.Stat(path); !os.IsNotExist(err) {
   140  				lib = path
   141  				enablePKCS11tests = true
   142  				break
   143  			}
   144  		}
   145  		if lib == "" {
   146  			enablePKCS11tests = false
   147  		}
   148  	} else {
   149  		enablePKCS11tests = true
   150  		pin = os.Getenv("PKCS11_PIN")
   151  		label = os.Getenv("PKCS11_LABEL")
   152  	}
   153  	return lib, pin, label, enablePKCS11tests
   154  }