2016-09-10 03:13:50 +00:00
|
|
|
package floodsub
|
2016-09-10 00:15:39 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
2016-09-14 22:11:41 +00:00
|
|
|
"sort"
|
2017-11-22 17:40:45 +00:00
|
|
|
"sync"
|
2016-09-10 00:15:39 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2016-10-05 19:47:20 +00:00
|
|
|
host "github.com/libp2p/go-libp2p-host"
|
|
|
|
peer "github.com/libp2p/go-libp2p-peer"
|
2018-02-17 03:42:39 +00:00
|
|
|
swarmt "github.com/libp2p/go-libp2p-swarm/testing"
|
2017-01-10 22:45:25 +00:00
|
|
|
//bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
|
|
|
|
bhost "github.com/libp2p/go-libp2p-blankhost"
|
2018-06-01 22:50:24 +00:00
|
|
|
"github.com/libp2p/go-libp2p-protocol"
|
2016-09-10 00:15:39 +00:00
|
|
|
)
|
|
|
|
|
2016-10-19 23:01:06 +00:00
|
|
|
func checkMessageRouting(t *testing.T, topic string, pubs []*PubSub, subs []*Subscription) {
|
2016-09-14 21:11:46 +00:00
|
|
|
data := make([]byte, 16)
|
|
|
|
rand.Read(data)
|
|
|
|
|
|
|
|
for _, p := range pubs {
|
|
|
|
err := p.Publish(topic, data)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range subs {
|
|
|
|
assertReceive(t, s, data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-10 23:03:53 +00:00
|
|
|
func getNetHosts(t *testing.T, ctx context.Context, n int) []host.Host {
|
2016-09-10 00:15:39 +00:00
|
|
|
var out []host.Host
|
|
|
|
|
|
|
|
for i := 0; i < n; i++ {
|
2018-02-17 03:42:39 +00:00
|
|
|
netw := swarmt.GenSwarm(t, ctx)
|
2017-01-10 22:45:25 +00:00
|
|
|
h := bhost.NewBlankHost(netw)
|
2016-09-10 00:15:39 +00:00
|
|
|
out = append(out, h)
|
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func connect(t *testing.T, a, b host.Host) {
|
|
|
|
pinfo := a.Peerstore().PeerInfo(a.ID())
|
|
|
|
err := b.Connect(context.Background(), pinfo)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-10 23:03:53 +00:00
|
|
|
func sparseConnect(t *testing.T, hosts []host.Host) {
|
|
|
|
for i, a := range hosts {
|
|
|
|
for j := 0; j < 3; j++ {
|
|
|
|
n := rand.Intn(len(hosts))
|
|
|
|
if n == i {
|
|
|
|
j--
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
b := hosts[n]
|
|
|
|
|
|
|
|
connect(t, a, b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-10 00:15:39 +00:00
|
|
|
func connectAll(t *testing.T, hosts []host.Host) {
|
|
|
|
for i, a := range hosts {
|
|
|
|
for j, b := range hosts {
|
|
|
|
if i == j {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
connect(t, a, b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-16 12:12:23 +00:00
|
|
|
func getPubsubs(ctx context.Context, hs []host.Host, opts ...Option) []*PubSub {
|
2016-09-10 00:15:39 +00:00
|
|
|
var psubs []*PubSub
|
2016-09-10 23:03:53 +00:00
|
|
|
for _, h := range hs {
|
2017-12-16 12:12:23 +00:00
|
|
|
ps, err := NewFloodSub(ctx, h, opts...)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
psubs = append(psubs, ps)
|
2016-09-10 00:15:39 +00:00
|
|
|
}
|
2016-09-10 23:03:53 +00:00
|
|
|
return psubs
|
|
|
|
}
|
|
|
|
|
2016-10-19 23:01:06 +00:00
|
|
|
func assertReceive(t *testing.T, ch *Subscription, exp []byte) {
|
2016-09-12 20:22:16 +00:00
|
|
|
select {
|
2016-10-19 23:01:06 +00:00
|
|
|
case msg := <-ch.ch:
|
2016-09-12 20:22:16 +00:00
|
|
|
if !bytes.Equal(msg.GetData(), exp) {
|
|
|
|
t.Fatalf("got wrong message, expected %s but got %s", string(exp), string(msg.GetData()))
|
|
|
|
}
|
|
|
|
case <-time.After(time.Second * 5):
|
2016-10-19 23:01:06 +00:00
|
|
|
t.Logf("%#v\n", ch)
|
2016-09-12 20:22:16 +00:00
|
|
|
t.Fatal("timed out waiting for message of: ", string(exp))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-10 23:03:53 +00:00
|
|
|
func TestBasicFloodsub(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
hosts := getNetHosts(t, ctx, 20)
|
|
|
|
|
|
|
|
psubs := getPubsubs(ctx, hosts)
|
2016-09-10 00:15:39 +00:00
|
|
|
|
2016-10-19 23:01:06 +00:00
|
|
|
var msgs []*Subscription
|
2016-09-10 00:15:39 +00:00
|
|
|
for _, ps := range psubs {
|
2016-11-11 23:47:53 +00:00
|
|
|
subch, err := ps.Subscribe("foobar")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-09-10 00:15:39 +00:00
|
|
|
|
|
|
|
msgs = append(msgs, subch)
|
|
|
|
}
|
|
|
|
|
2016-09-10 23:03:53 +00:00
|
|
|
//connectAll(t, hosts)
|
|
|
|
sparseConnect(t, hosts)
|
2016-09-10 00:15:39 +00:00
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
msg := []byte(fmt.Sprintf("%d the flooooooood %d", i, i))
|
|
|
|
|
|
|
|
owner := rand.Intn(len(psubs))
|
|
|
|
|
|
|
|
psubs[owner].Publish("foobar", msg)
|
|
|
|
|
2016-10-19 23:01:06 +00:00
|
|
|
for _, sub := range msgs {
|
2016-11-17 10:27:57 +00:00
|
|
|
got, err := sub.Next(ctx)
|
2016-10-19 23:01:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(sub.err)
|
|
|
|
}
|
2016-09-10 00:15:39 +00:00
|
|
|
if !bytes.Equal(msg, got.Data) {
|
|
|
|
t.Fatal("got wrong message!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-10 23:03:53 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMultihops(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
hosts := getNetHosts(t, ctx, 6)
|
|
|
|
|
|
|
|
psubs := getPubsubs(ctx, hosts)
|
|
|
|
|
|
|
|
connect(t, hosts[0], hosts[1])
|
|
|
|
connect(t, hosts[1], hosts[2])
|
|
|
|
connect(t, hosts[2], hosts[3])
|
|
|
|
connect(t, hosts[3], hosts[4])
|
|
|
|
connect(t, hosts[4], hosts[5])
|
|
|
|
|
2016-10-19 23:01:06 +00:00
|
|
|
var subs []*Subscription
|
2016-09-10 23:03:53 +00:00
|
|
|
for i := 1; i < 6; i++ {
|
2016-11-11 23:47:53 +00:00
|
|
|
ch, err := psubs[i].Subscribe("foobar")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-10-19 23:01:06 +00:00
|
|
|
subs = append(subs, ch)
|
2016-09-10 23:03:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
|
|
|
|
msg := []byte("i like cats")
|
|
|
|
err := psubs[0].Publish("foobar", msg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// last node in the chain should get the message
|
|
|
|
select {
|
2016-10-19 23:01:06 +00:00
|
|
|
case out := <-subs[4].ch:
|
2016-09-10 23:03:53 +00:00
|
|
|
if !bytes.Equal(out.GetData(), msg) {
|
|
|
|
t.Fatal("got wrong data")
|
|
|
|
}
|
|
|
|
case <-time.After(time.Second * 5):
|
|
|
|
t.Fatal("timed out waiting for message")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReconnects(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2016-09-12 20:22:16 +00:00
|
|
|
hosts := getNetHosts(t, ctx, 3)
|
2016-09-10 23:03:53 +00:00
|
|
|
|
|
|
|
psubs := getPubsubs(ctx, hosts)
|
|
|
|
|
|
|
|
connect(t, hosts[0], hosts[1])
|
|
|
|
connect(t, hosts[0], hosts[2])
|
|
|
|
|
2016-11-11 23:47:53 +00:00
|
|
|
A, err := psubs[1].Subscribe("cats")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-09-10 23:03:53 +00:00
|
|
|
|
2016-11-11 23:47:53 +00:00
|
|
|
B, err := psubs[2].Subscribe("cats")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-09-10 23:03:53 +00:00
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
|
|
|
|
msg := []byte("apples and oranges")
|
2016-11-11 23:47:53 +00:00
|
|
|
err = psubs[0].Publish("cats", msg)
|
2016-09-10 23:03:53 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assertReceive(t, A, msg)
|
|
|
|
assertReceive(t, B, msg)
|
|
|
|
|
2016-10-19 23:01:06 +00:00
|
|
|
B.Cancel()
|
2016-09-12 20:22:16 +00:00
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 50)
|
2016-09-10 23:03:53 +00:00
|
|
|
|
|
|
|
msg2 := []byte("potato")
|
|
|
|
err = psubs[0].Publish("cats", msg2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assertReceive(t, A, msg2)
|
2016-09-12 20:22:16 +00:00
|
|
|
select {
|
2016-10-19 23:01:06 +00:00
|
|
|
case _, ok := <-B.ch:
|
2016-09-12 20:22:16 +00:00
|
|
|
if ok {
|
|
|
|
t.Fatal("shouldnt have gotten data on this channel")
|
|
|
|
}
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatal("timed out waiting for B chan to be closed")
|
|
|
|
}
|
2016-09-10 23:03:53 +00:00
|
|
|
|
2016-10-19 23:01:06 +00:00
|
|
|
nSubs := len(psubs[2].myTopics["cats"])
|
|
|
|
if nSubs > 0 {
|
|
|
|
t.Fatal(`B should have 0 subscribers for channel "cats", has`, nSubs)
|
|
|
|
}
|
|
|
|
|
2016-11-11 23:47:53 +00:00
|
|
|
ch2, err := psubs[2].Subscribe("cats")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-09-10 23:03:53 +00:00
|
|
|
|
2016-10-19 23:01:06 +00:00
|
|
|
time.Sleep(time.Millisecond * 100)
|
2016-09-12 20:22:16 +00:00
|
|
|
|
|
|
|
nextmsg := []byte("ifps is kul")
|
|
|
|
err = psubs[0].Publish("cats", nextmsg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2016-09-10 23:03:53 +00:00
|
|
|
}
|
2016-09-12 20:22:16 +00:00
|
|
|
|
|
|
|
assertReceive(t, ch2, nextmsg)
|
2016-09-10 00:15:39 +00:00
|
|
|
}
|
2016-09-11 20:56:07 +00:00
|
|
|
|
2016-09-12 20:22:16 +00:00
|
|
|
// make sure messages arent routed between nodes who arent subscribed
|
2016-09-11 20:56:07 +00:00
|
|
|
func TestNoConnection(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
hosts := getNetHosts(t, ctx, 10)
|
|
|
|
|
|
|
|
psubs := getPubsubs(ctx, hosts)
|
|
|
|
|
2016-11-11 23:47:53 +00:00
|
|
|
ch, err := psubs[5].Subscribe("foobar")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-09-11 20:56:07 +00:00
|
|
|
|
2016-11-11 23:47:53 +00:00
|
|
|
err = psubs[0].Publish("foobar", []byte("TESTING"))
|
2016-09-11 20:56:07 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2016-10-19 23:01:06 +00:00
|
|
|
case <-ch.ch:
|
2016-09-11 20:56:07 +00:00
|
|
|
t.Fatal("shouldnt have gotten a message")
|
|
|
|
case <-time.After(time.Millisecond * 200):
|
|
|
|
}
|
|
|
|
}
|
2016-09-12 20:22:16 +00:00
|
|
|
|
|
|
|
func TestSelfReceive(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
host := getNetHosts(t, ctx, 1)[0]
|
|
|
|
|
2017-12-16 12:12:23 +00:00
|
|
|
psub, err := NewFloodSub(ctx, host)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-09-12 20:22:16 +00:00
|
|
|
|
|
|
|
msg := []byte("hello world")
|
|
|
|
|
2017-12-16 12:12:23 +00:00
|
|
|
err = psub.Publish("foobar", msg)
|
2016-09-12 20:22:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 10)
|
|
|
|
|
2016-11-11 23:47:53 +00:00
|
|
|
ch, err := psub.Subscribe("foobar")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-09-12 20:22:16 +00:00
|
|
|
|
|
|
|
msg2 := []byte("goodbye world")
|
|
|
|
err = psub.Publish("foobar", msg2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assertReceive(t, ch, msg2)
|
|
|
|
}
|
2016-09-14 21:11:46 +00:00
|
|
|
|
|
|
|
func TestOneToOne(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
hosts := getNetHosts(t, ctx, 2)
|
|
|
|
psubs := getPubsubs(ctx, hosts)
|
|
|
|
|
|
|
|
connect(t, hosts[0], hosts[1])
|
|
|
|
|
2017-11-08 19:00:52 +00:00
|
|
|
sub, err := psubs[1].Subscribe("foobar")
|
2016-11-11 23:47:53 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-09-14 21:11:46 +00:00
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 50)
|
|
|
|
|
2017-11-08 19:00:52 +00:00
|
|
|
checkMessageRouting(t, "foobar", psubs, []*Subscription{sub})
|
|
|
|
}
|
|
|
|
|
2018-02-28 20:26:42 +00:00
|
|
|
func TestRegisterUnregisterValidator(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
hosts := getNetHosts(t, ctx, 1)
|
|
|
|
psubs := getPubsubs(ctx, hosts)
|
|
|
|
|
|
|
|
err := psubs[0].RegisterTopicValidator("foo", func(context.Context, *Message) bool {
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = psubs[0].UnregisterTopicValidator("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = psubs[0].UnregisterTopicValidator("foo")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Unregistered bogus topic validator")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-08 19:00:52 +00:00
|
|
|
func TestValidate(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
hosts := getNetHosts(t, ctx, 2)
|
|
|
|
psubs := getPubsubs(ctx, hosts)
|
|
|
|
|
|
|
|
connect(t, hosts[0], hosts[1])
|
|
|
|
topic := "foobar"
|
|
|
|
|
2018-01-18 17:12:36 +00:00
|
|
|
err := psubs[1].RegisterTopicValidator(topic, func(ctx context.Context, msg *Message) bool {
|
2017-11-08 19:00:52 +00:00
|
|
|
return !bytes.Contains(msg.Data, []byte("illegal"))
|
2018-01-18 17:12:36 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sub, err := psubs[1].Subscribe(topic)
|
2017-11-08 19:00:52 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 50)
|
|
|
|
|
2017-11-22 16:37:01 +00:00
|
|
|
msgs := []struct {
|
|
|
|
msg []byte
|
|
|
|
validates bool
|
|
|
|
}{
|
|
|
|
{msg: []byte("this is a legal message"), validates: true},
|
|
|
|
{msg: []byte("there also is nothing controversial about this message"), validates: true},
|
|
|
|
{msg: []byte("openly illegal content will be censored"), validates: false},
|
|
|
|
{msg: []byte("but subversive actors will use leetspeek to spread 1ll3g4l content"), validates: true},
|
|
|
|
}
|
2017-11-08 19:00:52 +00:00
|
|
|
|
2017-11-22 16:37:01 +00:00
|
|
|
for _, tc := range msgs {
|
|
|
|
for _, p := range psubs {
|
|
|
|
err := p.Publish(topic, tc.msg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-11-08 19:00:52 +00:00
|
|
|
|
2017-11-22 16:37:01 +00:00
|
|
|
select {
|
|
|
|
case msg := <-sub.ch:
|
|
|
|
if !tc.validates {
|
|
|
|
t.Log(msg)
|
|
|
|
t.Error("expected message validation to filter out the message")
|
|
|
|
}
|
|
|
|
case <-time.After(333 * time.Millisecond):
|
|
|
|
if tc.validates {
|
|
|
|
t.Error("expected message validation to accept the message")
|
|
|
|
}
|
|
|
|
}
|
2017-11-08 19:00:52 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-14 21:11:46 +00:00
|
|
|
}
|
|
|
|
|
2017-11-22 17:40:45 +00:00
|
|
|
func TestValidateOverload(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2017-12-16 12:12:23 +00:00
|
|
|
type msg struct {
|
|
|
|
msg []byte
|
|
|
|
validates bool
|
2017-11-22 17:40:45 +00:00
|
|
|
}
|
|
|
|
|
2017-12-16 12:12:23 +00:00
|
|
|
tcs := []struct {
|
|
|
|
msgs []msg
|
2017-11-22 17:40:45 +00:00
|
|
|
|
2017-12-16 12:12:23 +00:00
|
|
|
maxConcurrency int
|
2017-11-22 17:40:45 +00:00
|
|
|
}{
|
2017-12-16 12:12:23 +00:00
|
|
|
{
|
|
|
|
maxConcurrency: 10,
|
|
|
|
msgs: []msg{
|
|
|
|
{msg: []byte("this is a legal message"), validates: true},
|
|
|
|
{msg: []byte("but subversive actors will use leetspeek to spread 1ll3g4l content"), validates: true},
|
|
|
|
{msg: []byte("there also is nothing controversial about this message"), validates: true},
|
|
|
|
{msg: []byte("also fine"), validates: true},
|
|
|
|
{msg: []byte("still, all good"), validates: true},
|
|
|
|
{msg: []byte("this is getting boring"), validates: true},
|
|
|
|
{msg: []byte("foo"), validates: true},
|
|
|
|
{msg: []byte("foobar"), validates: true},
|
|
|
|
{msg: []byte("foofoo"), validates: true},
|
|
|
|
{msg: []byte("barfoo"), validates: true},
|
|
|
|
{msg: []byte("oh no!"), validates: false},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
maxConcurrency: 2,
|
|
|
|
msgs: []msg{
|
|
|
|
{msg: []byte("this is a legal message"), validates: true},
|
|
|
|
{msg: []byte("but subversive actors will use leetspeek to spread 1ll3g4l content"), validates: true},
|
|
|
|
{msg: []byte("oh no!"), validates: false},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tcs {
|
|
|
|
|
|
|
|
hosts := getNetHosts(t, ctx, 2)
|
2018-01-13 12:31:34 +00:00
|
|
|
psubs := getPubsubs(ctx, hosts)
|
2017-12-16 12:12:23 +00:00
|
|
|
|
|
|
|
connect(t, hosts[0], hosts[1])
|
|
|
|
topic := "foobar"
|
|
|
|
|
|
|
|
block := make(chan struct{})
|
2017-11-22 17:40:45 +00:00
|
|
|
|
2018-01-18 17:12:36 +00:00
|
|
|
err := psubs[1].RegisterTopicValidator(topic,
|
|
|
|
func(ctx context.Context, msg *Message) bool {
|
2018-01-13 12:31:34 +00:00
|
|
|
<-block
|
|
|
|
return true
|
2018-01-18 17:12:36 +00:00
|
|
|
},
|
|
|
|
WithValidatorConcurrency(tc.maxConcurrency))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-01-13 12:31:34 +00:00
|
|
|
|
2018-01-18 17:12:36 +00:00
|
|
|
sub, err := psubs[1].Subscribe(topic)
|
2017-12-16 12:12:23 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-11-22 17:40:45 +00:00
|
|
|
|
2017-12-16 12:12:23 +00:00
|
|
|
time.Sleep(time.Millisecond * 50)
|
2017-11-22 17:40:45 +00:00
|
|
|
|
2017-12-16 12:12:23 +00:00
|
|
|
if len(tc.msgs) != tc.maxConcurrency+1 {
|
2018-01-13 18:52:38 +00:00
|
|
|
t.Fatalf("expected number of messages sent to be maxConcurrency+1. Got %d, expected %d", len(tc.msgs), tc.maxConcurrency+1)
|
2017-12-16 12:12:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p := psubs[0]
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
for _, tmsg := range tc.msgs {
|
|
|
|
select {
|
|
|
|
case msg := <-sub.ch:
|
|
|
|
if !tmsg.validates {
|
|
|
|
t.Log(msg)
|
|
|
|
t.Error("expected message validation to drop the message because all validator goroutines are taken")
|
|
|
|
}
|
|
|
|
case <-time.After(333 * time.Millisecond):
|
|
|
|
if tmsg.validates {
|
|
|
|
t.Error("expected message validation to accept the message")
|
|
|
|
}
|
2017-11-22 17:40:45 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-16 12:12:23 +00:00
|
|
|
wg.Done()
|
|
|
|
}()
|
2017-11-22 17:40:45 +00:00
|
|
|
|
2017-12-16 12:12:23 +00:00
|
|
|
for i, tmsg := range tc.msgs {
|
|
|
|
err := p.Publish(topic, tmsg.msg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-11-22 17:40:45 +00:00
|
|
|
|
2017-12-16 12:12:23 +00:00
|
|
|
// wait a bit to let pubsub's internal state machine start validating the message
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
2017-11-22 17:40:45 +00:00
|
|
|
|
2017-12-16 12:12:23 +00:00
|
|
|
// unblock validator goroutines after we sent one too many
|
|
|
|
if i == len(tc.msgs)-1 {
|
|
|
|
close(block)
|
|
|
|
}
|
2017-11-22 17:40:45 +00:00
|
|
|
}
|
2017-12-16 12:12:23 +00:00
|
|
|
wg.Wait()
|
2017-11-22 17:40:45 +00:00
|
|
|
}
|
2016-09-14 21:11:46 +00:00
|
|
|
}
|
|
|
|
|
2016-09-15 01:07:30 +00:00
|
|
|
func assertPeerLists(t *testing.T, hosts []host.Host, ps *PubSub, has ...int) {
|
2016-10-18 18:13:12 +00:00
|
|
|
peers := ps.ListPeers("")
|
2016-09-15 01:07:30 +00:00
|
|
|
set := make(map[peer.ID]struct{})
|
|
|
|
for _, p := range peers {
|
|
|
|
set[p] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, h := range has {
|
|
|
|
if _, ok := set[hosts[h].ID()]; !ok {
|
|
|
|
t.Fatal("expected to have connection to peer: ", h)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-14 21:11:46 +00:00
|
|
|
func TestTreeTopology(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
hosts := getNetHosts(t, ctx, 10)
|
|
|
|
psubs := getPubsubs(ctx, hosts)
|
|
|
|
|
|
|
|
connect(t, hosts[0], hosts[1])
|
|
|
|
connect(t, hosts[1], hosts[2])
|
|
|
|
connect(t, hosts[1], hosts[4])
|
|
|
|
connect(t, hosts[2], hosts[3])
|
|
|
|
connect(t, hosts[0], hosts[5])
|
|
|
|
connect(t, hosts[5], hosts[6])
|
|
|
|
connect(t, hosts[5], hosts[8])
|
|
|
|
connect(t, hosts[6], hosts[7])
|
|
|
|
connect(t, hosts[8], hosts[9])
|
|
|
|
|
|
|
|
/*
|
|
|
|
[0] -> [1] -> [2] -> [3]
|
|
|
|
| L->[4]
|
|
|
|
v
|
|
|
|
[5] -> [6] -> [7]
|
|
|
|
|
|
|
|
|
v
|
|
|
|
[8] -> [9]
|
|
|
|
*/
|
|
|
|
|
2016-10-19 23:01:06 +00:00
|
|
|
var chs []*Subscription
|
2016-09-14 21:11:46 +00:00
|
|
|
for _, ps := range psubs {
|
2016-11-11 23:47:53 +00:00
|
|
|
ch, err := ps.Subscribe("fizzbuzz")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-09-14 21:11:46 +00:00
|
|
|
|
|
|
|
chs = append(chs, ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 50)
|
|
|
|
|
2016-09-15 01:07:30 +00:00
|
|
|
assertPeerLists(t, hosts, psubs[0], 1, 5)
|
|
|
|
assertPeerLists(t, hosts, psubs[1], 0, 2, 4)
|
|
|
|
assertPeerLists(t, hosts, psubs[2], 1, 3)
|
|
|
|
|
2016-09-14 21:11:46 +00:00
|
|
|
checkMessageRouting(t, "fizzbuzz", []*PubSub{psubs[9], psubs[3]}, chs)
|
|
|
|
}
|
2016-09-14 22:11:41 +00:00
|
|
|
|
|
|
|
func assertHasTopics(t *testing.T, ps *PubSub, exptopics ...string) {
|
|
|
|
topics := ps.GetTopics()
|
|
|
|
sort.Strings(topics)
|
|
|
|
sort.Strings(exptopics)
|
|
|
|
|
|
|
|
if len(topics) != len(exptopics) {
|
|
|
|
t.Fatalf("expected to have %v, but got %v", exptopics, topics)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, v := range exptopics {
|
|
|
|
if topics[i] != v {
|
|
|
|
t.Fatalf("expected %s but have %s", v, topics[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-01 22:50:24 +00:00
|
|
|
func TestFloodSubPluggableProtocol(t *testing.T) {
|
|
|
|
t.Run("multi-procol router acts like a hub", func(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
hosts := getNetHosts(t, ctx, 3)
|
|
|
|
|
|
|
|
psubA := mustCreatePubSub(ctx, t, hosts[0], "/esh/floodsub", "/lsr/floodsub")
|
|
|
|
psubB := mustCreatePubSub(ctx, t, hosts[1], "/esh/floodsub")
|
|
|
|
psubC := mustCreatePubSub(ctx, t, hosts[2], "/lsr/floodsub")
|
|
|
|
|
|
|
|
subA := mustSubscribe(t, psubA, "foobar")
|
|
|
|
defer subA.Cancel()
|
|
|
|
|
|
|
|
subB := mustSubscribe(t, psubB, "foobar")
|
|
|
|
defer subB.Cancel()
|
|
|
|
|
|
|
|
subC := mustSubscribe(t, psubC, "foobar")
|
|
|
|
defer subC.Cancel()
|
|
|
|
|
|
|
|
// B --> A, C --> A
|
|
|
|
connect(t, hosts[1], hosts[0])
|
|
|
|
connect(t, hosts[2], hosts[0])
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
|
|
|
|
psubC.Publish("foobar", []byte("bar"))
|
|
|
|
|
|
|
|
assertReceive(t, subA, []byte("bar"))
|
|
|
|
assertReceive(t, subB, []byte("bar"))
|
|
|
|
assertReceive(t, subC, []byte("bar"))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("won't talk to routers with no protocol overlap", func(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
hosts := getNetHosts(t, ctx, 2)
|
|
|
|
|
|
|
|
psubA := mustCreatePubSub(ctx, t, hosts[0], "/esh/floodsub")
|
|
|
|
psubB := mustCreatePubSub(ctx, t, hosts[1], "/lsr/floodsub")
|
|
|
|
|
|
|
|
subA := mustSubscribe(t, psubA, "foobar")
|
|
|
|
defer subA.Cancel()
|
|
|
|
|
|
|
|
subB := mustSubscribe(t, psubB, "foobar")
|
|
|
|
defer subB.Cancel()
|
|
|
|
|
|
|
|
connect(t, hosts[1], hosts[0])
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
|
|
|
|
psubA.Publish("foobar", []byte("bar"))
|
|
|
|
|
|
|
|
assertReceive(t, subA, []byte("bar"))
|
|
|
|
|
|
|
|
pass := false
|
|
|
|
select {
|
|
|
|
case <-subB.ch:
|
|
|
|
t.Fatal("different protocols: should not have received message")
|
|
|
|
case <-time.After(time.Second * 1):
|
|
|
|
pass = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if !pass {
|
|
|
|
t.Fatal("should have timed out waiting for message")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func mustCreatePubSub(ctx context.Context, t *testing.T, h host.Host, ps ...protocol.ID) *PubSub {
|
2018-06-04 21:14:54 +00:00
|
|
|
psub, err := NewFloodsubWithProtocols(ctx, h, ps)
|
2018-06-01 22:50:24 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return psub
|
|
|
|
}
|
|
|
|
|
|
|
|
func mustSubscribe(t *testing.T, ps *PubSub, topic string) *Subscription {
|
|
|
|
sub, err := ps.Subscribe(topic)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return sub
|
|
|
|
}
|
|
|
|
|
2016-09-14 22:11:41 +00:00
|
|
|
func TestSubReporting(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
host := getNetHosts(t, ctx, 1)[0]
|
2017-12-16 12:12:23 +00:00
|
|
|
psub, err := NewFloodSub(ctx, host)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-09-14 22:11:41 +00:00
|
|
|
|
2016-11-11 23:47:53 +00:00
|
|
|
fooSub, err := psub.Subscribe("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-09-14 22:11:41 +00:00
|
|
|
|
2016-11-11 23:47:53 +00:00
|
|
|
barSub, err := psub.Subscribe("bar")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-09-14 22:11:41 +00:00
|
|
|
|
|
|
|
assertHasTopics(t, psub, "foo", "bar")
|
|
|
|
|
2016-11-11 23:47:53 +00:00
|
|
|
_, err = psub.Subscribe("baz")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-09-14 22:11:41 +00:00
|
|
|
|
|
|
|
assertHasTopics(t, psub, "foo", "bar", "baz")
|
|
|
|
|
2016-10-19 23:01:06 +00:00
|
|
|
barSub.Cancel()
|
2016-09-14 22:11:41 +00:00
|
|
|
assertHasTopics(t, psub, "foo", "baz")
|
2016-10-19 23:01:06 +00:00
|
|
|
fooSub.Cancel()
|
2016-09-14 22:11:41 +00:00
|
|
|
assertHasTopics(t, psub, "baz")
|
|
|
|
|
2016-11-11 23:47:53 +00:00
|
|
|
_, err = psub.Subscribe("fish")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-09-14 22:11:41 +00:00
|
|
|
|
|
|
|
assertHasTopics(t, psub, "baz", "fish")
|
|
|
|
}
|
2016-10-18 18:13:12 +00:00
|
|
|
|
|
|
|
func TestPeerTopicReporting(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
hosts := getNetHosts(t, ctx, 4)
|
|
|
|
psubs := getPubsubs(ctx, hosts)
|
|
|
|
|
|
|
|
connect(t, hosts[0], hosts[1])
|
|
|
|
connect(t, hosts[0], hosts[2])
|
|
|
|
connect(t, hosts[0], hosts[3])
|
|
|
|
|
2016-11-11 23:47:53 +00:00
|
|
|
_, err := psubs[1].Subscribe("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = psubs[1].Subscribe("bar")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = psubs[1].Subscribe("baz")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-10-18 18:13:12 +00:00
|
|
|
|
2016-11-11 23:47:53 +00:00
|
|
|
_, err = psubs[2].Subscribe("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = psubs[2].Subscribe("ipfs")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-10-18 18:13:12 +00:00
|
|
|
|
2016-11-11 23:47:53 +00:00
|
|
|
_, err = psubs[3].Subscribe("baz")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = psubs[3].Subscribe("ipfs")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-10-18 18:13:12 +00:00
|
|
|
|
2016-11-13 00:31:02 +00:00
|
|
|
time.Sleep(time.Millisecond * 10)
|
|
|
|
|
2016-10-18 18:13:12 +00:00
|
|
|
peers := psubs[0].ListPeers("ipfs")
|
|
|
|
assertPeerList(t, peers, hosts[2].ID(), hosts[3].ID())
|
|
|
|
|
|
|
|
peers = psubs[0].ListPeers("foo")
|
|
|
|
assertPeerList(t, peers, hosts[1].ID(), hosts[2].ID())
|
|
|
|
|
|
|
|
peers = psubs[0].ListPeers("baz")
|
|
|
|
assertPeerList(t, peers, hosts[1].ID(), hosts[3].ID())
|
|
|
|
|
|
|
|
peers = psubs[0].ListPeers("bar")
|
|
|
|
assertPeerList(t, peers, hosts[1].ID())
|
|
|
|
}
|
|
|
|
|
2016-11-11 15:22:47 +00:00
|
|
|
func TestSubscribeMultipleTimes(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
hosts := getNetHosts(t, ctx, 2)
|
|
|
|
psubs := getPubsubs(ctx, hosts)
|
|
|
|
|
|
|
|
connect(t, hosts[0], hosts[1])
|
|
|
|
|
2016-11-11 23:47:53 +00:00
|
|
|
sub1, err := psubs[0].Subscribe("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
sub2, err := psubs[0].Subscribe("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-11-11 15:22:47 +00:00
|
|
|
|
|
|
|
// make sure subscribing is finished by the time we publish
|
2018-01-16 09:46:33 +00:00
|
|
|
time.Sleep(10 * time.Millisecond)
|
2016-11-11 15:22:47 +00:00
|
|
|
|
|
|
|
psubs[1].Publish("foo", []byte("bar"))
|
|
|
|
|
2016-11-17 10:27:57 +00:00
|
|
|
msg, err := sub1.Next(ctx)
|
2016-11-11 15:22:47 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v.", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
data := string(msg.GetData())
|
|
|
|
|
|
|
|
if data != "bar" {
|
|
|
|
t.Fatalf("data is %s, expected %s.", data, "bar")
|
|
|
|
}
|
|
|
|
|
2016-11-17 10:27:57 +00:00
|
|
|
msg, err = sub2.Next(ctx)
|
2016-11-11 15:22:47 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v.", err)
|
|
|
|
}
|
|
|
|
data = string(msg.GetData())
|
|
|
|
|
|
|
|
if data != "bar" {
|
|
|
|
t.Fatalf("data is %s, expected %s.", data, "bar")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-14 16:32:31 +00:00
|
|
|
func TestPeerDisconnect(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
hosts := getNetHosts(t, ctx, 2)
|
|
|
|
psubs := getPubsubs(ctx, hosts)
|
|
|
|
|
|
|
|
connect(t, hosts[0], hosts[1])
|
|
|
|
|
|
|
|
_, err := psubs[0].Subscribe("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = psubs[1].Subscribe("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 10)
|
|
|
|
|
|
|
|
peers := psubs[0].ListPeers("foo")
|
|
|
|
assertPeerList(t, peers, hosts[1].ID())
|
|
|
|
for _, c := range hosts[1].Network().ConnsToPeer(hosts[0].ID()) {
|
2018-02-17 03:42:39 +00:00
|
|
|
for _, s := range c.GetStreams() {
|
2017-10-14 16:32:31 +00:00
|
|
|
s.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 10)
|
|
|
|
|
|
|
|
peers = psubs[0].ListPeers("foo")
|
|
|
|
assertPeerList(t, peers)
|
|
|
|
}
|
|
|
|
|
2016-10-18 18:13:12 +00:00
|
|
|
func assertPeerList(t *testing.T, peers []peer.ID, expected ...peer.ID) {
|
|
|
|
sort.Sort(peer.IDSlice(peers))
|
|
|
|
sort.Sort(peer.IDSlice(expected))
|
|
|
|
|
|
|
|
if len(peers) != len(expected) {
|
2016-10-20 11:46:52 +00:00
|
|
|
t.Fatalf("mismatch: %s != %s", peers, expected)
|
2016-10-18 18:13:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, p := range peers {
|
|
|
|
if expected[i] != p {
|
2016-10-20 11:46:52 +00:00
|
|
|
t.Fatalf("mismatch: %s != %s", peers, expected)
|
2016-10-18 18:13:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|