github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/common/localmsp/signer_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 localmsp
    18  
    19  import (
    20  	"os"
    21  	"testing"
    22  
    23  	"github.com/hyperledger/fabric/common/crypto"
    24  	mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  func TestMain(m *testing.M) {
    29  	if err := mspmgmt.LoadDevMsp(); err != nil {
    30  		os.Exit(-1)
    31  	}
    32  
    33  	os.Exit(m.Run())
    34  }
    35  
    36  func TestNewSigner(t *testing.T) {
    37  	signer := NewSigner()
    38  	assert.NotNil(t, signer, "Signer must be differentr from nil.")
    39  }
    40  
    41  func TestMspSigner_NewSignatureHeader(t *testing.T) {
    42  	signer := NewSigner()
    43  
    44  	sh, err := signer.NewSignatureHeader()
    45  	if err != nil {
    46  		t.Fatalf("Failed creting signature header [%s]", err)
    47  	}
    48  
    49  	assert.NotNil(t, sh, "SignatureHeader must be different from nil")
    50  	assert.Len(t, sh.Nonce, crypto.NonceSize, "SignatureHeader.Nonce must be of length %d", crypto.NonceSize)
    51  
    52  	mspIdentity, err := mspmgmt.GetLocalMSP().GetDefaultSigningIdentity()
    53  	assert.NoError(t, err, "Failed getting default MSP Identity")
    54  	publicIdentity := mspIdentity.GetPublicVersion()
    55  	assert.NotNil(t, publicIdentity, "Failed getting default public identity. It must be different from nil.")
    56  	publicIdentityRaw, err := publicIdentity.Serialize()
    57  	assert.NoError(t, err, "Failed serializing default public identity")
    58  	assert.Equal(t, publicIdentityRaw, sh.Creator, "Creator must be local default signer identity")
    59  }
    60  
    61  func TestMspSigner_Sign(t *testing.T) {
    62  	signer := NewSigner()
    63  
    64  	msg := []byte("Hello World")
    65  	sigma, err := signer.Sign(msg)
    66  	assert.NoError(t, err, "FAiled generating signature")
    67  
    68  	// Verify signature
    69  	mspIdentity, err := mspmgmt.GetLocalMSP().GetDefaultSigningIdentity()
    70  	assert.NoError(t, err, "Failed getting default MSP Identity")
    71  	err = mspIdentity.Verify(msg, sigma)
    72  	assert.NoError(t, err, "Failed verifiing signature")
    73  }