Last active
January 10, 2022 14:14
-
-
Save gfanton/5303cdff23abb3d3245d2559c20d7f5a to your computer and use it in GitHub Desktop.
4g test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package ltecirctuit | |
| import ( | |
| "context" | |
| "crypto/rand" | |
| "encoding/base64" | |
| "fmt" | |
| "testing" | |
| "time" | |
| p2pping "github.com/libp2p/go-libp2p/p2p/protocol/ping" | |
| ds "github.com/ipfs/go-datastore" | |
| ipfs_ds "github.com/ipfs/go-datastore" | |
| ds_sync "github.com/ipfs/go-datastore/sync" | |
| ipfs_cfg "github.com/ipfs/go-ipfs-config" | |
| "github.com/ipfs/go-ipfs/core" | |
| ipfs_p2p "github.com/ipfs/go-ipfs/core/node/libp2p" | |
| ipfs_repo "github.com/ipfs/go-ipfs/repo" | |
| "github.com/libp2p/go-libp2p" | |
| ci "github.com/libp2p/go-libp2p-core/crypto" | |
| "github.com/libp2p/go-libp2p-core/host" | |
| "github.com/libp2p/go-libp2p-core/peer" | |
| "github.com/libp2p/go-libp2p-core/peerstore" | |
| quict "github.com/libp2p/go-libp2p-quic-transport" | |
| tcpt "github.com/libp2p/go-tcp-transport" | |
| ma "github.com/multiformats/go-multiaddr" | |
| "github.com/stretchr/testify/assert" | |
| "github.com/stretchr/testify/require" | |
| ) | |
| var tcpBertyRelays = []string{ | |
| "/ip4/51.159.21.214/tcp/4040/p2p/QmdT7AmhhnbuwvCpa5PH1ySK9HJVB82jr3fo1bxMxBPW6p", | |
| "/ip4/51.15.25.224/tcp/4040/p2p/12D3KooWHhDBv6DJJ4XDWjzEXq6sVNEs6VuxsV1WyBBEhPENHzcZ", | |
| } | |
| var quicBertyRelays = []string{ | |
| "/ip4/51.159.21.214/udp/4040/quic/p2p/QmdT7AmhhnbuwvCpa5PH1ySK9HJVB82jr3fo1bxMxBPW6p", | |
| "/ip4/51.15.25.224/udp/4040/quic/p2p/12D3KooWHhDBv6DJJ4XDWjzEXq6sVNEs6VuxsV1WyBBEhPENHzcZ", | |
| } | |
| // var tcpIPFSRelays = []string{ | |
| // "/ip4/147.75.80.110/tcp/4001/p2p/QmbFgm5zan8P6eWWmeyfncR5feYEMPbht5b1FW1C37aQ7y", | |
| // "/ip4/147.75.195.153/tcp/4001/p2p/QmW9m57aiBDHAkKj9nmFSEn7ZqrcF1fZS4bipsTCHburei", | |
| // "/ip4/147.75.70.221/tcp/4001/p2p/Qme8g49gm3q4Acp7xWBKg3nAa9fxZ1YmyDJdyGgoG6LsXh", | |
| // } | |
| // var quicIPFSRelays = []string{ | |
| // "/ip4/147.75.80.110/udp/4001/quic/p2p/QmbFgm5zan8P6eWWmeyfncR5feYEMPbht5b1FW1C37aQ7y", | |
| // "/ip4/147.75.195.153/udp/4001/quic/p2p/QmW9m57aiBDHAkKj9nmFSEn7ZqrcF1fZS4bipsTCHburei", | |
| // "/ip4/147.75.70.221/udp/4001/quic/p2p/Qme8g49gm3q4Acp7xWBKg3nAa9fxZ1YmyDJdyGgoG6LsXh", | |
| // } | |
| type HostGeneratorFunc func(t *testing.T, ctx context.Context) (host.Host, error) | |
| func TestCircuitConnect(t *testing.T) { | |
| rootCtx := context.Background() | |
| cases := []struct { | |
| tname string | |
| timeout time.Duration | |
| relays []string | |
| genhosta HostGeneratorFunc | |
| genhostb HostGeneratorFunc | |
| }{ | |
| {"simple_host/tcp_relay/10s_timeout", time.Second * 10, tcpBertyRelays, genSimpleHost, genSimpleHost}, | |
| {"ipfs_client_host/tcp_relay/10s_timeout", time.Second * 10, tcpBertyRelays, genIpfsHost, genSimpleHost}, | |
| {"simple_host/quic_relay/10s_timeout", time.Second * 10, quicBertyRelays, genSimpleHost, genSimpleHost}, | |
| {"ipfs_client_host/quic_relay/10s_timeout", time.Second * 10, quicBertyRelays, genIpfsHost, genSimpleHost}, | |
| } | |
| for _, tc := range cases { | |
| t.Run(tc.tname, func(t *testing.T) { | |
| for _, relay := range tc.relays { | |
| relay := ma.StringCast(relay) | |
| relayname := fmt.Sprintf("relay%s", relay.String()) | |
| t.Run(relayname, func(t *testing.T) { | |
| hosta, err := tc.genhosta(t, rootCtx) | |
| require.NoError(t, err) | |
| require.NotNil(t, hosta) | |
| defer hosta.Close() | |
| hostb, err := tc.genhostb(t, rootCtx) | |
| require.NoError(t, err) | |
| require.NotNil(t, hostb) | |
| defer hosta.Close() | |
| ctx, cancel := context.WithTimeout(rootCtx, tc.timeout) | |
| defer cancel() | |
| testCircuitRelay(t, ctx, hosta, hostb, relay) | |
| }) | |
| } | |
| }) | |
| } | |
| } | |
| func testCircuitRelay(t *testing.T, ctx context.Context, hosta, hostb host.Host, relay ma.Multiaddr) { | |
| pirelay, err := peer.AddrInfoFromP2pAddr(relay) | |
| require.NoError(t, err) | |
| // try to connect to relay first with host B | |
| err = hostb.Connect(ctx, *pirelay) | |
| require.NoError(t, err, "hostB(%s) unable to connect to relay(%s)", hostb.ID().ShortString(), pirelay.ID.ShortString()) | |
| // hosta try to fetch hostb through relay | |
| err = hosta.Connect(ctx, *pirelay) | |
| require.NoError(t, err, "hostB(%s) unable to connect to relay(%s)", hosta.ID().ShortString(), pirelay.ID.ShortString()) | |
| // create circuit addr for hostbs | |
| circuitaddr := ma.StringCast("/p2p-circuit/p2p/" + hostb.ID().Pretty()) | |
| fulladdr := relay.Encapsulate(circuitaddr) | |
| hosta.Peerstore().AddAddr(hostb.ID(), fulladdr, peerstore.TempAddrTTL) | |
| s := p2pping.NewPingService(hosta) | |
| cp := s.Ping(ctx, hostb.ID()) | |
| for i := 0; i < 5; i++ { | |
| select { | |
| case <-ctx.Done(): | |
| case p := <-cp: | |
| assert.NoError(t, p.Error, "failed to ping HostB(%s)") | |
| } | |
| if !assert.NoError(t, ctx.Err()) { | |
| return | |
| } | |
| } | |
| } | |
| func genSimpleHost(t *testing.T, ctx context.Context) (host.Host, error) { | |
| return libp2p.New(ctx, | |
| libp2p.EnableRelay(), | |
| libp2p.Transport(quict.NewTransport), | |
| libp2p.Transport(tcpt.NewTCPTransport), | |
| ) | |
| } | |
| func genIpfsHost(t *testing.T, ctx context.Context) (host.Host, error) { | |
| dsync := ds_sync.MutexWrap(ds.NewMapDatastore()) | |
| repo, err := createDefaultMockedRepo(dsync) | |
| if err != nil { | |
| return nil, err | |
| } | |
| nodeOptions := &core.BuildCfg{ | |
| Online: true, | |
| Routing: ipfs_p2p.DHTOption, // This option sets the node to be a full DHT node (both fetching and storing DHT Records) | |
| // Routing: libp2p.DHTClientOption, // This option sets the node to be a client DHT node (only fetching records) | |
| // Routing: ipfs_p2p.NilRouterOption, // This option sets the node to nil | |
| Repo: repo, | |
| } | |
| node, err := core.NewNode(ctx, nodeOptions) | |
| if err != nil { | |
| return nil, err | |
| } | |
| t.Cleanup(func() { | |
| node.Close() | |
| }) | |
| return node.PeerHost, nil | |
| } | |
| func createDefaultMockedRepo(dstore ipfs_ds.Batching) (ipfs_repo.Repo, error) { | |
| c := ipfs_cfg.Config{} | |
| priv, pub, err := ci.GenerateKeyPairWithReader(ci.RSA, 2048, rand.Reader) | |
| if err != nil { | |
| return nil, err | |
| } | |
| pid, err := peer.IDFromPublicKey(pub) | |
| if err != nil { | |
| return nil, err | |
| } | |
| privkeyb, err := priv.Bytes() | |
| if err != nil { | |
| return nil, err | |
| } | |
| c.Bootstrap = ipfs_cfg.DefaultBootstrapAddresses | |
| // c.Bootstrap = ipfs_cfg.DefaultBootstrapAddresses // @NOTE(gfanton): if we remove bootstrap peer the test works | |
| c.Addresses.Swarm = []string{"/ip4/0.0.0.0/tcp/4001", "/ip4/0.0.0.0/udp/4001/quic"} | |
| c.Identity.PeerID = pid.Pretty() | |
| c.Identity.PrivKey = base64.StdEncoding.EncodeToString(privkeyb) | |
| return &ipfs_repo.Mock{ | |
| D: dstore, | |
| C: c, | |
| }, nil | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wifi DC4 - macOS 11.4 (20F71)
Tethering wifi - SFR (iPhone), macOS 11.4 (20F71)
Tethering wifi - Bouygues Telecom (Samsung - Android 11), macOS 11.4 (20F71)