github.com/tenywen/fabric@v1.0.0-beta.0.20170620030522-a5b1ed380643/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  var defaultSecureDialOpts = func() []grpc.DialOption {
    46  	var dialOpts []grpc.DialOption
    47  	dialOpts = append(dialOpts, grpc.WithInsecure())
    48  	return dialOpts
    49  }
    50  
    51  // This is just a test that shows how to instantiate a gossip component
    52  func TestNewGossipCryptoService(t *testing.T) {
    53  	setupTestEnv()
    54  	s1 := grpc.NewServer()
    55  	s2 := grpc.NewServer()
    56  	s3 := grpc.NewServer()
    57  	ll1, _ := net.Listen("tcp", fmt.Sprintf("%s:%d", "", 5611))
    58  	ll2, _ := net.Listen("tcp", fmt.Sprintf("%s:%d", "", 5612))
    59  	ll3, _ := net.Listen("tcp", fmt.Sprintf("%s:%d", "", 5613))
    60  	endpoint1 := "localhost:5611"
    61  	endpoint2 := "localhost:5612"
    62  	endpoint3 := "localhost:5613"
    63  	msptesttools.LoadMSPSetupForTesting()
    64  	peerIdentity, _ := mgmt.GetLocalSigningIdentityOrPanic().Serialize()
    65  	idMapper := identity.NewIdentityMapper(cryptSvc, peerIdentity)
    66  
    67  	g1 := NewGossipComponent(peerIdentity, endpoint1, s1, secAdv, cryptSvc, idMapper,
    68  		defaultSecureDialOpts)
    69  	g2 := NewGossipComponent(peerIdentity, endpoint2, s2, secAdv, cryptSvc, idMapper,
    70  		defaultSecureDialOpts, endpoint1)
    71  	g3 := NewGossipComponent(peerIdentity, endpoint3, s3, secAdv, cryptSvc, idMapper,
    72  		defaultSecureDialOpts, endpoint1)
    73  	defer g1.Stop()
    74  	defer g2.Stop()
    75  	defer g3.Stop()
    76  	go s1.Serve(ll1)
    77  	go s2.Serve(ll2)
    78  	go s3.Serve(ll3)
    79  }
    80  
    81  func TestBadInitialization(t *testing.T) {
    82  	msptesttools.LoadMSPSetupForTesting()
    83  	peerIdentity, _ := mgmt.GetLocalSigningIdentityOrPanic().Serialize()
    84  	s1 := grpc.NewServer()
    85  	idMapper := identity.NewIdentityMapper(cryptSvc, peerIdentity)
    86  	assert.Panics(t, func() {
    87  		newConfig("anEndpointWithoutAPort", "anEndpointWithoutAPort")
    88  	})
    89  	assert.Panics(t, func() {
    90  		viper.Set("peer.tls.enabled", true)
    91  		NewGossipComponent(peerIdentity, "localhost:5000", s1, secAdv, cryptSvc, idMapper,
    92  			defaultSecureDialOpts)
    93  	})
    94  }
    95  
    96  func setupTestEnv() {
    97  	viper.SetConfigName("core")
    98  	viper.SetEnvPrefix("CORE")
    99  	config.AddDevConfigPath(nil)
   100  	viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
   101  	viper.AutomaticEnv()
   102  	err := viper.ReadInConfig()
   103  	if err != nil { // Handle errors reading the config file
   104  		panic(fmt.Errorf("fatal error config file: %s", err))
   105  	}
   106  }
   107  
   108  type secAdviser struct {
   109  }
   110  
   111  func (sa *secAdviser) OrgByPeerIdentity(api.PeerIdentityType) api.OrgIdentityType {
   112  	return api.OrgIdentityType("DEFAULT")
   113  }
   114  
   115  type cryptoService struct {
   116  }
   117  
   118  func (s *cryptoService) GetPKIidOfCert(peerIdentity api.PeerIdentityType) common.PKIidType {
   119  	return common.PKIidType(peerIdentity)
   120  }
   121  
   122  func (s *cryptoService) VerifyBlock(chainID common.ChainID, seqNum uint64, signedBlock []byte) error {
   123  	return nil
   124  }
   125  
   126  func (s *cryptoService) Sign(msg []byte) ([]byte, error) {
   127  	return msg, nil
   128  }
   129  
   130  func (s *cryptoService) Verify(peerIdentity api.PeerIdentityType, signature, message []byte) error {
   131  	return nil
   132  }
   133  
   134  func (s *cryptoService) VerifyByChannel(chainID common.ChainID, peerIdentity api.PeerIdentityType, signature, message []byte) error {
   135  	return nil
   136  }
   137  
   138  func (s *cryptoService) ValidateIdentity(peerIdentity api.PeerIdentityType) error {
   139  	return nil
   140  }