Skip to content

Commit aedd498

Browse files
committed
[FAB-3915] Cleanup/improve coverage for protos/utils
Increased test coverage to 90%. A few minor cleanups, fixed some error handling based on tests and removed a bit of dead code. There's more that can be done here in terms of cleanup / refactor, but limited the scope to increasing UT coverage Change-Id: Iee2ae5b2f7628772f7e3c9edbd8ce096779afb97 Signed-off-by: Gari Singh <[email protected]>
1 parent e485b21 commit aedd498

6 files changed

+1038
-201
lines changed

protos/utils/blockutils_test.go

+145-31
Original file line numberDiff line numberDiff line change
@@ -20,67 +20,181 @@ package utils_test
2020
import (
2121
"testing"
2222

23+
"github.com/golang/protobuf/proto"
2324
configtxtest "github.com/hyperledger/fabric/common/configtx/test"
2425
"github.com/hyperledger/fabric/protos/common"
2526
cb "github.com/hyperledger/fabric/protos/common"
2627
"github.com/hyperledger/fabric/protos/utils"
28+
"github.com/stretchr/testify/assert"
2729
)
2830

31+
var testChainID = "myuniquetestchainid"
32+
33+
func TestGetChainIDFromBlockBytes(t *testing.T) {
34+
gb, err := configtxtest.MakeGenesisBlock(testChainID)
35+
assert.NoError(t, err, "Failed to create test configuration block")
36+
bytes, err := proto.Marshal(gb)
37+
cid, err := utils.GetChainIDFromBlockBytes(bytes)
38+
assert.NoError(t, err)
39+
assert.Equal(t, testChainID, cid, "Failed to return expected chain ID")
40+
41+
// bad block bytes
42+
_, err = utils.GetChainIDFromBlockBytes([]byte("bad block"))
43+
assert.Error(t, err, "Expected error with malformed block bytes")
44+
}
45+
2946
func TestGetChainIDFromBlock(t *testing.T) {
3047
var err error
3148
var gb *common.Block
3249
var cid string
3350

34-
testChainID := "myuniquetestchainid"
35-
3651
gb, err = configtxtest.MakeGenesisBlock(testChainID)
37-
if err != nil {
38-
t.Fatalf("failed to create test configuration block: %s", err)
39-
}
52+
assert.NoError(t, err, "Failed to create test configuration block")
53+
4054
cid, err = utils.GetChainIDFromBlock(gb)
41-
if err != nil {
42-
t.Fatalf("failed to get chain ID from block: %s", err)
43-
}
44-
if testChainID != cid {
45-
t.Fatalf("failed with wrong chain ID: Actual=%s; Got=%s", testChainID, cid)
46-
}
55+
assert.NoError(t, err, "Failed to get chain ID from block")
56+
assert.Equal(t, testChainID, cid, "Failed to return expected chain ID")
4757

58+
// missing data
4859
badBlock := gb
4960
badBlock.Data = nil
5061
_, err = utils.GetChainIDFromBlock(badBlock)
51-
// We should get error
52-
if err == nil {
53-
t.Fatalf("error is expected -- the block must not be marshallable")
62+
assert.Error(t, err, "Expected error with missing block data")
63+
64+
// no envelope
65+
badBlock = &cb.Block{
66+
Data: &cb.BlockData{
67+
Data: [][]byte{[]byte("bad envelope")},
68+
},
69+
}
70+
_, err = utils.GetChainIDFromBlock(badBlock)
71+
assert.Error(t, err, "Expected error with no envelope in data")
72+
73+
// bad payload
74+
env, _ := proto.Marshal(&cb.Envelope{
75+
Payload: []byte("bad payload"),
76+
})
77+
badBlock = &cb.Block{
78+
Data: &cb.BlockData{
79+
Data: [][]byte{env},
80+
},
5481
}
82+
_, err = utils.GetChainIDFromBlock(badBlock)
83+
assert.Error(t, err, "Expected error - malformed payload")
84+
85+
// bad channel header
86+
payload, _ := proto.Marshal(&cb.Payload{
87+
Header: &cb.Header{
88+
ChannelHeader: []byte("bad header"),
89+
},
90+
})
91+
env, _ = proto.Marshal(&cb.Envelope{
92+
Payload: payload,
93+
})
94+
badBlock = &cb.Block{
95+
Data: &cb.BlockData{
96+
Data: [][]byte{env},
97+
},
98+
}
99+
_, err = utils.GetChainIDFromBlock(badBlock)
100+
assert.Error(t, err, "Expected error with malformed channel header")
55101
}
56102

57103
func TestGetBlockFromBlockBytes(t *testing.T) {
58104
testChainID := "myuniquetestchainid"
59105
gb, err := configtxtest.MakeGenesisBlock(testChainID)
60-
if err != nil {
61-
t.Fatalf("failed to create test configuration block: %s", err)
62-
}
106+
assert.NoError(t, err, "Failed to create test configuration block")
63107
blockBytes, err := utils.Marshal(gb)
64-
if err != nil {
65-
t.Fatalf("failed to marshal block: %s", err)
66-
}
108+
assert.NoError(t, err, "Failed to marshal block")
67109
_, err = utils.GetBlockFromBlockBytes(blockBytes)
68-
if err != nil {
69-
t.Fatalf("failed to get block from block bytes: %s", err)
70-
}
110+
assert.NoError(t, err, "to get block from block bytes")
111+
112+
// bad block bytes
113+
_, err = utils.GetBlockFromBlockBytes([]byte("bad block"))
114+
assert.Error(t, err, "Expected error for malformed block bytes")
71115
}
72116

73117
func TestGetMetadataFromNewBlock(t *testing.T) {
74118
block := common.NewBlock(0, nil)
75119
md, err := utils.GetMetadataFromBlock(block, cb.BlockMetadataIndex_ORDERER)
76-
if err != nil {
77-
t.Fatal("Expected no error when extracting metadata from new block")
78-
}
79-
if md.Value != nil {
80-
t.Fatal("Expected metadata field value to be nil, got", md.Value)
81-
}
82-
if len(md.Value) > 0 {
83-
t.Fatal("Expected length of metadata field value to be 0, got", len(md.Value))
120+
assert.NoError(t, err, "Unexpected error extracting metadata from new block")
121+
assert.Nil(t, md.Value, "Expected metadata field value to be nil")
122+
assert.Equal(t, 0, len(md.Value), "Expected length of metadata field value to be 0")
123+
md = utils.GetMetadataFromBlockOrPanic(block, cb.BlockMetadataIndex_ORDERER)
124+
assert.NotNil(t, md, "Expected to get metadata from block")
125+
126+
// malformed metadata
127+
block.Metadata.Metadata[cb.BlockMetadataIndex_ORDERER] = []byte("bad metadata")
128+
_, err = utils.GetMetadataFromBlock(block, cb.BlockMetadataIndex_ORDERER)
129+
assert.Error(t, err, "Expected error with malformed metadata")
130+
assert.Panics(t, func() {
131+
_ = utils.GetMetadataFromBlockOrPanic(block, cb.BlockMetadataIndex_ORDERER)
132+
}, "Expected panic with malformed metadata")
133+
}
134+
135+
func TestInitBlockMeta(t *testing.T) {
136+
// block with no metadata
137+
block := &cb.Block{}
138+
utils.InitBlockMetadata(block)
139+
// should have 3 entries
140+
assert.Equal(t, 3, len(block.Metadata.Metadata), "Expected block to have 3 metadata entries")
141+
142+
// block with a single entry
143+
block = &cb.Block{
144+
Metadata: &cb.BlockMetadata{},
84145
}
146+
block.Metadata.Metadata = append(block.Metadata.Metadata, []byte{})
147+
utils.InitBlockMetadata(block)
148+
// should have 3 entries
149+
assert.Equal(t, 3, len(block.Metadata.Metadata), "Expected block to have 3 metadata entries")
150+
}
151+
152+
func TestCopyBlockMetadata(t *testing.T) {
153+
srcBlock := common.NewBlock(0, nil)
154+
dstBlock := &cb.Block{}
155+
156+
metadata, _ := proto.Marshal(&cb.Metadata{
157+
Value: []byte("orderer metadata"),
158+
})
159+
srcBlock.Metadata.Metadata[cb.BlockMetadataIndex_ORDERER] = metadata
160+
utils.CopyBlockMetadata(srcBlock, dstBlock)
161+
162+
// check that the copy worked
163+
assert.Equal(t, len(srcBlock.Metadata.Metadata), len(dstBlock.Metadata.Metadata),
164+
"Expected target block to have same number of metadata entries after copy")
165+
assert.Equal(t, metadata, dstBlock.Metadata.Metadata[cb.BlockMetadataIndex_ORDERER],
166+
"Unexpected metadata from target block")
167+
}
168+
169+
func TestGetLastConfigIndexFromBlock(t *testing.T) {
170+
block := common.NewBlock(0, nil)
171+
index := uint64(2)
172+
lc, _ := proto.Marshal(&cb.LastConfig{
173+
Index: index,
174+
})
175+
metadata, _ := proto.Marshal(&cb.Metadata{
176+
Value: lc,
177+
})
178+
block.Metadata.Metadata[cb.BlockMetadataIndex_LAST_CONFIG] = metadata
179+
result, err := utils.GetLastConfigIndexFromBlock(block)
180+
assert.NoError(t, err, "Unexpected error returning last config index")
181+
assert.Equal(t, index, result, "Unexpected last config index returned from block")
182+
result = utils.GetLastConfigIndexFromBlockOrPanic(block)
183+
assert.Equal(t, index, result, "Unexpected last config index returned from block")
184+
185+
// malformed metadata
186+
block.Metadata.Metadata[cb.BlockMetadataIndex_LAST_CONFIG] = []byte("bad metadata")
187+
_, err = utils.GetLastConfigIndexFromBlock(block)
188+
assert.Error(t, err, "Expected error with malformed metadata")
85189

190+
// malformed last config
191+
metadata, _ = proto.Marshal(&cb.Metadata{
192+
Value: []byte("bad last config"),
193+
})
194+
block.Metadata.Metadata[cb.BlockMetadataIndex_LAST_CONFIG] = metadata
195+
_, err = utils.GetLastConfigIndexFromBlock(block)
196+
assert.Error(t, err, "Expected error with malformed last config metadata")
197+
assert.Panics(t, func() {
198+
_ = utils.GetLastConfigIndexFromBlockOrPanic(block)
199+
}, "Expected panic with malformed last config metadata")
86200
}

0 commit comments

Comments
 (0)