github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/core/scc/vscc/validator_onevalidsignature_test.go (about)

     1  /*
     2  Copyright IBM Corp. 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  package vscc
    17  
    18  import (
    19  	"testing"
    20  
    21  	"fmt"
    22  	"os"
    23  
    24  	"github.com/hyperledger/fabric/common/cauthdsl"
    25  	"github.com/hyperledger/fabric/common/util"
    26  	"github.com/hyperledger/fabric/core/chaincode/shim"
    27  	"github.com/hyperledger/fabric/msp"
    28  	mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
    29  	"github.com/hyperledger/fabric/msp/mgmt/testtools"
    30  	"github.com/hyperledger/fabric/protos/common"
    31  	"github.com/hyperledger/fabric/protos/peer"
    32  	"github.com/hyperledger/fabric/protos/utils"
    33  )
    34  
    35  func createTx() (*common.Envelope, error) {
    36  	cis := &peer.ChaincodeInvocationSpec{ChaincodeSpec: &peer.ChaincodeSpec{ChaincodeId: &peer.ChaincodeID{Name: "foo"}}}
    37  
    38  	prop, _, err := utils.CreateProposalFromCIS(common.HeaderType_ENDORSER_TRANSACTION, util.GetTestChainID(), cis, sid)
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  
    43  	presp, err := utils.CreateProposalResponse(prop.Header, prop.Payload, &peer.Response{Status: 200}, []byte("res"), nil, nil, id)
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  
    48  	return utils.CreateSignedTx(prop, id, presp)
    49  }
    50  
    51  func TestInit(t *testing.T) {
    52  	v := new(ValidatorOneValidSignature)
    53  	stub := shim.NewMockStub("validatoronevalidsignature", v)
    54  
    55  	if res := stub.MockInit("1", nil); res.Status != shim.OK {
    56  		t.Fatalf("vscc init failed with %s", res.Message)
    57  	}
    58  }
    59  
    60  func getSignedByMSPMemberPolicy(mspID string) ([]byte, error) {
    61  	p := cauthdsl.SignedByMspMember(mspID)
    62  
    63  	b, err := utils.Marshal(p)
    64  	if err != nil {
    65  		return nil, fmt.Errorf("Could not marshal policy, err %s", err)
    66  	}
    67  
    68  	return b, err
    69  }
    70  
    71  func TestInvoke(t *testing.T) {
    72  	v := new(ValidatorOneValidSignature)
    73  	stub := shim.NewMockStub("validatoronevalidsignature", v)
    74  
    75  	// Failed path: Invalid arguments
    76  	args := [][]byte{[]byte("dv")}
    77  	if res := stub.MockInvoke("1", args); res.Status == shim.OK {
    78  		t.Fatalf("vscc invoke should have failed")
    79  		return
    80  	}
    81  
    82  	args = [][]byte{[]byte("dv"), []byte("tx")}
    83  	args[1] = nil
    84  	if res := stub.MockInvoke("1", args); res.Status == shim.OK {
    85  		t.Fatalf("vscc invoke should have failed")
    86  		return
    87  	}
    88  
    89  	tx, err := createTx()
    90  	if err != nil {
    91  		t.Fatalf("createTx returned err %s", err)
    92  		return
    93  	}
    94  
    95  	envBytes, err := utils.GetBytesEnvelope(tx)
    96  	if err != nil {
    97  		t.Fatalf("GetBytesEnvelope returned err %s", err)
    98  		return
    99  	}
   100  
   101  	// good path: signed by the right MSP
   102  	policy, err := getSignedByMSPMemberPolicy(mspid)
   103  	if err != nil {
   104  		t.Fatalf("failed getting policy, err %s", err)
   105  		return
   106  	}
   107  
   108  	args = [][]byte{[]byte("dv"), envBytes, policy}
   109  	if res := stub.MockInvoke("1", args); res.Status != shim.OK {
   110  		t.Fatalf("vscc invoke returned err %s", err)
   111  		return
   112  	}
   113  
   114  	// bad path: signed by the wrong MSP
   115  	policy, err = getSignedByMSPMemberPolicy("barf")
   116  	if err != nil {
   117  		t.Fatalf("failed getting policy, err %s", err)
   118  		return
   119  	}
   120  
   121  	args = [][]byte{[]byte("dv"), envBytes, policy}
   122  	if res := stub.MockInvoke("1", args); res.Status == shim.OK {
   123  		t.Fatalf("vscc invoke should have failed")
   124  		return
   125  	}
   126  }
   127  
   128  var id msp.SigningIdentity
   129  var sid []byte
   130  var mspid string
   131  var chainId string = util.GetTestChainID()
   132  
   133  func TestMain(m *testing.M) {
   134  	var err error
   135  
   136  	// setup the MSP manager so that we can sign/verify
   137  	mspMgrConfigDir := "../../../msp/sampleconfig/"
   138  	msptesttools.LoadMSPSetupForTesting(mspMgrConfigDir)
   139  
   140  	id, err = mspmgmt.GetLocalMSP().GetDefaultSigningIdentity()
   141  	if err != nil {
   142  		fmt.Printf("GetSigningIdentity failed with err %s", err)
   143  		os.Exit(-1)
   144  	}
   145  
   146  	sid, err = id.Serialize()
   147  	if err != nil {
   148  		fmt.Printf("Serialize failed with err %s", err)
   149  		os.Exit(-1)
   150  	}
   151  
   152  	// determine the MSP identifier for the first MSP in the default chain
   153  	var msp msp.MSP
   154  	mspMgr := mspmgmt.GetManagerForChain(chainId)
   155  	msps, err := mspMgr.GetMSPs()
   156  	if err != nil {
   157  		fmt.Printf("Could not retrieve the MSPs for the chain manager, err %s", err)
   158  		os.Exit(-1)
   159  	}
   160  	if len(msps) == 0 {
   161  		fmt.Printf("At least one MSP was expected")
   162  		os.Exit(-1)
   163  	}
   164  	for _, m := range msps {
   165  		msp = m
   166  		break
   167  	}
   168  	mspid, err = msp.GetIdentifier()
   169  	if err != nil {
   170  		fmt.Printf("Failure getting the msp identifier, err %s", err)
   171  		os.Exit(-1)
   172  	}
   173  
   174  	os.Exit(m.Run())
   175  }