github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/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 ccid := &peer.ChaincodeID{Name: "foo", Version: "v1"} 37 cis := &peer.ChaincodeInvocationSpec{ChaincodeSpec: &peer.ChaincodeSpec{ChaincodeId: ccid}} 38 39 prop, _, err := utils.CreateProposalFromCIS(common.HeaderType_ENDORSER_TRANSACTION, util.GetTestChainID(), cis, sid) 40 if err != nil { 41 return nil, err 42 } 43 44 presp, err := utils.CreateProposalResponse(prop.Header, prop.Payload, &peer.Response{Status: 200}, []byte("res"), nil, ccid, nil, id) 45 if err != nil { 46 return nil, err 47 } 48 49 return utils.CreateSignedTx(prop, id, presp) 50 } 51 52 func TestInit(t *testing.T) { 53 v := new(ValidatorOneValidSignature) 54 stub := shim.NewMockStub("validatoronevalidsignature", v) 55 56 if res := stub.MockInit("1", nil); res.Status != shim.OK { 57 t.Fatalf("vscc init failed with %s", res.Message) 58 } 59 } 60 61 func getSignedByMSPMemberPolicy(mspID string) ([]byte, error) { 62 p := cauthdsl.SignedByMspMember(mspID) 63 64 b, err := utils.Marshal(p) 65 if err != nil { 66 return nil, fmt.Errorf("Could not marshal policy, err %s", err) 67 } 68 69 return b, err 70 } 71 72 func TestInvoke(t *testing.T) { 73 v := new(ValidatorOneValidSignature) 74 stub := shim.NewMockStub("validatoronevalidsignature", v) 75 76 // Failed path: Invalid arguments 77 args := [][]byte{[]byte("dv")} 78 if res := stub.MockInvoke("1", args); res.Status == shim.OK { 79 t.Fatalf("vscc invoke should have failed") 80 return 81 } 82 83 args = [][]byte{[]byte("dv"), []byte("tx")} 84 args[1] = nil 85 if res := stub.MockInvoke("1", args); res.Status == shim.OK { 86 t.Fatalf("vscc invoke should have failed") 87 return 88 } 89 90 tx, err := createTx() 91 if err != nil { 92 t.Fatalf("createTx returned err %s", err) 93 return 94 } 95 96 envBytes, err := utils.GetBytesEnvelope(tx) 97 if err != nil { 98 t.Fatalf("GetBytesEnvelope returned err %s", err) 99 return 100 } 101 102 // good path: signed by the right MSP 103 policy, err := getSignedByMSPMemberPolicy(mspid) 104 if err != nil { 105 t.Fatalf("failed getting policy, err %s", err) 106 return 107 } 108 109 args = [][]byte{[]byte("dv"), envBytes, policy} 110 if res := stub.MockInvoke("1", args); res.Status != shim.OK { 111 t.Fatalf("vscc invoke returned err %s", err) 112 return 113 } 114 115 // bad path: signed by the wrong MSP 116 policy, err = getSignedByMSPMemberPolicy("barf") 117 if err != nil { 118 t.Fatalf("failed getting policy, err %s", err) 119 return 120 } 121 122 args = [][]byte{[]byte("dv"), envBytes, policy} 123 if res := stub.MockInvoke("1", args); res.Status == shim.OK { 124 t.Fatalf("vscc invoke should have failed") 125 return 126 } 127 } 128 129 var id msp.SigningIdentity 130 var sid []byte 131 var mspid string 132 var chainId string = util.GetTestChainID() 133 134 func TestMain(m *testing.M) { 135 var err error 136 137 // setup the MSP manager so that we can sign/verify 138 msptesttools.LoadMSPSetupForTesting() 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 }