Skip to content

Commit 9bd29d3

Browse files
committed
Add tests to static bootstrap helper
The static package did not contain any tests - this changeset fixes that. These simple tests deconstruct the nested message that ultimately gave us the genesis block and check that its values have been set properly. UPDATE: Revised for the latest proto changes. Change-Id: If0afcbcebb18d59158f05b70b7cc63c1752286cb Signed-off-by: Kostas Christidis <[email protected]>
1 parent 3b6c70d commit 9bd29d3

File tree

2 files changed

+175
-3
lines changed

2 files changed

+175
-3
lines changed

orderer/common/bootstrap/static/static.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func errorlessMarshal(thing proto.Message) []byte {
5353
return data
5454
}
5555

56-
func makeChainHeader(headerType cb.HeaderType, version int32, chainID []byte) *cb.ChainHeader {
56+
func makeChainHeader(headerType cb.HeaderType, version int32, chainID []byte, epoch uint64) *cb.ChainHeader {
5757
return &cb.ChainHeader{
5858
Type: int32(headerType),
5959
Version: version,
@@ -62,6 +62,7 @@ func makeChainHeader(headerType cb.HeaderType, version int32, chainID []byte) *c
6262
Nanos: 0,
6363
},
6464
ChainID: chainID,
65+
Epoch: epoch,
6566
}
6667
}
6768

@@ -74,7 +75,7 @@ func makeSignatureHeader(serializedCreatorCertChain []byte, nonce []byte) *cb.Si
7475

