github.com/defanghe/fabric@v2.1.1+incompatible/common/tools/protolator/integration/integration_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package integration 8 9 import ( 10 "bytes" 11 "io/ioutil" 12 "os" 13 "testing" 14 15 "github.com/golang/protobuf/proto" 16 cb "github.com/hyperledger/fabric-protos-go/common" 17 "github.com/hyperledger/fabric-protos-go/msp" 18 pb "github.com/hyperledger/fabric-protos-go/peer" 19 "github.com/hyperledger/fabric/common/tools/protolator" 20 "github.com/hyperledger/fabric/core/config/configtest" 21 "github.com/hyperledger/fabric/internal/configtxgen/encoder" 22 "github.com/hyperledger/fabric/internal/configtxgen/genesisconfig" 23 "github.com/hyperledger/fabric/protoutil" 24 . "github.com/onsi/gomega" 25 "github.com/stretchr/testify/assert" 26 "github.com/stretchr/testify/require" 27 ) 28 29 func bidirectionalMarshal(t *testing.T, doc proto.Message) { 30 var buffer bytes.Buffer 31 32 assert.NoError(t, protolator.DeepMarshalJSON(&buffer, doc)) 33 34 newRoot := proto.Clone(doc) 35 newRoot.Reset() 36 assert.NoError(t, protolator.DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newRoot)) 37 38 // Note, we cannot do an equality check between newRoot and sampleDoc 39 // because of the nondeterministic nature of binary proto marshaling 40 // So instead we re-marshal to JSON which is a deterministic marshaling 41 // and compare equality there instead 42 43 //t.Log(doc) 44 //t.Log(newRoot) 45 46 var remarshaled bytes.Buffer 47 assert.NoError(t, protolator.DeepMarshalJSON(&remarshaled, newRoot)) 48 assert.Equal(t, buffer.String(), remarshaled.String()) 49 //t.Log(buffer.String()) 50 //t.Log(remarshaled.String()) 51 } 52 53 func TestConfigUpdate(t *testing.T) { 54 cg, err := encoder.NewChannelGroup(genesisconfig.Load(genesisconfig.SampleSingleMSPSoloProfile, configtest.GetDevConfigDir())) 55 assert.NoError(t, err) 56 57 bidirectionalMarshal(t, &cb.ConfigUpdateEnvelope{ 58 ConfigUpdate: protoutil.MarshalOrPanic(&cb.ConfigUpdate{ 59 ReadSet: cg, 60 WriteSet: cg, 61 }), 62 }) 63 } 64 65 func TestIdemix(t *testing.T) { 66 bidirectionalMarshal(t, &msp.MSPConfig{ 67 Type: 1, 68 Config: protoutil.MarshalOrPanic(&msp.IdemixMSPConfig{ 69 Name: "fooo", 70 }), 71 }) 72 } 73 74 func TestGenesisBlock(t *testing.T) { 75 p := encoder.New(genesisconfig.Load(genesisconfig.SampleSingleMSPSoloProfile, configtest.GetDevConfigDir())) 76 gb := p.GenesisBlockForChannel("foo") 77 78 bidirectionalMarshal(t, gb) 79 } 80 81 func TestEmitDefaultsBug(t *testing.T) { 82 block := &cb.Block{ 83 Header: &cb.BlockHeader{ 84 PreviousHash: []byte("foo"), 85 }, 86 Data: &cb.BlockData{ 87 Data: [][]byte{ 88 protoutil.MarshalOrPanic(&cb.Envelope{ 89 Payload: protoutil.MarshalOrPanic(&cb.Payload{ 90 Header: &cb.Header{ 91 ChannelHeader: protoutil.MarshalOrPanic(&cb.ChannelHeader{ 92 Type: int32(cb.HeaderType_CONFIG), 93 }), 94 }, 95 }), 96 Signature: []byte("bar"), 97 }), 98 }, 99 }, 100 } 101 102 err := protolator.DeepMarshalJSON(os.Stdout, block) 103 assert.NoError(t, err) 104 } 105 106 func TestProposalResponsePayload(t *testing.T) { 107 prp := &pb.ProposalResponsePayload{} 108 assert.NoError(t, protolator.DeepUnmarshalJSON(bytes.NewReader([]byte(`{ 109 "extension": { 110 "chaincode_id": { 111 "name": "test", 112 "path": "", 113 "version": "1.0" 114 }, 115 "events": { 116 "chaincode_id": "test" 117 }, 118 "response": { 119 "message": "", 120 "payload": null, 121 "status": 200 122 }, 123 "results": { 124 "data_model": "KV", 125 "ns_rwset": [ 126 { 127 "collection_hashed_rwset": [], 128 "namespace": "lscc", 129 "rwset": { 130 "metadata_writes": [], 131 "range_queries_info": [], 132 "reads": [ 133 { 134 "key": "cc1", 135 "version": { 136 "block_num": "3", 137 "tx_num": "0" 138 } 139 }, 140 { 141 "key": "cc2", 142 "version": { 143 "block_num": "4", 144 "tx_num": "0" 145 } 146 } 147 ], 148 "writes": [] 149 } 150 }, 151 { 152 "collection_hashed_rwset": [], 153 "namespace": "cc1", 154 "rwset": { 155 "metadata_writes": [], 156 "range_queries_info": [], 157 "reads": [ 158 { 159 "key": "key1", 160 "version": { 161 "block_num": "8", 162 "tx_num": "0" 163 } 164 } 165 ], 166 "writes": [ 167 { 168 "is_delete": false, 169 "key": "key2" 170 } 171 ] 172 } 173 }, 174 { 175 "collection_hashed_rwset": [], 176 "namespace": "cc2", 177 "rwset": { 178 "metadata_writes": [], 179 "range_queries_info": [], 180 "reads": [ 181 { 182 "key": "key1", 183 "version": { 184 "block_num": "9", 185 "tx_num": "0" 186 } 187 }, 188 { 189 "key": "key2", 190 "version": { 191 "block_num": "10", 192 "tx_num": "0" 193 } 194 } 195 ], 196 "writes": [ 197 { 198 "is_delete": false, 199 "key": "key1" 200 }, 201 { 202 "is_delete": true, 203 "key": "key2" 204 } 205 ] 206 } 207 } 208 ] 209 } 210 } 211 }`)), prp)) 212 bidirectionalMarshal(t, prp) 213 } 214 215 func TestChannelCreationPolicy(t *testing.T) { 216 cu := &cb.ConfigUpdate{ 217 WriteSet: &cb.ConfigGroup{ 218 Groups: map[string]*cb.ConfigGroup{ 219 "Consortiums": { 220 Groups: map[string]*cb.ConfigGroup{ 221 "SampleConsortium": { 222 Values: map[string]*cb.ConfigValue{ 223 "ChannelCreationPolicy": { 224 Version: 0, 225 }, 226 }, 227 }, 228 }, 229 }, 230 }, 231 }, 232 } 233 234 bidirectionalMarshal(t, cu) 235 } 236 237 func TestStaticMarshal(t *testing.T) { 238 // To generate artifacts: 239 // e.g. 240 // FABRICPATH=$GOPATH/src/github.com/hyperledger/fabric 241 // configtxgen -channelID test -outputBlock block.pb -profile SampleSingleMSPSolo -configPath FABRICPATH/sampleconfig 242 // configtxgen -configPath FABRICPATH/sampleconfig -inspectBlock block.pb > block.json 243 244 blockBin, err := ioutil.ReadFile("testdata/block.pb") 245 require.NoError(t, err) 246 247 block := &cb.Block{} 248 err = proto.Unmarshal(blockBin, block) 249 require.NoError(t, err) 250 251 jsonBin, err := ioutil.ReadFile("testdata/block.json") 252 require.NoError(t, err) 253 254 buf := &bytes.Buffer{} 255 require.NoError(t, protolator.DeepMarshalJSON(buf, block)) 256 257 gt := NewGomegaWithT(t) 258 gt.Expect(buf).To(MatchJSON(jsonBin)) 259 }