github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/core/endorser/endorser_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 17 package endorser 18 19 import ( 20 "fmt" 21 "io/ioutil" 22 "net" 23 "os" 24 "testing" 25 "time" 26 27 "path/filepath" 28 29 "errors" 30 31 "github.com/golang/protobuf/proto" 32 mockpolicies "github.com/hyperledger/fabric/common/mocks/policies" 33 "github.com/hyperledger/fabric/common/policies" 34 "github.com/hyperledger/fabric/common/util" 35 "github.com/hyperledger/fabric/core/chaincode" 36 "github.com/hyperledger/fabric/core/common/ccprovider" 37 "github.com/hyperledger/fabric/core/container" 38 "github.com/hyperledger/fabric/core/peer" 39 syscc "github.com/hyperledger/fabric/core/scc" 40 "github.com/hyperledger/fabric/msp" 41 mspmgmt "github.com/hyperledger/fabric/msp/mgmt" 42 "github.com/hyperledger/fabric/msp/mgmt/testtools" 43 "github.com/hyperledger/fabric/protos/common" 44 pb "github.com/hyperledger/fabric/protos/peer" 45 pbutils "github.com/hyperledger/fabric/protos/utils" 46 "github.com/spf13/viper" 47 "golang.org/x/net/context" 48 "google.golang.org/grpc" 49 "google.golang.org/grpc/credentials" 50 ) 51 52 var endorserServer pb.EndorserServer 53 var mspInstance msp.MSP 54 var signer msp.SigningIdentity 55 56 type testEnvironment struct { 57 tempDir string 58 listener net.Listener 59 } 60 61 //initialize peer and start up. If security==enabled, login as vp 62 func initPeer(chainID string) (*testEnvironment, error) { 63 //start clean 64 // finitPeer(nil) 65 var opts []grpc.ServerOption 66 if viper.GetBool("peer.tls.enabled") { 67 creds, err := credentials.NewServerTLSFromFile(viper.GetString("peer.tls.cert.file"), viper.GetString("peer.tls.key.file")) 68 if err != nil { 69 return nil, fmt.Errorf("Failed to generate credentials %v", err) 70 } 71 opts = []grpc.ServerOption{grpc.Creds(creds)} 72 } 73 grpcServer := grpc.NewServer(opts...) 74 75 tempDir := newTempDir() 76 viper.Set("peer.fileSystemPath", filepath.Join(tempDir, "hyperledger", "production")) 77 78 peerAddress, err := peer.GetLocalAddress() 79 if err != nil { 80 return nil, fmt.Errorf("Error obtaining peer address: %s", err) 81 } 82 lis, err := net.Listen("tcp", peerAddress) 83 if err != nil { 84 return nil, fmt.Errorf("Error starting peer listener %s", err) 85 } 86 87 //initialize ledger 88 peer.MockInitialize() 89 90 getPeerEndpoint := func() (*pb.PeerEndpoint, error) { 91 return &pb.PeerEndpoint{Id: &pb.PeerID{Name: "testpeer"}, Address: peerAddress}, nil 92 } 93 94 ccStartupTimeout := time.Duration(30000) * time.Millisecond 95 pb.RegisterChaincodeSupportServer(grpcServer, chaincode.NewChaincodeSupport(getPeerEndpoint, false, ccStartupTimeout)) 96 97 syscc.RegisterSysCCs() 98 99 if err = peer.MockCreateChain(chainID); err != nil { 100 closeListenerAndSleep(lis) 101 return nil, err 102 } 103 104 syscc.DeploySysCCs(chainID) 105 106 go grpcServer.Serve(lis) 107 108 return &testEnvironment{tempDir: tempDir, listener: lis}, nil 109 } 110 111 func finitPeer(tev *testEnvironment) { 112 closeListenerAndSleep(tev.listener) 113 os.RemoveAll(tev.tempDir) 114 } 115 116 func closeListenerAndSleep(l net.Listener) { 117 if l != nil { 118 l.Close() 119 time.Sleep(2 * time.Second) 120 } 121 } 122 123 // getInvokeProposal gets the proposal for the chaincode invocation 124 // Currently supported only for Invokes 125 // It returns the proposal and the transaction id associated to the proposal 126 func getInvokeProposal(cis *pb.ChaincodeInvocationSpec, chainID string, creator []byte) (*pb.Proposal, string, error) { 127 return pbutils.CreateChaincodeProposal(common.HeaderType_ENDORSER_TRANSACTION, chainID, cis, creator) 128 } 129 130 // getInvokeProposalOverride allows to get a proposal for the chaincode invocation 131 // overriding transaction id and nonce which are by default auto-generated. 132 // It returns the proposal and the transaction id associated to the proposal 133 func getInvokeProposalOverride(txid string, cis *pb.ChaincodeInvocationSpec, chainID string, nonce, creator []byte) (*pb.Proposal, string, error) { 134 return pbutils.CreateChaincodeProposalWithTxIDNonceAndTransient(txid, common.HeaderType_ENDORSER_TRANSACTION, chainID, cis, nonce, creator, nil) 135 } 136 137 func getDeployProposal(cds *pb.ChaincodeDeploymentSpec, chainID string, creator []byte) (*pb.Proposal, error) { 138 return getDeployOrUpgradeProposal(cds, chainID, creator, false) 139 } 140 141 func getUpgradeProposal(cds *pb.ChaincodeDeploymentSpec, chainID string, creator []byte) (*pb.Proposal, error) { 142 return getDeployOrUpgradeProposal(cds, chainID, creator, true) 143 } 144 145 //getDeployOrUpgradeProposal gets the proposal for the chaincode deploy or upgrade 146 //the payload is a ChaincodeDeploymentSpec 147 func getDeployOrUpgradeProposal(cds *pb.ChaincodeDeploymentSpec, chainID string, creator []byte, upgrade bool) (*pb.Proposal, error) { 148 b, err := proto.Marshal(cds) 149 if err != nil { 150 return nil, err 151 } 152 153 var propType string 154 if upgrade { 155 propType = "upgrade" 156 } else { 157 propType = "deploy" 158 } 159 sccver := util.GetSysCCVersion() 160 //wrap the deployment in an invocation spec to lccc... 161 lcccSpec := &pb.ChaincodeInvocationSpec{ChaincodeSpec: &pb.ChaincodeSpec{Type: pb.ChaincodeSpec_GOLANG, ChaincodeId: &pb.ChaincodeID{Name: "lccc", Version: sccver}, Input: &pb.ChaincodeInput{Args: [][]byte{[]byte(propType), []byte(chainID), b}}}} 162 163 //...and get the proposal for it 164 var prop *pb.Proposal 165 if prop, _, err = getInvokeProposal(lcccSpec, chainID, creator); err != nil { 166 return nil, err 167 } 168 169 if err = ccprovider.PutChaincodeIntoFS(cds); err != nil { 170 return nil, err 171 } 172 173 return prop, nil 174 } 175 176 func getSignedProposal(prop *pb.Proposal, signer msp.SigningIdentity) (*pb.SignedProposal, error) { 177 propBytes, err := pbutils.GetBytesProposal(prop) 178 if err != nil { 179 return nil, err 180 } 181 182 signature, err := signer.Sign(propBytes) 183 if err != nil { 184 return nil, err 185 } 186 187 return &pb.SignedProposal{ProposalBytes: propBytes, Signature: signature}, nil 188 } 189 190 func getDeploymentSpec(context context.Context, spec *pb.ChaincodeSpec) (*pb.ChaincodeDeploymentSpec, error) { 191 codePackageBytes, err := container.GetChaincodePackageBytes(spec) 192 if err != nil { 193 return nil, err 194 } 195 chaincodeDeploymentSpec := &pb.ChaincodeDeploymentSpec{ChaincodeSpec: spec, CodePackage: codePackageBytes} 196 return chaincodeDeploymentSpec, nil 197 } 198 199 func deploy(endorserServer pb.EndorserServer, chainID string, spec *pb.ChaincodeSpec, f func(*pb.ChaincodeDeploymentSpec)) (*pb.ProposalResponse, *pb.Proposal, error) { 200 return deployOrUpgrade(endorserServer, chainID, spec, f, false) 201 } 202 203 func upgrade(endorserServer pb.EndorserServer, chainID string, spec *pb.ChaincodeSpec, f func(*pb.ChaincodeDeploymentSpec)) (*pb.ProposalResponse, *pb.Proposal, error) { 204 return deployOrUpgrade(endorserServer, chainID, spec, f, true) 205 } 206 207 func deployOrUpgrade(endorserServer pb.EndorserServer, chainID string, spec *pb.ChaincodeSpec, f func(*pb.ChaincodeDeploymentSpec), upgrade bool) (*pb.ProposalResponse, *pb.Proposal, error) { 208 var err error 209 var depSpec *pb.ChaincodeDeploymentSpec 210 211 ctxt := context.Background() 212 depSpec, err = getDeploymentSpec(ctxt, spec) 213 if err != nil { 214 return nil, nil, err 215 } 216 217 if f != nil { 218 f(depSpec) 219 } 220 221 creator, err := signer.Serialize() 222 if err != nil { 223 return nil, nil, err 224 } 225 226 var prop *pb.Proposal 227 if upgrade { 228 prop, err = getUpgradeProposal(depSpec, chainID, creator) 229 } else { 230 prop, err = getDeployProposal(depSpec, chainID, creator) 231 } 232 if err != nil { 233 return nil, nil, err 234 } 235 236 var signedProp *pb.SignedProposal 237 signedProp, err = getSignedProposal(prop, signer) 238 if err != nil { 239 return nil, nil, err 240 } 241 242 var resp *pb.ProposalResponse 243 resp, err = endorserServer.ProcessProposal(context.Background(), signedProp) 244 245 return resp, prop, err 246 } 247 248 func invoke(chainID string, spec *pb.ChaincodeSpec) (*pb.Proposal, *pb.ProposalResponse, string, []byte, error) { 249 invocation := &pb.ChaincodeInvocationSpec{ChaincodeSpec: spec} 250 251 creator, err := signer.Serialize() 252 if err != nil { 253 return nil, nil, "", nil, err 254 } 255 256 var prop *pb.Proposal 257 prop, txID, err := getInvokeProposal(invocation, chainID, creator) 258 if err != nil { 259 return nil, nil, "", nil, fmt.Errorf("Error creating proposal %s: %s\n", spec.ChaincodeId, err) 260 } 261 262 nonce, err := pbutils.GetNonce(prop) 263 if err != nil { 264 return nil, nil, "", nil, fmt.Errorf("Failed getting nonce %s: %s\n", spec.ChaincodeId, err) 265 } 266 267 var signedProp *pb.SignedProposal 268 signedProp, err = getSignedProposal(prop, signer) 269 if err != nil { 270 return nil, nil, "", nil, err 271 } 272 273 resp, err := endorserServer.ProcessProposal(context.Background(), signedProp) 274 if err != nil { 275 return nil, nil, "", nil, fmt.Errorf("Error endorsing %s: %s\n", spec.ChaincodeId, err) 276 } 277 278 return prop, resp, txID, nonce, err 279 } 280 281 func invokeWithOverride(txid string, chainID string, spec *pb.ChaincodeSpec, nonce []byte) (*pb.ProposalResponse, error) { 282 invocation := &pb.ChaincodeInvocationSpec{ChaincodeSpec: spec} 283 284 creator, err := signer.Serialize() 285 if err != nil { 286 return nil, err 287 } 288 289 var prop *pb.Proposal 290 prop, _, err = getInvokeProposalOverride(txid, invocation, chainID, nonce, creator) 291 if err != nil { 292 return nil, fmt.Errorf("Error creating proposal with override %s %s: %s\n", txid, spec.ChaincodeId, err) 293 } 294 295 var signedProp *pb.SignedProposal 296 signedProp, err = getSignedProposal(prop, signer) 297 if err != nil { 298 return nil, err 299 } 300 301 resp, err := endorserServer.ProcessProposal(context.Background(), signedProp) 302 if err != nil { 303 return nil, fmt.Errorf("Error endorsing %s %s: %s\n", txid, spec.ChaincodeId, err) 304 } 305 306 return resp, err 307 } 308 309 func deleteChaincodeOnDisk(chaincodeID string) { 310 os.RemoveAll(filepath.Join(viper.GetString("peer.fileSystemPath"), "chaincodes", chaincodeID)) 311 } 312 313 //begin tests. Note that we rely upon the system chaincode and peer to be created 314 //once and be used for all the tests. In order to avoid dependencies / collisions 315 //due to deployed chaincodes, trying to use different chaincodes for different 316 //tests 317 318 //TestDeploy deploy chaincode example01 319 func TestDeploy(t *testing.T) { 320 chainID := util.GetTestChainID() 321 spec := &pb.ChaincodeSpec{Type: 1, ChaincodeId: &pb.ChaincodeID{Name: "ex01", Path: "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example01", Version: "0"}, Input: &pb.ChaincodeInput{Args: [][]byte{[]byte("init"), []byte("a"), []byte("100"), []byte("b"), []byte("200")}}} 322 defer deleteChaincodeOnDisk("ex01.0") 323 324 cccid := ccprovider.NewCCContext(chainID, "ex01", "0", "", false, nil, nil) 325 326 _, _, err := deploy(endorserServer, chainID, spec, nil) 327 if err != nil { 328 t.Fail() 329 t.Logf("Deploy-error in deploy %s", err) 330 chaincode.GetChain().Stop(context.Background(), cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: spec}) 331 return 332 } 333 chaincode.GetChain().Stop(context.Background(), cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: spec}) 334 } 335 336 //TestRedeploy - deploy two times, second time should fail but example02 should remain deployed 337 func TestRedeploy(t *testing.T) { 338 chainID := util.GetTestChainID() 339 340 //invalid arguments 341 spec := &pb.ChaincodeSpec{Type: 1, ChaincodeId: &pb.ChaincodeID{Name: "ex02", Path: "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02", Version: "0"}, Input: &pb.ChaincodeInput{Args: [][]byte{[]byte("init"), []byte("a"), []byte("100"), []byte("b"), []byte("200")}}} 342 343 defer deleteChaincodeOnDisk("ex02.0") 344 345 cccid := ccprovider.NewCCContext(chainID, "ex02", "0", "", false, nil, nil) 346 347 _, _, err := deploy(endorserServer, chainID, spec, nil) 348 if err != nil { 349 t.Fail() 350 t.Logf("error in endorserServer.ProcessProposal %s", err) 351 chaincode.GetChain().Stop(context.Background(), cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: spec}) 352 return 353 } 354 355 deleteChaincodeOnDisk("ex02.0") 356 357 //second time should not fail as we are just simulating 358 _, _, err = deploy(endorserServer, chainID, spec, nil) 359 if err != nil { 360 t.Fail() 361 t.Logf("error in endorserServer.ProcessProposal %s", err) 362 chaincode.GetChain().Stop(context.Background(), cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: spec}) 363 return 364 } 365 chaincode.GetChain().Stop(context.Background(), cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: spec}) 366 } 367 368 // TestDeployAndInvoke deploys and invokes chaincode_example01 369 func TestDeployAndInvoke(t *testing.T) { 370 chainID := util.GetTestChainID() 371 var ctxt = context.Background() 372 373 url := "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example01" 374 chaincodeID := &pb.ChaincodeID{Path: url, Name: "ex01", Version: "0"} 375 376 defer deleteChaincodeOnDisk("ex01.0") 377 378 args := []string{"10"} 379 380 f := "init" 381 argsDeploy := util.ToChaincodeArgs(f, "a", "100", "b", "200") 382 spec := &pb.ChaincodeSpec{Type: 1, ChaincodeId: chaincodeID, Input: &pb.ChaincodeInput{Args: argsDeploy}} 383 384 cccid := ccprovider.NewCCContext(chainID, "ex01", "0", "", false, nil, nil) 385 386 resp, prop, err := deploy(endorserServer, chainID, spec, nil) 387 chaincodeID1 := spec.ChaincodeId.Name 388 if err != nil { 389 t.Fail() 390 t.Logf("Error deploying <%s>: %s", chaincodeID1, err) 391 chaincode.GetChain().Stop(ctxt, cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: chaincodeID}}) 392 return 393 } 394 var nextBlockNumber uint64 // first block 395 err = endorserServer.(*Endorser).commitTxSimulation(prop, chainID, signer, resp, nextBlockNumber) 396 if err != nil { 397 t.Fail() 398 t.Logf("Error committing deploy <%s>: %s", chaincodeID1, err) 399 chaincode.GetChain().Stop(ctxt, cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: chaincodeID}}) 400 return 401 } 402 403 f = "invoke" 404 invokeArgs := append([]string{f}, args...) 405 spec = &pb.ChaincodeSpec{Type: 1, ChaincodeId: chaincodeID, Input: &pb.ChaincodeInput{Args: util.ToChaincodeArgs(invokeArgs...)}} 406 prop, resp, txid, nonce, err := invoke(chainID, spec) 407 if err != nil { 408 t.Fail() 409 t.Logf("Error invoking transaction: %s", err) 410 chaincode.GetChain().Stop(ctxt, cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: chaincodeID}}) 411 return 412 } 413 // Commit invoke 414 nextBlockNumber++ 415 err = endorserServer.(*Endorser).commitTxSimulation(prop, chainID, signer, resp, nextBlockNumber) 416 if err != nil { 417 t.Fail() 418 t.Logf("Error committing first invoke <%s>: %s", chaincodeID1, err) 419 chaincode.GetChain().Stop(ctxt, cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: chaincodeID}}) 420 return 421 } 422 423 // Now test for an invalid TxID 424 f = "invoke" 425 invokeArgs = append([]string{f}, args...) 426 spec = &pb.ChaincodeSpec{Type: 1, ChaincodeId: chaincodeID, Input: &pb.ChaincodeInput{Args: util.ToChaincodeArgs(invokeArgs...)}} 427 _, err = invokeWithOverride("invalid_tx_id", chainID, spec, nonce) 428 if err == nil { 429 t.Fail() 430 t.Log("Replay attack protection faild. Transaction with invalid txid passed") 431 chaincode.GetChain().Stop(ctxt, cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: chaincodeID}}) 432 return 433 } 434 435 // Now test for duplicated TxID 436 f = "invoke" 437 invokeArgs = append([]string{f}, args...) 438 spec = &pb.ChaincodeSpec{Type: 1, ChaincodeId: chaincodeID, Input: &pb.ChaincodeInput{Args: util.ToChaincodeArgs(invokeArgs...)}} 439 _, err = invokeWithOverride(txid, chainID, spec, nonce) 440 if err == nil { 441 t.Fail() 442 t.Log("Replay attack protection faild. Transaction with duplicaged txid passed") 443 chaincode.GetChain().Stop(ctxt, cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: chaincodeID}}) 444 return 445 } 446 447 fmt.Printf("Invoke test passed\n") 448 t.Logf("Invoke test passed") 449 450 chaincode.GetChain().Stop(ctxt, cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: chaincodeID}}) 451 } 452 453 // TestUpgradeAndInvoke deploys chaincode_example01, upgrade it with chaincode_example02, then invoke it 454 func TestDeployAndUpgrade(t *testing.T) { 455 chainID := util.GetTestChainID() 456 var ctxt = context.Background() 457 458 url1 := "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example01" 459 url2 := "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02" 460 chaincodeID1 := &pb.ChaincodeID{Path: url1, Name: "upgradeex01", Version: "0"} 461 chaincodeID2 := &pb.ChaincodeID{Path: url2, Name: "upgradeex01", Version: "1"} 462 463 defer deleteChaincodeOnDisk("upgradeex01.0") 464 defer deleteChaincodeOnDisk("upgradeex01.1") 465 466 f := "init" 467 argsDeploy := util.ToChaincodeArgs(f, "a", "100", "b", "200") 468 spec := &pb.ChaincodeSpec{Type: 1, ChaincodeId: chaincodeID1, Input: &pb.ChaincodeInput{Args: argsDeploy}} 469 470 cccid1 := ccprovider.NewCCContext(chainID, "upgradeex01", "0", "", false, nil, nil) 471 cccid2 := ccprovider.NewCCContext(chainID, "upgradeex01", "1", "", false, nil, nil) 472 473 resp, prop, err := deploy(endorserServer, chainID, spec, nil) 474 475 chaincodeName := spec.ChaincodeId.Name 476 if err != nil { 477 t.Fail() 478 chaincode.GetChain().Stop(ctxt, cccid1, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: chaincodeID1}}) 479 chaincode.GetChain().Stop(ctxt, cccid2, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: chaincodeID2}}) 480 t.Logf("Error deploying <%s>: %s", chaincodeName, err) 481 return 482 } 483 484 var nextBlockNumber uint64 = 2 // something above created block 0 485 err = endorserServer.(*Endorser).commitTxSimulation(prop, chainID, signer, resp, nextBlockNumber) 486 if err != nil { 487 t.Fail() 488 chaincode.GetChain().Stop(ctxt, cccid1, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: chaincodeID1}}) 489 chaincode.GetChain().Stop(ctxt, cccid2, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: chaincodeID2}}) 490 t.Logf("Error committing <%s>: %s", chaincodeName, err) 491 return 492 } 493 494 argsUpgrade := util.ToChaincodeArgs(f, "a", "150", "b", "300") 495 spec = &pb.ChaincodeSpec{Type: 1, ChaincodeId: chaincodeID2, Input: &pb.ChaincodeInput{Args: argsUpgrade}} 496 _, _, err = upgrade(endorserServer, chainID, spec, nil) 497 if err != nil { 498 t.Fail() 499 chaincode.GetChain().Stop(ctxt, cccid1, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: chaincodeID1}}) 500 chaincode.GetChain().Stop(ctxt, cccid2, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: chaincodeID2}}) 501 t.Logf("Error upgrading <%s>: %s", chaincodeName, err) 502 return 503 } 504 505 fmt.Printf("Upgrade test passed\n") 506 t.Logf("Upgrade test passed") 507 508 chaincode.GetChain().Stop(ctxt, cccid1, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: chaincodeID1}}) 509 chaincode.GetChain().Stop(ctxt, cccid2, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: chaincodeID2}}) 510 } 511 512 // TestWritersACLFail deploys a chaincode and then tries to invoke it; 513 // however we inject a special policy for writers to simulate 514 // the scenario in which the creator of this proposal is not among 515 // the writers for the chain 516 func TestWritersACLFail(t *testing.T) { 517 //skip pending FAB-2457 fix 518 t.Skip() 519 chainID := util.GetTestChainID() 520 var ctxt = context.Background() 521 522 url := "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example01" 523 chaincodeID := &pb.ChaincodeID{Path: url, Name: "ex01-fail", Version: "0"} 524 525 defer deleteChaincodeOnDisk("ex01-fail.0") 526 527 args := []string{"10"} 528 529 f := "init" 530 argsDeploy := util.ToChaincodeArgs(f, "a", "100", "b", "200") 531 spec := &pb.ChaincodeSpec{Type: 1, ChaincodeId: chaincodeID, Input: &pb.ChaincodeInput{Args: argsDeploy}} 532 533 cccid := ccprovider.NewCCContext(chainID, "ex01-fail", "0", "", false, nil, nil) 534 535 resp, prop, err := deploy(endorserServer, chainID, spec, nil) 536 chaincodeID1 := spec.ChaincodeId.Name 537 if err != nil { 538 t.Fail() 539 t.Logf("Error deploying <%s>: %s", chaincodeID1, err) 540 chaincode.GetChain().Stop(ctxt, cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: chaincodeID}}) 541 return 542 } 543 var nextBlockNumber uint64 = 3 // The tests that ran before this test created blocks 0-2 544 err = endorserServer.(*Endorser).commitTxSimulation(prop, chainID, signer, resp, nextBlockNumber) 545 if err != nil { 546 t.Fail() 547 t.Logf("Error committing deploy <%s>: %s", chaincodeID1, err) 548 chaincode.GetChain().Stop(ctxt, cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: chaincodeID}}) 549 return 550 } 551 552 // here we inject a reject policy for writers 553 // to simulate the scenario in which the invoker 554 // is not authorized to issue this proposal 555 rejectpolicy := &mockpolicies.Policy{ 556 Err: errors.New("The creator of this proposal does not fulfil the writers policy of this chain"), 557 } 558 pm := peer.GetPolicyManager(chainID) 559 pm.(*mockpolicies.Manager).PolicyMap = map[string]*mockpolicies.Policy{policies.ChannelApplicationWriters: rejectpolicy} 560 561 f = "invoke" 562 invokeArgs := append([]string{f}, args...) 563 spec = &pb.ChaincodeSpec{Type: 1, ChaincodeId: chaincodeID, Input: &pb.ChaincodeInput{Args: util.ToChaincodeArgs(invokeArgs...)}} 564 prop, resp, _, _, err = invoke(chainID, spec) 565 if err == nil { 566 t.Fail() 567 t.Logf("Invocation should have failed") 568 chaincode.GetChain().Stop(ctxt, cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: chaincodeID}}) 569 return 570 } 571 572 fmt.Println("TestWritersACLFail passed") 573 t.Logf("TestWritersACLFail passed") 574 575 chaincode.GetChain().Stop(ctxt, cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: chaincodeID}}) 576 } 577 578 // TestAdminACLFail deploys tried to deploy a chaincode; 579 // however we inject a special policy for admins to simulate 580 // the scenario in which the creator of this proposal is not among 581 // the admins for the chain 582 func TestAdminACLFail(t *testing.T) { 583 //skip pending FAB-2457 fix 584 t.Skip() 585 chainID := util.GetTestChainID() 586 587 // here we inject a reject policy for admins 588 // to simulate the scenario in which the invoker 589 // is not authorized to issue this proposal 590 rejectpolicy := &mockpolicies.Policy{ 591 Err: errors.New("The creator of this proposal does not fulfil the writers policy of this chain"), 592 } 593 pm := peer.GetPolicyManager(chainID) 594 pm.(*mockpolicies.Manager).PolicyMap = map[string]*mockpolicies.Policy{policies.ChannelApplicationAdmins: rejectpolicy} 595 596 var ctxt = context.Background() 597 598 url := "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example01" 599 chaincodeID := &pb.ChaincodeID{Path: url, Name: "ex01-fail1", Version: "0"} 600 601 defer deleteChaincodeOnDisk("ex01-fail1.0") 602 603 f := "init" 604 argsDeploy := util.ToChaincodeArgs(f, "a", "100", "b", "200") 605 spec := &pb.ChaincodeSpec{Type: 1, ChaincodeId: chaincodeID, Input: &pb.ChaincodeInput{Args: argsDeploy}} 606 607 cccid := ccprovider.NewCCContext(chainID, "ex01-fail1", "0", "", false, nil, nil) 608 609 _, _, err := deploy(endorserServer, chainID, spec, nil) 610 if err == nil { 611 t.Fail() 612 t.Logf("Deploying chaincode should have failed!") 613 chaincode.GetChain().Stop(ctxt, cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: chaincodeID}}) 614 return 615 } 616 617 fmt.Println("TestAdminACLFail passed") 618 t.Logf("TestATestAdminACLFailCLFail passed") 619 620 chaincode.GetChain().Stop(ctxt, cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{ChaincodeId: chaincodeID}}) 621 } 622 623 func newTempDir() string { 624 tempDir, err := ioutil.TempDir("", "fabric-") 625 if err != nil { 626 panic(err) 627 } 628 return tempDir 629 } 630 631 func TestMain(m *testing.M) { 632 SetupTestConfig() 633 634 chainID := util.GetTestChainID() 635 tev, err := initPeer(chainID) 636 if err != nil { 637 os.Exit(-1) 638 fmt.Printf("Could not initialize tests") 639 finitPeer(tev) 640 return 641 } 642 643 endorserServer = NewEndorserServer() 644 645 // setup the MSP manager so that we can sign/verify 646 mspMgrConfigDir := "../../msp/sampleconfig/" 647 err = msptesttools.LoadMSPSetupForTesting(mspMgrConfigDir) 648 if err != nil { 649 fmt.Printf("Could not initialize msp/signer, err %s", err) 650 finitPeer(tev) 651 os.Exit(-1) 652 return 653 } 654 signer, err = mspmgmt.GetLocalMSP().GetDefaultSigningIdentity() 655 if err != nil { 656 fmt.Printf("Could not initialize msp/signer") 657 finitPeer(tev) 658 os.Exit(-1) 659 return 660 } 661 662 retVal := m.Run() 663 664 finitPeer(tev) 665 666 os.Exit(retVal) 667 }