master
go 193 lines 3.86 KB
Raw
1 package tests
2
3 import (
4 "math"
5 "strings"
6 "testing"
7
8 "github.com/ipfs/boxo/path"
9 coreiface "github.com/ipfs/kubo/core/coreiface"
10
11 ipldcbor "github.com/ipfs/go-ipld-cbor"
12 ipld "github.com/ipfs/go-ipld-format"
13 mh "github.com/multiformats/go-multihash"
14 )
15
16 func (tp *TestSuite) TestDag(t *testing.T) {
17 tp.hasApi(t, func(api coreiface.CoreAPI) error {
18 if api.Dag() == nil {
19 return errAPINotImplemented
20 }
21 return nil
22 })
23
24 t.Run("TestPut", tp.TestPut)
25 t.Run("TestPutWithHash", tp.TestPutWithHash)
26 t.Run("TestPath", tp.TestDagPath)
27 t.Run("TestTree", tp.TestTree)
28 t.Run("TestBatch", tp.TestBatch)
29 }
30
31 var treeExpected = map[string]struct{}{
32 "a": {},
33 "b": {},
34 "c": {},
35 "c/d": {},
36 "c/e": {},
37 }
38
39 func (tp *TestSuite) TestPut(t *testing.T) {
40 ctx := t.Context()
41 api, err := tp.makeAPI(t, ctx)
42 if err != nil {
43 t.Fatal(err)
44 }
45
46 nd, err := ipldcbor.FromJSON(strings.NewReader(`"Hello"`), math.MaxUint64, -1)
47 if err != nil {
48 t.Fatal(err)
49 }
50
51 err = api.Dag().Add(ctx, nd)
52 if err != nil {
53 t.Fatal(err)
54 }
55
56 if nd.Cid().String() != "bafyreicnga62zhxnmnlt6ymq5hcbsg7gdhqdu6z4ehu3wpjhvqnflfy6nm" {
57 t.Errorf("got wrong cid: %s", nd.Cid().String())
58 }
59 }
60
61 func (tp *TestSuite) TestPutWithHash(t *testing.T) {
62 ctx := t.Context()
63 api, err := tp.makeAPI(t, ctx)
64 if err != nil {
65 t.Fatal(err)
66 }
67
68 nd, err := ipldcbor.FromJSON(strings.NewReader(`"Hello"`), mh.SHA3_256, -1)
69 if err != nil {
70 t.Fatal(err)
71 }
72
73 err = api.Dag().Add(ctx, nd)
74 if err != nil {
75 t.Fatal(err)
76 }
77
78 if nd.Cid().String() != "bafyrmifu7haikttpqqgc5ewvmp76z3z4ebp7h2ph4memw7dq4nt6btmxny" {
79 t.Errorf("got wrong cid: %s", nd.Cid().String())
80 }
81 }
82
83 func (tp *TestSuite) TestDagPath(t *testing.T) {
84 ctx := t.Context()
85 api, err := tp.makeAPI(t, ctx)
86 if err != nil {
87 t.Fatal(err)
88 }
89
90 snd, err := ipldcbor.FromJSON(strings.NewReader(`"foo"`), math.MaxUint64, -1)
91 if err != nil {
92 t.Fatal(err)
93 }
94
95 err = api.Dag().Add(ctx, snd)
96 if err != nil {
97 t.Fatal(err)
98 }
99
100 nd, err := ipldcbor.FromJSON(strings.NewReader(`{"lnk": {"/": "`+snd.Cid().String()+`"}}`), math.MaxUint64, -1)
101 if err != nil {
102 t.Fatal(err)
103 }
104
105 err = api.Dag().Add(ctx, nd)
106 if err != nil {
107 t.Fatal(err)
108 }
109
110 p, err := path.Join(path.FromCid(nd.Cid()), "lnk")
111 if err != nil {
112 t.Fatal(err)
113 }
114
115 rp, _, err := api.ResolvePath(ctx, p)
116 if err != nil {
117 t.Fatal(err)
118 }
119
120 ndd, err := api.Dag().Get(ctx, rp.RootCid())
121 if err != nil {
122 t.Fatal(err)
123 }
124
125 if ndd.Cid().String() != snd.Cid().String() {
126 t.Errorf("got unexpected cid %s, expected %s", ndd.Cid().String(), snd.Cid().String())
127 }
128 }
129
130 func (tp *TestSuite) TestTree(t *testing.T) {
131 ctx := t.Context()
132 api, err := tp.makeAPI(t, ctx)
133 if err != nil {
134 t.Fatal(err)
135 }
136
137 nd, err := ipldcbor.FromJSON(strings.NewReader(`{"a": 123, "b": "foo", "c": {"d": 321, "e": 111}}`), math.MaxUint64, -1)
138 if err != nil {
139 t.Fatal(err)
140 }
141
142 err = api.Dag().Add(ctx, nd)
143 if err != nil {
144 t.Fatal(err)
145 }
146
147 res, err := api.Dag().Get(ctx, nd.Cid())
148 if err != nil {
149 t.Fatal(err)
150 }
151
152 lst := res.Tree("", -1)
153 if len(lst) != len(treeExpected) {
154 t.Errorf("tree length of %d doesn't match expected %d", len(lst), len(treeExpected))
155 }
156
157 for _, ent := range lst {
158 if _, ok := treeExpected[ent]; !ok {
159 t.Errorf("unexpected tree entry %s", ent)
160 }
161 }
162 }
163
164 func (tp *TestSuite) TestBatch(t *testing.T) {
165 ctx := t.Context()
166 api, err := tp.makeAPI(t, ctx)
167 if err != nil {
168 t.Fatal(err)
169 }
170
171 nd, err := ipldcbor.FromJSON(strings.NewReader(`"Hello"`), math.MaxUint64, -1)
172 if err != nil {
173 t.Fatal(err)
174 }
175
176 if nd.Cid().String() != "bafyreicnga62zhxnmnlt6ymq5hcbsg7gdhqdu6z4ehu3wpjhvqnflfy6nm" {
177 t.Errorf("got wrong cid: %s", nd.Cid().String())
178 }
179
180 _, err = api.Dag().Get(ctx, nd.Cid())
181 if err == nil || !strings.Contains(err.Error(), "not found") {
182 t.Fatal(err)
183 }
184
185 if err := api.Dag().AddMany(ctx, []ipld.Node{nd}); err != nil {
186 t.Fatal(err)
187 }
188
189 _, err = api.Dag().Get(ctx, nd.Cid())
190 if err != nil {
191 t.Fatal(err)
192 }
193 }