path.go 4.23 KB
Newer Older
1
package tests
2 3 4 5 6 7 8 9 10 11

import (
	"context"
	"strings"
	"testing"

	coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
	"github.com/ipfs/go-ipfs/core/coreapi/interface/options"
)

12 13 14 15 16 17
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)
18
	t.Run("TestPathJoin", tp.TestPathJoin)
19 20
}

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

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

34 35
	if blk.Path().Mutable() {
		t.Error("expected /ipld path to be immutable")
36 37
	}

38 39 40 41 42 43
	// get self /ipns path
	if api.Key() == nil {
		t.Fatal(".Key not implemented")
	}

	keys, err := api.Key().List(ctx)
44
	if err != nil {
45
		t.Fatal(err)
46 47
	}

48 49
	if !keys[0].Path().Mutable() {
		t.Error("expected self /ipns path to be mutable")
50 51 52
	}
}

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

61 62 63 64
	if api.Dag() == nil {
		t.Fatal(".Dag not implemented")
	}

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
	obj, err := api.Dag().Put(ctx, strings.NewReader(`{"foo": {"bar": "baz"}}`))
	if err != nil {
		t.Fatal(err)
	}

	p1, err := coreiface.ParsePath(obj.String() + "/foo/bar")
	if err != nil {
		t.Error(err)
	}

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

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

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

93 94 95 96
	if api.Dag() == nil {
		t.Fatal(".Dag not implemented")
	}

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
	obj, err := api.Dag().Put(ctx, strings.NewReader(`{"foo": {"bar": "baz"}}`))
	if err != nil {
		t.Fatal(err)
	}

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

	p1, err := coreiface.ParsePath(obj.String())
	if err != nil {
		t.Error(err)
	}

	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")
	}
}

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

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

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
	obj, err := api.Dag().Put(ctx, strings.NewReader(`{"foo": {"bar": "baz"}}`))
	if err != nil {
		t.Fatal(err)
	}

	p1, err := coreiface.ParsePath(obj.String() + "/bar/baz")
	if err != nil {
		t.Error(err)
	}

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

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

157 158 159 160
	if api.Block() == nil {
		t.Fatal(".Block not implemented")
	}

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

	if api.Dag() == nil {
		t.Fatal(".Dag not implemented")
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
	}

	obj, err := api.Dag().Put(ctx, strings.NewReader(`{"foo": {"/": "`+blk.Path().Cid().String()+`"}}`))
	if err != nil {
		t.Fatal(err)
	}

	p1, err := coreiface.ParsePath(obj.String() + "/foo")
	if err != nil {
		t.Error(err)
	}

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

	if rp.Root().String() != obj.Cid().String() {
		t.Error("unexpected path root")
	}

	if rp.Cid().String() != blk.Path().Cid().String() {
		t.Error("unexpected path cid")
	}
}
193 194 195 196 197 198 199 200 201 202 203

func (tp *provider) TestPathJoin(t *testing.T) {
	p1, err := coreiface.ParsePath("/ipfs/QmYNmQKp6SuaVrpgWRsPTgCQCnpxUYGq76YEKBXuj2N4H6/bar/baz")
	if err != nil {
		t.Error(err)
	}

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