github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/common/tools/cryptogen/msp/msp_test.go (about) 1 /* 2 Copyright IBM Corp. 2017 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 msp_test 17 18 import ( 19 "os" 20 "path/filepath" 21 "testing" 22 23 "github.com/hyperledger/fabric/common/tools/cryptogen/ca" 24 "github.com/hyperledger/fabric/common/tools/cryptogen/msp" 25 fabricmsp "github.com/hyperledger/fabric/msp" 26 "github.com/stretchr/testify/assert" 27 ) 28 29 const ( 30 testCAName = "root0" 31 testName = "peer0" 32 ) 33 34 var testDir = filepath.Join(os.TempDir(), "msp-test") 35 36 func TestGenerateLocalMSP(t *testing.T) { 37 38 cleanup(testDir) 39 40 err := msp.GenerateLocalMSP(testDir, testName, &ca.CA{}) 41 assert.Error(t, err, "Empty CA should have failed") 42 43 caDir := filepath.Join(testDir, "ca") 44 mspDir := filepath.Join(testDir, "msp") 45 rootCA, err := ca.NewCA(caDir, testCAName) 46 assert.NoError(t, err, "Error generating CA") 47 err = msp.GenerateLocalMSP(mspDir, testName, rootCA) 48 assert.NoError(t, err, "Failed to generate local MSP") 49 50 // check to see that the right files were generated/saved 51 files := []string{ 52 filepath.Join(mspDir, "admincerts", testCAName+"-cert.pem"), 53 filepath.Join(mspDir, "cacerts", testCAName+"-cert.pem"), 54 filepath.Join(mspDir, "keystore"), 55 filepath.Join(mspDir, "signcerts", testName+"-cert.pem"), 56 } 57 58 for _, file := range files { 59 assert.Equal(t, true, checkForFile(file), 60 "Expected to find file "+file) 61 } 62 63 // finally check to see if we can load this as a local MSP config 64 testMSPConfig, err := fabricmsp.GetLocalMspConfig(mspDir, nil, testName) 65 assert.NoError(t, err, "Error parsing local MSP config") 66 testMSP, err := fabricmsp.NewBccspMsp() 67 assert.NoError(t, err, "Error creating new BCCSP MSP") 68 err = testMSP.Setup(testMSPConfig) 69 assert.NoError(t, err, "Error setting up local MSP") 70 71 rootCA.Name = "test/fail" 72 err = msp.GenerateLocalMSP(testDir, testName, rootCA) 73 assert.Error(t, err, "Should have failed with CA name 'test/fail'") 74 t.Log(err) 75 cleanup(testDir) 76 77 } 78 79 func TestGenerateVerifyingMSP(t *testing.T) { 80 81 caDir := filepath.Join(testDir, "ca") 82 mspDir := filepath.Join(testDir, "msp") 83 rootCA, err := ca.NewCA(caDir, testCAName) 84 assert.NoError(t, err, "Failed to create new CA") 85 86 err = msp.GenerateVerifyingMSP(mspDir, rootCA) 87 assert.NoError(t, err, "Failed to generate verifying MSP") 88 89 // check to see that the right files were generated/saved 90 files := []string{ 91 filepath.Join(mspDir, "admincerts", testCAName+"-cert.pem"), 92 filepath.Join(mspDir, "cacerts", testCAName+"-cert.pem"), 93 filepath.Join(mspDir, "signcerts", testCAName+"-cert.pem"), 94 } 95 96 for _, file := range files { 97 assert.Equal(t, true, checkForFile(file), 98 "Expected to find file "+file) 99 } 100 // finally check to see if we can load this as a verifying MSP config 101 testMSPConfig, err := fabricmsp.GetVerifyingMspConfig(mspDir, nil, testName) 102 assert.NoError(t, err, "Error parsing verifying MSP config") 103 testMSP, err := fabricmsp.NewBccspMsp() 104 assert.NoError(t, err, "Error creating new BCCSP MSP") 105 err = testMSP.Setup(testMSPConfig) 106 assert.NoError(t, err, "Error setting up verifying MSP") 107 108 rootCA.Name = "test/fail" 109 err = msp.GenerateVerifyingMSP(mspDir, rootCA) 110 assert.Error(t, err, "Should have failed with CA name 'test/fail'") 111 t.Log(err) 112 cleanup(testDir) 113 } 114 115 func cleanup(dir string) { 116 os.RemoveAll(dir) 117 } 118 119 func checkForFile(file string) bool { 120 if _, err := os.Stat(file); os.IsNotExist(err) { 121 return false 122 } 123 return true 124 }