| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/ipfs/boxo/ipns" |
| 7 | "github.com/ipfs/go-test/random" |
| 8 | "github.com/ipfs/kubo/config" |
| 9 | "github.com/ipfs/kubo/test/cli/harness" |
| 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(3).Init() |
| 19 | |
| 20 | // Node 0 uses DHT and exposes the Routing API. For the DHT |
| 21 | // to actually work there will need to be another DHT-enabled |
| 22 | // node. |
| 23 | nodes[0].UpdateConfig(func(cfg *config.Config) { |
| 24 | cfg.Gateway.ExposeRoutingAPI = config.True |
| 25 | cfg.Discovery.MDNS.Enabled = false |
| 26 | cfg.Routing.Type = config.NewOptionalString("dht") |
| 27 | }) |
| 28 | nodes[0].StartDaemon() |
| 29 | |
| 30 | // Node 1 uses Node 0 as Routing V1 source, no DHT. |
| 31 | nodes[1].UpdateConfig(func(cfg *config.Config) { |
| 32 | cfg.Discovery.MDNS.Enabled = false |
| 33 | cfg.Routing.Type = config.NewOptionalString("custom") |
| 34 | cfg.Routing.Methods = config.Methods{ |
| 35 | config.MethodNameFindPeers: {RouterName: "KuboA"}, |
| 36 | config.MethodNameFindProviders: {RouterName: "KuboA"}, |
| 37 | config.MethodNameGetIPNS: {RouterName: "KuboA"}, |
| 38 | config.MethodNamePutIPNS: {RouterName: "KuboA"}, |
| 39 | config.MethodNameProvide: {RouterName: "KuboA"}, |
| 40 | } |
| 41 | cfg.Routing.Routers = config.Routers{ |
| 42 | "KuboA": config.RouterParser{ |
| 43 | Router: config.Router{ |
| 44 | Type: config.RouterTypeHTTP, |
| 45 | Parameters: &config.HTTPRouterParams{ |
| 46 | Endpoint: nodes[0].GatewayURL(), |
| 47 | }, |
| 48 | }, |
| 49 | }, |
| 50 | } |
| 51 | }) |
| 52 | nodes[1].StartDaemon() |
| 53 | |
| 54 | // This is the second DHT node. Only used so that the DHT is |
| 55 | // operative. |
| 56 | nodes[2].UpdateConfig(func(cfg *config.Config) { |
| 57 | cfg.Gateway.ExposeRoutingAPI = config.True |
| 58 | cfg.Discovery.MDNS.Enabled = false |
| 59 | cfg.Routing.Type = config.NewOptionalString("dht") |
| 60 | }) |
| 61 | nodes[2].StartDaemon() |
| 62 | |
| 63 | t.Cleanup(func() { |
| 64 | nodes.StopDaemons() |
| 65 | }) |
| 66 | |
| 67 | // Connect them. |
| 68 | nodes.Connect() |
| 69 | |
| 70 | return nodes |
| 71 | } |
| 72 | |
| 73 | t.Run("Kubo can find provider for CID via Routing V1", func(t *testing.T) { |
| 74 | t.Parallel() |
| 75 | nodes := setupNodes(t) |
| 76 | |
| 77 | cidStr := nodes[0].IPFSAddStr(string(random.Bytes(1000))) |
| 78 | // Reprovide as initialProviderDelay still ongoing |
| 79 | waitUntilProvidesComplete(t, nodes[0]) |
| 80 | |
| 81 | res := nodes[1].IPFS("routing", "findprovs", cidStr) |
| 82 | assert.Equal(t, nodes[0].PeerID().String(), res.Stdout.Trimmed()) |
| 83 | }) |
| 84 | |
| 85 | t.Run("Kubo can find peer via Routing V1", func(t *testing.T) { |
| 86 | t.Parallel() |
| 87 | nodes := setupNodes(t) |
| 88 | |
| 89 | // Start lonely node that is not connected to other nodes. |
| 90 | node := harness.NewT(t).NewNode().Init() |
| 91 | node.UpdateConfig(func(cfg *config.Config) { |
| 92 | cfg.Discovery.MDNS.Enabled = false |
| 93 | cfg.Routing.Type = config.NewOptionalString("dht") |
| 94 | }) |
| 95 | node.StartDaemon() |
| 96 | |
| 97 | // Connect Node 0 to Lonely Node. |
| 98 | nodes[0].Connect(node) |
| 99 | |
| 100 | // Node 1 must find Lonely Node through Node 0 Routing V1. |
| 101 | res := nodes[1].IPFS("routing", "findpeer", node.PeerID().String()) |
| 102 | assert.Equal(t, node.SwarmAddrs()[0].String(), res.Stdout.Trimmed()) |
| 103 | }) |
| 104 | |
| 105 | t.Run("Kubo can retrieve IPNS record via Routing V1", func(t *testing.T) { |
| 106 | t.Parallel() |
| 107 | nodes := setupNodes(t) |
| 108 | |
| 109 | nodeName := "/ipns/" + ipns.NameFromPeer(nodes[0].PeerID()).String() |
| 110 | |
| 111 | // Can't resolve the name as isn't published yet. |
| 112 | res := nodes[1].RunIPFS("routing", "get", nodeName) |
| 113 | require.Error(t, res.ExitErr) |
| 114 | |
| 115 | // Publish record on Node 0. |
| 116 | path := "/ipfs/" + nodes[0].IPFSAddStr(string(random.Bytes(1000))) |
| 117 | nodes[0].IPFS("name", "publish", "--allow-offline", path) |
| 118 | |
| 119 | // Get record on Node 1 (no DHT). |
| 120 | res = nodes[1].IPFS("routing", "get", nodeName) |
| 121 | record, err := ipns.UnmarshalRecord(res.Stdout.Bytes()) |
| 122 | require.NoError(t, err) |
| 123 | value, err := record.Value() |
| 124 | require.NoError(t, err) |
| 125 | require.Equal(t, path, value.String()) |
| 126 | }) |
| 127 | |
| 128 | t.Run("Kubo can resolve IPNS name via Routing V1", func(t *testing.T) { |
| 129 | t.Parallel() |
| 130 | nodes := setupNodes(t) |
| 131 | |
| 132 | nodeName := "/ipns/" + ipns.NameFromPeer(nodes[0].PeerID()).String() |
| 133 | |
| 134 | // Can't resolve the name as isn't published yet. |
| 135 | res := nodes[1].RunIPFS("routing", "get", nodeName) |
| 136 | require.Error(t, res.ExitErr) |
| 137 | |
| 138 | // Publish name. |
| 139 | path := "/ipfs/" + nodes[0].IPFSAddStr(string(random.Bytes(1000))) |
| 140 | nodes[0].IPFS("name", "publish", "--allow-offline", path) |
| 141 | |
| 142 | // Resolve IPNS name |
| 143 | res = nodes[1].IPFS("name", "resolve", nodeName) |
| 144 | require.Equal(t, path, res.Stdout.Trimmed()) |
| 145 | }) |
| 146 | |
| 147 | t.Run("Kubo can provide IPNS record via Routing V1", func(t *testing.T) { |
| 148 | t.Parallel() |
| 149 | nodes := setupNodes(t) |
| 150 | |
| 151 | // Publish something on Node 1 (no DHT). |
| 152 | nodeName := "/ipns/" + ipns.NameFromPeer(nodes[1].PeerID()).String() |
| 153 | path := "/ipfs/" + nodes[1].IPFSAddStr(string(random.Bytes(1000))) |
| 154 | nodes[1].IPFS("name", "publish", "--allow-offline", path) |
| 155 | |
| 156 | // Retrieve through Node 0. |
| 157 | res := nodes[0].IPFS("routing", "get", nodeName) |
| 158 | record, err := ipns.UnmarshalRecord(res.Stdout.Bytes()) |
| 159 | require.NoError(t, err) |
| 160 | value, err := record.Value() |
| 161 | require.NoError(t, err) |
| 162 | require.Equal(t, path, value.String()) |
| 163 | }) |
| 164 | } |