diff --git a/peer/chaincode/common.go b/peer/chaincode/common.go index cf0f6f8d254..2ef4e60a0a2 100755 --- a/peer/chaincode/common.go +++ b/peer/chaincode/common.go @@ -187,19 +187,17 @@ func checkChaincodeCmdParams(cmd *cobra.Command) error { return fmt.Errorf("Chaincode argument error: %s", err) } m := f.(map[string]interface{}) - if len(m) != 2 { - return fmt.Errorf("Non-empty JSON chaincode parameters must contain exactly 2 keys: 'Function' and 'Args'") - } + sm := make(map[string]interface{}) for k := range m { - switch strings.ToLower(k) { - case "args": - case "function": - default: - return fmt.Errorf("Illegal chaincode key '%s' - must be 'Function' or 'Args'", k) - } + sm[strings.ToLower(k)] = m[k] + } + _, argsPresent := sm["args"] + _, funcPresent := sm["function"] + if !argsPresent || (len(m) == 2 && !funcPresent) || len(m) > 2 { + return fmt.Errorf("Non-empty JSON chaincode parameters must contain the following keys: 'Args' or 'Function' and 'Args'") } } else { - return errors.New("Empty JSON chaincode parameters must contain exactly 2 keys: 'Function' and 'Args'") + return errors.New("Empty JSON chaincode parameters must contain the following keys: 'Args' or 'Function' and 'Args'") } if chaincodeAttributesJSON != "[]" { diff --git a/peer/chaincode/common_test.go b/peer/chaincode/common_test.go new file mode 100644 index 00000000000..2ba6fc592f1 --- /dev/null +++ b/peer/chaincode/common_test.go @@ -0,0 +1,63 @@ +/* + Copyright Digital Asset Holdings, LLC 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 chaincode + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestCheckChaincodeCmdParamsWithNewCallingSchema(t *testing.T) { + chaincodeAttributesJSON = "[]" + chaincodeCtorJSON = `{ "Args":["func", "param"] }` + chaincodePath = "some/path" + require := require.New(t) + result := checkChaincodeCmdParams(nil) + + require.Nil(result) +} + +func TestCheckChaincodeCmdParamsWithOldCallingSchema(t *testing.T) { + chaincodeAttributesJSON = "[]" + chaincodeCtorJSON = `{ "Function":"func", "Args":["param"] }` + chaincodePath = "some/path" + require := require.New(t) + result := checkChaincodeCmdParams(nil) + + require.Nil(result) +} + +func TestCheckChaincodeCmdParamsWithFunctionOnly(t *testing.T) { + chaincodeAttributesJSON = "[]" + chaincodeCtorJSON = `{ "Function":"func" }` + chaincodePath = "some/path" + require := require.New(t) + result := checkChaincodeCmdParams(nil) + + require.Error(result) +} + +func TestCheckChaincodeCmdParamsEmptyCtor(t *testing.T) { + chaincodeAttributesJSON = "[]" + chaincodeCtorJSON = `{}` + chaincodePath = "some/path" + require := require.New(t) + result := checkChaincodeCmdParams(nil) + + require.Error(result) +}