|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/ipfs/boxo/ipns" |
| 7 | + "github.com/ipfs/kubo/config" |
| 8 | + "github.com/ipfs/kubo/test/cli/harness" |
| 9 | + "github.com/ipfs/kubo/test/cli/testutils" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +func TestRoutingV1Proxy(t *testing.T) { |
| 15 | + t.Parallel() |
| 16 | + |
| 17 | + setupNodes := func(t *testing.T) harness.Nodes { |
| 18 | + nodes := harness.NewT(t).NewNodes(2).Init() |
| 19 | + |
| 20 | + // Node 0 uses DHT and exposes the Routing API. |
| 21 | + nodes[0].UpdateConfig(func(cfg *config.Config) { |
| 22 | + cfg.Gateway.ExposeRoutingAPI = config.True |
| 23 | + cfg.Routing.Type = config.NewOptionalString("dht") |
| 24 | + }) |
| 25 | + nodes[0].StartDaemon() |
| 26 | + |
| 27 | + // Node 1 uses Node 0 as Routing V1 source, no DHT. |
| 28 | + nodes[1].UpdateConfig(func(cfg *config.Config) { |
| 29 | + cfg.Routing.Type = config.NewOptionalString("custom") |
| 30 | + cfg.Routing.Methods = config.Methods{ |
| 31 | + config.MethodNameFindPeers: {RouterName: "KuboA"}, |
| 32 | + config.MethodNameFindProviders: {RouterName: "KuboA"}, |
| 33 | + config.MethodNameGetIPNS: {RouterName: "KuboA"}, |
| 34 | + config.MethodNamePutIPNS: {RouterName: "KuboA"}, |
| 35 | + config.MethodNameProvide: {RouterName: "KuboA"}, |
| 36 | + } |
| 37 | + cfg.Routing.Routers = config.Routers{ |
| 38 | + "KuboA": config.RouterParser{ |
| 39 | + Router: config.Router{ |
| 40 | + Type: config.RouterTypeHTTP, |
| 41 | + Parameters: &config.HTTPRouterParams{ |
| 42 | + Endpoint: nodes[0].GatewayURL(), |
| 43 | + }, |
| 44 | + }, |
| 45 | + }, |
| 46 | + } |
| 47 | + }) |
| 48 | + nodes[1].StartDaemon() |
| 49 | + |
| 50 | + // Connect them. |
| 51 | + nodes.Connect() |
| 52 | + |
| 53 | + return nodes |
| 54 | + } |
| 55 | + |
| 56 | + t.Run("Kubo can find provider for CID via Routing V1", func(t *testing.T) { |
| 57 | + t.Parallel() |
| 58 | + nodes := setupNodes(t) |
| 59 | + |
| 60 | + cidStr := nodes[0].IPFSAddStr(testutils.RandomStr(1000)) |
| 61 | + |
| 62 | + res := nodes[1].IPFS("routing", "findprovs", cidStr) |
| 63 | + assert.Equal(t, nodes[0].PeerID().String(), res.Stdout.Trimmed()) |
| 64 | + }) |
| 65 | + |
| 66 | + t.Run("Kubo can find peer via Routing V1", func(t *testing.T) { |
| 67 | + t.Parallel() |
| 68 | + nodes := setupNodes(t) |
| 69 | + |
| 70 | + // Start lonely node that is not connected to other nodes. |
| 71 | + node := harness.NewT(t).NewNode().Init() |
| 72 | + node.UpdateConfig(func(cfg *config.Config) { |
| 73 | + cfg.Routing.Type = config.NewOptionalString("dht") |
| 74 | + }) |
| 75 | + node.StartDaemon() |
| 76 | + |
| 77 | + // Connect Node 0 to Lonely Node. |
| 78 | + nodes[0].Connect(node) |
| 79 | + |
| 80 | + // Node 1 must find Lonely Node through Node 0 Routing V1. |
| 81 | + res := nodes[1].IPFS("routing", "findpeer", node.PeerID().String()) |
| 82 | + assert.Equal(t, node.SwarmAddrs()[0].String(), res.Stdout.Trimmed()) |
| 83 | + }) |
| 84 | + |
| 85 | + t.Run("Kubo can retrieve IPNS record via Routing V1", func(t *testing.T) { |
| 86 | + t.Parallel() |
| 87 | + nodes := setupNodes(t) |
| 88 | + |
| 89 | + nodeName := "/ipns/" + ipns.NameFromPeer(nodes[0].PeerID()).String() |
| 90 | + |
| 91 | + // Can't resolve the name as isn't published yet. |
| 92 | + res := nodes[1].RunIPFS("routing", "get", nodeName) |
| 93 | + require.Error(t, res.ExitErr) |
| 94 | + |
| 95 | + // Publish record on Node 0. |
| 96 | + path := "/ipfs/" + nodes[0].IPFSAddStr(testutils.RandomStr(1000)) |
| 97 | + nodes[0].IPFS("name", "publish", "--allow-offline", path) |
| 98 | + |
| 99 | + // Get record on Node 1 (no DHT). |
| 100 | + res = nodes[1].IPFS("routing", "get", nodeName) |
| 101 | + record, err := ipns.UnmarshalRecord(res.Stdout.Bytes()) |
| 102 | + require.NoError(t, err) |
| 103 | + value, err := record.Value() |
| 104 | + require.NoError(t, err) |
| 105 | + require.Equal(t, path, value.String()) |
| 106 | + }) |
| 107 | + |
| 108 | + t.Run("Kubo can resolve IPNS name via Routing V1", func(t *testing.T) { |
| 109 | + t.Parallel() |
| 110 | + nodes := setupNodes(t) |
| 111 | + |
| 112 | + nodeName := "/ipns/" + ipns.NameFromPeer(nodes[0].PeerID()).String() |
| 113 | + |
| 114 | + // Can't resolve the name as isn't published yet. |
| 115 | + res := nodes[1].RunIPFS("routing", "get", nodeName) |
| 116 | + require.Error(t, res.ExitErr) |
| 117 | + |
| 118 | + // Publish name. |
| 119 | + path := "/ipfs/" + nodes[0].IPFSAddStr(testutils.RandomStr(1000)) |
| 120 | + nodes[0].IPFS("name", "publish", "--allow-offline", path) |
| 121 | + |
| 122 | + // Resolve IPNS name |
| 123 | + res = nodes[1].IPFS("name", "resolve", nodeName) |
| 124 | + require.Equal(t, path, res.Stdout.Trimmed()) |
| 125 | + }) |
| 126 | + |
| 127 | + t.Run("Kubo can provide IPNS record via Routing V1", func(t *testing.T) { |
| 128 | + t.Parallel() |
| 129 | + nodes := setupNodes(t) |
| 130 | + |
| 131 | + // Publish something on Node 1 (no DHT). |
| 132 | + nodeName := "/ipns/" + ipns.NameFromPeer(nodes[1].PeerID()).String() |
| 133 | + path := "/ipfs/" + nodes[1].IPFSAddStr(testutils.RandomStr(1000)) |
| 134 | + nodes[1].IPFS("name", "publish", "--allow-offline", path) |
| 135 | + |
| 136 | + // Retrieve through Node 0. |
| 137 | + res := nodes[0].IPFS("routing", "get", nodeName) |
| 138 | + record, err := ipns.UnmarshalRecord(res.Stdout.Bytes()) |
| 139 | + require.NoError(t, err) |
| 140 | + value, err := record.Value() |
| 141 | + require.NoError(t, err) |
| 142 | + require.Equal(t, path, value.String()) |
| 143 | + }) |
| 144 | +} |
0 commit comments