Skip to content
This repository was archived by the owner on Jun 19, 2023. It is now read-only.

Commit 2de38ba

Browse files
committed
test: basic routing interface test
1 parent 455c16d commit 2de38ba

File tree

4 files changed

+97
-1
lines changed

4 files changed

+97
-1
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
module github.com/ipfs/interface-go-ipfs-core
22

33
require (
4+
github.com/gogo/protobuf v1.3.2
45
github.com/ipfs/go-cid v0.3.2
56
github.com/ipfs/go-ipld-cbor v0.0.5
67
github.com/ipfs/go-ipld-format v0.3.0
8+
github.com/ipfs/go-ipns v0.3.0
79
github.com/ipfs/go-libipfs v0.1.0
810
github.com/ipfs/go-merkledag v0.6.0
911
github.com/ipfs/go-path v0.1.1
@@ -18,7 +20,6 @@ require (
1820
require (
1921
github.com/crackcomm/go-gitignore v0.0.0-20170627025303-887ab5e44cc3 // indirect
2022
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect
21-
github.com/gogo/protobuf v1.3.2 // indirect
2223
github.com/google/uuid v1.3.0 // indirect
2324
github.com/hashicorp/golang-lru v0.5.4 // indirect
2425
github.com/ipfs/bbloom v0.0.4 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,8 @@ github.com/ipfs/go-ipld-format v0.3.0 h1:Mwm2oRLzIuUwEPewWAWyMuuBQUsn3awfFEYVb8a
341341
github.com/ipfs/go-ipld-format v0.3.0/go.mod h1:co/SdBE8h99968X0hViiw1MNlh6fvxxnHpvVLnH7jSM=
342342
github.com/ipfs/go-ipld-legacy v0.1.0 h1:wxkkc4k8cnvIGIjPO0waJCe7SHEyFgl+yQdafdjGrpA=
343343
github.com/ipfs/go-ipld-legacy v0.1.0/go.mod h1:86f5P/srAmh9GcIcWQR9lfFLZPrIyyXQeVlOWeeWEuI=
344+
github.com/ipfs/go-ipns v0.3.0 h1:ai791nTgVo+zTuq2bLvEGmWP1M0A6kGTXUsgv/Yq67A=
345+
github.com/ipfs/go-ipns v0.3.0/go.mod h1:3cLT2rbvgPZGkHJoPO1YMJeh6LtkxopCkKFcio/wE24=
344346
github.com/ipfs/go-libipfs v0.1.0 h1:I6CrHHp4cIiqsWJPVU3QBH4BZrRWSljS2aAbA3Eg9AY=
345347
github.com/ipfs/go-libipfs v0.1.0/go.mod h1:qX0d9h+wu53PFtCTXxdXVBakd6ZCvGDdkZUKmdLMLx0=
346348
github.com/ipfs/go-log v0.0.1/go.mod h1:kL1d2/hzSpI0thNYjiKfjanbVNU+IIGA/WnNESY9leM=

tests/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func TestApi(p Provider) func(t *testing.T) {
6969
t.Run("Path", tp.TestPath)
7070
t.Run("Pin", tp.TestPin)
7171
t.Run("PubSub", tp.TestPubSub)
72+
t.Run("Routing", tp.TestRouting)
7273
t.Run("Unixfs", tp.TestUnixfs)
7374

7475
apis <- -1

tests/routing.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package tests
2+
3+
import (
4+
"context"
5+
"testing"
6+
"time"
7+
8+
"github.com/gogo/protobuf/proto"
9+
ipns_pb "github.com/ipfs/go-ipns/pb"
10+
iface "github.com/ipfs/interface-go-ipfs-core"
11+
)
12+
13+
func (tp *TestSuite) TestRouting(t *testing.T) {
14+
tp.hasApi(t, func(api iface.CoreAPI) error {
15+
if api.Routing() == nil {
16+
return errAPINotImplemented
17+
}
18+
return nil
19+
})
20+
21+
t.Run("TestRoutingGet", tp.TestRoutingGet)
22+
t.Run("TestRoutingPut", tp.TestRoutingPut)
23+
}
24+
25+
func (tp *TestSuite) testRoutingPublishKey(t *testing.T, ctx context.Context, api iface.CoreAPI) iface.IpnsEntry {
26+
p, err := addTestObject(ctx, api)
27+
if err != nil {
28+
t.Fatal(err)
29+
}
30+
31+
entry, err := api.Name().Publish(ctx, p)
32+
if err != nil {
33+
t.Fatal(err)
34+
}
35+
36+
time.Sleep(3 * time.Second)
37+
return entry
38+
}
39+
40+
func (tp *TestSuite) TestRoutingGet(t *testing.T) {
41+
ctx, cancel := context.WithCancel(context.Background())
42+
defer cancel()
43+
44+
apis, err := tp.MakeAPISwarm(ctx, true, 2)
45+
if err != nil {
46+
t.Fatal(err)
47+
}
48+
49+
// Node 1: publishes an IPNS name
50+
ipnsEntry := tp.testRoutingPublishKey(t, ctx, apis[0])
51+
52+
// Node 2: retrieves the best value for the IPNS name.
53+
data, err := apis[1].Routing().Get(ctx, "/ipns/"+ipnsEntry.Name())
54+
if err != nil {
55+
t.Fatal(err)
56+
}
57+
58+
// Checks if values match.
59+
var entry ipns_pb.IpnsEntry
60+
err = proto.Unmarshal(data, &entry)
61+
if err != nil {
62+
t.Fatal(err)
63+
}
64+
65+
if string(entry.GetValue()) != ipnsEntry.Value().String() {
66+
t.Fatalf("routing key has wrong value, expected %s, got %s", ipnsEntry.Value().String(), string(entry.GetValue()))
67+
}
68+
}
69+
70+
func (tp *TestSuite) TestRoutingPut(t *testing.T) {
71+
ctx, cancel := context.WithCancel(context.Background())
72+
defer cancel()
73+
apis, err := tp.MakeAPISwarm(ctx, true, 1)
74+
if err != nil {
75+
t.Fatal(err)
76+
}
77+
78+
// Create and publish IPNS entry.
79+
ipnsEntry := tp.testRoutingPublishKey(t, ctx, apis[0])
80+
81+
// Get valid routing value.
82+
data, err := apis[0].Routing().Get(ctx, "/ipns/"+ipnsEntry.Name())
83+
if err != nil {
84+
t.Fatal(err)
85+
}
86+
87+
// Put routing value.
88+
err = apis[0].Routing().Put(ctx, "/ipns/"+ipnsEntry.Name(), data)
89+
if err != nil {
90+
t.Fatal(err)
91+
}
92+
}

0 commit comments

Comments
 (0)