github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/peer/gossip/mcs/mcs_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 mcs
    18  
    19  import (
    20  	"os"
    21  	"testing"
    22  
    23  	"fmt"
    24  
    25  	"github.com/hyperledger/fabric/bccsp"
    26  	"github.com/hyperledger/fabric/bccsp/factory"
    27  	mockpolicies "github.com/hyperledger/fabric/common/mocks/policies"
    28  	"github.com/hyperledger/fabric/gossip/api"
    29  	"github.com/hyperledger/fabric/msp/mgmt"
    30  	"github.com/hyperledger/fabric/msp/mgmt/testtools"
    31  	"github.com/stretchr/testify/assert"
    32  )
    33  
    34  var (
    35  	msgCryptoService api.MessageCryptoService
    36  )
    37  
    38  func TestMain(m *testing.M) {
    39  	// Setup the MSP manager so that we can sign/verify
    40  	// TODO: Additional tests will be inclluded as soon
    41  	// as the MSP-related classes can be easly mocked.
    42  
    43  	mspMgrConfigDir := "./../../../msp/sampleconfig/"
    44  	err := msptesttools.LoadMSPSetupForTesting(mspMgrConfigDir)
    45  	if err != nil {
    46  		fmt.Printf("Failed LoadFakeSetupWithLocalMspAndTestChainMsp [%s]", err)
    47  		os.Exit(-1)
    48  	}
    49  
    50  	// Init the MSP-based MessageCryptoService
    51  	msgCryptoService = New(&mockpolicies.PolicyManagerMgmt{})
    52  
    53  	os.Exit(m.Run())
    54  }
    55  
    56  func TestPKIidOfCert(t *testing.T) {
    57  	// Recall that a peerIdentity is the serialized MSP identity.
    58  
    59  	id, err := mgmt.GetLocalMSP().GetDefaultSigningIdentity()
    60  	assert.NoError(t, err, "Failed getting local default signing identity")
    61  	peerIdentity, err := id.Serialize()
    62  	assert.NoError(t, err, "Failed serializing local default signing identity")
    63  
    64  	pkid := msgCryptoService.GetPKIidOfCert(peerIdentity)
    65  
    66  	// Check pkid is not nil
    67  	assert.NotNil(t, pkid, "PKID must be different from nil")
    68  	// Check that pkid is the SHA2-256 of ithe peerIdentity
    69  	digest, err := factory.GetDefault().Hash(peerIdentity, &bccsp.SHA256Opts{})
    70  	assert.NoError(t, err, "Failed computing digest of serialized identity [% x]", []byte(peerIdentity))
    71  	assert.Equal(t, digest, []byte(pkid), "PKID must be the SHA2-256 of peerIdentity")
    72  }
    73  
    74  func TestPKIidOfNil(t *testing.T) {
    75  	pkid := msgCryptoService.GetPKIidOfCert(nil)
    76  	// Check pkid is not nil
    77  	assert.Nil(t, pkid, "PKID must be nil")
    78  }
    79  
    80  func TestSign(t *testing.T) {
    81  	msg := []byte("Hello World!!!")
    82  	sigma, err := msgCryptoService.Sign(msg)
    83  	assert.NoError(t, err, "Failed generating signature")
    84  	assert.NotNil(t, sigma, "Signature must be different from nil")
    85  }
    86  
    87  func TestVerify(t *testing.T) {
    88  	msg := []byte("Hello World!!!")
    89  	sigma, err := msgCryptoService.Sign(msg)
    90  	assert.NoError(t, err, "Failed generating signature")
    91  
    92  	// Verify signature using the identity that created the identity
    93  	id, err := mgmt.GetLocalMSP().GetDefaultSigningIdentity()
    94  	assert.NoError(t, err, "Failed getting local default signing identity")
    95  	peerIdentity, err := id.Serialize()
    96  	assert.NoError(t, err, "Failed serializing local default signing identity")
    97  	err = msgCryptoService.Verify(peerIdentity, sigma, msg)
    98  	assert.NoError(t, err, "Failed verifying signature")
    99  }