github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/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{}, &ca.CA{})
    42  	assert.Error(t, err, "Empty CA should have failed")
    43  
    44  	caDir := filepath.Join(testDir, "ca")
    45  	tlsCADir := filepath.Join(testDir, "tlsca")
    46  	mspDir := filepath.Join(testDir, "msp")
    47  
    48  	// generate signing CA
    49  	signCA, err := ca.NewCA(caDir, testCAOrg, testCAName)
    50  	assert.NoError(t, err, "Error generating CA")
    51  	// generate TLS CA
    52  	tlsCA, err := ca.NewCA(tlsCADir, testCAOrg, testCAName)
    53  	assert.NoError(t, err, "Error generating CA")
    54  	// generate local MSP
    55  	err = msp.GenerateLocalMSP(testDir, testName, nil, signCA, tlsCA)
    56  	assert.NoError(t, err, "Failed to generate local MSP")
    57  
    58  	// check to see that the right files were generated/saved
    59  	files := []string{
    60  		filepath.Join(mspDir, "admincerts", testName+"-cert.pem"),
    61  		filepath.Join(mspDir, "cacerts", testCAName+"-cert.pem"),
    62  		filepath.Join(mspDir, "tlscacerts", testCAName+"-cert.pem"),
    63  		filepath.Join(mspDir, "keystore"),
    64  		filepath.Join(mspDir, "signcerts", testName+"-cert.pem"),
    65  	}
    66  
    67  	for _, file := range files {
    68  		assert.Equal(t, true, checkForFile(file),
    69  			"Expected to find file "+file)
    70  	}
    71  
    72  	// finally check to see if we can load this as a local MSP config
    73  	testMSPConfig, err := fabricmsp.GetLocalMspConfig(mspDir, nil, testName)
    74  	assert.NoError(t, err, "Error parsing local MSP config")
    75  	testMSP, err := fabricmsp.NewBccspMsp()
    76  	assert.NoError(t, err, "Error creating new BCCSP MSP")
    77  	err = testMSP.Setup(testMSPConfig)
    78  	assert.NoError(t, err, "Error setting up local MSP")
    79  
    80  	tlsCA.Name = "test/fail"
    81  	err = msp.GenerateLocalMSP(testDir, testName, nil, signCA, tlsCA)
    82  	assert.Error(t, err, "Should have failed with CA name 'test/fail'")
    83  	signCA.Name = "test/fail"
    84  	err = msp.GenerateLocalMSP(testDir, testName, nil, signCA, tlsCA)
    85  	assert.Error(t, err, "Should have failed with CA name 'test/fail'")
    86  	t.Log(err)
    87  	cleanup(testDir)
    88  
    89  }
    90  
    91  func TestGenerateVerifyingMSP(t *testing.T) {
    92  
    93  	caDir := filepath.Join(testDir, "ca")
    94  	tlsCADir := filepath.Join(testDir, "tlsca")
    95  	mspDir := filepath.Join(testDir, "msp")
    96  	// generate signing CA
    97  	signCA, err := ca.NewCA(caDir, testCAOrg, testCAName)
    98  	assert.NoError(t, err, "Error generating CA")
    99  	// generate TLS CA
   100  	tlsCA, err := ca.NewCA(tlsCADir, testCAOrg, testCAName)
   101  	assert.NoError(t, err, "Error generating CA")
   102  
   103  	err = msp.GenerateVerifyingMSP(mspDir, signCA, tlsCA)
   104  	assert.NoError(t, err, "Failed to generate verifying MSP")
   105  
   106  	// check to see that the right files were generated/saved
   107  	files := []string{
   108  		filepath.Join(mspDir, "admincerts", testCAName+"-cert.pem"),
   109  		filepath.Join(mspDir, "cacerts", testCAName+"-cert.pem"),
   110  		filepath.Join(mspDir, "tlscacerts", testCAName+"-cert.pem"),
   111  	}
   112  
   113  	for _, file := range files {
   114  		assert.Equal(t, true, checkForFile(file),
   115  			"Expected to find file "+file)
   116  	}
   117  	// finally check to see if we can load this as a verifying MSP config
   118  	testMSPConfig, err := fabricmsp.GetVerifyingMspConfig(mspDir, testName)
   119  	assert.NoError(t, err, "Error parsing verifying MSP config")
   120  	testMSP, err := fabricmsp.NewBccspMsp()
   121  	assert.NoError(t, err, "Error creating new BCCSP MSP")
   122  	err = testMSP.Setup(testMSPConfig)
   123  	assert.NoError(t, err, "Error setting up verifying MSP")
   124  
   125  	tlsCA.Name = "test/fail"
   126  	err = msp.GenerateVerifyingMSP(mspDir, signCA, tlsCA)
   127  	assert.Error(t, err, "Should have failed with CA name 'test/fail'")
   128  	signCA.Name = "test/fail"
   129  	err = msp.GenerateVerifyingMSP(mspDir, signCA, tlsCA)
   130  	assert.Error(t, err, "Should have failed with CA name 'test/fail'")
   131  	t.Log(err)
   132  	cleanup(testDir)
   133  }
   134  
   135  func cleanup(dir string) {
   136  	os.RemoveAll(dir)
   137  }
   138  
   139  func checkForFile(file string) bool {
   140  	if _, err := os.Stat(file); os.IsNotExist(err) {
   141  		return false
   142  	}
   143  	return true
   144  }