Commit a8e25bf2 authored by Steven Allen's avatar Steven Allen
Browse files

mocknet: refuse to connect to self

The swarm does this as well and most of our services will fail if we don't have
this.
No related merge requests found
Showing with 17 additions and 15 deletions
+17 -15
......@@ -44,14 +44,9 @@ func FullMeshConnected(ctx context.Context, n int) (Mocknet, error) {
return nil, err
}
nets := m.Nets()
for _, n1 := range nets {
for _, n2 := range nets {
if _, err := m.ConnectNets(n1, n2); err != nil {
return nil, err
}
}
err = m.ConnectAllButSelf()
if err != nil {
return nil, err
}
return m, nil
}
......@@ -118,6 +118,10 @@ func (pn *peernet) DialPeer(ctx context.Context, p peer.ID) (inet.Conn, error) {
}
func (pn *peernet) connect(p peer.ID) (*conn, error) {
if p == pn.peer {
return nil, fmt.Errorf("attempted to dial self %s", p)
}
// first, check if we already have live connections
pn.RLock()
cs, found := pn.connsByPeer[p]
......
......@@ -225,14 +225,14 @@ func TestNetworkSetup(t *testing.T) {
t.Error("should not be able to connect")
}
// connect p1->p1 (should work)
if _, err := n1.DialPeer(ctx, p1); err != nil {
t.Error("p1 should be able to dial self.", err)
// connect p1->p1 (should fail)
if _, err := n1.DialPeer(ctx, p1); err == nil {
t.Error("p1 shouldn't be able to dial self")
}
// and a stream too
if _, err := n1.NewStream(ctx, p1); err != nil {
t.Error(err)
if _, err := n1.NewStream(ctx, p1); err == nil {
t.Error("p1 shouldn't be able to dial self")
}
// connect p1->p2
......@@ -383,8 +383,11 @@ func TestStreamsStress(t *testing.T) {
wg.Add(1)
go func(i int) {
defer wg.Done()
from := rand.Intn(len(hosts))
to := rand.Intn(len(hosts))
var from, to int
for from == to {
from = rand.Intn(len(hosts))
to = rand.Intn(len(hosts))
}
s, err := hosts[from].NewStream(ctx, hosts[to].ID(), protocol.TestingID)
if err != nil {
log.Debugf("%d (%s) %d (%s)", from, hosts[from], to, hosts[to])
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment