github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/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, err := NewGossipComponent(peerIdentity, endpoint1, s1, secAdv, cryptSvc, idMapper,
    68  		defaultSecureDialOpts)
    69  	assert.NoError(t, err)
    70  	g2, err := NewGossipComponent(peerIdentity, endpoint2, s2, secAdv, cryptSvc, idMapper,
    71  		defaultSecureDialOpts, endpoint1)
    72  	assert.NoError(t, err)
    73  	g3, err := NewGossipComponent(peerIdentity, endpoint3, s3, secAdv, cryptSvc, idMapper,
    74  		defaultSecureDialOpts, endpoint1)
    75  	assert.NoError(t, err)
    76  	defer g1.Stop()
    77  	defer g2.Stop()
    78  	defer g3.Stop()
    79  	go s1.Serve(ll1)
    80  	go s2.Serve(ll2)
    81  	go s3.Serve(ll3)
    82  }
    83  
    84  func TestBadInitialization(t *testing.T) {
    85  	msptesttools.LoadMSPSetupForTesting()
    86  	peerIdentity, _ := mgmt.GetLocalSigningIdentityOrPanic().Serialize()
    87  	s1 := grpc.NewServer()
    88  	idMapper := identity.NewIdentityMapper(cryptSvc, peerIdentity)
    89  	_, err := newConfig("anEndpointWithoutAPort", "anEndpointWithoutAPort")
    90  
    91  	viper.Set("peer.tls.enabled", true)
    92  	_, err = NewGossipComponent(peerIdentity, "localhost:5000", s1, secAdv, cryptSvc, idMapper,
    93  		defaultSecureDialOpts)
    94  	assert.Error(t, err)
    95  }
    96  
    97  func setupTestEnv() {
    98  	viper.SetConfigName("core")
    99  	viper.SetEnvPrefix("CORE")
   100  	config.AddDevConfigPath(nil)
   101  	viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
   102  	viper.AutomaticEnv()
   103  	err := viper.ReadInConfig()
   104  	if err != nil { // Handle errors reading the config file
   105  		panic(fmt.Errorf("fatal error config file: %s", err))
   106  	}
   107  }
   108  
   109  type secAdviser struct {
   110  }
   111  
   112  func (sa *secAdviser) OrgByPeerIdentity(api.PeerIdentityType) api.OrgIdentityType {
   113  	return api.OrgIdentityType("DEFAULT")
   114  }
   115  
   116  type cryptoService struct {
   117  }
   118  
   119  func (s *cryptoService) GetPKIidOfCert(peerIdentity api.PeerIdentityType) common.PKIidType {
   120  	return common.PKIidType(peerIdentity)
   121  }
   122  
   123  func (s *cryptoService) VerifyBlock(chainID common.ChainID, seqNum uint64, signedBlock []byte) error {
   124  	return nil
   125  }
   126  
   127  func (s *cryptoService) Sign(msg []byte) ([]byte, error) {
   128  	return msg, nil
   129  }
   130  
   131  func (s *cryptoService) Verify(peerIdentity api.PeerIdentityType, signature, message []byte) error {
   132  	return nil
   133  }
   134  
   135  func (s *cryptoService) VerifyByChannel(chainID common.ChainID, peerIdentity api.PeerIdentityType, signature, message []byte) error {
   136  	return nil
   137  }
   138  
   139  func (s *cryptoService) ValidateIdentity(peerIdentity api.PeerIdentityType) error {
   140  	return nil
   141  }