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