@@ -20,67 +20,181 @@ package utils_test
20
20
import (
21
21
"testing"
22
22
23
+ "github.com/golang/protobuf/proto"
23
24
configtxtest "github.com/hyperledger/fabric/common/configtx/test"
24
25
"github.com/hyperledger/fabric/protos/common"
25
26
cb "github.com/hyperledger/fabric/protos/common"
26
27
"github.com/hyperledger/fabric/protos/utils"
28
+ "github.com/stretchr/testify/assert"
27
29
)
28
30
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
+
29
46
func TestGetChainIDFromBlock (t * testing.T ) {
30
47
var err error
31
48
var gb * common.Block
32
49
var cid string
33
50
34
- testChainID := "myuniquetestchainid"
35
-
36
51
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
+
40
54
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" )
47
57
58
+ // missing data
48
59
badBlock := gb
49
60
badBlock .Data = nil
50
61
_ , 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
+ },
54
81
}
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" )
55
101
}
56
102
57
103
func TestGetBlockFromBlockBytes (t * testing.T ) {
58
104
testChainID := "myuniquetestchainid"
59
105
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" )
63
107
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" )
67
109
_ , 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" )
71
115
}
72
116
73
117
func TestGetMetadataFromNewBlock (t * testing.T ) {
74
118
block := common .NewBlock (0 , nil )
75
119
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 {},
84
145
}
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" )
85
189
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" )
86
200
}
0 commit comments