github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/gossip/integration/integration_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 integration
    18  
    19  import (
    20  	"fmt"
    21  	"net"
    22  	"strings"
    23  	"testing"
    24  
    25  	"github.com/hyperledger/fabric/core/config"
    26  	"github.com/hyperledger/fabric/gossip/api"
    27  	"github.com/hyperledger/fabric/gossip/common"
    28  	"github.com/hyperledger/fabric/gossip/identity"
    29  	"github.com/hyperledger/fabric/gossip/util"
    30  	"github.com/hyperledger/fabric/msp/mgmt"
    31  	"github.com/hyperledger/fabric/msp/mgmt/testtools"
    32  	"github.com/spf13/viper"
    33  	"github.com/stretchr/testify/assert"
    34  	"google.golang.org/grpc"
    35  )
    36  
    37  func init() {
    38  	util.SetupTestLogging()
    39  }
    40  
    41  var (
    42  	cryptSvc = &cryptoService{}
    43  	secAdv   = &secAdviser{}
    44  )
    45  
    46  // This is just a test that shows how to instantiate a gossip component
    47  func TestNewGossipCryptoService(t *testing.T) {
    48  	setupTestEnv()
    49  	s1 := grpc.NewServer()
    50  	s2 := grpc.NewServer()
    51  	s3 := grpc.NewServer()
    52  	ll1, _ := net.Listen("tcp", fmt.Sprintf("%s:%d", "", 5611))
    53  	ll2, _ := net.Listen("tcp", fmt.Sprintf("%s:%d", "", 5612))
    54  	ll3, _ := net.Listen("tcp", fmt.Sprintf("%s:%d", "", 5613))
    55  	endpoint1 := "localhost:5611"
    56  	endpoint2 := "localhost:5612"
    57  	endpoint3 := "localhost:5613"
    58  	msptesttools.LoadMSPSetupForTesting()
    59  	peerIdentity, _ := mgmt.GetLocalSigningIdentityOrPanic().Serialize()
    60  	idMapper := identity.NewIdentityMapper(cryptSvc)
    61  
    62  	g1 := NewGossipComponent(peerIdentity, endpoint1, s1, secAdv, cryptSvc, idMapper, []grpc.DialOption{grpc.WithInsecure()})
    63  	g2 := NewGossipComponent(peerIdentity, endpoint2, s2, secAdv, cryptSvc, idMapper, []grpc.DialOption{grpc.WithInsecure()}, endpoint1)
    64  	g3 := NewGossipComponent(peerIdentity, endpoint3, s3, secAdv, cryptSvc, idMapper, []grpc.DialOption{grpc.WithInsecure()}, endpoint1)
    65  	defer g1.Stop()
    66  	defer g2.Stop()
    67  	defer g3.Stop()
    68  	go s1.Serve(ll1)
    69  	go s2.Serve(ll2)
    70  	go s3.Serve(ll3)
    71  }
    72  
    73  func TestBadInitialization(t *testing.T) {
    74  	msptesttools.LoadMSPSetupForTesting()
    75  	peerIdentity, _ := mgmt.GetLocalSigningIdentityOrPanic().Serialize()
    76  	s1 := grpc.NewServer()
    77  	idMapper := identity.NewIdentityMapper(cryptSvc)
    78  	assert.Panics(t, func() {
    79  		newConfig("anEndpointWithoutAPort", "anEndpointWithoutAPort")
    80  	})
    81  	assert.Panics(t, func() {
    82  		viper.Set("peer.tls.enabled", true)
    83  		NewGossipComponent(peerIdentity, "localhost:5000", s1, secAdv, cryptSvc, idMapper, []grpc.DialOption{grpc.WithInsecure()})
    84  	})
    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, 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  }