github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/hyperledger/fabric-config/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 "github.com/hellobchain/third_party/hyperledger/fabric-config/protolator" 12 "io/ioutil" 13 "testing" 14 15 "github.com/golang/protobuf/proto" 16 cb "github.com/hyperledger/fabric-protos-go/common" 17 mb "github.com/hyperledger/fabric-protos-go/msp" 18 pb "github.com/hyperledger/fabric-protos-go/peer" 19 . "github.com/onsi/gomega" 20 ) 21 22 func bidirectionalMarshal(t *testing.T, doc proto.Message) { 23 gt := NewGomegaWithT(t) 24 25 var buffer bytes.Buffer 26 27 err := protolator.DeepMarshalJSON(&buffer, doc) 28 gt.Expect(err).NotTo(HaveOccurred()) 29 t.Log("ret1", buffer.String()) 30 31 newRoot := proto.Clone(doc) 32 newRoot.Reset() 33 err = protolator.DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newRoot) 34 gt.Expect(err).NotTo(HaveOccurred()) 35 36 // Note, we cannot do an equality check between newRoot and sampleDoc 37 // because of the nondeterministic nature of binary proto marshaling 38 // So instead we re-marshal to JSON which is a deterministic marshaling 39 // and compare equality there instead 40 41 var remarshaled bytes.Buffer 42 err = protolator.DeepMarshalJSON(&remarshaled, newRoot) 43 gt.Expect(err).NotTo(HaveOccurred()) 44 gt.Expect(remarshaled.String()).To(MatchJSON(buffer.String())) 45 } 46 47 func TestConfigUpdate(t *testing.T) { 48 gt := NewGomegaWithT(t) 49 50 blockBin, err := ioutil.ReadFile("testdata/block.pb") 51 gt.Expect(err).NotTo(HaveOccurred()) 52 53 block := &cb.Block{} 54 err = proto.Unmarshal(blockBin, block) 55 gt.Expect(err).NotTo(HaveOccurred()) 56 57 envelope := &cb.Envelope{} 58 err = proto.Unmarshal(block.Data.Data[0], envelope) 59 gt.Expect(err).NotTo(HaveOccurred()) 60 61 blockDataPayload := &cb.Payload{} 62 err = proto.Unmarshal(envelope.Payload, blockDataPayload) 63 gt.Expect(err).NotTo(HaveOccurred()) 64 65 config := &cb.ConfigEnvelope{} 66 err = proto.Unmarshal(blockDataPayload.Data, config) 67 gt.Expect(err).NotTo(HaveOccurred()) 68 69 bidirectionalMarshal(t, &cb.ConfigUpdateEnvelope{ 70 ConfigUpdate: protoMarshalOrPanic(&cb.ConfigUpdate{ 71 ReadSet: config.Config.ChannelGroup, 72 WriteSet: config.Config.ChannelGroup, 73 }), 74 }) 75 } 76 77 func TestIdemix(t *testing.T) { 78 bidirectionalMarshal(t, &mb.MSPConfig{ 79 Type: 1, 80 Config: protoMarshalOrPanic(&mb.IdemixMSPConfig{ 81 Name: "fooo", 82 }), 83 }) 84 } 85 86 func TestBlock(t *testing.T) { 87 gt := NewGomegaWithT(t) 88 89 blockBin, err := ioutil.ReadFile("testdata/block.pb") 90 gt.Expect(err).NotTo(HaveOccurred()) 91 92 block := &cb.Block{} 93 err = proto.Unmarshal(blockBin, block) 94 gt.Expect(err).NotTo(HaveOccurred()) 95 96 bidirectionalMarshal(t, block) 97 } 98 99 func TestEmitDefaultsBug(t *testing.T) { 100 gt := NewGomegaWithT(t) 101 102 block := &cb.Block{ 103 Header: &cb.BlockHeader{ 104 PreviousHash: []byte("foo"), 105 }, 106 Data: &cb.BlockData{ 107 Data: [][]byte{ 108 protoMarshalOrPanic(&cb.Envelope{ 109 Payload: protoMarshalOrPanic(&cb.Payload{ 110 Header: &cb.Header{ 111 ChannelHeader: protoMarshalOrPanic(&cb.ChannelHeader{ 112 Type: int32(cb.HeaderType_CONFIG), 113 }), 114 }, 115 }), 116 Signature: []byte("bar"), 117 }), 118 }, 119 }, 120 } 121 122 buf := &bytes.Buffer{} 123 err := protolator.DeepMarshalJSON(buf, block) 124 gt.Expect(err).NotTo(HaveOccurred()) 125 gt.Expect(buf.String()).To(MatchJSON(` 126 { 127 "data": { 128 "data": [ 129 { 130 "payload": { 131 "data": null, 132 "header": { 133 "channel_header": { 134 "channel_id": "", 135 "epoch": "0", 136 "extension": null, 137 "timestamp": null, 138 "tls_cert_hash": null, 139 "tx_id": "", 140 "type": 1, 141 "version": 0 142 }, 143 "signature_header": null 144 } 145 }, 146 "signature": "YmFy" 147 } 148 ] 149 }, 150 "header": { 151 "data_hash": null, 152 "number": "0", 153 "previous_hash": "Zm9v" 154 }, 155 "metadata": null 156 } 157 `)) 158 } 159 160 func TestProposalResponsePayload(t *testing.T) { 161 gt := NewGomegaWithT(t) 162 163 prp := &pb.ProposalResponsePayload{} 164 err := protolator.DeepUnmarshalJSON(bytes.NewReader([]byte(`{ 165 "extension": { 166 "chaincode_id": { 167 "name": "test", 168 "path": "", 169 "version": "1.0" 170 }, 171 "events": { 172 "chaincode_id": "test" 173 }, 174 "response": { 175 "message": "", 176 "payload": null, 177 "status": 200 178 }, 179 "results": { 180 "data_model": "KV", 181 "ns_rwset": [ 182 { 183 "collection_hashed_rwset": [], 184 "namespace": "lscc", 185 "rwset": { 186 "metadata_writes": [], 187 "range_queries_info": [], 188 "reads": [ 189 { 190 "key": "cc1", 191 "version": { 192 "block_num": "3", 193 "tx_num": "0" 194 } 195 }, 196 { 197 "key": "cc2", 198 "version": { 199 "block_num": "4", 200 "tx_num": "0" 201 } 202 } 203 ], 204 "writes": [] 205 } 206 }, 207 { 208 "collection_hashed_rwset": [], 209 "namespace": "cc1", 210 "rwset": { 211 "metadata_writes": [], 212 "range_queries_info": [], 213 "reads": [ 214 { 215 "key": "key1", 216 "version": { 217 "block_num": "8", 218 "tx_num": "0" 219 } 220 } 221 ], 222 "writes": [ 223 { 224 "is_delete": false, 225 "key": "key2" 226 } 227 ] 228 } 229 }, 230 { 231 "collection_hashed_rwset": [], 232 "namespace": "cc2", 233 "rwset": { 234 "metadata_writes": [], 235 "range_queries_info": [], 236 "reads": [ 237 { 238 "key": "key1", 239 "version": { 240 "block_num": "9", 241 "tx_num": "0" 242 } 243 }, 244 { 245 "key": "key2", 246 "version": { 247 "block_num": "10", 248 "tx_num": "0" 249 } 250 } 251 ], 252 "writes": [ 253 { 254 "is_delete": false, 255 "key": "key1" 256 }, 257 { 258 "is_delete": true, 259 "key": "key2" 260 } 261 ] 262 } 263 } 264 ] 265 } 266 } 267 }`)), prp) 268 gt.Expect(err).NotTo(HaveOccurred()) 269 bidirectionalMarshal(t, prp) 270 } 271 272 func TestChannelCreationPolicy(t *testing.T) { 273 cu := &cb.ConfigUpdate{ 274 WriteSet: &cb.ConfigGroup{ 275 Groups: map[string]*cb.ConfigGroup{ 276 "Consortiums": { 277 Groups: map[string]*cb.ConfigGroup{ 278 "SampleConsortium": { 279 Values: map[string]*cb.ConfigValue{ 280 "ChannelCreationPolicy": { 281 Version: 0, 282 }, 283 }, 284 }, 285 }, 286 }, 287 }, 288 }, 289 } 290 291 bidirectionalMarshal(t, cu) 292 } 293 294 func TestStaticMarshal(t *testing.T) { 295 gt := NewGomegaWithT(t) 296 297 // To generate artifacts: 298 // e.g. 299 // FABRICPATH=$GOPATH/src/github.com/hyperledger/fabric 300 // configtxgen -channelID test -outputBlock block.pb -profile SampleSingleMSPSolo -configPath FABRICPATH/sampleconfig 301 // configtxgen -configPath FABRICPATH/sampleconfig -inspectBlock block.pb > block.json 302 303 blockBin, err := ioutil.ReadFile("testdata/block.pb") 304 gt.Expect(err).NotTo(HaveOccurred()) 305 306 block := &cb.Block{} 307 err = proto.Unmarshal(blockBin, block) 308 gt.Expect(err).NotTo(HaveOccurred()) 309 310 jsonBin, err := ioutil.ReadFile("testdata/block.json") 311 gt.Expect(err).NotTo(HaveOccurred()) 312 313 buf := &bytes.Buffer{} 314 err = protolator.DeepMarshalJSON(buf, block) 315 gt.Expect(err).NotTo(HaveOccurred()) 316 gt.Expect(buf).To(MatchJSON(jsonBin)) 317 } 318 319 // protoMarshalOrPanic serializes a protobuf message and panics if this 320 // operation fails 321 func protoMarshalOrPanic(pb proto.Message) []byte { 322 data, err := proto.Marshal(pb) 323 if err != nil { 324 panic(err) 325 } 326 327 return data 328 }