7576
func (b *bootstrapper) makeSignedConfigurationItem(configurationItemType cb.ConfigurationItem_ConfigurationType, modificationPolicyID string, key string, value []byte) *cb.SignedConfigurationItem {
7677
marshaledConfigurationItem := errorlessMarshal(&cb.ConfigurationItem{
77-
Header: makeChainHeader(cb.HeaderType_CONFIGURATION_ITEM, 1, b.chainID),
78+
Header: makeChainHeader(cb.HeaderType_CONFIGURATION_ITEM, 1, b.chainID, 0),
7879
Type: configurationItemType,
7980
LastModified: 0,
8081
ModificationPolicy: modificationPolicyID,
@@ -101,7 +102,7 @@ func (b *bootstrapper) makeEnvelope(configurationEnvelope *cb.ConfigurationEnvel
101102
}
102103
marshaledPayload := errorlessMarshal(&cb.Payload{
103104
Header: &cb.Header{
104-
ChainHeader: makeChainHeader(cb.HeaderType_CONFIGURATION_TRANSACTION, 1, b.chainID),
105+
ChainHeader: makeChainHeader(cb.HeaderType_CONFIGURATION_TRANSACTION, 1, b.chainID, 0),
105106
SignatureHeader: makeSignatureHeader(nil, nonce),
106107
},
107108
Data: errorlessMarshal(configurationEnvelope),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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 static
18+
19+
import (
20+
"bytes"
21+
"testing"
22+
23+
"github.com/hyperledger/fabric/orderer/common/cauthdsl"
24+
"github.com/hyperledger/fabric/orderer/common/configtx"
25+
cb "github.com/hyperledger/fabric/protos/common"
26+
27+
"github.com/golang/protobuf/proto"
28+
)
29+
30+
func TestGenesisBlockCreation(t *testing.T) {
31+
_, err := New().GenesisBlock()
32+
if err != nil {
33+
t.Fatalf("Cannot create genesis block: %s", err)
34+
}
35+
}
36+
37+
func TestGenesisBlockHeader(t *testing.T) {
38+
expectedHeaderNumber := uint64(0)
39+
40+
genesisBlock, _ := New().GenesisBlock() // The error has been checked in a previous test
41+
42+
if genesisBlock.Header.Number != expectedHeaderNumber {
43+
t.Fatalf("Expected header number %d, got %d", expectedHeaderNumber, genesisBlock.Header.Number)
44+
}
45+
46+
if !bytes.Equal(genesisBlock.Header.PreviousHash, nil) {
47+
t.Fatalf("Expected header previousHash to be nil, got %x", genesisBlock.Header.PreviousHash)
48+
}
49+
}
50+
51+
func TestGenesisBlockData(t *testing.T) {
52+
expectedBlockDataLength := 1
53+
expectedPayloadChainHeaderType := int32(cb.HeaderType_CONFIGURATION_TRANSACTION)
54+
expectedChainHeaderVersion := int32(1)
55+
expectedChainHeaderEpoch := uint64(0)
56+
expectedConfigEnvelopeItemsLength := 1
57+
expectedConfigurationItemChainHeaderType := int32(cb.HeaderType_CONFIGURATION_ITEM)
58+
expectedConfigurationItemChainHeaderVersion := int32(1)
59+
expectedConfigurationItemType := cb.ConfigurationItem_Policy
60+
expectedConfigEnvelopeSequence := uint64(0)
61+
expectedConfigurationItemModificationPolicy := configtx.DefaultModificationPolicyID
62+
expectedConfigurationItemValueSignaturePolicyEnvelope := cauthdsl.RejectAllPolicy
63+
64+
genesisBlock, _ := New().GenesisBlock() // The error has been checked in a previous test
65+
66+
if len(genesisBlock.Data.Data) != expectedBlockDataLength {
67+
t.Fatalf("Expected genesis block data length %d, got %d", expectedBlockDataLength, len(genesisBlock.Data.Data))
68+
}
69+
70+
marshaledEnvelope := genesisBlock.Data.Data[0]
71+
envelope := &cb.Envelope{}
72+
if err := proto.Unmarshal(marshaledEnvelope, envelope); err != nil {
73+
t.Fatalf("Expected genesis block to carry an Envelope")
74+
}
75+
76+
envelopeSignature := envelope.Signature
77+
if !bytes.Equal(envelopeSignature, nil) {
78+
t.Fatalf("Expected envelope signature to be nil, got %x", envelopeSignature)
79+
}
80+
81+
marshaledPayload := envelope.Payload
82+
payload := &cb.Payload{}
83+
if err := proto.Unmarshal(marshaledPayload, payload); err != nil {
84+
t.Fatalf("Expected genesis block to carry a Payload")
85+
}
86+
87+
signatureHeader := payload.Header.SignatureHeader
88+
if !bytes.Equal(signatureHeader.Creator, nil) {
89+
t.Fatalf("Expected payload signature header creator to be nil, got %x", signatureHeader.Creator)
90+
}
91+
if bytes.Equal(signatureHeader.Nonce, nil) {
92+
t.Fatal("Expected non-nil nonce")
93+
}
94+
95+
payloadChainHeader := payload.Header.ChainHeader
96+
if payloadChainHeader.Type != expectedPayloadChainHeaderType {
97+
t.Fatalf("Expected payload chain header type %d, got %d", expectedPayloadChainHeaderType, payloadChainHeader.Type)
98+
}
99+
if payloadChainHeader.Version != expectedChainHeaderVersion {
100+
t.Fatalf("Expected payload chain header version %d, got %d", expectedChainHeaderVersion, payloadChainHeader.Version)
101+
}
102+
if payloadChainHeader.Epoch != expectedChainHeaderEpoch {
103+
t.Fatalf("Expected payload chain header epoch to be %d, got %d", expectedChainHeaderEpoch, payloadChainHeader.Epoch)
104+
}
105+
106+
marshaledConfigurationEnvelope := payload.Data
107+
configurationEnvelope := &cb.ConfigurationEnvelope{}
108+
if err := proto.Unmarshal(marshaledConfigurationEnvelope, configurationEnvelope); err != nil {
109+
t.Fatalf("Expected genesis block to carry a ConfigurationEnvelope")
110+
}
111+
if len(configurationEnvelope.Items) != expectedConfigEnvelopeItemsLength {
112+
t.Fatalf("Expected configuration envelope to have %d configuration item(s), got %d", expectedConfigEnvelopeItemsLength, len(configurationEnvelope.Items))
113+
}
114+
115+
signedConfigurationItem := configurationEnvelope.Items[0]
116+
marshaledConfigurationItem := signedConfigurationItem.ConfigurationItem
117+
configurationItem := &cb.ConfigurationItem{}
118+
if err := proto.Unmarshal(marshaledConfigurationItem, configurationItem); err != nil {
119+
t.Fatalf("Expected genesis block to carry a ConfigurationItem")
120+
}
121+
122+
configurationItemChainHeader := configurationItem.Header
123+
if configurationItemChainHeader.Type != expectedConfigurationItemChainHeaderType {
124+
t.Fatalf("Expected configuration item chain header type %d, got %d", expectedConfigurationItemChainHeaderType, configurationItemChainHeader.Type)
125+
}
126+
if configurationItemChainHeader.Version != expectedConfigurationItemChainHeaderVersion {
127+
t.Fatalf("Expected configuration item chain header version %d, got %d", expectedConfigurationItemChainHeaderVersion, configurationItemChainHeader.Version)
128+
}
129+
if !bytes.Equal(configurationItemChainHeader.ChainID, payloadChainHeader.ChainID) {
130+
t.Fatalf("Expected chain ID in chain headers of configuration item and payload to match, got %x and %x respectively", configurationItemChainHeader.ChainID, payloadChainHeader.ChainID)
131+
}
132+
if configurationItemChainHeader.Epoch != payloadChainHeader.Epoch {
133+
t.Fatalf("Expected epoch in chain headers of configuration item and payload to match, got %d, and %d respectively", configurationItemChainHeader.Epoch, payloadChainHeader.Epoch)
134+
}
135+
136+
if configurationItem.Type != expectedConfigurationItemType {
137+
t.Fatalf("Expected configuration item type %s, got %s", expectedConfigurationItemType.String(), configurationItem.Type.String())
138+
}
139+
if configurationItem.LastModified != expectedConfigEnvelopeSequence {
140+
t.Fatalf("Expected configuration item sequence to match configuration envelope sequence %d, got %d", expectedConfigEnvelopeSequence, configurationItem.LastModified)
141+
}
142+
if configurationItem.ModificationPolicy != expectedConfigurationItemModificationPolicy {
143+
t.Fatalf("Expected configuration item modification policy %s, got %s", expectedConfigurationItemModificationPolicy, configurationItem.ModificationPolicy)
144+
}
145+
if configurationItem.Key != expectedConfigurationItemModificationPolicy {
146+
t.Fatalf("Expected configuration item key to be equal to the modification policy %s, got %s", expectedConfigurationItemModificationPolicy, configurationItem.Key)
147+
}
148+
149+
marshaledPolicy := configurationItem.Value
150+
policy := &cb.Policy{}
151+
if err := proto.Unmarshal(marshaledPolicy, policy); err != nil {
152+
t.Fatalf("Expected genesis block to carry a policy in its configuration item value")
153+
}
154+
switch policy.GetType().(type) {
155+
case *cb.Policy_SignaturePolicy:
156+
default:
157+
t.Fatalf("Got unexpected configuration item value policy type")
158+
}
159+
signaturePolicyEnvelope := policy.GetSignaturePolicy()
160+
if !proto.Equal(signaturePolicyEnvelope, expectedConfigurationItemValueSignaturePolicyEnvelope) {
161+
t.Fatalf("Expected configuration item value signature policy envelope %s, got %s", expectedConfigurationItemValueSignaturePolicyEnvelope.String(), signaturePolicyEnvelope.String())
162+
}
163+
}
164+
165+
func TestGenesisMetadata(t *testing.T) {
166+
genesisBlock, _ := New().GenesisBlock() // The error has been checked in a previous test
167+
168+
if genesisBlock.Metadata != nil {
169+
t.Fatalf("Expected metadata nil, got %x", genesisBlock.Metadata)
170+
}
171+
}

0 commit comments

Comments
 (0)