@@ -25,10 +25,12 @@ import (
25
25
"github.com/hyperledger/fabric/gossip/api"
26
26
"github.com/hyperledger/fabric/gossip/common"
27
27
"github.com/hyperledger/fabric/gossip/identity"
28
+ "github.com/hyperledger/fabric/gossip/mocks"
28
29
"github.com/hyperledger/fabric/gossip/util"
29
30
proto "github.com/hyperledger/fabric/protos/gossip"
30
31
"github.com/spf13/viper"
31
32
"github.com/stretchr/testify/assert"
33
+ "github.com/stretchr/testify/mock"
32
34
"golang.org/x/net/context"
33
35
"google.golang.org/grpc"
34
36
"google.golang.org/grpc/credentials"
@@ -38,6 +40,7 @@ func init() {
38
40
util .SetupTestLogging ()
39
41
rand .Seed (time .Now ().UnixNano ())
40
42
factory .InitFactories (nil )
43
+ naiveSec .On ("OrgByPeerIdentity" , mock .Anything ).Return (api.OrgIdentityType {})
41
44
}
42
45
43
46
func acceptAll (msg interface {}) bool {
@@ -54,10 +57,11 @@ var (
54
57
)
55
58
56
59
type naiveSecProvider struct {
60
+ mocks.SecurityAdvisor
57
61
}
58
62
59
- func (* naiveSecProvider ) OrgByPeerIdentity (api.PeerIdentityType ) api.OrgIdentityType {
60
- return nil
63
+ func (nsp * naiveSecProvider ) OrgByPeerIdentity (identity api.PeerIdentityType ) api.OrgIdentityType {
64
+ return nsp . SecurityAdvisor . Called ( identity ). Get ( 0 ).(api. OrgIdentityType )
61
65
}
62
66
63
67
func (* naiveSecProvider ) Expiration (peerIdentity api.PeerIdentityType ) (time.Time , error ) {
@@ -109,7 +113,7 @@ func (*naiveSecProvider) VerifyByChannel(_ common.ChainID, _ api.PeerIdentityTyp
109
113
func newCommInstance (port int , sec * naiveSecProvider ) (Comm , error ) {
110
114
endpoint := fmt .Sprintf ("localhost:%d" , port )
111
115
id := []byte (endpoint )
112
- inst , err := NewCommInstanceWithServer (port , identity .NewIdentityMapper (sec , id , noopPurgeIdentity , sec ), id , nil )
116
+ inst , err := NewCommInstanceWithServer (port , identity .NewIdentityMapper (sec , id , noopPurgeIdentity , sec ), id , nil , sec )
113
117
return inst , err
114
118
}
115
119
@@ -273,7 +277,7 @@ func TestHandshake(t *testing.T) {
273
277
idMapper := identity .NewIdentityMapper (naiveSec , id , noopPurgeIdentity , naiveSec )
274
278
inst , err := NewCommInstance (s , nil , idMapper , api .PeerIdentityType ("localhost:9611" ), func () []grpc.DialOption {
275
279
return []grpc.DialOption {grpc .WithInsecure ()}
276
- })
280
+ }, naiveSec )
277
281
go s .Serve (ll )
278
282
assert .NoError (t , err )
279
283
var msg proto.ReceivedMessage
@@ -396,20 +400,87 @@ func TestBasic(t *testing.T) {
396
400
waitForMessages (t , out , 2 , "Didn't receive 2 messages" )
397
401
}
398
402
403
+ func TestConnectUnexpectedPeer (t * testing.T ) {
404
+ t .Parallel ()
405
+ // Scenarios: In both scenarios, comm1 connects to comm2 or comm3.
406
+ // and expects to see a PKI-ID which is equal to comm4's PKI-ID.
407
+ // The connection attempt would succeed or fail based on whether comm2 or comm3
408
+ // are in the same org as comm4
409
+ comm1Port := 1548
410
+ comm2Port := 1549
411
+ comm3Port := 1550
412
+ comm4Port := 1551
413
+
414
+ identityByPort := func (port int ) api.PeerIdentityType {
415
+ return api .PeerIdentityType (fmt .Sprintf ("localhost:%d" , port ))
416
+ }
417
+
418
+ customNaiveSec := & naiveSecProvider {}
419
+ customNaiveSec .On ("OrgByPeerIdentity" , identityByPort (comm1Port )).Return (api .OrgIdentityType ("O" ))
420
+ customNaiveSec .On ("OrgByPeerIdentity" , identityByPort (comm2Port )).Return (api .OrgIdentityType ("A" ))
421
+ customNaiveSec .On ("OrgByPeerIdentity" , identityByPort (comm3Port )).Return (api .OrgIdentityType ("B" ))
422
+ customNaiveSec .On ("OrgByPeerIdentity" , identityByPort (comm4Port )).Return (api .OrgIdentityType ("A" ))
423
+
424
+ comm1 , _ := newCommInstance (comm1Port , customNaiveSec )
425
+ comm2 , _ := newCommInstance (comm2Port , naiveSec )
426
+ comm3 , _ := newCommInstance (comm3Port , naiveSec )
427
+ comm4 , _ := newCommInstance (comm4Port , naiveSec )
428
+
429
+ defer comm1 .Stop ()
430
+ defer comm2 .Stop ()
431
+ defer comm3 .Stop ()
432
+ defer comm4 .Stop ()
433
+
434
+ messagesForComm1 := comm1 .Accept (acceptAll )
435
+ messagesForComm2 := comm2 .Accept (acceptAll )
436
+ messagesForComm3 := comm3 .Accept (acceptAll )
437
+
438
+ // Have comm4 send a message to comm1
439
+ // in order for comm1 to know comm4
440
+ comm4 .Send (createGossipMsg (), remotePeer (comm1Port ))
441
+ <- messagesForComm1
442
+ // Close the connection with comm4
443
+ comm1 .CloseConn (remotePeer (comm4Port ))
444
+ // At this point, comm1 knows comm4's identity and organization
445
+
446
+ t .Run ("Same organization" , func (t * testing.T ) {
447
+ unexpectedRemotePeer := remotePeer (comm2Port )
448
+ unexpectedRemotePeer .PKIID = remotePeer (comm4Port ).PKIID
449
+ comm1 .Send (createGossipMsg (), unexpectedRemotePeer )
450
+ select {
451
+ case <- messagesForComm2 :
452
+ case <- time .After (time .Second * 5 ):
453
+ assert .Fail (t , "Didn't receive a message within a timely manner" )
454
+ util .PrintStackTrace ()
455
+ }
456
+ })
457
+
458
+ t .Run ("Unexpected organization" , func (t * testing.T ) {
459
+ unexpectedRemotePeer := remotePeer (comm3Port )
460
+ unexpectedRemotePeer .PKIID = remotePeer (comm4Port ).PKIID
461
+ comm1 .Send (createGossipMsg (), unexpectedRemotePeer )
462
+ select {
463
+ case <- messagesForComm3 :
464
+ assert .Fail (t , "Message shouldn't have been received" )
465
+ case <- time .After (time .Second * 5 ):
466
+ }
467
+ })
468
+ }
469
+
399
470
func TestProdConstructor (t * testing.T ) {
400
471
t .Parallel ()
401
472
srv , lsnr , dialOpts , certs := createGRPCLayer (20000 )
402
473
defer srv .Stop ()
403
474
defer lsnr .Close ()
404
475
id := []byte ("localhost:20000" )
405
- comm1 , _ := NewCommInstance (srv , certs , identity .NewIdentityMapper (naiveSec , id , noopPurgeIdentity , naiveSec ), id , dialOpts )
476
+ comm1 , _ := NewCommInstance (srv , certs , identity .NewIdentityMapper (naiveSec , id , noopPurgeIdentity , naiveSec ), id , dialOpts , naiveSec )
406
477
go srv .Serve (lsnr )
407
478
408
479
srv , lsnr , dialOpts , certs = createGRPCLayer (30000 )
409
480
defer srv .Stop ()
410
481
defer lsnr .Close ()
411
482
id = []byte ("localhost:30000" )
412
- comm2 , _ := NewCommInstance (srv , certs , identity .NewIdentityMapper (naiveSec , id , noopPurgeIdentity , naiveSec ), id , dialOpts )
483
+ comm2 , _ := NewCommInstance (srv , certs , identity .NewIdentityMapper (naiveSec , id , noopPurgeIdentity , naiveSec ), id , dialOpts , naiveSec )
413
484
go srv .Serve (lsnr )
414
485
defer comm1 .Stop ()
415
486
defer comm2 .Stop ()
0 commit comments