github.com/zhouxv/fabric@v2.1.1+incompatible/msp/ouconfig_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 17 package msp 18 19 import ( 20 "path/filepath" 21 "testing" 22 23 "github.com/hyperledger/fabric/bccsp/factory" 24 "github.com/hyperledger/fabric/bccsp/sw" 25 "github.com/stretchr/testify/assert" 26 ) 27 28 func TestBadConfigOU(t *testing.T) { 29 // testdata/badconfigou: 30 // the configuration is such that only identities 31 // with OU=COP2 and signed by the root ca should be validated 32 thisMSP := getLocalMSP(t, "testdata/badconfigou") 33 34 id, err := thisMSP.GetDefaultSigningIdentity() 35 assert.NoError(t, err) 36 37 // the default signing identity OU is COP but the msp is configured 38 // to validate only identities whose OU is COP2 39 err = id.Validate() 40 assert.Error(t, err) 41 } 42 43 func TestBadConfigOUCert(t *testing.T) { 44 // testdata/badconfigoucert: 45 // the configuration of the OU identifier points to a 46 // certificate that is neither a CA nor an intermediate CA for the msp. 47 conf, err := GetLocalMspConfig("testdata/badconfigoucert", nil, "SampleOrg") 48 assert.NoError(t, err) 49 50 thisMSP, err := newBccspMsp(MSPv1_0, factory.GetDefault()) 51 assert.NoError(t, err) 52 53 err = thisMSP.Setup(conf) 54 assert.Error(t, err) 55 assert.Contains(t, err.Error(), "Failed adding OU. Certificate [") 56 assert.Contains(t, err.Error(), "] not in root or intermediate certs.") 57 } 58 59 func TestValidateIntermediateConfigOU(t *testing.T) { 60 // testdata/external: 61 // the configuration is such that only identities with 62 // OU=Hyperledger Testing and signed by the intermediate ca should be validated 63 thisMSP := getLocalMSP(t, "testdata/external") 64 65 id, err := thisMSP.GetDefaultSigningIdentity() 66 assert.NoError(t, err) 67 68 err = id.Validate() 69 assert.NoError(t, err) 70 71 conf, err := GetLocalMspConfig("testdata/external", nil, "SampleOrg") 72 assert.NoError(t, err) 73 74 cryptoProvider, err := sw.NewDefaultSecurityLevelWithKeystore(sw.NewDummyKeyStore()) 75 assert.NoError(t, err) 76 thisMSP, err = newBccspMsp(MSPv1_0, cryptoProvider) 77 assert.NoError(t, err) 78 ks, err := sw.NewFileBasedKeyStore(nil, filepath.Join("testdata/external", "keystore"), true) 79 assert.NoError(t, err) 80 csp, err := sw.NewWithParams(256, "SHA2", ks) 81 assert.NoError(t, err) 82 thisMSP.(*bccspmsp).bccsp = csp 83 84 err = thisMSP.Setup(conf) 85 assert.NoError(t, err) 86 }