github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/common/tools/protolator/blackbox_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 protolator_test 18 19 import ( 20 "bytes" 21 "testing" 22 23 genesisconfig "github.com/hyperledger/fabric/common/configtx/tool/localconfig" 24 "github.com/hyperledger/fabric/common/configtx/tool/provisional" 25 . "github.com/hyperledger/fabric/common/tools/protolator" 26 27 "github.com/golang/protobuf/proto" 28 "github.com/stretchr/testify/assert" 29 ) 30 31 func bidirectionalMarshal(t *testing.T, doc proto.Message) { 32 var buffer bytes.Buffer 33 34 assert.NoError(t, DeepMarshalJSON(&buffer, doc)) 35 36 newRoot := proto.Clone(doc) 37 newRoot.Reset() 38 assert.NoError(t, DeepUnmarshalJSON(bytes.NewReader(buffer.Bytes()), newRoot)) 39 40 // Note, we cannot do an equality check between newRoot and sampleDoc 41 // because of the nondeterministic nature of binary proto marshaling 42 // So instead we re-marshal to JSON which is a deterministic marshaling 43 // and compare equality there instead 44 45 //t.Log(doc) 46 //t.Log(newRoot) 47 48 var remarshaled bytes.Buffer 49 assert.NoError(t, DeepMarshalJSON(&remarshaled, newRoot)) 50 assert.Equal(t, string(buffer.Bytes()), string(remarshaled.Bytes())) 51 //t.Log(string(buffer.Bytes())) 52 //t.Log(string(remarshaled.Bytes())) 53 } 54 55 func TestConfigUpdate(t *testing.T) { 56 p := provisional.New(genesisconfig.Load(genesisconfig.SampleSingleMSPSoloProfile)) 57 ct := p.ChannelTemplate() 58 cue, err := ct.Envelope("Foo") 59 assert.NoError(t, err) 60 61 bidirectionalMarshal(t, cue) 62 } 63 64 func TestGenesisBlock(t *testing.T) { 65 p := provisional.New(genesisconfig.Load(genesisconfig.SampleSingleMSPSoloProfile)) 66 gb := p.GenesisBlockForChannel("foo") 67 68 bidirectionalMarshal(t, gb) 69 70 }