Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
40 lines
1.1 KiB
Go
40 lines
1.1 KiB
Go
package p2p
|
|
|
|
import (
|
|
"slices"
|
|
"testing"
|
|
)
|
|
|
|
const (
|
|
idA = "12D3KooWQsiJgoN2o9KzvSFdvB5RZPursnQUqSD3v5i9sXrZZciw"
|
|
idB = "12D3KooWPA8Gigzxf2cBVkBKytSh8HUPpcufsFdVmiin8RQ8tFnx"
|
|
)
|
|
|
|
func TestOnlyAddressesFromTheInternetAreShared(t *testing.T) {
|
|
got := shareable([]string{
|
|
"/ip4/127.0.0.1/tcp/4001/p2p/" + idB,
|
|
"/ip4/192.168.1.20/tcp/4001/p2p/" + idB,
|
|
"/ip6/::1/tcp/4001/p2p/" + idB,
|
|
"/ip4/10.1.2.3/udp/4001/quic-v1/p2p/" + idB,
|
|
"/ip4/1.1.1.1/tcp/4001/p2p/" + idA + "/p2p-circuit/p2p/" + idB, // through a relay
|
|
"/ip4/8.8.8.8/tcp/4001/p2p/" + idB, // direct
|
|
"/dns4/node.example.org/tcp/4001/p2p/" + idB,
|
|
"not an address",
|
|
})
|
|
want := []string{
|
|
"/dns4/node.example.org/tcp/4001/p2p/" + idB,
|
|
"/ip4/8.8.8.8/tcp/4001/p2p/" + idB,
|
|
"/ip4/1.1.1.1/tcp/4001/p2p/" + idA + "/p2p-circuit/p2p/" + idB, // relayed last
|
|
}
|
|
if !slices.Equal(got, want) {
|
|
t.Fatalf("got\n %v\nwant\n %v", got, want)
|
|
}
|
|
}
|
|
|
|
func TestANodeWithOnlyLocalAddressesSharesNothing(t *testing.T) {
|
|
a := startNode(t, nil, "u1x") // listens on 127.0.0.1 only
|
|
if got := a.ShareAddrs(); len(got) != 0 {
|
|
t.Fatalf("shared %v", got)
|
|
}
|
|
}
|