Skip to content

Commit

Permalink
[FAB-4173] UT for protos/common
Browse files Browse the repository at this point in the history
After the grpc-go upgrade, a few new methods
are generated for the protos so need to add
coverage for them.  Also, some new code was
added to help with the configtx tool so
started some coverage for them as well.

Change-Id: If841a7ae34ea123abd519fb7b48503cc96712a4a
Signed-off-by: Gari Singh <[email protected]>
  • Loading branch information
mastersingh24 committed Jun 2, 2017
1 parent 1c8dc30 commit a5b0bf9
Show file tree
Hide file tree
Showing 5 changed files with 312 additions and 76 deletions.
205 changes: 181 additions & 24 deletions protos/common/common_test.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
/*
Copyright IBM Corp. 2017 All Rights Reserved.
Copyright IBM Corp. 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.
SPDX-License-Identifier: Apache-2.0
*/

package common

import (
"testing"

"github.com/golang/protobuf/proto"
google_protobuf "github.com/golang/protobuf/ptypes/timestamp"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -75,54 +66,89 @@ func TestCommonEnums(t *testing.T) {

func TestCommonStructs(t *testing.T) {
var last *LastConfig
last = nil
assert.Equal(t, uint64(0), last.GetIndex())
last = &LastConfig{
Index: uint64(1),
}
assert.Equal(t, uint64(1), last.GetIndex())
last.Reset()
assert.Equal(t, uint64(0), last.Index)
assert.Equal(t, uint64(0), last.GetIndex())
_, _ = last.Descriptor()
_ = last.String()
last.ProtoMessage()

var meta *Metadata
meta = nil
assert.Nil(t, meta.GetSignatures())
assert.Nil(t, meta.GetValue())
meta = &Metadata{
Value: []byte("value"),
Signatures: []*MetadataSignature{&MetadataSignature{}},
}
assert.NotNil(t, meta.GetSignatures())
assert.NotNil(t, meta.GetValue())
meta.Reset()
assert.Nil(t, meta.GetSignatures())
_, _ = meta.Descriptor()
_ = meta.String()
meta.ProtoMessage()

var sig *MetadataSignature
sig = nil
assert.Nil(t, sig.GetSignature())
assert.Nil(t, sig.GetSignatureHeader())
sig = &MetadataSignature{
Signature: []byte("signature"),
SignatureHeader: []byte("header"),
Signature: []byte("signature"),
}
assert.NotNil(t, sig.GetSignature())
assert.NotNil(t, sig.GetSignatureHeader())
sig.Reset()
assert.Nil(t, sig.Signature)
assert.Nil(t, sig.GetSignature())
assert.Nil(t, sig.GetSignatureHeader())
_, _ = sig.Descriptor()
_ = sig.String()
sig.ProtoMessage()

var header *Header
header = nil
assert.Nil(t, header.GetChannelHeader())
assert.Nil(t, header.GetSignatureHeader())
header = &Header{
ChannelHeader: []byte("channel header"),
ChannelHeader: []byte("channel header"),
SignatureHeader: []byte("signature header"),
}
assert.NotNil(t, header.GetChannelHeader())
assert.NotNil(t, header.GetSignatureHeader())
header.Reset()
assert.Nil(t, header.ChannelHeader)
assert.Nil(t, header.GetChannelHeader())
assert.Nil(t, header.GetSignatureHeader())
_, _ = header.Descriptor()
_ = header.String()
header.ProtoMessage()

var chheader *ChannelHeader
chheader = nil
assert.Equal(t, "", chheader.GetChannelId())
assert.Equal(t, "", chheader.GetTxId())
assert.Equal(t, uint64(0), chheader.GetEpoch())
assert.Equal(t, int32(0), chheader.GetType())
assert.Equal(t, int32(0), chheader.GetVersion())
assert.Nil(t, chheader.GetExtension())
assert.Nil(t, chheader.GetTimestamp())
chheader = &ChannelHeader{
ChannelId: "ChannelId",
TxId: "TxId",
Timestamp: &google_protobuf.Timestamp{},
Extension: []byte{},
}
assert.Equal(t, "ChannelId", chheader.GetChannelId())
assert.Equal(t, "TxId", chheader.GetTxId())
assert.Equal(t, uint64(0), chheader.GetEpoch())
assert.Equal(t, int32(0), chheader.GetType())
assert.Equal(t, int32(0), chheader.GetVersion())
assert.NotNil(t, chheader.GetExtension())
assert.NotNil(t, chheader.GetTimestamp())
chheader.Reset()
assert.Nil(t, chheader.GetTimestamp())
Expand All @@ -131,9 +157,15 @@ func TestCommonStructs(t *testing.T) {
chheader.ProtoMessage()

var sigheader *SignatureHeader
sigheader = nil
assert.Nil(t, sigheader.GetCreator())
assert.Nil(t, sigheader.GetNonce())
sigheader = &SignatureHeader{
Creator: []byte("creator"),
Nonce: []byte("nonce"),
}
assert.NotNil(t, sigheader.GetCreator())
assert.NotNil(t, sigheader.GetNonce())
sigheader.Reset()
assert.Nil(t, sigheader.Creator)
_, _ = sigheader.Descriptor()
Expand All @@ -143,20 +175,29 @@ func TestCommonStructs(t *testing.T) {
var payload *Payload
payload = nil
assert.Nil(t, payload.GetHeader())
assert.Nil(t, payload.GetData())
payload = &Payload{
Header: &Header{},
Data: []byte("data"),
}
assert.NotNil(t, payload.GetHeader())
assert.NotNil(t, payload.GetData())
payload.Reset()
assert.Nil(t, payload.Data)
_, _ = payload.Descriptor()
_ = payload.String()
payload.ProtoMessage()

var env *Envelope
env = nil
assert.Nil(t, env.GetPayload())
assert.Nil(t, env.GetSignature())
env = &Envelope{
Payload: []byte("payload"),
Payload: []byte("payload"),
Signature: []byte("signature"),
}
assert.NotNil(t, env.GetPayload())
assert.NotNil(t, env.GetSignature())
env.Reset()
assert.Nil(t, env.Payload)
_, _ = env.Descriptor()
Expand All @@ -172,31 +213,147 @@ func TestCommonStructs(t *testing.T) {
_ = b.String()
b.ProtoMessage()

bh := &BlockHeader{
var bh *BlockHeader
bh = nil
assert.Nil(t, bh.GetDataHash())
assert.Nil(t, bh.GetPreviousHash())
assert.Equal(t, uint64(0), bh.GetNumber())
bh = &BlockHeader{
PreviousHash: []byte("hash"),
DataHash: []byte("dataHash"),
Number: uint64(1),
}
assert.NotNil(t, bh.GetDataHash())
assert.NotNil(t, bh.GetPreviousHash())
assert.Equal(t, uint64(1), bh.GetNumber())
bh.Reset()
assert.Nil(t, bh.PreviousHash)
assert.Nil(t, bh.GetPreviousHash())
_, _ = bh.Descriptor()
_ = bh.String()
bh.ProtoMessage()

bd := &BlockData{
var bd *BlockData
bd = nil
assert.Nil(t, bd.GetData())
bd = &BlockData{
Data: [][]byte{},
}
assert.NotNil(t, bd.GetData())
bd.Reset()
assert.Nil(t, bd.Data)
assert.Nil(t, bd.GetData())
_, _ = bd.Descriptor()
_ = bd.String()
bd.ProtoMessage()

bm := &BlockMetadata{
var bm *BlockMetadata
bm = nil
assert.Nil(t, bm.GetMetadata())
bm = &BlockMetadata{
Metadata: [][]byte{},
}
assert.NotNil(t, bm.GetMetadata())
bm.Reset()
assert.Nil(t, bm.Metadata)
assert.Nil(t, bm.GetMetadata())
_, _ = bm.Descriptor()
_ = bm.String()
bm.ProtoMessage()

}

func TestCommonProtolator(t *testing.T) {
// Envelope
env := &Envelope{}
assert.Equal(t, []string{"payload"}, env.StaticallyOpaqueFields())
msg, err := env.StaticallyOpaqueFieldProto("badproto")
assert.Nil(t, msg)
assert.Error(t, err)
msg, err = env.StaticallyOpaqueFieldProto("payload")
// Payload
payload := &Payload{}
assert.Equal(t, &Payload{}, msg)
assert.NoError(t, err)
assert.Equal(t, []string{"data"}, payload.VariablyOpaqueFields())
msg, err = payload.VariablyOpaqueFieldProto("badproto")
assert.Nil(t, msg)
assert.Error(t, err)
msg, err = payload.VariablyOpaqueFieldProto("data")
assert.Nil(t, msg)
assert.Error(t, err)

payload = &Payload{
Header: &Header{
ChannelHeader: []byte("badbytes"),
},
}
msg, err = payload.VariablyOpaqueFieldProto("data")
assert.Nil(t, msg)
assert.Error(t, err)

ch := &ChannelHeader{
Type: int32(HeaderType_CONFIG),
}
chbytes, _ := proto.Marshal(ch)
payload = &Payload{
Header: &Header{
ChannelHeader: chbytes,
},
}
msg, err = payload.VariablyOpaqueFieldProto("data")
assert.Equal(t, &ConfigEnvelope{}, msg)
assert.NoError(t, err)

ch = &ChannelHeader{
Type: int32(HeaderType_CONFIG_UPDATE),
}
chbytes, _ = proto.Marshal(ch)
payload = &Payload{
Header: &Header{
ChannelHeader: chbytes,
},
}
msg, err = payload.VariablyOpaqueFieldProto("data")
assert.Equal(t, &ConfigUpdateEnvelope{}, msg)
assert.NoError(t, err)

ch = &ChannelHeader{
Type: int32(HeaderType_CHAINCODE_PACKAGE),
}
chbytes, _ = proto.Marshal(ch)
payload = &Payload{
Header: &Header{
ChannelHeader: chbytes,
},
}
msg, err = payload.VariablyOpaqueFieldProto("data")
assert.Nil(t, msg)
assert.Error(t, err)

// Header
var header *Header
assert.Equal(t, []string{"channel_header", "signature_header"},
header.StaticallyOpaqueFields())

msg, err = header.StaticallyOpaqueFieldProto("badproto")
assert.Nil(t, msg)
assert.Error(t, err)

msg, err = header.StaticallyOpaqueFieldProto("channel_header")
assert.Equal(t, &ChannelHeader{}, msg)
assert.NoError(t, err)

msg, err = header.StaticallyOpaqueFieldProto("signature_header")
assert.Equal(t, &SignatureHeader{}, msg)
assert.NoError(t, err)

// BlockData
var bd *BlockData
assert.Equal(t, []string{"data"}, bd.StaticallyOpaqueSliceFields())

msg, err = bd.StaticallyOpaqueSliceFieldProto("badslice", 0)
assert.Nil(t, msg)
assert.Error(t, err)
msg, err = bd.StaticallyOpaqueSliceFieldProto("data", 0)
assert.Equal(t, &Envelope{}, msg)
assert.NoError(t, err)

}
Loading

0 comments on commit a5b0bf9

Please sign in to comment.