github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/peer/chaincode/common_test.go (about)

     1  /*
     2   Copyright Digital Asset Holdings, LLC 2016 All Rights Reserved.
     3  
     4   Licensed under the Apache License, Version 2.0 (the "License");
     5   you may not use this file except in compliance with the License.
     6   You may obtain a copy of the License at
     7  
     8        http://www.apache.org/licenses/LICENSE-2.0
     9  
    10   Unless required by applicable law or agreed to in writing, software
    11   distributed under the License is distributed on an "AS IS" BASIS,
    12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   See the License for the specific language governing permissions and
    14   limitations under the License.
    15  */
    16  
    17  package chaincode
    18  
    19  import (
    20  	"encoding/json"
    21  	"testing"
    22  
    23  	pb "github.com/hyperledger/fabric/protos/peer"
    24  	"github.com/spf13/cobra"
    25  	"github.com/stretchr/testify/require"
    26  )
    27  
    28  func TestCheckChaincodeCmdParamsWithNewCallingSchema(t *testing.T) {
    29  	chaincodeCtorJSON = `{ "Args":["func", "param"] }`
    30  	chaincodePath = "some/path"
    31  	chaincodeName = "somename"
    32  	require := require.New(t)
    33  	result := checkChaincodeCmdParams(&cobra.Command{})
    34  
    35  	require.Nil(result)
    36  }
    37  
    38  func TestCheckChaincodeCmdParamsWithOldCallingSchema(t *testing.T) {
    39  	chaincodeCtorJSON = `{ "Function":"func", "Args":["param"] }`
    40  	chaincodePath = "some/path"
    41  	chaincodeName = "somename"
    42  	require := require.New(t)
    43  	result := checkChaincodeCmdParams(&cobra.Command{})
    44  
    45  	require.Nil(result)
    46  }
    47  
    48  func TestCheckChaincodeCmdParamsWithoutName(t *testing.T) {
    49  	chaincodeCtorJSON = `{ "Function":"func", "Args":["param"] }`
    50  	chaincodePath = "some/path"
    51  	chaincodeName = ""
    52  	require := require.New(t)
    53  	result := checkChaincodeCmdParams(&cobra.Command{})
    54  
    55  	require.Error(result)
    56  }
    57  
    58  func TestCheckChaincodeCmdParamsWithFunctionOnly(t *testing.T) {
    59  	chaincodeCtorJSON = `{ "Function":"func" }`
    60  	chaincodePath = "some/path"
    61  	chaincodeName = "somename"
    62  	require := require.New(t)
    63  	result := checkChaincodeCmdParams(&cobra.Command{})
    64  
    65  	require.Error(result)
    66  }
    67  
    68  func TestCheckChaincodeCmdParamsEmptyCtor(t *testing.T) {
    69  	chaincodeCtorJSON = `{}`
    70  	chaincodePath = "some/path"
    71  	chaincodeName = "somename"
    72  	require := require.New(t)
    73  	result := checkChaincodeCmdParams(&cobra.Command{})
    74  
    75  	require.Error(result)
    76  }
    77  
    78  func TestCheckValidJSON(t *testing.T) {
    79  	validJSON := `{"Args":["a","b","c"]}`
    80  	input := &pb.ChaincodeInput{}
    81  	if err := json.Unmarshal([]byte(validJSON), &input); err != nil {
    82  		t.Fail()
    83  		t.Logf("Chaincode argument error: %s", err)
    84  		return
    85  	}
    86  
    87  	validJSON = `{"Function":"f", "Args":["a","b","c"]}`
    88  	if err := json.Unmarshal([]byte(validJSON), &input); err != nil {
    89  		t.Fail()
    90  		t.Logf("Chaincode argument error: %s", err)
    91  		return
    92  	}
    93  
    94  	validJSON = `{"Function":"f", "Args":[]}`
    95  	if err := json.Unmarshal([]byte(validJSON), &input); err != nil {
    96  		t.Fail()
    97  		t.Logf("Chaincode argument error: %s", err)
    98  		return
    99  	}
   100  
   101  	validJSON = `{"Function":"f"}`
   102  	if err := json.Unmarshal([]byte(validJSON), &input); err != nil {
   103  		t.Fail()
   104  		t.Logf("Chaincode argument error: %s", err)
   105  		return
   106  	}
   107  }
   108  
   109  func TestCheckInvalidJSON(t *testing.T) {
   110  	invalidJSON := `{["a","b","c"]}`
   111  	input := &pb.ChaincodeInput{}
   112  	if err := json.Unmarshal([]byte(invalidJSON), &input); err == nil {
   113  		t.Fail()
   114  		t.Logf("Bar argument error should have been caught: %s", invalidJSON)
   115  		return
   116  	}
   117  
   118  	invalidJSON = `{"Function":}`
   119  	if err := json.Unmarshal([]byte(invalidJSON), &input); err == nil {
   120  		t.Fail()
   121  		t.Logf("Chaincode argument error: %s", err)
   122  		t.Logf("Bar argument error should have been caught: %s", invalidJSON)
   123  		return
   124  	}
   125  }