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