Merge pull request #8 from gfanton/feat/bump-libp2p-23

This commit is contained in:
Guilhem Fanton 2022-11-15 10:39:09 +01:00 committed by GitHub
commit 796f4b6cdb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 415 additions and 975 deletions

62
.github/workflows/go.yml vendored Normal file
View File

@ -0,0 +1,62 @@
name: Go
on:
push:
tags:
- v*
branches:
- master
pull_request:
jobs:
golangci-lint:
name: "GolangCI-lint"
runs-on: ubuntu-latest
strategy:
matrix:
golang:
- 1.18.x
steps:
- uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.golang }}
- name: golangci-lint
uses: golangci/golangci-lint-action@v3.3.0
with:
go-version: ${{ matrix.golang }}
version: v1.50.1
args: --timeout=10m
go-tests-on-linux:
runs-on: ubuntu-latest
strategy:
matrix:
golang: ['1.18', '1.19']
os: ['ubuntu-latest', 'macos-latest']
env:
OS: ${{ matrix.os }}
GOLANG: ${{ matrix.golang }}
steps:
- name: Checkout
uses: actions/checkout@master
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.golang }}
- name: Cache Go modules
uses: actions/cache@v1
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{matrix.golang}}-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-${{matrix.golang}}-
- name: Download modules
run: go mod download
- name: Check go.mod and go.sum
run: |
go mod tidy -v
git --no-pager diff go.mod go.sum
git --no-pager diff --quiet go.mod go.sum
- name: Run tests with race
run: go test -v -tags "libsqlite3" -race ./... -test.timeout=10m

32
.golangci.yml Normal file
View File

@ -0,0 +1,32 @@
run:
deadline: 1m
tests: false
skip-files:
- "test/.*"
- "test/.*/.*"
linters-settings:
golint:
min-confidence: 0
maligned:
suggest-new: true
goconst:
min-len: 5
min-occurrences: 4
misspell:
locale: US
linters:
disable-all: true
enable:
- goconst
- misspell
- unused
- staticcheck
- unconvert
- gofmt
- goimports
# @TODO(gfanton): disable revive for now has it generate to many errors,
# it should be enable in a dedicated PR
# - revive
- ineffassign

View File

@ -7,9 +7,9 @@ import (
"time"
ggio "github.com/gogo/protobuf/io"
"github.com/libp2p/go-libp2p-core/host"
inet "github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p/core/host"
inet "github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
pb "github.com/libp2p/go-libp2p-rendezvous/pb"
)

View File

