From cef2688260d7c6cd4874c0dbcb078aa2844d7985 Mon Sep 17 00:00:00 2001 From: Gari Singh Date: Sat, 13 May 2017 17:41:25 -0400 Subject: [PATCH] [FAB-3907] Improve test coverage for protos/common Since there is some not generated logic in this package, it is included in the unit tests. This change set increases coverage to over 90% for the package. Change-Id: I75b93170a2f012bab2aa48f58ce5154c04257d61 Signed-off-by: Gari Singh --- protos/common/block_test.go | 31 ++++ protos/common/common_test.go | 202 +++++++++++++++++++++++++ protos/common/configtx_test.go | 219 ++++++++++++++++++++++++++++ protos/common/configuration_test.go | 59 ++++++++ protos/common/policies_test.go | 131 +++++++++++++++++ 5 files changed, 642 insertions(+) create mode 100644 protos/common/common_test.go create mode 100644 protos/common/configtx_test.go create mode 100644 protos/common/configuration_test.go create mode 100644 protos/common/policies_test.go diff --git a/protos/common/block_test.go b/protos/common/block_test.go index 00f9f5feeb5..ea5d203e69f 100644 --- a/protos/common/block_test.go +++ b/protos/common/block_test.go @@ -17,10 +17,41 @@ limitations under the License. package common import ( + "encoding/asn1" "math" "testing" + + "github.com/hyperledger/fabric/common/util" + "github.com/stretchr/testify/assert" ) +func TestBlock(t *testing.T) { + var block *Block + assert.Nil(t, block.GetHeader()) + assert.Nil(t, block.GetData()) + assert.Nil(t, block.GetMetadata()) + + data := &BlockData{ + Data: [][]byte{[]byte{0, 1, 2}}, + } + block = NewBlock(uint64(0), []byte("datahash")) + assert.Equal(t, []byte("datahash"), block.Header.PreviousHash, "Incorrect previous hash") + assert.NotNil(t, block.GetData()) + assert.NotNil(t, block.GetMetadata()) + block.GetHeader().DataHash = data.Hash() + + asn1Bytes, err := asn1.Marshal(asn1Header{ + Number: int64(uint64(0)), + DataHash: data.Hash(), + PreviousHash: []byte("datahash"), + }) + headerHash := util.ComputeSHA256(asn1Bytes) + assert.NoError(t, err) + assert.Equal(t, asn1Bytes, block.Header.Bytes(), "Incorrect marshaled blockheader bytes") + assert.Equal(t, headerHash, block.Header.Hash(), "Incorrect blockheader hash") + +} + func TestGoodBlockHeaderBytes(t *testing.T) { goodBlockHeader := &BlockHeader{ Number: 1, diff --git a/protos/common/common_test.go b/protos/common/common_test.go new file mode 100644 index 00000000000..06f6f42d128 --- /dev/null +++ b/protos/common/common_test.go @@ -0,0 +1,202 @@ +/* +Copyright IBM Corp. 2017 All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package common + +import ( + "testing" + + google_protobuf "github.com/golang/protobuf/ptypes/timestamp" + "github.com/stretchr/testify/assert" +) + +func TestCommonEnums(t *testing.T) { + var status Status + + status = 0 + assert.Equal(t, "UNKNOWN", status.String()) + status = 200 + assert.Equal(t, "SUCCESS", status.String()) + status = 400 + assert.Equal(t, "BAD_REQUEST", status.String()) + status = 403 + assert.Equal(t, "FORBIDDEN", status.String()) + status = 404 + assert.Equal(t, "NOT_FOUND", status.String()) + status = 413 + assert.Equal(t, "REQUEST_ENTITY_TOO_LARGE", status.String()) + status = 500 + assert.Equal(t, "INTERNAL_SERVER_ERROR", status.String()) + status = 503 + assert.Equal(t, "SERVICE_UNAVAILABLE", status.String()) + _, _ = status.EnumDescriptor() + + var header HeaderType + header = 0 + assert.Equal(t, "MESSAGE", header.String()) + header = 1 + assert.Equal(t, "CONFIG", header.String()) + header = 2 + assert.Equal(t, "CONFIG_UPDATE", header.String()) + header = 3 + assert.Equal(t, "ENDORSER_TRANSACTION", header.String()) + header = 4 + assert.Equal(t, "ORDERER_TRANSACTION", header.String()) + header = 5 + assert.Equal(t, "DELIVER_SEEK_INFO", header.String()) + header = 6 + assert.Equal(t, "CHAINCODE_PACKAGE", header.String()) + _, _ = header.EnumDescriptor() + + var index BlockMetadataIndex + index = 0 + assert.Equal(t, "SIGNATURES", index.String()) + index = 1 + assert.Equal(t, "LAST_CONFIG", index.String()) + index = 2 + assert.Equal(t, "TRANSACTIONS_FILTER", index.String()) + index = 3 + assert.Equal(t, "ORDERER", index.String()) + _, _ = index.EnumDescriptor() +} + +func TestCommonStructs(t *testing.T) { + var last *LastConfig + last = &LastConfig{ + Index: uint64(1), + } + last.Reset() + assert.Equal(t, uint64(0), last.Index) + _, _ = last.Descriptor() + _ = last.String() + last.ProtoMessage() + + var meta *Metadata + meta = nil + assert.Nil(t, meta.GetSignatures()) + meta = &Metadata{ + Signatures: []*MetadataSignature{&MetadataSignature{}}, + } + assert.NotNil(t, meta.GetSignatures()) + meta.Reset() + assert.Nil(t, meta.GetSignatures()) + _, _ = meta.Descriptor() + _ = meta.String() + meta.ProtoMessage() + + var sig *MetadataSignature + sig = &MetadataSignature{ + Signature: []byte("signature"), + } + sig.Reset() + assert.Nil(t, sig.Signature) + _, _ = sig.Descriptor() + _ = sig.String() + sig.ProtoMessage() + + var header *Header + header = &Header{ + ChannelHeader: []byte("channel header"), + } + header.Reset() + assert.Nil(t, header.ChannelHeader) + _, _ = header.Descriptor() + _ = header.String() + header.ProtoMessage() + + var chheader *ChannelHeader + chheader = nil + assert.Nil(t, chheader.GetTimestamp()) + chheader = &ChannelHeader{ + Timestamp: &google_protobuf.Timestamp{}, + } + assert.NotNil(t, chheader.GetTimestamp()) + chheader.Reset() + assert.Nil(t, chheader.GetTimestamp()) + _, _ = chheader.Descriptor() + _ = chheader.String() + chheader.ProtoMessage() + + var sigheader *SignatureHeader + sigheader = &SignatureHeader{ + Creator: []byte("creator"), + } + sigheader.Reset() + assert.Nil(t, sigheader.Creator) + _, _ = sigheader.Descriptor() + _ = sigheader.String() + sigheader.ProtoMessage() + + var payload *Payload + payload = nil + assert.Nil(t, payload.GetHeader()) + payload = &Payload{ + Header: &Header{}, + } + assert.NotNil(t, payload.GetHeader()) + payload.Reset() + assert.Nil(t, payload.Data) + _, _ = payload.Descriptor() + _ = payload.String() + payload.ProtoMessage() + + var env *Envelope + env = &Envelope{ + Payload: []byte("payload"), + } + env.Reset() + assert.Nil(t, env.Payload) + _, _ = env.Descriptor() + _ = env.String() + env.ProtoMessage() + + b := &Block{ + Data: &BlockData{}, + } + b.Reset() + assert.Nil(t, b.GetData()) + _, _ = b.Descriptor() + _ = b.String() + b.ProtoMessage() + + bh := &BlockHeader{ + PreviousHash: []byte("hash"), + } + bh.Reset() + assert.Nil(t, bh.PreviousHash) + _, _ = bh.Descriptor() + _ = bh.String() + bh.ProtoMessage() + + bd := &BlockData{ + Data: [][]byte{}, + } + bd.Reset() + assert.Nil(t, bd.Data) + _, _ = bd.Descriptor() + _ = bd.String() + bd.ProtoMessage() + + bm := &BlockMetadata{ + Metadata: [][]byte{}, + } + bm.Reset() + assert.Nil(t, bm.Metadata) + _, _ = bm.Descriptor() + _ = bm.String() + bm.ProtoMessage() + +} diff --git a/protos/common/configtx_test.go b/protos/common/configtx_test.go new file mode 100644 index 00000000000..6f80f5fc3bc --- /dev/null +++ b/protos/common/configtx_test.go @@ -0,0 +1,219 @@ +/* +Copyright IBM Corp. 2017 All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package common + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestConfigGroup(t *testing.T) { + var cg *ConfigGroup + + cg = nil + + assert.Nil(t, cg.GetGroups()) + assert.Nil(t, cg.GetValues()) + assert.Nil(t, cg.GetPolicies()) + + cg = &ConfigGroup{ + Groups: make(map[string]*ConfigGroup), + Values: make(map[string]*ConfigValue), + Policies: make(map[string]*ConfigPolicy), + } + assert.NotNil(t, cg.GetGroups()) + assert.NotNil(t, cg.GetValues()) + assert.NotNil(t, cg.GetPolicies()) + + cg = NewConfigGroup() + cg.Groups["test"] = NewConfigGroup() + cg.Values["test"] = &ConfigValue{} + cg.Policies["test"] = &ConfigPolicy{} + _, ok := cg.GetGroups()["test"] + assert.Equal(t, true, ok) + _, ok = cg.GetValues()["test"] + assert.Equal(t, true, ok) + _, ok = cg.GetPolicies()["test"] + assert.Equal(t, true, ok) + + cg.Reset() + assert.Nil(t, cg.GetGroups()) + + _ = cg.String() + _, _ = cg.Descriptor() + cg.ProtoMessage() +} + +func TestConfigValue(t *testing.T) { + cv := &ConfigValue{} + cv.Reset() + _ = cv.String() + _, _ = cv.Descriptor() + cv.ProtoMessage() +} + +func TestConfigEnvelope(t *testing.T) { + var env *ConfigEnvelope + + env = nil + assert.Nil(t, env.GetConfig()) + assert.Nil(t, env.GetLastUpdate()) + + env = &ConfigEnvelope{ + Config: &Config{}, + LastUpdate: &Envelope{}, + } + assert.NotNil(t, env.GetConfig()) + assert.NotNil(t, env.GetLastUpdate()) + + env.Reset() + assert.Nil(t, env.GetConfig()) + + _ = env.String() + _, _ = env.Descriptor() + env.ProtoMessage() +} + +func TestConfigGroupSchema(t *testing.T) { + var cgs *ConfigGroupSchema + + cgs = nil + assert.Nil(t, cgs.GetGroups()) + assert.Nil(t, cgs.GetPolicies()) + assert.Nil(t, cgs.GetValues()) + + cgs = &ConfigGroupSchema{ + Groups: make(map[string]*ConfigGroupSchema), + Values: make(map[string]*ConfigValueSchema), + Policies: make(map[string]*ConfigPolicySchema), + } + assert.NotNil(t, cgs.GetGroups()) + assert.NotNil(t, cgs.GetPolicies()) + assert.NotNil(t, cgs.GetValues()) + + cgs.Reset() + assert.Nil(t, cgs.GetGroups()) + + _ = cgs.String() + _, _ = cgs.Descriptor() + cgs.ProtoMessage() +} + +func TestGenericSchema(t *testing.T) { + cvs := &ConfigValueSchema{} + cvs.Reset() + _ = cvs.String() + _, _ = cvs.Descriptor() + cvs.ProtoMessage() + + cps := &ConfigPolicySchema{} + cps.Reset() + _ = cps.String() + _, _ = cps.Descriptor() + cps.ProtoMessage() +} + +func TestConfig(t *testing.T) { + var c *Config + + c = nil + assert.Nil(t, c.GetChannelGroup()) + + c = &Config{ + ChannelGroup: &ConfigGroup{}, + } + assert.NotNil(t, c.GetChannelGroup()) + + c.Reset() + assert.Nil(t, c.GetChannelGroup()) + + _ = c.String() + _, _ = c.Descriptor() + c.ProtoMessage() +} + +func TestConfigUpdateEnvelope(t *testing.T) { + var env *ConfigUpdateEnvelope + + env = nil + assert.Nil(t, env.GetSignatures()) + + env = &ConfigUpdateEnvelope{ + Signatures: []*ConfigSignature{ + &ConfigSignature{}, + }, + } + assert.NotNil(t, env.GetSignatures()) + + env.Reset() + assert.Nil(t, env.GetSignatures()) + + _ = env.String() + _, _ = env.Descriptor() + env.ProtoMessage() +} + +func TestConfigUpdate(t *testing.T) { + var c *ConfigUpdate + + c = nil + assert.Nil(t, c.GetReadSet()) + assert.Nil(t, c.GetWriteSet()) + + c = &ConfigUpdate{ + ReadSet: &ConfigGroup{}, + WriteSet: &ConfigGroup{}, + } + assert.NotNil(t, c.GetReadSet()) + assert.NotNil(t, c.GetWriteSet()) + + c.Reset() + assert.Nil(t, c.GetReadSet()) + + _ = c.String() + _, _ = c.Descriptor() + c.ProtoMessage() +} + +func TestConfigPolicy(t *testing.T) { + var cp *ConfigPolicy + + cp = nil + assert.Nil(t, cp.GetPolicy()) + + cp = &ConfigPolicy{ + Policy: &Policy{}, + } + assert.NotNil(t, cp.GetPolicy()) + + cp.Reset() + assert.Nil(t, cp.GetPolicy()) + + _ = cp.String() + _, _ = cp.Descriptor() + cp.ProtoMessage() +} + +func TestConfigSignature(t *testing.T) { + cs := &ConfigSignature{} + + cs.Reset() + _ = cs.String() + _, _ = cs.Descriptor() + cs.ProtoMessage() +} diff --git a/protos/common/configuration_test.go b/protos/common/configuration_test.go new file mode 100644 index 00000000000..1c80b536b25 --- /dev/null +++ b/protos/common/configuration_test.go @@ -0,0 +1,59 @@ +/* +Copyright IBM Corp. 2017 All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package common + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestConfiguration(t *testing.T) { + var h *HashingAlgorithm + var b *BlockDataHashingStructure + var o *OrdererAddresses + var c *Consortium + + h = &HashingAlgorithm{Name: "SHA256"} + h.Reset() + _ = h.String() + _, _ = h.Descriptor() + h.ProtoMessage() + assert.Equal(t, "", h.Name) + + b = &BlockDataHashingStructure{Width: uint32(1)} + b.Reset() + _ = b.String() + _, _ = b.Descriptor() + b.ProtoMessage() + assert.Equal(t, uint32(0), b.Width) + + o = &OrdererAddresses{Addresses: []string{"address"}} + o.Reset() + _ = o.String() + _, _ = o.Descriptor() + o.ProtoMessage() + assert.Nil(t, o.Addresses) + + c = &Consortium{Name: "consortium"} + c.Reset() + _ = c.String() + _, _ = c.Descriptor() + c.ProtoMessage() + assert.Equal(t, "", c.Name) + +} diff --git a/protos/common/policies_test.go b/protos/common/policies_test.go new file mode 100644 index 00000000000..00e0ce14bc5 --- /dev/null +++ b/protos/common/policies_test.go @@ -0,0 +1,131 @@ +/* +Copyright IBM Corp. 2017 All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package common + +import ( + "testing" + + proto "github.com/golang/protobuf/proto" + common1 "github.com/hyperledger/fabric/protos/msp" + "github.com/stretchr/testify/assert" +) + +func TestPoliciesEnums(t *testing.T) { + var policy Policy_PolicyType + policy = 0 + assert.Equal(t, "UNKNOWN", policy.String()) + policy = 1 + assert.Equal(t, "SIGNATURE", policy.String()) + policy = 2 + assert.Equal(t, "MSP", policy.String()) + policy = 3 + assert.Equal(t, "IMPLICIT_META", policy.String()) + + _, _ = policy.EnumDescriptor() + + var meta ImplicitMetaPolicy_Rule + meta = 0 + assert.Equal(t, "ANY", meta.String()) + meta = 1 + assert.Equal(t, "ALL", meta.String()) + meta = 2 + assert.Equal(t, "MAJORITY", meta.String()) + + _, _ = meta.EnumDescriptor() +} + +func TestPoliciesStructs(t *testing.T) { + policy := &Policy{ + Policy: []byte("policy"), + } + policy.Reset() + assert.Nil(t, policy.Policy) + _, _ = policy.Descriptor() + _ = policy.String() + policy.ProtoMessage() + + var env *SignaturePolicyEnvelope + env = nil + assert.Nil(t, env.GetIdentities()) + assert.Nil(t, env.GetPolicy()) + env = &SignaturePolicyEnvelope{ + Policy: &SignaturePolicy{}, + Identities: []*common1.MSPPrincipal{&common1.MSPPrincipal{}}, + } + assert.NotNil(t, env.GetIdentities()) + assert.NotNil(t, env.GetPolicy()) + env.Reset() + assert.Nil(t, env.GetIdentities()) + assert.Nil(t, env.GetPolicy()) + _, _ = env.Descriptor() + _ = env.String() + env.ProtoMessage() + + var sigpolicy *SignaturePolicy + sigpolicy = nil + assert.Nil(t, sigpolicy.GetType()) + assert.Nil(t, sigpolicy.GetNOutOf()) + assert.Equal(t, int32(0), sigpolicy.GetSignedBy()) + sigpolicy = &SignaturePolicy{ + Type: &SignaturePolicy_SignedBy{ + SignedBy: 2, + }, + } + assert.Equal(t, int32(2), sigpolicy.GetSignedBy()) + bytes, err := proto.Marshal(sigpolicy) + assert.NoError(t, err, "Error marshaling SignedBy policy") + _ = proto.Size(sigpolicy) + err = proto.Unmarshal(bytes, &SignaturePolicy{}) + assert.NoError(t, err, "Error unmarshaling SignedBy policy") + sigpolicy = &SignaturePolicy{ + Type: &SignaturePolicy_NOutOf_{ + NOutOf: &SignaturePolicy_NOutOf{}, + }, + } + assert.NotNil(t, sigpolicy.GetNOutOf()) + bytes, err = proto.Marshal(sigpolicy) + assert.NoError(t, err, "Error marshaling NOutOf policy") + _ = proto.Size(sigpolicy) + err = proto.Unmarshal(bytes, &SignaturePolicy{}) + assert.NoError(t, err, "Error unmarshaling NoutOf policy") + _, _ = sigpolicy.Descriptor() + _ = sigpolicy.String() + sigpolicy.ProtoMessage() + + var n *SignaturePolicy_NOutOf + n = nil + assert.Nil(t, n.GetPolicies()) + n = &SignaturePolicy_NOutOf{ + Policies: []*SignaturePolicy{&SignaturePolicy{}}, + } + assert.NotNil(t, n.GetPolicies()) + n.Reset() + assert.Nil(t, n.GetPolicies()) + _, _ = n.Descriptor() + _ = n.String() + n.ProtoMessage() + + impolicy := &ImplicitMetaPolicy{ + SubPolicy: "subpolicy", + } + impolicy.Reset() + assert.Equal(t, "", impolicy.SubPolicy) + _, _ = impolicy.Descriptor() + _ = impolicy.String() + impolicy.ProtoMessage() + +}