github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/peer/chaincode/signpackage_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  	"fmt"
    21  	"io/ioutil"
    22  	"os"
    23  	"testing"
    24  
    25  	"github.com/golang/protobuf/proto"
    26  
    27  	"github.com/hyperledger/fabric/peer/common"
    28  	pcommon "github.com/hyperledger/fabric/protos/common"
    29  )
    30  
    31  //helper to sign an existing package
    32  func signExistingPackage(env *pcommon.Envelope, infile, outfile string) error {
    33  	InitMSP()
    34  	signer, err := common.GetDefaultSigner()
    35  	if err != nil {
    36  		return fmt.Errorf("Get default signer error: %v", err)
    37  	}
    38  
    39  	mockCF := &ChaincodeCmdFactory{Signer: signer}
    40  
    41  	cmd := signpackageCmd(mockCF)
    42  	addFlags(cmd)
    43  
    44  	cmd.SetArgs([]string{infile, outfile})
    45  
    46  	if err := cmd.Execute(); err != nil {
    47  		return err
    48  	}
    49  
    50  	return nil
    51  }
    52  
    53  // TestSignExistingPackage signs an existing package
    54  func TestSignExistingPackage(t *testing.T) {
    55  	pdir := newTempDir()
    56  	defer os.RemoveAll(pdir)
    57  
    58  	ccpackfile := pdir + "/ccpack.file"
    59  	err := createSignedCDSPackage([]string{"-n", "somecc", "-p", "some/go/package", "-v", "0", "-s", "-S", ccpackfile}, true)
    60  	if err != nil {
    61  		t.Fatalf("error creating signed :%v", err)
    62  	}
    63  
    64  	b, err := ioutil.ReadFile(ccpackfile)
    65  	if err != nil {
    66  		t.Fatalf("package file %s not created", ccpackfile)
    67  	}
    68  
    69  	e := &pcommon.Envelope{}
    70  	err = proto.Unmarshal(b, e)
    71  	if err != nil {
    72  		t.Fatalf("could not unmarshall envelope")
    73  	}
    74  
    75  	signedfile := pdir + "/signed.file"
    76  	err = signExistingPackage(e, ccpackfile, signedfile)
    77  	if err != nil {
    78  		t.Fatalf("could not sign envelope")
    79  	}
    80  
    81  	b, err = ioutil.ReadFile(signedfile)
    82  	if err != nil {
    83  		t.Fatalf("signed package file %s not created", signedfile)
    84  	}
    85  
    86  	e = &pcommon.Envelope{}
    87  	err = proto.Unmarshal(b, e)
    88  	if err != nil {
    89  		t.Fatalf("could not unmarshall signed envelope")
    90  	}
    91  
    92  	_, p, err := extractSignedCCDepSpec(e)
    93  	if err != nil {
    94  		t.Fatalf("could not extract signed dep spec")
    95  	}
    96  
    97  	if p.OwnerEndorsements == nil {
    98  		t.Fatalf("expected endorsements")
    99  	}
   100  
   101  	if len(p.OwnerEndorsements) != 2 {
   102  		t.Fatalf("expected 2 endorserments but found %d", len(p.OwnerEndorsements))
   103  	}
   104  }
   105  
   106  // TestFailSignUnsignedPackage tries to signs a package that was not originally signed
   107  func TestFailSignUnsignedPackage(t *testing.T) {
   108  	pdir := newTempDir()
   109  	defer os.RemoveAll(pdir)
   110  
   111  	ccpackfile := pdir + "/ccpack.file"
   112  	//don't sign it ... no "-S"
   113  	err := createSignedCDSPackage([]string{"-n", "somecc", "-p", "some/go/package", "-v", "0", "-s", ccpackfile}, true)
   114  	if err != nil {
   115  		t.Fatalf("error creating signed :%v", err)
   116  	}
   117  
   118  	b, err := ioutil.ReadFile(ccpackfile)
   119  	if err != nil {
   120  		t.Fatalf("package file %s not created", ccpackfile)
   121  	}
   122  
   123  	e := &pcommon.Envelope{}
   124  	err = proto.Unmarshal(b, e)
   125  	if err != nil {
   126  		t.Fatalf("could not unmarshall envelope")
   127  	}
   128  
   129  	signedfile := pdir + "/signed.file"
   130  	err = signExistingPackage(e, ccpackfile, signedfile)
   131  	if err == nil {
   132  		t.Fatalf("expected signing a package that's not originally signed to fail")
   133  	}
   134  }