github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/gossip/integration/integration_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package integration
     8  
     9  import (
    10  	"fmt"
    11  	"net"
    12  	"strings"
    13  	"testing"
    14  
    15  	"github.com/hyperledger/fabric/core/config"
    16  	"github.com/hyperledger/fabric/gossip/api"
    17  	"github.com/hyperledger/fabric/gossip/common"
    18  	"github.com/hyperledger/fabric/gossip/identity"
    19  	"github.com/hyperledger/fabric/gossip/util"
    20  	"github.com/hyperledger/fabric/msp/mgmt"
    21  	"github.com/hyperledger/fabric/msp/mgmt/testtools"
    22  	"github.com/spf13/viper"
    23  	"github.com/stretchr/testify/assert"
    24  	"google.golang.org/grpc"
    25  )
    26  
    27  func init() {
    28  	util.SetupTestLogging()
    29  }
    30  
    31  var (
    32  	cryptSvc = &cryptoService{}
    33  	secAdv   = &secAdviser{}
    34  )
    35  var defaultSecureDialOpts = func() []grpc.DialOption {
    36  	var dialOpts []grpc.DialOption
    37  	dialOpts = append(dialOpts, grpc.WithInsecure())
    38  	return dialOpts
    39  }
    40  
    41  // This is just a test that shows how to instantiate a gossip component
    42  func TestNewGossipCryptoService(t *testing.T) {
    43  	setupTestEnv()
    44  	s1 := grpc.NewServer()
    45  	s2 := grpc.NewServer()
    46  	s3 := grpc.NewServer()
    47  	ll1, _ := net.Listen("tcp", fmt.Sprintf("%s:%d", "", 5611))
    48  	ll2, _ := net.Listen("tcp", fmt.Sprintf("%s:%d", "", 5612))
    49  	ll3, _ := net.Listen("tcp", fmt.Sprintf("%s:%d", "", 5613))
    50  	endpoint1 := "localhost:5611"
    51  	endpoint2 := "localhost:5612"
    52  	endpoint3 := "localhost:5613"
    53  	msptesttools.LoadMSPSetupForTesting()
    54  	peerIdentity, _ := mgmt.GetLocalSigningIdentityOrPanic().Serialize()
    55  	idMapper := identity.NewIdentityMapper(cryptSvc, peerIdentity)
    56  
    57  	g1, err := NewGossipComponent(peerIdentity, endpoint1, s1, secAdv, cryptSvc, idMapper,
    58  		defaultSecureDialOpts)
    59  	assert.NoError(t, err)
    60  	g2, err := NewGossipComponent(peerIdentity, endpoint2, s2, secAdv, cryptSvc, idMapper,
    61  		defaultSecureDialOpts, endpoint1)
    62  	assert.NoError(t, err)
    63  	g3, err := NewGossipComponent(peerIdentity, endpoint3, s3, secAdv, cryptSvc, idMapper,
    64  		defaultSecureDialOpts, endpoint1)
    65  	assert.NoError(t, err)
    66  	defer g1.Stop()
    67  	defer g2.Stop()
    68  	defer g3.Stop()
    69  	go s1.Serve(ll1)
    70  	go s2.Serve(ll2)
    71  	go s3.Serve(ll3)
    72  }
    73  
    74  func TestBadInitialization(t *testing.T) {
    75  	msptesttools.LoadMSPSetupForTesting()
    76  	peerIdentity, _ := mgmt.GetLocalSigningIdentityOrPanic().Serialize()
    77  	s1 := grpc.NewServer()
    78  	idMapper := identity.NewIdentityMapper(cryptSvc, peerIdentity)
    79  	_, err := newConfig("anEndpointWithoutAPort", "anEndpointWithoutAPort")
    80  
    81  	viper.Set("peer.tls.enabled", true)
    82  	_, err = NewGossipComponent(peerIdentity, "localhost:5000", s1, secAdv, cryptSvc, idMapper,
    83  		defaultSecureDialOpts)
    84  	assert.Error(t, err)
    85  }
    86  
    87  func setupTestEnv() {
    88  	viper.SetConfigName("core")
    89  	viper.SetEnvPrefix("CORE")
    90  	config.AddDevConfigPath(nil)
    91  	viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
    92  	viper.AutomaticEnv()
    93  	err := viper.ReadInConfig()
    94  	if err != nil { // Handle errors reading the config file
    95  		panic(fmt.Errorf("fatal error config file: %s", err))
    96  	}
    97  }
    98  
    99  type secAdviser struct {
   100  }
   101  
   102  func (sa *secAdviser) OrgByPeerIdentity(api.PeerIdentityType) api.OrgIdentityType {
   103  	return api.OrgIdentityType("DEFAULT")
   104  }
   105  
   106  type cryptoService struct {
   107  }
   108  
   109  func (s *cryptoService) GetPKIidOfCert(peerIdentity api.PeerIdentityType) common.PKIidType {
   110  	return common.PKIidType(peerIdentity)
   111  }
   112  
   113  func (s *cryptoService) VerifyBlock(chainID common.ChainID, seqNum uint64, signedBlock []byte) error {
   114  	return nil
   115  }
   116  
   117  func (s *cryptoService) Sign(msg []byte) ([]byte, error) {
   118  	return msg, nil
   119  }
   120  
   121  func (s *cryptoService) Verify(peerIdentity api.PeerIdentityType, signature, message []byte) error {
   122  	return nil
   123  }
   124  
   125  func (s *cryptoService) VerifyByChannel(chainID common.ChainID, peerIdentity api.PeerIdentityType, signature, message []byte) error {
   126  	return nil
   127  }
   128  
   129  func (s *cryptoService) ValidateIdentity(peerIdentity api.PeerIdentityType) error {
   130  	return nil
   131  }