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

Commit 766e760

Browse files
committed
test: basic routing interface test
1 parent 455c16d commit 766e760

File tree

4 files changed

+98
-1
lines changed

4 files changed

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

0 commit comments

Comments
 (0)