@ -2,12 +2,14 @@ package rendezvous
import (
"context"
"github.com/libp2p/go-libp2p-core/peer"
"testing"
"time"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p/core/peer"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
"github.com/libp2p/go-libp2p-rendezvous/test_utils"
"github.com/libp2p/go-libp2p/core/host"
)
func getRendezvousClients(t *testing.T, hosts []host.Host) []RendezvousClient {
@ -26,7 +28,10 @@ func TestClientRegistrationAndDiscovery(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
hosts := getRendezvousHosts(t, ctx, 5)
m := mocknet.New()
defer m.Close()
hosts := getRendezvousHosts(t, ctx, m, 5)
svc, err := makeRendezvousService(ctx, hosts[0], ":memory:")
if err != nil {
@ -88,10 +93,15 @@ func TestClientRegistrationAndDiscovery(t *testing.T) {
}
func TestClientRegistrationAndDiscoveryAsync(t *testing.T) {
DiscoverAsyncInterval = 1 * time.Second
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
hosts := getRendezvousHosts(t, ctx, 5)
m := mocknet.New()
defer m.Close()
hosts := getRendezvousHosts(t, ctx, m, 5)
svc, err := makeRendezvousService(ctx, hosts[0], ":memory:")
if err != nil {
@ -101,8 +111,6 @@ func TestClientRegistrationAndDiscoveryAsync(t *testing.T) {
clients := getRendezvousClients(t, hosts)
DiscoverAsyncInterval = 1 * time.Second
ch, err := clients[0].DiscoverAsync(ctx, "foo1")
if err != nil {
t.Fatal(err)
@ -120,6 +128,4 @@ func TestClientRegistrationAndDiscoveryAsync(t *testing.T) {
pi := <-ch
checkPeerInfo(t, pi, hosts[1+i])
}
DiscoverAsyncInterval = 2 * time.Minute
}

View File

@ -1,7 +1,7 @@
package dbi
import (
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p/core/peer"
)
type RegistrationRecord struct {

View File

@ -16,7 +16,7 @@ import (
_ "github.com/mutecomm/go-sqlcipher/v4"
logging "github.com/ipfs/go-log/v2"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p/core/peer"
)
var log = logging.Logger("rendezvous/db")

View File

@ -7,7 +7,7 @@ import (
"testing"
"time"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p/core/peer"
ma "github.com/multiformats/go-multiaddr"
)

View File

@ -16,7 +16,7 @@ import (
_ "github.com/mattn/go-sqlite3"
logging "github.com/ipfs/go-log/v2"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p/core/peer"
)
var log = logging.Logger("rendezvous/db")

View File

@ -7,7 +7,7 @@ import (
"testing"
"time"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p/core/peer"
ma "github.com/multiformats/go-multiaddr"
)

View File

@ -7,9 +7,9 @@ import (
"sync"
"time"
"github.com/libp2p/go-libp2p-core/discovery"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p/core/discovery"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
)
type rendezvousDiscovery struct {

View File

@ -6,9 +6,10 @@ import (
"testing"
"time"
"github.com/libp2p/go-libp2p-core/discovery"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p/core/discovery"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
)
func getRendezvousDiscovery(hosts []host.Host) []discovery.Discovery {
@ -49,13 +50,16 @@ func TestDiscoveryClientAdvertiseAndFindPeers(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
m := mocknet.New()
defer m.Close()
// Define parameters
const namespace = "foo1"
const numClients = 4
const ttl = DefaultTTL * time.Second
// Instantiate server and clients
hosts := getRendezvousHosts(t, ctx, numClients+1)
hosts := getRendezvousHosts(t, ctx, m, numClients+1)
svc, err := makeRendezvousService(ctx, hosts[0], ":memory:")
if err != nil {
@ -106,8 +110,11 @@ func BaseDiscoveryClientCacheExpirationTest(t *testing.T, onlyRequestFromCache b
const longTTL = DefaultTTL * time.Second
const shortTTL = 2 * time.Second
m := mocknet.New()
defer m.Close()
// Instantiate server and clients
hosts := getRendezvousHosts(t, ctx, numBaseRegs+3)
hosts := getRendezvousHosts(t, ctx, m, numBaseRegs+3)
svc, err := makeRendezvousService(ctx, hosts[0], ":memory:")
if err != nil {

62
go.mod
View File

@ -1,16 +1,62 @@
module github.com/libp2p/go-libp2p-rendezvous
go 1.15
go 1.18
require (
github.com/gogo/protobuf v1.3.2
github.com/golang/protobuf v1.5.2
github.com/google/uuid v1.1.1
github.com/ipfs/go-log/v2 v2.1.3
github.com/libp2p/go-libp2p-blankhost v0.2.0
github.com/libp2p/go-libp2p-core v0.10.0
github.com/libp2p/go-libp2p-swarm v0.6.0
github.com/mattn/go-sqlite3 v1.14.4
github.com/multiformats/go-multiaddr v0.4.0
github.com/google/uuid v1.3.0
github.com/ipfs/go-log/v2 v2.5.1
github.com/libp2p/go-libp2p v0.23.3
github.com/mattn/go-sqlite3 v1.14.16
github.com/multiformats/go-multiaddr v0.7.0
github.com/mutecomm/go-sqlcipher/v4 v4.4.2
github.com/stretchr/testify v1.8.0
)
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect
github.com/google/gopacket v1.1.19 // indirect
github.com/huin/goupnp v1.0.3 // indirect
github.com/ipfs/go-cid v0.3.2 // indirect
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
github.com/klauspost/cpuid/v2 v2.1.1 // indirect
github.com/koron/go-ssdp v0.0.3 // indirect
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/libp2p/go-cidranger v1.1.0 // indirect
github.com/libp2p/go-libp2p-asn-util v0.2.0 // indirect
github.com/libp2p/go-msgio v0.2.0 // indirect
github.com/libp2p/go-nat v0.1.0 // indirect
github.com/libp2p/go-netroute v0.2.0 // indirect
github.com/libp2p/go-openssl v0.1.0 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mattn/go-pointer v0.0.1 // indirect
github.com/miekg/dns v1.1.50 // indirect
github.com/minio/sha256-simd v1.0.0 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/multiformats/go-base32 v0.1.0 // indirect
github.com/multiformats/go-base36 v0.1.0 // indirect
github.com/multiformats/go-multiaddr-dns v0.3.1 // indirect
github.com/multiformats/go-multiaddr-fmt v0.1.0 // indirect
github.com/multiformats/go-multibase v0.1.1 // indirect
github.com/multiformats/go-multicodec v0.6.0 // indirect
github.com/multiformats/go-multihash v0.2.1 // indirect
github.com/multiformats/go-multistream v0.3.3 // indirect
github.com/multiformats/go-varint v0.0.6 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
go.uber.org/zap v1.23.0 // indirect
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/net v0.0.0-20220920183852-bf014ff85ad5 // indirect
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab // indirect
golang.org/x/tools v0.1.12 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/blake3 v1.1.7 // indirect
)

917
go.sum generated

File diff suppressed because it is too large Load Diff

3
pb/rendezvous.pb.go generated
View File

@ -5,10 +5,11 @@ package rendezvous_pb
import (
fmt "fmt"
proto "github.com/golang/protobuf/proto"
io "io"
math "math"
math_bits "math/bits"
proto "github.com/golang/protobuf/proto"
)
// Reference imports to suppress errors if they are not otherwise used.

View File

@ -8,8 +8,8 @@ import (
pb "github.com/libp2p/go-libp2p-rendezvous/pb"
logging "github.com/ipfs/go-log/v2"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/protocol"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
ma "github.com/multiformats/go-multiaddr"
)
@ -27,7 +27,7 @@ type RendezvousError struct {
}
func (e RendezvousError) Error() string {
return fmt.Sprintf("Rendezvous error: %s (%s)", e.Text, pb.Message_ResponseStatus(e.Status).String())
return fmt.Sprintf("Rendezvous error: %s (%s)", e.Text, e.Status.String())
}
func NewRegisterMessage(ns string, pi peer.AddrInfo, ttl int) *pb.Message {

6
svc.go
View File

@ -4,9 +4,9 @@ import (
"fmt"
ggio "github.com/gogo/protobuf/io"
"github.com/libp2p/go-libp2p-core/host"
inet "github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p/core/host"
inet "github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
db "github.com/libp2p/go-libp2p-rendezvous/db"
pb "github.com/libp2p/go-libp2p-rendezvous/pb"

View File

@ -8,17 +8,19 @@ import (
"time"
ggio "github.com/gogo/protobuf/io"
"github.com/libp2p/go-libp2p-core/host"
inet "github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p/core/host"
inet "github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
"github.com/stretchr/testify/require"
db "github.com/libp2p/go-libp2p-rendezvous/db/sqlite"
pb "github.com/libp2p/go-libp2p-rendezvous/pb"
"github.com/libp2p/go-libp2p-rendezvous/test_utils"
)
func getRendezvousHosts(t *testing.T, ctx context.Context, n int) []host.Host {
return test_utils.GetRendezvousHosts(t, ctx, n)
func getRendezvousHosts(t *testing.T, ctx context.Context, m mocknet.Mocknet, n int) []host.Host {
return test_utils.GetRendezvousHosts(t, ctx, m, n)
}
func makeRendezvousService(ctx context.Context, host host.Host, path string) (*RendezvousService, error) {
@ -42,61 +44,42 @@ func TestSVCRegistrationAndDiscovery(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
hosts := getRendezvousHosts(t, ctx, 5)
m := mocknet.New()
defer m.Close()
hosts := getRendezvousHosts(t, ctx, m, 5)
svc, err := makeRendezvousService(ctx, hosts[0], ":memory:")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
defer svc.DB.Close()
clients := getRendezvousPointsTest(t, hosts)
const registerTTL = 60
recordTTL, err := clients[0].Register(ctx, "foo1", registerTTL)
if err != nil {
t.Fatal(err)
}
if recordTTL != registerTTL*time.Second {
t.Fatalf("Expected record TTL to be %d seconds", DefaultTTL)
}
require.NoError(t, err)
require.Equalf(t, registerTTL*time.Second, recordTTL, "expected record TTL to be %d seconds", DefaultTTL)
rrs, cookie, err := clients[0].Discover(ctx, "foo1", 10, nil)
if err != nil {
t.Fatal(err)
}
if len(rrs) != 1 {
t.Fatal("Expected 1 registration")
}
require.NoError(t, err)
require.Len(t, rrs, 1)
checkHostRegistration(t, rrs[0], hosts[1])
for i, client := range clients[1:] {
recordTTL, err = client.Register(ctx, "foo1", registerTTL)
if err != nil {
t.Fatal(err)
}
if recordTTL != registerTTL*time.Second {
t.Fatalf("Expected record TTL to be %d seconds", DefaultTTL)
}
require.NoError(t, err)
require.Equalf(t, registerTTL*time.Second, recordTTL, "expected record TTL to be %d seconds", DefaultTTL)
rrs, cookie, err = clients[0].Discover(ctx, "foo1", 10, cookie)
if err != nil {
t.Fatal(err)
}
if len(rrs) != 1 {
t.Fatal("Expected 1 registration")
}
require.NoError(t, err)
require.Len(t, rrs, 1)
checkHostRegistration(t, rrs[0], hosts[2+i])
}
for _, client := range clients[1:] {
rrs, _, err = client.Discover(ctx, "foo1", 10, nil)
if err != nil {
t.Fatal(err)
}
if len(rrs) != 4 {
t.Fatal("Expected 4 registrations")
}
require.NoError(t, err)
require.Len(t, rrs, 4)
for j, rr := range rrs {
checkHostRegistration(t, rr, hosts[1+j])
@ -104,18 +87,12 @@ func TestSVCRegistrationAndDiscovery(t *testing.T) {
}
err = clients[0].Unregister(ctx, "foo1")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
for _, client := range clients[0:] {
rrs, _, err = client.Discover(ctx, "foo1", 10, nil)
if err != nil {
t.Fatal(err)
}
if len(rrs) != 3 {
t.Fatalf("Expected 3 registrations, got %d", len(rrs))
}
require.NoError(t, err)
require.Lenf(t, rrs, 3, "Expected 3 registrations, got %d", len(rrs))
for j, rr := range rrs {
checkHostRegistration(t, rr, hosts[2+j])
@ -123,18 +100,12 @@ func TestSVCRegistrationAndDiscovery(t *testing.T) {
}
err = clients[1].Unregister(ctx, "")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
for _, client := range clients[0:] {
rrs, _, err = client.Discover(ctx, "foo1", 10, nil)
if err != nil {
t.Fatal(err)
}
if len(rrs) != 2 {
t.Fatal("Expected 2 registrations")
}
require.NoError(t, err)
require.Len(t, rrs, 2)
for j, rr := range rrs {
checkHostRegistration(t, rr, hosts[3+j])
@ -148,14 +119,11 @@ func checkHostRegistration(t *testing.T, rr Registration, host host.Host) {
}
addrs := host.Addrs()
raddrs := rr.Peer.Addrs
if len(addrs) != len(raddrs) {
t.Fatal("bad registration: peer address length mismatch")
}
require.Equal(t, len(addrs), len(raddrs), "bad registration: peer address length mismatch")
for i, addr := range addrs {
raddr := raddrs[i]
if !addr.Equal(raddr) {
t.Fatal("bad registration: peer address mismatch")
}
require.True(t, addr.Equal(raddr), "bad registration: peer address mismatch")
}
}
@ -163,84 +131,55 @@ func TestSVCErrors(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
hosts := getRendezvousHosts(t, ctx, 2)
m := mocknet.New()
defer m.Close()
hosts := getRendezvousHosts(t, ctx, m, 2)
svc, err := makeRendezvousService(ctx, hosts[0], ":memory:")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
defer svc.DB.Close()
// testable registration errors
res, err := doTestRequest(ctx, hosts[1], hosts[0].ID(),
newRegisterMessage("", peer.AddrInfo{}, 0))
if err != nil {
t.Fatal(err)
}
if res.GetRegisterResponse().GetStatus() != pb.Message_E_INVALID_NAMESPACE {
t.Fatal("expected E_INVALID_NAMESPACE")
}
require.NoError(t, err)
require.Equal(t, pb.Message_E_INVALID_NAMESPACE, res.GetRegisterResponse().GetStatus())
badns := make([]byte, 2*MaxNamespaceLength)
rand.Read(badns)
res, err = doTestRequest(ctx, hosts[1], hosts[0].ID(),
newRegisterMessage(string(badns), peer.AddrInfo{}, 0))
if err != nil {
t.Fatal(err)
}
if res.GetRegisterResponse().GetStatus() != pb.Message_E_INVALID_NAMESPACE {
t.Fatal("expected E_INVALID_NAMESPACE")
}
require.NoError(t, err)
require.Equal(t, pb.Message_E_INVALID_NAMESPACE, res.GetRegisterResponse().GetStatus())
res, err = doTestRequest(ctx, hosts[1], hosts[0].ID(),
newRegisterMessage("foo", peer.AddrInfo{}, 0))
if err != nil {
t.Fatal(err)
}
if res.GetRegisterResponse().GetStatus() != pb.Message_E_INVALID_PEER_INFO {
t.Fatal("expected E_INVALID_PEER_INFO")
}
require.NoError(t, err)
require.Equal(t, pb.Message_E_INVALID_PEER_INFO, res.GetRegisterResponse().GetStatus())
res, err = doTestRequest(ctx, hosts[1], hosts[0].ID(),
newRegisterMessage("foo", peer.AddrInfo{ID: peer.ID("blah")}, 0))
if err != nil {
t.Fatal(err)
}
if res.GetRegisterResponse().GetStatus() != pb.Message_E_INVALID_PEER_INFO {
t.Fatal("expected E_INVALID_PEER_INFO")
}
require.NoError(t, err)
require.Equal(t, pb.Message_E_INVALID_PEER_INFO, res.GetRegisterResponse().GetStatus())
p, err := peer.Decode("QmVr26fY1tKyspEJBniVhqxQeEjhF78XerGiqWAwraVLQH")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
res, err = doTestRequest(ctx, hosts[1], hosts[0].ID(),
newRegisterMessage("foo", peer.AddrInfo{ID: p}, 0))
if err != nil {
t.Fatal(err)
}
if res.GetRegisterResponse().GetStatus() != pb.Message_E_INVALID_PEER_INFO {
t.Fatal("expected E_INVALID_PEER_INFO")
}
require.NoError(t, err)
require.Equal(t, pb.Message_E_INVALID_PEER_INFO, res.GetRegisterResponse().GetStatus())
res, err = doTestRequest(ctx, hosts[1], hosts[0].ID(),
newRegisterMessage("foo", peer.AddrInfo{ID: hosts[1].ID()}, 0))
if err != nil {
t.Fatal(err)
}
if res.GetRegisterResponse().GetStatus() != pb.Message_E_INVALID_PEER_INFO {
t.Fatal("expected E_INVALID_PEER_INFO")
}
require.NoError(t, err)
require.Equal(t, pb.Message_E_INVALID_PEER_INFO, res.GetRegisterResponse().GetStatus())
res, err = doTestRequest(ctx, hosts[1], hosts[0].ID(),
newRegisterMessage("foo", peer.AddrInfo{ID: hosts[1].ID(), Addrs: hosts[1].Addrs()}, 2*MaxTTL))
if err != nil {
t.Fatal(err)
}
if res.GetRegisterResponse().GetStatus() != pb.Message_E_INVALID_TTL {
t.Fatal("expected E_INVALID_TTL")
}
require.NoError(t, err)
require.Equal(t, pb.Message_E_INVALID_TTL, res.GetRegisterResponse().GetStatus())
// do MaxRegistrations
for i := 0; i < MaxRegistrations+1; i++ {
@ -257,45 +196,28 @@ func TestSVCErrors(t *testing.T) {
// and now fail
res, err = doTestRequest(ctx, hosts[1], hosts[0].ID(),
newRegisterMessage("foo", peer.AddrInfo{ID: hosts[1].ID(), Addrs: hosts[1].Addrs()}, 0))
if err != nil {
t.Fatal(err)
}
if res.GetRegisterResponse().GetStatus() != pb.Message_E_NOT_AUTHORIZED {
t.Fatal("expected E_NOT_AUTHORIZED")
}
require.NoError(t, err)
require.Equal(t, pb.Message_E_NOT_AUTHORIZED, res.GetRegisterResponse().GetStatus())
// testable discovery errors
res, err = doTestRequest(ctx, hosts[1], hosts[0].ID(),
newDiscoverMessage(string(badns), 0, nil))
if err != nil {
t.Fatal(err)
}
if res.GetDiscoverResponse().GetStatus() != pb.Message_E_INVALID_NAMESPACE {
t.Fatal("expected E_INVALID_NAMESPACE")
}
require.NoError(t, err)
require.Equal(t, pb.Message_E_INVALID_NAMESPACE, res.GetDiscoverResponse().GetStatus())
badcookie := make([]byte, 10)
rand.Read(badcookie)
res, err = doTestRequest(ctx, hosts[1], hosts[0].ID(),
newDiscoverMessage("foo", 0, badcookie))
if err != nil {
t.Fatal(err)
}
if res.GetDiscoverResponse().GetStatus() != pb.Message_E_INVALID_COOKIE {
t.Fatal("expected E_INVALID_COOKIE")
}
require.NoError(t, err)
require.Equal(t, pb.Message_E_INVALID_COOKIE, res.GetDiscoverResponse().GetStatus())
badcookie = make([]byte, 40)
rand.Read(badcookie)
res, err = doTestRequest(ctx, hosts[1], hosts[0].ID(),
newDiscoverMessage("foo", 0, badcookie))
if err != nil {
t.Fatal(err)
}
if res.GetDiscoverResponse().GetStatus() != pb.Message_E_INVALID_COOKIE {
t.Fatal("expected E_INVALID_COOKIE")
}
require.NoError(t, err)
require.Equal(t, pb.Message_E_INVALID_COOKIE, res.GetDiscoverResponse().GetStatus())
}
func doTestRequest(ctx context.Context, host host.Host, rp peer.ID, m *pb.Message) (*pb.Message, error) {

View File

@ -2,7 +2,8 @@ package rendezvous
import (
"context"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p/core/peer"
)
type RendezvousSync interface {

View File

@ -8,9 +8,9 @@ import (
ggio "github.com/gogo/protobuf/io"
"github.com/google/uuid"
"github.com/libp2p/go-libp2p-core/host"
inet "github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p/core/host"
inet "github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
pb "github.com/libp2p/go-libp2p-rendezvous/pb"

View File

@ -7,11 +7,11 @@ import (
"time"
ggio "github.com/gogo/protobuf/io"
"github.com/libp2p/go-libp2p-core/host"
inet "github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/protocol"
pb "github.com/libp2p/go-libp2p-rendezvous/pb"
"github.com/libp2p/go-libp2p/core/host"
inet "github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
)
const (

View File

@ -7,10 +7,11 @@ import (
"testing"
"time"
"github.com/libp2p/go-libp2p-core/host"
rendezvous "github.com/libp2p/go-libp2p-rendezvous"
db "github.com/libp2p/go-libp2p-rendezvous/db/sqlite"
"github.com/libp2p/go-libp2p-rendezvous/test_utils"
"github.com/libp2p/go-libp2p/core/host"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
)
func makeRendezvousService(ctx context.Context, host host.Host, path string, rzs ...rendezvous.RendezvousSync) (*rendezvous.RendezvousService, error) {
@ -37,8 +38,11 @@ func TestFlow(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
m := mocknet.New()
defer m.Close()
// Instantiate server and clients
hosts := test_utils.GetRendezvousHosts(t, ctx, 4)
hosts := test_utils.GetRendezvousHosts(t, ctx, m, 4)
inmemPubSubSync, err := rendezvous.NewSyncInMemProvider(hosts[0])
if err != nil {

View File

@ -4,29 +4,31 @@ import (
"context"
"testing"
bhost "github.com/libp2p/go-libp2p-blankhost"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
testutil "github.com/libp2p/go-libp2p-swarm/testing"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
"github.com/stretchr/testify/require"
)
func GetRendezvousHosts(t *testing.T, ctx context.Context, n int) []host.Host {
hosts := GetNetHosts(t, ctx, n)
func GetRendezvousHosts(t *testing.T, ctx context.Context, m mocknet.Mocknet, n int) []host.Host {
hosts := GetNetHosts(t, ctx, m, n)
for i := 1; i < len(hosts); i++ {
Connect(t, hosts[0], hosts[i])
}
return hosts
}
func GetNetHosts(t *testing.T, ctx context.Context, n int) []host.Host {
func GetNetHosts(t *testing.T, ctx context.Context, m mocknet.Mocknet, n int) []host.Host {
var out []host.Host
for i := 0; i < n; i++ {
netw := testutil.GenSwarm(t)
h := bhost.NewBlankHost(netw)
h, err := m.GenPeer()
require.NoError(t, err)
out = append(out, h)
}
err := m.LinkAll()
require.NoError(t, err)
return out
}