github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/orderer/common/bootstrap/file/bootstrap_test.go (about) 1 /* 2 Copyright IBM Corp. 2016 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 file 18 19 import ( 20 "bytes" 21 "os" 22 "testing" 23 24 "github.com/golang/protobuf/proto" 25 cb "github.com/hyperledger/fabric/protos/common" 26 ) 27 28 const file = "./abc" 29 30 func TestNoFile(t *testing.T) { 31 defer func() { 32 if r := recover(); r == nil { 33 t.Errorf("TestNoFile should have panicked") 34 } 35 }() 36 37 helper := New(file) 38 _ = helper.GenesisBlock() 39 40 } // TestNoFile 41 42 func TestBadBlock(t *testing.T) { 43 defer func() { 44 if r := recover(); r == nil { 45 t.Errorf("TestBadBlock should have panicked") 46 } 47 }() 48 49 testFile, _ := os.Create(file) 50 defer os.Remove(file) 51 testFile.Write([]byte("abc")) 52 testFile.Close() 53 helper := New(file) 54 _ = helper.GenesisBlock() 55 } // TestBadBlock 56 57 func TestGenesisBlock(t *testing.T) { 58 defer func() { 59 if r := recover(); r != nil { 60 t.Errorf("TestGenesisBlock: unexpected panic") 61 } 62 }() 63 64 header := &cb.BlockHeader{ 65 Number: 0, 66 PreviousHash: nil, 67 DataHash: []byte("abc"), 68 } 69 data := &cb.BlockData{ 70 Data: [][]byte{[]byte("abc")}, 71 } 72 metadata := &cb.BlockMetadata{ 73 Metadata: [][]byte{[]byte("abc")}, 74 } 75 block := &cb.Block{ 76 Header: header, 77 Data: data, 78 Metadata: metadata, 79 } 80 marshalledBlock, _ := proto.Marshal(block) 81 82 testFile, _ := os.Create(file) 83 defer os.Remove(file) 84 testFile.Write(marshalledBlock) 85 testFile.Close() 86 87 helper := New(file) 88 outBlock := helper.GenesisBlock() 89 90 outHeader := outBlock.Header 91 if outHeader.Number != 0 || outHeader.PreviousHash != nil || !bytes.Equal(outHeader.DataHash, []byte("abc")) { 92 t.Errorf("block header not read correctly. Got %+v\n . Should have been %+v\n", outHeader, header) 93 } 94 outData := outBlock.Data 95 if len(outData.Data) != 1 && !bytes.Equal(outData.Data[0], []byte("abc")) { 96 t.Errorf("block data not read correctly. Got %+v\n . Should have been %+v\n", outData, data) 97 } 98 outMeta := outBlock.Metadata 99 if len(outMeta.Metadata) != 1 && !bytes.Equal(outMeta.Metadata[0], []byte("abc")) { 100 t.Errorf("Metadata data not read correctly. Got %+v\n . Should have been %+v\n", outMeta, metadata) 101 } 102 } // TestGenesisBlock