Skip to content

Commit

Permalink
add a test case
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Oct 25, 2023
1 parent a7c46cb commit cce6b5b
Showing 1 changed file with 35 additions and 19 deletions.
54 changes: 35 additions & 19 deletions p2p/net/swarm/swarm_dial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,16 +197,9 @@ func TestAddrResolution(t *testing.T) {
}

func TestAddrResolutionRecursive(t *testing.T) {
ctx := context.Background()
p1 := test.RandPeerIDFatal(t)
p2 := test.RandPeerIDFatal(t)

p1, err := test.RandPeerID()
if err != nil {
t.Error(err)
}
p2, err := test.RandPeerID()
if err != nil {
t.Error(err)
}
addr1 := ma.StringCast("/dnsaddr/example.com")
addr2 := ma.StringCast("/ip4/192.0.2.1/tcp/123")
p2paddr1 := ma.StringCast("/dnsaddr/example.com/p2p/" + p1.String())
Expand All @@ -221,25 +214,19 @@ func TestAddrResolutionRecursive(t *testing.T) {
"dnsaddr=" + p2paddr1i.String(),
"dnsaddr=" + p2paddr2i.String(),
},
"_dnsaddr.foo.example.com": {
"dnsaddr=" + p2paddr1f.String(),
},
"_dnsaddr.bar.example.com": {
"dnsaddr=" + p2paddr2i.String(),
},
"_dnsaddr.foo.example.com": {"dnsaddr=" + p2paddr1f.String()},
"_dnsaddr.bar.example.com": {"dnsaddr=" + p2paddr2i.String()},
},
}
resolver, err := madns.NewResolver(madns.WithDefaultResolver(backend))
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

s := newTestSwarmWithResolver(t, resolver)

pi1, err := peer.AddrInfoFromP2pAddr(p2paddr1)
require.NoError(t, err)

tctx, cancel := context.WithTimeout(ctx, time.Millisecond*100)
tctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*100)
defer cancel()
s.Peerstore().AddAddrs(pi1.ID, pi1.Addrs, peerstore.TempAddrTTL)
_, _, err = s.addrsForDial(tctx, p1)
Expand All @@ -263,6 +250,35 @@ func TestAddrResolutionRecursive(t *testing.T) {
require.Contains(t, addrs2, addr1)
}

// see https://github.com/libp2p/go-libp2p/issues/2562
func TestAddrResolutionRecursiveTransportSpecific(t *testing.T) {
p := test.RandPeerIDFatal(t)

backend := &madns.MockResolver{
IP: map[string][]net.IPAddr{
"sub.example.com": {net.IPAddr{IP: net.IPv4(1, 2, 3, 4)}},
},
TXT: map[string][]string{
"_dnsaddr.example.com": {"dnsaddr=/dns4/sub.example.com/tcp/443/wss/p2p/" + p.String()},
"_dnsaddr.am6.bootstrap.libp2p.io": {"dnsaddr=/ip4/1.2.3.4/tcp/443/wss"},
},
}
resolver, err := madns.NewResolver(madns.WithDefaultResolver(backend))
require.NoError(t, err)

s := newTestSwarmWithResolver(t, resolver)
pi1, err := peer.AddrInfoFromP2pAddr(ma.StringCast("/dnsaddr/example.com/p2p/" + p.String()))
require.NoError(t, err)

tctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*100)
defer cancel()
s.Peerstore().AddAddrs(pi1.ID, pi1.Addrs, peerstore.TempAddrTTL)
addrs, _, err := s.addrsForDial(tctx, p)
require.NoError(t, err)
require.Len(t, addrs, 1)
require.Equal(t, addrs[0].String(), "/ip4/1.2.3.4/tcp/443/tls/sni/sub.example.com/ws")
}

func TestAddrsForDialFiltering(t *testing.T) {
q1 := ma.StringCast("/ip4/1.2.3.4/udp/1/quic-v1")
q1v1 := ma.StringCast("/ip4/1.2.3.4/udp/1/quic-v1")
Expand Down

0 comments on commit cce6b5b

Please sign in to comment.