github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/core/common/ccprovider/cdspackage_test.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 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 ccprovider
    18  
    19  import (
    20  	"fmt"
    21  	"io/ioutil"
    22  	"os"
    23  	"testing"
    24  
    25  	pb "github.com/hyperledger/fabric/protos/peer"
    26  	"github.com/hyperledger/fabric/protos/utils"
    27  )
    28  
    29  func setupccdir() string {
    30  	tempDir, err := ioutil.TempDir("/tmp", "ccprovidertest")
    31  	if err != nil {
    32  		panic(err)
    33  	}
    34  	SetChaincodesPath(tempDir)
    35  	return tempDir
    36  }
    37  
    38  func processCDS(cds *pb.ChaincodeDeploymentSpec, tofs bool) (*CDSPackage, []byte, *ChaincodeData, error) {
    39  	b := utils.MarshalOrPanic(cds)
    40  
    41  	ccpack := &CDSPackage{}
    42  	cd, err := ccpack.InitFromBuffer(b)
    43  	if err != nil {
    44  		return nil, nil, nil, fmt.Errorf("error owner creating package %s", err)
    45  	}
    46  
    47  	if tofs {
    48  		if err = ccpack.PutChaincodeToFS(); err != nil {
    49  			return nil, nil, nil, fmt.Errorf("error putting package on the FS %s", err)
    50  		}
    51  	}
    52  
    53  	return ccpack, b, cd, nil
    54  }
    55  
    56  func TestPutCDSCC(t *testing.T) {
    57  	ccdir := setupccdir()
    58  	defer os.RemoveAll(ccdir)
    59  
    60  	cds := &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{Type: 1, ChaincodeId: &pb.ChaincodeID{Name: "testcc", Version: "0"}, Input: &pb.ChaincodeInput{Args: [][]byte{[]byte("")}}}, CodePackage: []byte("code")}
    61  
    62  	ccpack, _, cd, err := processCDS(cds, true)
    63  	if err != nil {
    64  		t.Fatalf("error putting CDS to FS %s", err)
    65  		return
    66  	}
    67  
    68  	if err = ccpack.ValidateCC(cd); err != nil {
    69  		t.Fatalf("error validating package %s", err)
    70  		return
    71  	}
    72  }
    73  
    74  func TestPutCDSErrorPaths(t *testing.T) {
    75  	ccdir := setupccdir()
    76  	defer os.RemoveAll(ccdir)
    77  
    78  	cds := &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{Type: 1, ChaincodeId: &pb.ChaincodeID{Name: "testcc", Version: "0"}, Input: &pb.ChaincodeInput{Args: [][]byte{[]byte("")}}}, CodePackage: []byte("code")}
    79  
    80  	ccpack, b, _, err := processCDS(cds, true)
    81  	if err != nil {
    82  		t.Fatalf("error putting CDS to FS %s", err)
    83  		return
    84  	}
    85  
    86  	//validate with invalid name
    87  	if err = ccpack.ValidateCC(&ChaincodeData{Name: "invalname", Version: "0"}); err == nil {
    88  		t.Fatalf("expected error validating package")
    89  		return
    90  	}
    91  	//remove the buffer
    92  	ccpack.buf = nil
    93  	if err = ccpack.PutChaincodeToFS(); err == nil {
    94  		t.Fatalf("expected error putting package on the FS")
    95  		return
    96  	}
    97  
    98  	//put back  the buffer but remove the depspec
    99  	ccpack.buf = b
   100  	savDepSpec := ccpack.depSpec
   101  	ccpack.depSpec = nil
   102  	if err = ccpack.PutChaincodeToFS(); err == nil {
   103  		t.Fatalf("expected error putting package on the FS")
   104  		return
   105  	}
   106  
   107  	//put back dep spec
   108  	ccpack.depSpec = savDepSpec
   109  
   110  	//...but remove the chaincode directory
   111  	os.RemoveAll(ccdir)
   112  	if err = ccpack.PutChaincodeToFS(); err == nil {
   113  		t.Fatalf("expected error putting package on the FS")
   114  		return
   115  	}
   116  }
   117  
   118  func TestCDSGetCCPackage(t *testing.T) {
   119  	cds := &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{Type: 1, ChaincodeId: &pb.ChaincodeID{Name: "testcc", Version: "0"}, Input: &pb.ChaincodeInput{Args: [][]byte{[]byte("")}}}, CodePackage: []byte("code")}
   120  
   121  	b := utils.MarshalOrPanic(cds)
   122  
   123  	ccpack, err := GetCCPackage(b)
   124  	if err != nil {
   125  		t.Fatalf("failed to get CDS CCPackage %s", err)
   126  		return
   127  	}
   128  
   129  	cccdspack, ok := ccpack.(*CDSPackage)
   130  	if !ok || cccdspack == nil {
   131  		t.Fatalf("failed to get CDS CCPackage")
   132  		return
   133  	}
   134  
   135  	cds2 := cccdspack.GetDepSpec()
   136  	if cds2 == nil {
   137  		t.Fatalf("nil dep spec in CDS CCPackage")
   138  		return
   139  	}
   140  
   141  	if cds2.ChaincodeSpec.ChaincodeId.Name != cds.ChaincodeSpec.ChaincodeId.Name || cds2.ChaincodeSpec.ChaincodeId.Version != cds.ChaincodeSpec.ChaincodeId.Version {
   142  		t.Fatalf("dep spec in CDS CCPackage does not match %v != %v", cds, cds2)
   143  		return
   144  	}
   145  }
   146  
   147  //switch the chaincodes on the FS and validate
   148  func TestCDSSwitchChaincodes(t *testing.T) {
   149  	ccdir := setupccdir()
   150  	defer os.RemoveAll(ccdir)
   151  
   152  	//someone modified the code on the FS with "badcode"
   153  	cds := &pb.ChaincodeDeploymentSpec{ChaincodeSpec: &pb.ChaincodeSpec{Type: 1, ChaincodeId: &pb.ChaincodeID{Name: "testcc", Version: "0"}, Input: &pb.ChaincodeInput{Args: [][]byte{[]byte("")}}}, CodePackage: []byte("badcode")}
   154  
   155  	//write the bad code to the fs
   156  	badccpack, _, _, err := processCDS(cds, true)
   157  	if err != nil {
   158  		t.Fatalf("error putting CDS to FS %s", err)
   159  		return
   160  	}
   161  
   162  	//mimic the good code ChaincodeData from the instantiate...
   163  	cds.CodePackage = []byte("goodcode")
   164  
   165  	//...and generate the CD for it (don't overwrite the bad code)
   166  	_, _, goodcd, err := processCDS(cds, false)
   167  	if err != nil {
   168  		t.Fatalf("error putting CDS to FS %s", err)
   169  		return
   170  	}
   171  
   172  	if err = badccpack.ValidateCC(goodcd); err == nil {
   173  		t.Fatalf("expected goodcd to fail against bad package but succeeded!")
   174  		return
   175  	}
   176  }