@@ -7,14 +7,23 @@ SPDX-License-Identifier: Apache-2.0
7
7
package config_test
8
8
9
9
import (
10
+ "crypto/rand"
11
+ "encoding/hex"
12
+ "fmt"
13
+ "os"
14
+ "os/exec"
15
+ "path/filepath"
10
16
"testing"
11
17
12
18
"github.com/golang/protobuf/proto"
13
19
"github.com/hyperledger/fabric/common/channelconfig"
14
20
"github.com/hyperledger/fabric/common/configtx/test"
21
+ "github.com/hyperledger/fabric/common/tools/configtxgen/encoder"
22
+ genesisconfig "github.com/hyperledger/fabric/common/tools/configtxgen/localconfig"
15
23
"github.com/hyperledger/fabric/discovery/support/config"
16
24
"github.com/hyperledger/fabric/discovery/support/mocks"
17
25
"github.com/hyperledger/fabric/protos/common"
26
+ "github.com/onsi/gomega/gexec"
18
27
"github.com/stretchr/testify/assert"
19
28
)
20
29
@@ -46,6 +55,83 @@ func blockWithConfigEnvelope() *common.Block {
46
55
}
47
56
}
48
57
58
+ func TestMSPIDMapping (t * testing.T ) {
59
+ randString := func () string {
60
+ buff := make ([]byte , 10 )
61
+ rand .Read (buff )
62
+ return hex .EncodeToString (buff )
63
+ }
64
+
65
+ dir := filepath .Join (os .TempDir (), fmt .Sprintf ("TestMSPIDMapping_%s" , randString ()))
66
+ os .Mkdir (dir , 0700 )
67
+ defer os .RemoveAll (dir )
68
+
69
+ cryptogen , err := gexec .Build (filepath .Join ("github.com" , "hyperledger" , "fabric" , "common" , "tools" , "cryptogen" ))
70
+ assert .NoError (t , err )
71
+ defer os .Remove (cryptogen )
72
+
73
+ idemixgen , err := gexec .Build (filepath .Join ("github.com" , "hyperledger" , "fabric" , "common" , "tools" , "idemixgen" ))
74
+ assert .NoError (t , err )
75
+ defer os .Remove (idemixgen )
76
+
77
+ cryptoConfigDir := filepath .Join (dir , "crypto-config" )
78
+ b , err := exec .Command (cryptogen , "generate" , fmt .Sprintf ("--output=%s" , cryptoConfigDir )).CombinedOutput ()
79
+ assert .NoError (t , err , string (b ))
80
+
81
+ idemixConfigDir := filepath .Join (dir , "crypto-config" , "idemix" )
82
+ b , err = exec .Command (idemixgen , "ca-keygen" , fmt .Sprintf ("--output=%s" , idemixConfigDir )).CombinedOutput ()
83
+ assert .NoError (t , err , string (b ))
84
+
85
+ profileConfig := genesisconfig .Load ("TwoOrgsChannel" , "../../../examples/e2e_cli/" )
86
+ ordererConfig := genesisconfig .Load ("TwoOrgsOrdererGenesis" , "../../../examples/e2e_cli/" )
87
+ profileConfig .Orderer = ordererConfig .Orderer
88
+
89
+ // Override the MSP directory with our randomly generated and populated path
90
+ for _ , org := range ordererConfig .Orderer .Organizations {
91
+ org .MSPDir = filepath .Join (cryptoConfigDir , "ordererOrganizations" , "example.com" , "msp" )
92
+ org .Name = randString ()
93
+ }
94
+
95
+ // Randomize organization names
96
+ for _ , org := range profileConfig .Application .Organizations {
97
+ org .Name = randString ()
98
+ // Non bccsp-msp orgs don't have the crypto material produced by cryptogen,
99
+ // we need to use the idemix crypto folder instead.
100
+ if org .MSPType != "bccsp" {
101
+ org .MSPDir = filepath .Join (idemixConfigDir )
102
+ continue
103
+ }
104
+ org .MSPDir = filepath .Join (cryptoConfigDir , "peerOrganizations" , "org1.example.com" , "msp" )
105
+ }
106
+
107
+ gen := encoder .New (profileConfig )
108
+ block := gen .GenesisBlockForChannel ("mychannel" )
109
+
110
+ fakeBlockGetter := & mocks.ConfigBlockGetter {}
111
+ fakeBlockGetter .GetCurrConfigBlockReturnsOnCall (0 , block )
112
+
113
+ cs := config .NewDiscoverySupport (fakeBlockGetter )
114
+ res , err := cs .Config ("mychannel" )
115
+
116
+ actualKeys := make (map [string ]struct {})
117
+ for key := range res .Orderers {
118
+ actualKeys [key ] = struct {}{}
119
+ }
120
+
121
+ for key := range res .Msps {
122
+ actualKeys [key ] = struct {}{}
123
+ }
124
+
125
+ // Note that Org3MSP is an idemix org, but it shouldn't be listed here
126
+ // because peers can't have idemix credentials
127
+ expected := map [string ]struct {}{
128
+ "OrdererMSP" : {},
129
+ "Org1MSP" : {},
130
+ "Org2MSP" : {},
131
+ }
132
+ assert .Equal (t , expected , actualKeys )
133
+ }
134
+
49
135
func TestSupportGreenPath (t * testing.T ) {
50
136
fakeBlockGetter := & mocks.ConfigBlockGetter {}
51
137
fakeBlockGetter .GetCurrConfigBlockReturnsOnCall (0 , nil )
0 commit comments