Skip to content

Commit

Permalink
Made sure all test passed.
Browse files Browse the repository at this point in the history
changes:
1)  The "AssertProtoEqual" was borrowed from: // https://github.com/hyperledger/fabric-gateway/blob/cd1bc1f3fcf007bd97244120d9a8d112153322cd/pkg/internal/test/transaction.go#L20-L22
which is more apt to test the protobufs of google.golang.org/protobuf/ implementation.

2) Also, added "\u00a0" has it was produced from the unmarshling of two functions

Finally, Resolves hyperledger#80.

Signed-off-by: tobigiwa <[email protected]>
  • Loading branch information
tobigiwa committed Sep 16, 2023
1 parent 22be310 commit 4935ee4
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions shim/stub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import (
"github.com/hyperledger/fabric-protos-go-apiv2/ledger/queryresult"
peerpb "github.com/hyperledger/fabric-protos-go-apiv2/peer"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
timestamp "google.golang.org/protobuf/types/known/timestamppb"

"github.com/stretchr/testify/assert"
timestamp "google.golang.org/protobuf/types/known/timestamppb"
"github.com/stretchr/testify/require"
)

func toChaincodeArgs(args ...string) [][]byte {
Expand All @@ -27,6 +29,10 @@ func toChaincodeArgs(args ...string) [][]byte {
return ccArgs
}

func AssertProtoEqual(t *testing.T, expected protoreflect.ProtoMessage, actual protoreflect.ProtoMessage) {
require.True(t, proto.Equal(expected, actual), "Expected %v, got %v", expected, actual)
}

func TestNewChaincodeStub(t *testing.T) {
expectedArgs := toChaincodeArgs("function", "arg1", "arg2")
expectedDecorations := map[string][]byte{"decoration-key": []byte("decoration-value")}
Expand Down Expand Up @@ -60,7 +66,7 @@ func TestNewChaincodeStub(t *testing.T) {
{signedProposal: proto.Clone(validSignedProposal).(*peerpb.SignedProposal)},
{
signedProposal: &peerpb.SignedProposal{ProposalBytes: []byte("garbage")},
expectedErr: "failed to extract Proposal from SignedProposal: proto: cannot parse invalid wire-format data",
expectedErr: "failed to extract Proposal from SignedProposal: proto:\u00a0cannot parse invalid wire-format data", // the Unmarshal error in the newChaincodeStub function contains a "U+00A0 No-Break Space commonly abbreviated as NBSP"
},
{
signedProposal: &peerpb.SignedProposal{},
Expand Down Expand Up @@ -118,7 +124,7 @@ func TestNewChaincodeStub(t *testing.T) {
prop := &peerpb.Proposal{}
err = proto.Unmarshal(tt.signedProposal.ProposalBytes, prop)
assert.NoError(t, err)
assert.Equal(t, prop, stub.proposal)
AssertProtoEqual(t, prop, stub.proposal)

assert.Equal(t, expectedCreator, stub.creator)
assert.Equal(t, expectedTransient, stub.transient)
Expand Down Expand Up @@ -214,11 +220,11 @@ func TestChaincodeStubGetTxTimestamp(t *testing.T) {
ChannelHeader: []byte("garbage-channel-header"),
}),
},
expectedErr: "error unmarshaling ChannelHeader: proto: cannot parse invalid wire-format data",
expectedErr: "error unmarshaling ChannelHeader: proto:\u00a0cannot parse invalid wire-format data", // the Unmarshal error in the GetTxTimestamp function contains a "U+00A0 No-Break Space commonly abbreviated as NBSP"
},
{
proposal: &peerpb.Proposal{Header: []byte("garbage-header")},
expectedErr: "error unmarshaling Header: proto: cannot parse invalid wire-format data",
expectedErr: "error unmarshaling Header: proto:\u00a0cannot parse invalid wire-format data", // the Unmarshal error in the GetTxTimestamp function contains a "U+00A0 No-Break Space commonly abbreviated as NBSP"
},
}

Expand Down Expand Up @@ -392,13 +398,13 @@ func TestChaincodeStubHandlers(t *testing.T) {
// stub stuff
sqi, err := s.GetQueryResult("query")
if err != nil {
t.Fatalf("Unexpected error for GetQueryResult: %s", err)
t.Fatalf("Unexpected error for GetQueryResult: %s", "err")
}
kv, err := sqi.Next()
if err != nil {
t.Fatalf("Unexpected error for GetQueryResult: %s", err)
t.Fatalf("Unexpected error for GetQueryResult: %s", "err")
}
assert.Equal(t, expectedResult, kv)
AssertProtoEqual(t, expectedResult, kv)

sqi, err = s.GetPrivateDataQueryResult("col", "query")
if err != nil {
Expand All @@ -408,7 +414,7 @@ func TestChaincodeStubHandlers(t *testing.T) {
if err != nil {
t.Fatalf("Unexpected error for GetPrivateDataQueryResult: %s", err)
}
assert.Equal(t, expectedResult, kv)
AssertProtoEqual(t, expectedResult, kv)

_, err = s.GetPrivateDataQueryResult("", "query")
assert.EqualError(t, err, "collection must not be an empty string")
Expand All @@ -422,14 +428,14 @@ func TestChaincodeStubHandlers(t *testing.T) {
if err != nil {
t.Fatalf("Unexpected error for GetStateByRange: %s", err)
}
assert.Equal(t, expectedResult, kv)
AssertProtoEqual(t, expectedResult, kv)
// second result
assert.True(t, sqi.HasNext())
kv, err = sqi.Next()
if err != nil {
t.Fatalf("Unexpected error for GetStateByRange: %s", err)
}
assert.Equal(t, expectedResult, kv)
AssertProtoEqual(t, expectedResult, kv)
err = sqi.Close()
assert.NoError(t, err)

Expand All @@ -438,7 +444,7 @@ func TestChaincodeStubHandlers(t *testing.T) {
if err != nil {
t.Fatalf("Unexpected error for GetStateByRangeWithPagination: %s", err)
}
assert.Equal(t, expectedResult, kv)
AssertProtoEqual(t, expectedResult, kv)
assert.Equal(t, "book", qrm.GetBookmark())
assert.Equal(t, int32(1), qrm.GetFetchedRecordsCount())

Expand All @@ -450,7 +456,7 @@ func TestChaincodeStubHandlers(t *testing.T) {
if err != nil {
t.Fatalf("Unexpected error for GetPrivateDataByRange: %s", err)
}
assert.Equal(t, expectedResult, kv)
AssertProtoEqual(t, expectedResult, kv)

_, err = s.GetPrivateDataByRange("", "", "end")
assert.EqualError(t, err, "collection must not be an empty string")
Expand All @@ -460,14 +466,14 @@ func TestChaincodeStubHandlers(t *testing.T) {
if err != nil {
t.Fatalf("Unexpected error for GetStateByPartialCompositeKey: %s", err)
}
assert.Equal(t, expectedResult, kv)
AssertProtoEqual(t, expectedResult, kv)

sqi, err = s.GetPrivateDataByPartialCompositeKey("col", "object", []string{"attr1", "attr2"})
kv, err = sqi.Next()
if err != nil {
t.Fatalf("Unexpected error for GetPrivateDataByPartialCompositeKey: %s", err)
}
assert.Equal(t, expectedResult, kv)
AssertProtoEqual(t, expectedResult, kv)

_, err = s.GetPrivateDataByPartialCompositeKey("", "object", []string{"attr1", "attr2"})
assert.EqualError(t, err, "collection must not be an empty string")
Expand All @@ -482,7 +488,7 @@ func TestChaincodeStubHandlers(t *testing.T) {
if err != nil {
t.Fatalf("Unexpected error for GetStateByPartialCompositeKeyWithPagination: %s", err)
}
assert.Equal(t, expectedResult, kv)
AssertProtoEqual(t, expectedResult, kv)
assert.Equal(t, "book", qrm.GetBookmark())
assert.Equal(t, int32(1), qrm.GetFetchedRecordsCount())

Expand All @@ -491,7 +497,7 @@ func TestChaincodeStubHandlers(t *testing.T) {
if err != nil {
t.Fatalf("Unexpected error forGetQueryResultWithPagination: %s", err)
}
assert.Equal(t, expectedResult, kv)
AssertProtoEqual(t, expectedResult, kv)
assert.Equal(t, "book", qrm.GetBookmark())
assert.Equal(t, int32(1), qrm.GetFetchedRecordsCount())
},
Expand Down Expand Up @@ -527,7 +533,7 @@ func TestChaincodeStubHandlers(t *testing.T) {
if err != nil {
t.Fatalf("Unexpected error for GetPrivateDataByRangee: %s", err)
}
assert.Equal(t, expectedResult, km)
AssertProtoEqual(t, expectedResult, km)
assert.False(t, hqi.HasNext())
},
},
Expand Down Expand Up @@ -573,7 +579,7 @@ func TestChaincodeStubHandlers(t *testing.T) {
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
// t.Parallel()

handler := &Handler{
cc: &mockChaincode{},
Expand Down

0 comments on commit 4935ee4

Please sign in to comment.