path.go 4.55 KB
Newer Older
1
package tests
2 3 4

import (
	"context"
5
	"math"
6 7 8
	"strings"
	"testing"

Łukasz Magiera's avatar
Łukasz Magiera committed
9 10
	coreiface "github.com/ipfs/interface-go-ipfs-core"
	"github.com/ipfs/interface-go-ipfs-core/options"
11

Łukasz Magiera's avatar
Łukasz Magiera committed
12
	ipldcbor "github.com/ipfs/go-ipld-cbor"
13 14
)

15 16 17 18 19 20
func (tp *provider) TestPath(t *testing.T) {
	t.Run("TestMutablePath", tp.TestMutablePath)
	t.Run("TestPathRemainder", tp.TestPathRemainder)
	t.Run("TestEmptyPathRemainder", tp.TestEmptyPathRemainder)
	t.Run("TestInvalidPathRemainder", tp.TestInvalidPathRemainder)
	t.Run("TestPathRoot", tp.TestPathRoot)
21
	t.Run("TestPathJoin", tp.TestPathJoin)
22 23
}

24
func (tp *provider) TestMutablePath(t *testing.T) {
25 26
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
27
	api, err := tp.makeAPI(ctx)
28 29 30 31
	if err != nil {
		t.Fatal(err)
	}

32
	blk, err := api.Block().Put(ctx, strings.NewReader(`foo`))
33 34 35 36
	if err != nil {
		t.Fatal(err)
	}

37 38
	if blk.Path().Mutable() {
		t.Error("expected /ipld path to be immutable")
39 40
	}

Łukasz Magiera's avatar
Łukasz Magiera committed
41
	// get self /ipns path
42

43 44 45 46 47
	if api.Key() == nil {
		t.Fatal(".Key not implemented")
	}

	keys, err := api.Key().List(ctx)
48
	if err != nil {
49
		t.Fatal(err)
50 51
	}

52 53
	if !keys[0].Path().Mutable() {
		t.Error("expected self /ipns path to be mutable")
54 55 56
	}
}

57
func (tp *provider) TestPathRemainder(t *testing.T) {
58 59
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
60
	api, err := tp.makeAPI(ctx)
61 62 63 64
	if err != nil {
		t.Fatal(err)
	}

65 66 67 68
	if api.Dag() == nil {
		t.Fatal(".Dag not implemented")
	}

69
	nd, err := ipldcbor.FromJSON(strings.NewReader(`{"foo": {"bar": "baz"}}`), math.MaxUint64, -1)
70
	if err != nil {
71
		t.Fatal(err)
72 73
	}

74
	if err := api.Dag().Add(ctx, nd); err != nil {
75 76 77
		t.Fatal(err)
	}

78
	p1, err := coreiface.ParsePath(nd.String() + "/foo/bar")
79
	if err != nil {
80
		t.Fatal(err)
81 82 83 84 85 86 87 88 89 90 91 92
	}

	rp1, err := api.ResolvePath(ctx, p1)
	if err != nil {
		t.Fatal(err)
	}

	if rp1.Remainder() != "foo/bar" {
		t.Error("expected to get path remainder")
	}
}

93
func (tp *provider) TestEmptyPathRemainder(t *testing.T) {
94 95
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
96
	api, err := tp.makeAPI(ctx)
97 98 99 100
	if err != nil {
		t.Fatal(err)
	}

101 102 103 104
	if api.Dag() == nil {
		t.Fatal(".Dag not implemented")
	}

105
	nd, err := ipldcbor.FromJSON(strings.NewReader(`{"foo": {"bar": "baz"}}`), math.MaxUint64, -1)
106
	if err != nil {
107
		t.Fatal(err)
108 109
	}

110
	if err := api.Dag().Add(ctx, nd); err != nil {
111
		t.Fatal(err)
112 113
	}

114
	p1, err := coreiface.ParsePath(nd.Cid().String())
115
	if err != nil {
116
		t.Fatal(err)
117 118 119 120 121 122 123 124 125 126 127 128
	}

	rp1, err := api.ResolvePath(ctx, p1)
	if err != nil {
		t.Fatal(err)
	}

	if rp1.Remainder() != "" {
		t.Error("expected the resolved path to not have a remainder")
	}
}

129
func (tp *provider) TestInvalidPathRemainder(t *testing.T) {
130 131
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
132
	api, err := tp.makeAPI(ctx)
133 134 135 136
	if err != nil {
		t.Fatal(err)
	}

137 138 139 140
	if api.Dag() == nil {
		t.Fatal(".Dag not implemented")
	}

141
	nd, err := ipldcbor.FromJSON(strings.NewReader(`{"foo": {"bar": "baz"}}`), math.MaxUint64, -1)
142
	if err != nil {
143
		t.Fatal(err)
144 145
	}

146
	if err := api.Dag().Add(ctx, nd); err != nil {
147 148 149
		t.Fatal(err)
	}

150
	p1, err := coreiface.ParsePath("/ipld/" + nd.Cid().String() + "/bar/baz")
151
	if err != nil {
152
		t.Fatal(err)
153 154 155
	}

	_, err = api.ResolvePath(ctx, p1)
156
	if err == nil || !strings.Contains(err.Error(), "no such link found") {
157 158 159 160
		t.Fatalf("unexpected error: %s", err)
	}
}

161
func (tp *provider) TestPathRoot(t *testing.T) {
162 163
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
164
	api, err := tp.makeAPI(ctx)
165 166 167 168
	if err != nil {
		t.Fatal(err)
	}

169 170 171 172
	if api.Block() == nil {
		t.Fatal(".Block not implemented")
	}

173 174
	blk, err := api.Block().Put(ctx, strings.NewReader(`foo`), options.Block.Format("raw"))
	if err != nil {
175 176 177 178 179
		t.Fatal(err)
	}

	if api.Dag() == nil {
		t.Fatal(".Dag not implemented")
180 181
	}

182
	nd, err := ipldcbor.FromJSON(strings.NewReader(`{"foo": {"/": "`+blk.Path().Cid().String()+`"}}`), math.MaxUint64, -1)
183
	if err != nil {
184
		t.Fatal(err)
185 186
	}

187
	if err := api.Dag().Add(ctx, nd); err != nil {
188 189 190
		t.Fatal(err)
	}

191
	p1, err := coreiface.ParsePath("/ipld/" + nd.Cid().String() + "/foo")
192
	if err != nil {
193
		t.Fatal(err)
194 195 196 197 198 199 200
	}

	rp, err := api.ResolvePath(ctx, p1)
	if err != nil {
		t.Fatal(err)
	}

201
	if rp.Root().String() != nd.Cid().String() {
202 203 204 205 206 207 208
		t.Error("unexpected path root")
	}

	if rp.Cid().String() != blk.Path().Cid().String() {
		t.Error("unexpected path cid")
	}
}
209 210 211 212

func (tp *provider) TestPathJoin(t *testing.T) {
	p1, err := coreiface.ParsePath("/ipfs/QmYNmQKp6SuaVrpgWRsPTgCQCnpxUYGq76YEKBXuj2N4H6/bar/baz")
	if err != nil {
213
		t.Fatal(err)
214 215 216 217 218 219
	}

	if coreiface.Join(p1, "foo").String() != "/ipfs/QmYNmQKp6SuaVrpgWRsPTgCQCnpxUYGq76YEKBXuj2N4H6/bar/baz/foo" {
		t.Error("unexpected path")
	}
}