2023-07-07 06:07:10 +00:00
|
|
|
package blossomsub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMapBlacklist(t *testing.T) {
|
|
|
|
b := NewMapBlacklist()
|
|
|
|
|
|
|
|
p := peer.ID("test")
|
|
|
|
|
|
|
|
b.Add(p)
|
|
|
|
if !b.Contains(p) {
|
|
|
|
t.Fatal("peer not in the blacklist")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTimeCachedBlacklist(t *testing.T) {
|
|
|
|
b, err := NewTimeCachedBlacklist(10 * time.Minute)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
p := peer.ID("test")
|
|
|
|
|
|
|
|
b.Add(p)
|
|
|
|
if !b.Contains(p) {
|
|
|
|
t.Fatal("peer not in the blacklist")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBlacklist(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2024-10-12 18:55:17 +00:00
|
|
|
hosts := getDefaultHosts(t, 2)
|
|
|
|
psubs := getBlossomSubs(ctx, hosts)
|
2023-07-07 06:07:10 +00:00
|
|
|
connect(t, hosts[0], hosts[1])
|
|
|
|
|
2024-10-12 18:55:17 +00:00
|
|
|
bitmasks, err := psubs[0].Join([]byte{0x00, 0x80, 0x00, 0x00})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sub, err := psubs[1].Subscribe([]byte{0x00, 0x80, 0x00, 0x00})
|
2023-07-07 06:07:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
psubs[1].BlacklistPeer(hosts[0].ID())
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
|
2024-10-12 18:55:17 +00:00
|
|
|
bitmasks[0].Publish(ctx, bitmasks[0].bitmask, []byte("message"))
|
2023-07-07 06:07:10 +00:00
|
|
|
|
|
|
|
wctx, cancel := context.WithTimeout(ctx, 1*time.Second)
|
|
|
|
defer cancel()
|
2024-10-12 18:55:17 +00:00
|
|
|
_, err = sub[0].Next(wctx)
|
2023-07-07 06:07:10 +00:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("got message from blacklisted peer")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBlacklist2(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2024-10-12 18:55:17 +00:00
|
|
|
hosts := getDefaultHosts(t, 2)
|
|
|
|
psubs := getBlossomSubs(ctx, hosts)
|
2023-07-07 06:07:10 +00:00
|
|
|
connect(t, hosts[0], hosts[1])
|
|
|
|
|
2024-10-12 18:55:17 +00:00
|
|
|
bitmasks, err := psubs[0].Join([]byte{0x00, 0x80, 0x00, 0x00})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = psubs[0].Subscribe([]byte{0x00, 0x80, 0x00, 0x00})
|
2023-07-07 06:07:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-10-12 18:55:17 +00:00
|
|
|
sub1, err := psubs[1].Subscribe([]byte{0x00, 0x80, 0x00, 0x00})
|
2023-07-07 06:07:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
psubs[1].BlacklistPeer(hosts[0].ID())
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
|
2024-10-12 18:55:17 +00:00
|
|
|
bitmasks[0].Publish(ctx, bitmasks[0].bitmask, []byte("message"))
|
2023-07-07 06:07:10 +00:00
|
|
|
|
|
|
|
wctx, cancel := context.WithTimeout(ctx, 1*time.Second)
|
|
|
|
defer cancel()
|
2024-10-12 18:55:17 +00:00
|
|
|
_, err = sub1[0].Next(wctx)
|
2023-07-07 06:07:10 +00:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("got message from blacklisted peer")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBlacklist3(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2024-10-12 18:55:17 +00:00
|
|
|
hosts := getDefaultHosts(t, 2)
|
|
|
|
psubs := getBlossomSubs(ctx, hosts)
|
2023-07-07 06:07:10 +00:00
|
|
|
|
|
|
|
psubs[1].BlacklistPeer(hosts[0].ID())
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
connect(t, hosts[0], hosts[1])
|
|
|
|
|
2024-10-12 18:55:17 +00:00
|
|
|
bitmasks, err := psubs[0].Join([]byte{0x00, 0x80, 0x00, 0x00})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sub, err := psubs[1].Subscribe([]byte{0x00, 0x80, 0x00, 0x00})
|
2023-07-07 06:07:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
|
2024-10-12 18:55:17 +00:00
|
|
|
bitmasks[0].Publish(ctx, bitmasks[0].bitmask, []byte("message"))
|
2023-07-07 06:07:10 +00:00
|
|
|
|
|
|
|
wctx, cancel := context.WithTimeout(ctx, 1*time.Second)
|
|
|
|
defer cancel()
|
2024-10-12 18:55:17 +00:00
|
|
|
_, err = sub[0].Next(wctx)
|
2023-07-07 06:07:10 +00:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("got message from blacklisted peer")
|
|
|
|
}
|
|
|
|
}
|