From 1b844c2370b55561a5209ba1be7daa043ea47d2e Mon Sep 17 00:00:00 2001 From: Srinivasan Muralidharan Date: Sun, 27 Nov 2016 19:25:24 -0500 Subject: [PATCH] FAB-1200 wrong type assertion on ledger.KV https://jira.hyperledger.org/browse/FAB-1200 While investing table structures (still in works) found that chaincode handler was asserting the wrong KV type. Also found that an UnmarshallJSON was missing on proto.ChaincodeInput. This function was defined in transaction.go (wrong file for a ChaincodeInput function!) which went away when old Transaction was axed. Interesting that the tests in common_test.go did not catch that. Added explicit tests for Unmarshalling JSON into ChaincodeInput. Change-Id: I3adc92c888efee974c5f537bc911cb0a253d1e01 Signed-off-by: Srinivasan Muralidharan --- core/chaincode/handler.go | 6 ++-- peer/chaincode/common_test.go | 51 ++++++++++++++++++++++++++++++ protos/peer/chaincodeunmarshall.go | 44 ++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 protos/peer/chaincodeunmarshall.go diff --git a/core/chaincode/handler.go b/core/chaincode/handler.go index 240e414d651..591f66f0b40 100644 --- a/core/chaincode/handler.go +++ b/core/chaincode/handler.go @@ -636,8 +636,7 @@ func (handler *Handler) handleRangeQueryState(msg *pb.ChaincodeMessage) { if qresult == nil { break } - //PDMP - let it panic if not KV - kv := qresult.(ledger.KV) + kv := qresult.(*ledger.KV) keyAndValue := pb.RangeQueryStateKeyValue{Key: kv.Key, Value: kv.Value} keysAndValues = append(keysAndValues, &keyAndValue) } @@ -735,8 +734,7 @@ func (handler *Handler) handleRangeQueryStateNext(msg *pb.ChaincodeMessage) { if qresult != nil { break } - //PDMP - let it panic if not KV - kv := qresult.(ledger.KV) + kv := qresult.(*ledger.KV) keyAndValue := pb.RangeQueryStateKeyValue{Key: kv.Key, Value: kv.Value} keysAndValues = append(keysAndValues, &keyAndValue) } diff --git a/peer/chaincode/common_test.go b/peer/chaincode/common_test.go index 33dc564ffbe..2de1dde15c9 100644 --- a/peer/chaincode/common_test.go +++ b/peer/chaincode/common_test.go @@ -17,8 +17,10 @@ package chaincode import ( + "encoding/json" "testing" + pb "github.com/hyperledger/fabric/protos/peer" "github.com/stretchr/testify/require" ) @@ -76,3 +78,52 @@ func TestCheckChaincodeCmdParamsEmptyCtor(t *testing.T) { require.Error(result) } + +func TestCheckValidJSON(t *testing.T) { + validJSON := `{"Args":["a","b","c"]}` + input := &pb.ChaincodeInput{} + if err := json.Unmarshal([]byte(validJSON), &input); err != nil { + t.Fail() + t.Logf("Chaincode argument error: %s", err) + return + } + + validJSON = `{"Function":"f", "Args":["a","b","c"]}` + if err := json.Unmarshal([]byte(validJSON), &input); err != nil { + t.Fail() + t.Logf("Chaincode argument error: %s", err) + return + } + + validJSON = `{"Function":"f", "Args":[]}` + if err := json.Unmarshal([]byte(validJSON), &input); err != nil { + t.Fail() + t.Logf("Chaincode argument error: %s", err) + return + } + + validJSON = `{"Function":"f"}` + if err := json.Unmarshal([]byte(validJSON), &input); err != nil { + t.Fail() + t.Logf("Chaincode argument error: %s", err) + return + } +} + +func TestCheckInvalidJSON(t *testing.T) { + invalidJSON := `{["a","b","c"]}` + input := &pb.ChaincodeInput{} + if err := json.Unmarshal([]byte(invalidJSON), &input); err == nil { + t.Fail() + t.Logf("Bar argument error should have been caught: %s", invalidJSON) + return + } + + invalidJSON = `{"Function":}` + if err := json.Unmarshal([]byte(invalidJSON), &input); err == nil { + t.Fail() + t.Logf("Chaincode argument error: %s", err) + t.Logf("Bar argument error should have been caught: %s", invalidJSON) + return + } +} diff --git a/protos/peer/chaincodeunmarshall.go b/protos/peer/chaincodeunmarshall.go new file mode 100644 index 00000000000..97ae74e177d --- /dev/null +++ b/protos/peer/chaincodeunmarshall.go @@ -0,0 +1,44 @@ +/* +Copyright IBM Corp. 2016 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 peer + +import ( + "encoding/json" + + "github.com/hyperledger/fabric/core/util" +) + +type strArgs struct { + Function string + Args []string +} + +// UnmarshalJSON converts the string-based REST/JSON input to +// the []byte-based current ChaincodeInput structure. +func (c *ChaincodeInput) UnmarshalJSON(b []byte) error { + sa := &strArgs{} + err := json.Unmarshal(b, sa) + if err != nil { + return err + } + allArgs := sa.Args + if sa.Function != "" { + allArgs = append([]string{sa.Function}, sa.Args...) + } + c.Args = util.ToChaincodeArgs(allArgs...) + return nil +}