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