github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/gossip/identity/identity_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 identity
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"testing"
    23  
    24  	"github.com/hyperledger/fabric/gossip/api"
    25  	"github.com/hyperledger/fabric/gossip/common"
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  var msgCryptoService = &naiveCryptoService{}
    30  
    31  type naiveCryptoService struct {
    32  }
    33  
    34  func (*naiveCryptoService) ValidateIdentity(peerIdentity api.PeerIdentityType) error {
    35  	return nil
    36  }
    37  
    38  // GetPKIidOfCert returns the PKI-ID of a peer's identity
    39  func (*naiveCryptoService) GetPKIidOfCert(peerIdentity api.PeerIdentityType) common.PKIidType {
    40  	return common.PKIidType(peerIdentity)
    41  }
    42  
    43  // VerifyBlock returns nil if the block is properly signed,
    44  // else returns error
    45  func (*naiveCryptoService) VerifyBlock(chainID common.ChainID, signedBlock api.SignedBlock) error {
    46  	return nil
    47  }
    48  
    49  // VerifyByChannel verifies a peer's signature on a message in the context
    50  // of a specific channel
    51  func (*naiveCryptoService) VerifyByChannel(_ common.ChainID, _ api.PeerIdentityType, _, _ []byte) error {
    52  	return nil
    53  }
    54  
    55  // Sign signs msg with this peer's signing key and outputs
    56  // the signature if no error occurred.
    57  func (*naiveCryptoService) Sign(msg []byte) ([]byte, error) {
    58  	return msg, nil
    59  }
    60  
    61  // Verify checks that signature is a valid signature of message under a peer's verification key.
    62  // If the verification succeeded, Verify returns nil meaning no error occurred.
    63  // If peerCert is nil, then the signature is verified against this peer's verification key.
    64  func (*naiveCryptoService) Verify(peerIdentity api.PeerIdentityType, signature, message []byte) error {
    65  	equal := bytes.Equal(signature, message)
    66  	if !equal {
    67  		return fmt.Errorf("Wrong certificate")
    68  	}
    69  	return nil
    70  }
    71  
    72  func TestPut(t *testing.T) {
    73  	idStore := NewIdentityMapper(msgCryptoService)
    74  	identity := []byte("yacovm")
    75  	identity2 := []byte("not-yacovm")
    76  	pkiID := msgCryptoService.GetPKIidOfCert(api.PeerIdentityType(identity))
    77  	pkiID2 := msgCryptoService.GetPKIidOfCert(api.PeerIdentityType(identity2))
    78  	assert.NoError(t, idStore.Put(pkiID, identity))
    79  	assert.Error(t, idStore.Put(nil, identity))
    80  	assert.Error(t, idStore.Put(pkiID2, nil))
    81  	assert.Error(t, idStore.Put(pkiID2, identity))
    82  	assert.Error(t, idStore.Put(pkiID, identity2))
    83  }
    84  
    85  func TestGet(t *testing.T) {
    86  	idStore := NewIdentityMapper(msgCryptoService)
    87  	identity := []byte("yacovm")
    88  	identity2 := []byte("not-yacovm")
    89  	pkiID := msgCryptoService.GetPKIidOfCert(api.PeerIdentityType(identity))
    90  	pkiID2 := msgCryptoService.GetPKIidOfCert(api.PeerIdentityType(identity2))
    91  	assert.NoError(t, idStore.Put(pkiID, identity))
    92  	cert, err := idStore.Get(pkiID)
    93  	assert.NoError(t, err)
    94  	assert.Equal(t, api.PeerIdentityType(identity), cert)
    95  	cert, err = idStore.Get(pkiID2)
    96  	assert.Nil(t, cert)
    97  	assert.Error(t, err)
    98  }
    99  
   100  func TestVerify(t *testing.T) {
   101  	idStore := NewIdentityMapper(msgCryptoService)
   102  	identity := []byte("yacovm")
   103  	identity2 := []byte("not-yacovm")
   104  	pkiID := msgCryptoService.GetPKIidOfCert(api.PeerIdentityType(identity))
   105  	pkiID2 := msgCryptoService.GetPKIidOfCert(api.PeerIdentityType(identity2))
   106  	idStore.Put(pkiID, api.PeerIdentityType(identity))
   107  	signed, err := idStore.Sign([]byte("bla bla"))
   108  	assert.NoError(t, err)
   109  	assert.NoError(t, idStore.Verify(pkiID, signed, []byte("bla bla")))
   110  	assert.Error(t, idStore.Verify(pkiID2, signed, []byte("bla bla")))
   111  }