test: basic routing interface test
This commit was moved from ipfs/interface-go-ipfs-core@d069f41be1eea938a4bbaa16dae953eed24bc945 This commit was moved from ipfs/boxo@48f8c69a903b1e66f8725f6b5ce9741b8e88c5a1
Henrique Dias committed
Feb 8, 2023 at 08:37 UTC
3f7c35bdb9239717c6e7f807e4effb6302842562
2 files changed
+93
core/coreiface/tests/api.go
+1
@@ -69,6 +69,7 @@ func TestApi(p Provider) func(t *testing.T) {
69
t.Run("Path", tp.TestPath)
70
t.Run("Pin", tp.TestPin)
71
t.Run("PubSub", tp.TestPubSub)
72
+ t.Run("Routing", tp.TestRouting)
73
t.Run("Unixfs", tp.TestUnixfs)
74
75
apis <- -1
core/coreiface/tests/routing.go
new
+92
@@ -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
+}