forked from libp2p/go-libp2p-pubsub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscription_filter_test.go
210 lines (185 loc) · 4.79 KB
/
subscription_filter_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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package pubsub
import (
"context"
"regexp"
"testing"
"time"
pb "github.com/libp2p/go-libp2p-pubsub/pb"
"github.com/libp2p/go-libp2p/core/peer"
)
func TestBasicSubscriptionFilter(t *testing.T) {
peerA := peer.ID("A")
topic1 := "test1"
topic2 := "test2"
topic3 := "test3"
yes := true
subs := []*pb.RPC_SubOpts{
&pb.RPC_SubOpts{
Topicid: &topic1,
Subscribe: &yes,
},
&pb.RPC_SubOpts{
Topicid: &topic2,
Subscribe: &yes,
},
&pb.RPC_SubOpts{
Topicid: &topic3,
Subscribe: &yes,
},
}
filter := NewAllowlistSubscriptionFilter(topic1, topic2)
canSubscribe := filter.CanSubscribe(topic1)
if !canSubscribe {
t.Fatal("expected allowed subscription")
}
canSubscribe = filter.CanSubscribe(topic2)
if !canSubscribe {
t.Fatal("expected allowed subscription")
}
canSubscribe = filter.CanSubscribe(topic3)
if canSubscribe {
t.Fatal("expected disallowed subscription")
}
allowedSubs, err := filter.FilterIncomingSubscriptions(peerA, subs)
if err != nil {
t.Fatal(err)
}
if len(allowedSubs) != 2 {
t.Fatalf("expected 2 allowed subscriptions but got %d", len(allowedSubs))
}
for _, sub := range allowedSubs {
if sub.GetTopicid() == topic3 {
t.Fatal("unpexted subscription to test3")
}
}
limitFilter := WrapLimitSubscriptionFilter(filter, 2)
_, err = limitFilter.FilterIncomingSubscriptions(peerA, subs)
if err != ErrTooManySubscriptions {
t.Fatal("expected rejection because of too many subscriptions")
}
filter = NewRegexpSubscriptionFilter(regexp.MustCompile("test[12]"))
canSubscribe = filter.CanSubscribe(topic1)
if !canSubscribe {
t.Fatal("expected allowed subscription")
}
canSubscribe = filter.CanSubscribe(topic2)
if !canSubscribe {
t.Fatal("expected allowed subscription")
}
canSubscribe = filter.CanSubscribe(topic3)
if canSubscribe {
t.Fatal("expected disallowed subscription")
}
allowedSubs, err = filter.FilterIncomingSubscriptions(peerA, subs)
if err != nil {
t.Fatal(err)
}
if len(allowedSubs) != 2 {
t.Fatalf("expected 2 allowed subscriptions but got %d", len(allowedSubs))
}
for _, sub := range allowedSubs {
if sub.GetTopicid() == topic3 {
t.Fatal("unexpected subscription")
}
}
limitFilter = WrapLimitSubscriptionFilter(filter, 2)
_, err = limitFilter.FilterIncomingSubscriptions(peerA, subs)
if err != ErrTooManySubscriptions {
t.Fatal("expected rejection because of too many subscriptions")
}
}
func TestSubscriptionFilterDeduplication(t *testing.T) {
peerA := peer.ID("A")
topic1 := "test1"
topic2 := "test2"
topic3 := "test3"
yes := true
no := false
subs := []*pb.RPC_SubOpts{
&pb.RPC_SubOpts{
Topicid: &topic1,
Subscribe: &yes,
},
&pb.RPC_SubOpts{
Topicid: &topic1,
Subscribe: &yes,
},
&pb.RPC_SubOpts{
Topicid: &topic2,
Subscribe: &yes,
},
&pb.RPC_SubOpts{
Topicid: &topic2,
Subscribe: &no,
},
&pb.RPC_SubOpts{
Topicid: &topic3,
Subscribe: &yes,
},
}
filter := NewAllowlistSubscriptionFilter(topic1, topic2)
allowedSubs, err := filter.FilterIncomingSubscriptions(peerA, subs)
if err != nil {
t.Fatal(err)
}
if len(allowedSubs) != 1 {
t.Fatalf("expected 2 allowed subscriptions but got %d", len(allowedSubs))
}
for _, sub := range allowedSubs {
if sub.GetTopicid() == topic3 || sub.GetTopicid() == topic2 {
t.Fatal("unexpected subscription")
}
}
}
func TestSubscriptionFilterRPC(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
hosts := getDefaultHosts(t, 2)
ps1 := getPubsub(ctx, hosts[0], WithSubscriptionFilter(NewAllowlistSubscriptionFilter("test1", "test2")))
ps2 := getPubsub(ctx, hosts[1], WithSubscriptionFilter(NewAllowlistSubscriptionFilter("test2", "test3")))
_ = mustSubscribe(t, ps1, "test1")
_ = mustSubscribe(t, ps1, "test2")
_ = mustSubscribe(t, ps2, "test2")
_ = mustSubscribe(t, ps2, "test3")
// check the rejection as well
_, err := ps1.Join("test3")
if err == nil {
t.Fatal("expected subscription error")
}
connect(t, hosts[0], hosts[1])
time.Sleep(time.Second)
var sub1, sub2, sub3 bool
ready := make(chan struct{})
ps1.eval <- func() {
_, sub1 = ps1.topics["test1"][hosts[1].ID()]
_, sub2 = ps1.topics["test2"][hosts[1].ID()]
_, sub3 = ps1.topics["test3"][hosts[1].ID()]
ready <- struct{}{}
}
<-ready
if sub1 {
t.Fatal("expected no subscription for test1")
}
if !sub2 {
t.Fatal("expected subscription for test2")
}
if sub3 {
t.Fatal("expected no subscription for test1")
}
ps2.eval <- func() {
_, sub1 = ps2.topics["test1"][hosts[0].ID()]
_, sub2 = ps2.topics["test2"][hosts[0].ID()]
_, sub3 = ps2.topics["test3"][hosts[0].ID()]
ready <- struct{}{}
}
<-ready
if sub1 {
t.Fatal("expected no subscription for test1")
}
if !sub2 {
t.Fatal("expected subscription for test1")
}
if sub3 {
t.Fatal("expected no subscription for test1")
}
}