github.com/turingchain2020/turingchain@v1.1.21/system/p2p/dht/extension/relayAddr_test.go (about) 1 package extension 2 3 import ( 4 "testing" 5 6 ma "github.com/multiformats/go-multiaddr" 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 ) 10 11 func TestRelayAddrs_OnlyFactory(t *testing.T) { 12 relay := "/ip4/127.0.0.1/tcp/6660/p2p/QmQ7zhY7nGY66yK1n8hLGevfVyjbtvHSgtZuXkCH9oTrgi" 13 f := WithRelayAddrs([]string{relay}) 14 15 a, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/33201/p2p/QmaXZhW44pwQxBSeLkE5FNeLz8tGTTEsRciFg1DNWXXrWG") 16 require.NoError(t, err) 17 t.Log("addrstring", a.String()) 18 addrs := []ma.Multiaddr{a} 19 20 result := f(addrs) 21 t.Log("result", len(result), "addrs", result) 22 assert.Equal(t, 2, len(result), "Unexpected number of addresses") 23 24 expected := "/ip4/127.0.0.1/tcp/6660/p2p/QmQ7zhY7nGY66yK1n8hLGevfVyjbtvHSgtZuXkCH9oTrgi/p2p-circuit/ip4/127.0.0.1/tcp/33201/p2p/QmaXZhW44pwQxBSeLkE5FNeLz8tGTTEsRciFg1DNWXXrWG" 25 assert.Equal(t, expected, result[1].String(), "Address at index 1 (%s) is not the expected p2p-circuit address", result[1].String()) 26 } 27 28 func TestRelayAddrs_UseNonRelayAddrs(t *testing.T) { 29 relay := "/ip4/127.0.0.1/tcp/6660/p2p/QmQ7zhY7nGY66yK1n8hLGevfVyjbtvHSgtZuXkCH9oTrgi" 30 f := WithRelayAddrs([]string{relay}) 31 32 expected := []string{ 33 "/ip4/127.0.0.1/tcp/6660/p2p/QmQ7zhY7nGY66yK1n8hLGevfVyjbtvHSgtZuXkCH9oTrgi/p2p-circuit/ip4/127.0.0.1/tcp/33201/p2p/QmaXZhW44pwQxBSeLkE5FNeLz8tGTTEsRciFg1DNWXXrWG", 34 "/ip4/127.0.0.1/tcp/6660/p2p/QmQ7zhY7nGY66yK1n8hLGevfVyjbtvHSgtZuXkCH9oTrgi/p2p-circuit/ip4/127.0.0.1/tcp/33203/p2p/QmaXZhW44pwQxBSeLkE5FNeLz8tGTTEsRciFg1DNWXXrWG", 35 } 36 37 addrs := make([]ma.Multiaddr, len(expected)) 38 for i, addr := range expected { 39 a, err := ma.NewMultiaddr(addr) 40 require.NoError(t, err) 41 addrs[i] = a 42 } 43 44 result := f(addrs) 45 t.Log("result", result) 46 assert.Equal(t, 2, len(result)) 47 48 } 49 50 func Test_WithRelayAddrs(t *testing.T) { 51 addrF := WithRelayAddrs([]string{"/ip4/127.0.0.1/tcp/6660/p2p/QmQ7zhY7nGY66yK1n8hLGevfVyjbtvHSgtZuXkCH9oTrgi"}) 52 assert.NotNil(t, addrF) 53 var testAddr = "/ip4/127.0.0.1/tcp/33201/p2p/QmaXZhW44pwQxBSeLkE5FNeLz8tGTTEsRciFg1DNWXXrWG" 54 a, err := ma.NewMultiaddr(testAddr) 55 require.NoError(t, err) 56 maddrs := addrF([]ma.Multiaddr{a}) 57 assert.Equal(t, len(maddrs), 2) 58 59 addrF = WithRelayAddrs([]string{}) 60 maddrs = addrF([]ma.Multiaddr{a}) 61 assert.Equal(t, len(maddrs), 1) 62 63 }