path.go 4.3 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)
	}

Łukasz Magiera's avatar
Łukasz Magiera committed
78
	rp1, err := api.ResolvePath(ctx, coreiface.ParsePath(nd.String()+"/foo/bar"))
79 80 81 82 83 84 85 86 87
	if err != nil {
		t.Fatal(err)
	}

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

88
func (tp *provider) TestEmptyPathRemainder(t *testing.T) {
89 90
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
91
	api, err := tp.makeAPI(ctx)
92 93 94 95
	if err != nil {
		t.Fatal(err)
	}

96 97 98 99
	if api.Dag() == nil {
		t.Fatal(".Dag not implemented")
	}

100
	nd, err := ipldcbor.FromJSON(strings.NewReader(`{"foo": {"bar": "baz"}}`), math.MaxUint64, -1)
101
	if err != nil {
102
		t.Fatal(err)
103 104
	}

105
	if err := api.Dag().Add(ctx, nd); err != nil {
106
		t.Fatal(err)
107 108
	}

Łukasz Magiera's avatar
Łukasz Magiera committed
109
	rp1, err := api.ResolvePath(ctx, coreiface.ParsePath(nd.Cid().String()))
110 111 112 113 114 115 116 117 118
	if err != nil {
		t.Fatal(err)
	}

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

119
func (tp *provider) TestInvalidPathRemainder(t *testing.T) {
120 121
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
122
	api, err := tp.makeAPI(ctx)
123 124 125 126
	if err != nil {
		t.Fatal(err)
	}

127 128 129 130
	if api.Dag() == nil {
		t.Fatal(".Dag not implemented")
	}

131
	nd, err := ipldcbor.FromJSON(strings.NewReader(`{"foo": {"bar": "baz"}}`), math.MaxUint64, -1)
132
	if err != nil {
133
		t.Fatal(err)
134 135
	}

136
	if err := api.Dag().Add(ctx, nd); err != nil {
137 138 139
		t.Fatal(err)
	}

Łukasz Magiera's avatar
Łukasz Magiera committed
140
	_, err = api.ResolvePath(ctx, coreiface.ParsePath("/ipld/"+nd.Cid().String()+"/bar/baz"))
141
	if err == nil || !strings.Contains(err.Error(), "no such link found") {
142 143 144 145
		t.Fatalf("unexpected error: %s", err)
	}
}

146
func (tp *provider) TestPathRoot(t *testing.T) {
147 148
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
149
	api, err := tp.makeAPI(ctx)
150 151 152 153
	if err != nil {
		t.Fatal(err)
	}

154 155 156 157
	if api.Block() == nil {
		t.Fatal(".Block not implemented")
	}

158 159
	blk, err := api.Block().Put(ctx, strings.NewReader(`foo`), options.Block.Format("raw"))
	if err != nil {
160 161 162 163 164
		t.Fatal(err)
	}

	if api.Dag() == nil {
		t.Fatal(".Dag not implemented")
165 166
	}

167
	nd, err := ipldcbor.FromJSON(strings.NewReader(`{"foo": {"/": "`+blk.Path().Cid().String()+`"}}`), math.MaxUint64, -1)
168
	if err != nil {
169
		t.Fatal(err)
170 171
	}

172
	if err := api.Dag().Add(ctx, nd); err != nil {
173 174 175
		t.Fatal(err)
	}

Łukasz Magiera's avatar
Łukasz Magiera committed
176
	rp, err := api.ResolvePath(ctx, coreiface.ParsePath("/ipld/"+nd.Cid().String()+"/foo"))
177 178 179 180
	if err != nil {
		t.Fatal(err)
	}

181
	if rp.Root().String() != nd.Cid().String() {
182 183 184 185 186 187 188
		t.Error("unexpected path root")
	}

	if rp.Cid().String() != blk.Path().Cid().String() {
		t.Error("unexpected path cid")
	}
}
189 190

func (tp *provider) TestPathJoin(t *testing.T) {
Łukasz Magiera's avatar
Łukasz Magiera committed
191
	p1 := coreiface.ParsePath("/ipfs/QmYNmQKp6SuaVrpgWRsPTgCQCnpxUYGq76YEKBXuj2N4H6/bar/baz")
192 193 194 195 196

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