forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistributor_test.go
129 lines (117 loc) · 3.83 KB
/
distributor_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package privdata
import (
"errors"
"testing"
"github.com/hyperledger/fabric/gossip/api"
gcommon "github.com/hyperledger/fabric/gossip/common"
"github.com/hyperledger/fabric/gossip/discovery"
"github.com/hyperledger/fabric/gossip/filter"
gossip2 "github.com/hyperledger/fabric/gossip/gossip"
"github.com/hyperledger/fabric/protos/common"
proto "github.com/hyperledger/fabric/protos/gossip"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
type gossipMock struct {
err error
mock.Mock
api.PeerSignature
}
func (g *gossipMock) SendByCriteria(message *proto.SignedGossipMessage, criteria gossip2.SendCriteria) error {
args := g.Called(message, criteria)
if args.Get(0) != nil {
return args.Get(0).(error)
}
return nil
}
func (g *gossipMock) PeerFilter(channel gcommon.ChainID, messagePredicate api.SubChannelSelectionCriteria) (filter.RoutingFilter, error) {
if g.err != nil {
return nil, g.err
}
return func(member discovery.NetworkMember) bool {
return messagePredicate(g.PeerSignature)
}, nil
}
func TestDistributor(t *testing.T) {
g := &gossipMock{
Mock: mock.Mock{},
PeerSignature: api.PeerSignature{
Signature: []byte{3, 4, 5},
Message: []byte{6, 7, 8},
PeerIdentity: []byte{0, 1, 2},
},
}
sendings := make(chan struct {
*proto.PrivatePayload
gossip2.SendCriteria
}, 8)
g.On("SendByCriteria", mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
msg := args.Get(0).(*proto.SignedGossipMessage)
sendCriteria := args.Get(1).(gossip2.SendCriteria)
sendings <- struct {
*proto.PrivatePayload
gossip2.SendCriteria
}{
PrivatePayload: msg.GetPrivateData().Payload,
SendCriteria: sendCriteria,
}
}).Return(nil)
d := NewDistributor("test", g)
pdFactory := &pvtDataFactory{}
peerSelfSignedData := common.SignedData{
Identity: []byte{0, 1, 2},
Signature: []byte{3, 4, 5},
Data: []byte{6, 7, 8},
}
cs := createcollectionStore(peerSelfSignedData).thatAccepts(common.CollectionCriteria{
TxId: "tx1",
Namespace: "ns1",
Channel: "test",
Collection: "c1",
}).thatAccepts(common.CollectionCriteria{
TxId: "tx2",
Namespace: "ns2",
Channel: "test",
Collection: "c2",
}).andIsLenient()
pvtData := pdFactory.addRWSet().addNSRWSet("ns1", "c1", "c2").addRWSet().addNSRWSet("ns2", "c1", "c2").create()
err := d.Distribute("tx1", pvtData[0].WriteSet, cs)
assert.NoError(t, err)
err = d.Distribute("tx2", pvtData[1].WriteSet, cs)
assert.NoError(t, err)
assertACL := func(pp *proto.PrivatePayload, sc gossip2.SendCriteria) {
eligible := pp.Namespace == "ns1" && pp.CollectionName == "c1"
eligible = eligible || (pp.Namespace == "ns2" && pp.CollectionName == "c2")
// The mock collection store returns policies that for ns1 and c1, or ns2 and c2 return true regardless
// of the network member, and for any other collection and namespace combination - return false
// Ensure MaxPeers is maxInternalPeers which is 2
//and MinAck is minInternalPeers which is 1
assert.Equal(t, 2, sc.MaxPeers)
assert.Equal(t, 1, sc.MinAck)
}
i := 0
for dis := range sendings {
assertACL(dis.PrivatePayload, dis.SendCriteria)
i++
if i == 4 {
break
}
}
// Channel is empty after we read 8 times from it
assert.Len(t, sendings, 0)
// Bad path: dependencies (gossip and others) don't work properly
g.err = errors.New("failed obtaining filter")
err = d.Distribute("tx1", pvtData[0].WriteSet, cs)
assert.Error(t, err)
assert.Contains(t, err.Error(), "failed obtaining filter")
g.Mock = mock.Mock{}
g.On("SendByCriteria", mock.Anything, mock.Anything).Return(errors.New("failed sending"))
g.err = nil
err = d.Distribute("tx1", pvtData[0].WriteSet, cs)
assert.Error(t, err)
assert.Contains(t, err.Error(), "Failed disseminating 2 out of 2 private RWSets")
}