github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/gossip/service/join_test.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 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 service
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/hyperledger/fabric/common/config"
    24  	"github.com/hyperledger/fabric/gossip/api"
    25  	"github.com/hyperledger/fabric/gossip/comm"
    26  	"github.com/hyperledger/fabric/gossip/common"
    27  	"github.com/hyperledger/fabric/gossip/discovery"
    28  	"github.com/hyperledger/fabric/gossip/util"
    29  	proto "github.com/hyperledger/fabric/protos/gossip"
    30  	"github.com/hyperledger/fabric/protos/peer"
    31  	"github.com/stretchr/testify/assert"
    32  	"github.com/stretchr/testify/mock"
    33  )
    34  
    35  type secAdvMock struct {
    36  }
    37  
    38  func init() {
    39  	util.SetupTestLogging()
    40  }
    41  
    42  func (s *secAdvMock) OrgByPeerIdentity(identity api.PeerIdentityType) api.OrgIdentityType {
    43  	return api.OrgIdentityType(identity)
    44  }
    45  
    46  type gossipMock struct {
    47  	mock.Mock
    48  }
    49  
    50  func (*gossipMock) SuspectPeers(s api.PeerSuspector) {
    51  	panic("implement me")
    52  }
    53  
    54  func (*gossipMock) Send(msg *proto.GossipMessage, peers ...*comm.RemotePeer) {
    55  	panic("implement me")
    56  }
    57  
    58  func (*gossipMock) Peers() []discovery.NetworkMember {
    59  	panic("implement me")
    60  }
    61  
    62  func (*gossipMock) PeersOfChannel(common.ChainID) []discovery.NetworkMember {
    63  	panic("implement me")
    64  }
    65  
    66  func (*gossipMock) UpdateMetadata(metadata []byte) {
    67  	panic("implement me")
    68  }
    69  
    70  func (*gossipMock) UpdateChannelMetadata(metadata []byte, chainID common.ChainID) {
    71  	panic("implement me")
    72  }
    73  
    74  func (*gossipMock) Gossip(msg *proto.GossipMessage) {
    75  	panic("implement me")
    76  }
    77  
    78  func (*gossipMock) Accept(acceptor common.MessageAcceptor, passThrough bool) (<-chan *proto.GossipMessage, <-chan proto.ReceivedMessage) {
    79  	panic("implement me")
    80  }
    81  
    82  func (g *gossipMock) JoinChan(joinMsg api.JoinChannelMessage, chainID common.ChainID) {
    83  	g.Called()
    84  }
    85  
    86  func (*gossipMock) Stop() {
    87  	panic("implement me")
    88  }
    89  
    90  type appOrgMock struct {
    91  	id string
    92  }
    93  
    94  func (*appOrgMock) Name() string {
    95  	panic("implement me")
    96  }
    97  
    98  func (ao *appOrgMock) MSPID() string {
    99  	return ao.id
   100  }
   101  
   102  func (*appOrgMock) AnchorPeers() []*peer.AnchorPeer {
   103  	return []*peer.AnchorPeer{{Host: "1.2.3.4", Port: 5611}}
   104  }
   105  
   106  type configMock struct {
   107  }
   108  
   109  func (*configMock) ChainID() string {
   110  	return "A"
   111  }
   112  
   113  func (*configMock) Organizations() map[string]config.ApplicationOrg {
   114  	return map[string]config.ApplicationOrg{
   115  		"Org0": &appOrgMock{"Org0"},
   116  	}
   117  }
   118  
   119  func (*configMock) Sequence() uint64 {
   120  	return 0
   121  }
   122  
   123  func TestJoinChannelConfig(t *testing.T) {
   124  	// Scenarios: The channel we're joining has a single org - Org0
   125  	// but our org ID is actually Org0MSP in the negative path
   126  	// and Org0 in the positive path
   127  
   128  	failChan := make(chan struct{}, 1)
   129  	g1SvcMock := &gossipMock{}
   130  	g1SvcMock.On("JoinChan", mock.Anything).Run(func(_ mock.Arguments) {
   131  		failChan <- struct{}{}
   132  	})
   133  	g1 := &gossipServiceImpl{secAdv: &secAdvMock{}, peerIdentity: api.PeerIdentityType("OrgMSP0"), gossipSvc: g1SvcMock}
   134  	g1.configUpdated(&configMock{})
   135  	select {
   136  	case <-time.After(time.Second):
   137  	case <-failChan:
   138  		assert.Fail(t, "Joined a badly configured channel")
   139  	}
   140  
   141  	succChan := make(chan struct{}, 1)
   142  	g2SvcMock := &gossipMock{}
   143  	g2SvcMock.On("JoinChan", mock.Anything).Run(func(_ mock.Arguments) {
   144  		succChan <- struct{}{}
   145  	})
   146  	g2 := &gossipServiceImpl{secAdv: &secAdvMock{}, peerIdentity: api.PeerIdentityType("Org0"), gossipSvc: g2SvcMock}
   147  	g2.configUpdated(&configMock{})
   148  	select {
   149  	case <-time.After(time.Second):
   150  		assert.Fail(t, "Didn't join a channel (should have done so within the time period)")
   151  	case <-succChan:
   152  
   153  	}
   154  }