github.com/darrenli6/fabric-sdk-example@v0.0.0-20220109053535-94b13b56df8c/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  	"github.com/hyperledger/fabric/bccsp/factory"
    24  	genesisconfig "github.com/hyperledger/fabric/common/configtx/tool/localconfig"
    25  	"github.com/hyperledger/fabric/common/configtx/tool/provisional"
    26  	"github.com/hyperledger/fabric/peer/common"
    27  	pb "github.com/hyperledger/fabric/protos/peer"
    28  	"github.com/hyperledger/fabric/protos/utils"
    29  	"github.com/spf13/cobra"
    30  	"github.com/stretchr/testify/assert"
    31  	"github.com/stretchr/testify/require"
    32  )
    33  
    34  func TestCheckChaincodeCmdParamsWithNewCallingSchema(t *testing.T) {
    35  	chaincodeCtorJSON = `{ "Args":["func", "param"] }`
    36  	chaincodePath = "some/path"
    37  	chaincodeName = "somename"
    38  	require := require.New(t)
    39  	result := checkChaincodeCmdParams(&cobra.Command{})
    40  
    41  	require.Nil(result)
    42  }
    43  
    44  func TestCheckChaincodeCmdParamsWithOldCallingSchema(t *testing.T) {
    45  	chaincodeCtorJSON = `{ "Function":"func", "Args":["param"] }`
    46  	chaincodePath = "some/path"
    47  	chaincodeName = "somename"
    48  	require := require.New(t)
    49  	result := checkChaincodeCmdParams(&cobra.Command{})
    50  
    51  	require.Nil(result)
    52  }
    53  
    54  func TestCheckChaincodeCmdParamsWithoutName(t *testing.T) {
    55  	chaincodeCtorJSON = `{ "Function":"func", "Args":["param"] }`
    56  	chaincodePath = "some/path"
    57  	chaincodeName = ""
    58  	require := require.New(t)
    59  	result := checkChaincodeCmdParams(&cobra.Command{})
    60  
    61  	require.Error(result)
    62  }
    63  
    64  func TestCheckChaincodeCmdParamsWithFunctionOnly(t *testing.T) {
    65  	chaincodeCtorJSON = `{ "Function":"func" }`
    66  	chaincodePath = "some/path"
    67  	chaincodeName = "somename"
    68  	require := require.New(t)
    69  	result := checkChaincodeCmdParams(&cobra.Command{})
    70  
    71  	require.Error(result)
    72  }
    73  
    74  func TestCheckChaincodeCmdParamsEmptyCtor(t *testing.T) {
    75  	chaincodeCtorJSON = `{}`
    76  	chaincodePath = "some/path"
    77  	chaincodeName = "somename"
    78  	require := require.New(t)
    79  	result := checkChaincodeCmdParams(&cobra.Command{})
    80  
    81  	require.Error(result)
    82  }
    83  
    84  func TestCheckValidJSON(t *testing.T) {
    85  	validJSON := `{"Args":["a","b","c"]}`
    86  	input := &pb.ChaincodeInput{}
    87  	if err := json.Unmarshal([]byte(validJSON), &input); err != nil {
    88  		t.Fail()
    89  		t.Logf("Chaincode argument error: %s", err)
    90  		return
    91  	}
    92  
    93  	validJSON = `{"Function":"f", "Args":["a","b","c"]}`
    94  	if err := json.Unmarshal([]byte(validJSON), &input); err != nil {
    95  		t.Fail()
    96  		t.Logf("Chaincode argument error: %s", err)
    97  		return
    98  	}
    99  
   100  	validJSON = `{"Function":"f", "Args":[]}`
   101  	if err := json.Unmarshal([]byte(validJSON), &input); err != nil {
   102  		t.Fail()
   103  		t.Logf("Chaincode argument error: %s", err)
   104  		return
   105  	}
   106  
   107  	validJSON = `{"Function":"f"}`
   108  	if err := json.Unmarshal([]byte(validJSON), &input); err != nil {
   109  		t.Fail()
   110  		t.Logf("Chaincode argument error: %s", err)
   111  		return
   112  	}
   113  }
   114  
   115  func TestCheckInvalidJSON(t *testing.T) {
   116  	invalidJSON := `{["a","b","c"]}`
   117  	input := &pb.ChaincodeInput{}
   118  	if err := json.Unmarshal([]byte(invalidJSON), &input); err == nil {
   119  		t.Fail()
   120  		t.Logf("Bar argument error should have been caught: %s", invalidJSON)
   121  		return
   122  	}
   123  
   124  	invalidJSON = `{"Function":}`
   125  	if err := json.Unmarshal([]byte(invalidJSON), &input); err == nil {
   126  		t.Fail()
   127  		t.Logf("Chaincode argument error: %s", err)
   128  		t.Logf("Bar argument error should have been caught: %s", invalidJSON)
   129  		return
   130  	}
   131  }
   132  
   133  func TestGetOrdererEndpointFromConfigTx(t *testing.T) {
   134  	initMSP()
   135  
   136  	signer, err := common.GetDefaultSigner()
   137  	assert.NoError(t, err)
   138  
   139  	mockchain := "mockchain"
   140  	factory.InitFactories(nil)
   141  	config := genesisconfig.Load(genesisconfig.SampleInsecureProfile)
   142  	pgen := provisional.New(config)
   143  	genesisBlock := pgen.GenesisBlockForChannel(mockchain)
   144  
   145  	mockResponse := &pb.ProposalResponse{
   146  		Response:    &pb.Response{Status: 200, Payload: utils.MarshalOrPanic(genesisBlock)},
   147  		Endorsement: &pb.Endorsement{},
   148  	}
   149  	mockEndorserClient := common.GetMockEndorserClient(mockResponse, nil)
   150  
   151  	ordererEndpoints, err := common.GetOrdererEndpointOfChain(mockchain, signer, mockEndorserClient)
   152  	assert.NoError(t, err, "GetOrdererEndpointOfChain from genesis block")
   153  
   154  	assert.Equal(t, len(ordererEndpoints), 1)
   155  	assert.Equal(t, ordererEndpoints[0], "127.0.0.1:7050")
   156  }
   157  
   158  func TestGetOrdererEndpointFail(t *testing.T) {
   159  	initMSP()
   160  
   161  	signer, err := common.GetDefaultSigner()
   162  	assert.NoError(t, err)
   163  
   164  	mockchain := "mockchain"
   165  	factory.InitFactories(nil)
   166  
   167  	mockResponse := &pb.ProposalResponse{
   168  		Response:    &pb.Response{Status: 404, Payload: []byte{}},
   169  		Endorsement: &pb.Endorsement{},
   170  	}
   171  	mockEndorserClient := common.GetMockEndorserClient(mockResponse, nil)
   172  
   173  	_, err = common.GetOrdererEndpointOfChain(mockchain, signer, mockEndorserClient)
   174  	assert.Error(t, err, "GetOrdererEndpointOfChain from invalid response")
   175  }