github.com/kaituanwang/hyperledger@v2.0.1+incompatible/cmd/common/signer/signer_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package signer
     8  
     9  import (
    10  	"crypto/ecdsa"
    11  	"path/filepath"
    12  	"testing"
    13  
    14  	"github.com/hyperledger/fabric/bccsp/utils"
    15  	"github.com/hyperledger/fabric/common/util"
    16  	"github.com/stretchr/testify/assert"
    17  )
    18  
    19  func TestSigner(t *testing.T) {
    20  	conf := Config{
    21  		MSPID:        "SampleOrg",
    22  		IdentityPath: filepath.Join("testdata", "signer", "cert.pem"),
    23  		KeyPath:      filepath.Join("testdata", "signer", "8150cb2d09628ccc89727611ebb736189f6482747eff9b8aaaa27e9a382d2e93_sk"),
    24  	}
    25  
    26  	signer, err := NewSigner(conf)
    27  	assert.NoError(t, err)
    28  
    29  	msg := []byte("foo")
    30  	sig, err := signer.Sign(msg)
    31  	assert.NoError(t, err)
    32  
    33  	r, s, err := utils.UnmarshalECDSASignature(sig)
    34  	ecdsa.Verify(&signer.key.PublicKey, util.ComputeSHA256(msg), r, s)
    35  }
    36  
    37  func TestSignerBadConfig(t *testing.T) {
    38  	conf := Config{
    39  		MSPID:        "SampleOrg",
    40  		IdentityPath: filepath.Join("testdata", "signer", "non_existent_cert"),
    41  	}
    42  
    43  	signer, err := NewSigner(conf)
    44  	assert.EqualError(t, err, "open testdata/signer/non_existent_cert: no such file or directory")
    45  	assert.Nil(t, signer)
    46  
    47  	conf = Config{
    48  		MSPID:        "SampleOrg",
    49  		IdentityPath: filepath.Join("testdata", "signer", "cert.pem"),
    50  		KeyPath:      filepath.Join("testdata", "signer", "non_existent_cert"),
    51  	}
    52  
    53  	signer, err = NewSigner(conf)
    54  	assert.EqualError(t, err, "open testdata/signer/non_existent_cert: no such file or directory")
    55  	assert.Nil(t, signer)
    56  
    57  	conf = Config{
    58  		MSPID:        "SampleOrg",
    59  		IdentityPath: filepath.Join("testdata", "signer", "cert.pem"),
    60  		KeyPath:      filepath.Join("testdata", "signer", "broken_private_key"),
    61  	}
    62  
    63  	signer, err = NewSigner(conf)
    64  	assert.EqualError(t, err, "failed to decode PEM block from testdata/signer/broken_private_key")
    65  	assert.Nil(t, signer)
    66  
    67  	conf = Config{
    68  		MSPID:        "SampleOrg",
    69  		IdentityPath: filepath.Join("testdata", "signer", "cert.pem"),
    70  		KeyPath:      filepath.Join("testdata", "signer", "empty_private_key"),
    71  	}
    72  
    73  	signer, err = NewSigner(conf)
    74  	assert.EqualError(t, err, "failed to parse private key from testdata/signer/empty_private_key: asn1: syntax error: sequence truncated")
    75  	assert.Nil(t, signer)
    76  }