object.go 9.86 KB
Newer Older
1
package tests
2 3 4 5 6 7 8 9 10 11 12 13 14

import (
	"bytes"
	"context"
	"encoding/hex"
	"io/ioutil"
	"strings"
	"testing"

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

15
func (tp *provider) TestObject(t *testing.T) {
16 17 18 19 20 21 22
	tp.hasApi(t, func(api iface.CoreAPI) error {
		if api.Object() == nil {
			return apiNotImplemented
		}
		return nil
	})

23 24 25 26 27 28 29 30 31 32 33 34
	t.Run("TestNew", tp.TestNew)
	t.Run("TestObjectPut", tp.TestObjectPut)
	t.Run("TestObjectGet", tp.TestObjectGet)
	t.Run("TestObjectData", tp.TestObjectData)
	t.Run("TestObjectLinks", tp.TestObjectLinks)
	t.Run("TestObjectStat", tp.TestObjectStat)
	t.Run("TestObjectAddLink", tp.TestObjectAddLink)
	t.Run("TestObjectAddLinkCreate", tp.TestObjectAddLinkCreate)
	t.Run("TestObjectRmLink", tp.TestObjectRmLink)
	t.Run("TestObjectAddData", tp.TestObjectAddData)
	t.Run("TestObjectSetData", tp.TestObjectSetData)
	t.Run("TestDiffTest", tp.TestDiffTest)
35 36
}

37
func (tp *provider) TestNew(t *testing.T) {
38 39
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
40
	api, err := tp.makeAPI(ctx)
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
	if err != nil {
		t.Fatal(err)
	}

	emptyNode, err := api.Object().New(ctx)
	if err != nil {
		t.Fatal(err)
	}

	dirNode, err := api.Object().New(ctx, opt.Object.Type("unixfs-dir"))
	if err != nil {
		t.Fatal(err)
	}

	if emptyNode.String() != "QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n" {
		t.Errorf("Unexpected emptyNode path: %s", emptyNode.String())
	}

	if dirNode.String() != "QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn" {
		t.Errorf("Unexpected dirNode path: %s", dirNode.String())
	}
}

64
func (tp *provider) TestObjectPut(t *testing.T) {
65 66
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
67
	api, err := tp.makeAPI(ctx)
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
	if err != nil {
		t.Fatal(err)
	}

	p1, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"foo"}`))
	if err != nil {
		t.Fatal(err)
	}

	p2, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"YmFy"}`), opt.Object.DataType("base64")) //bar
	if err != nil {
		t.Fatal(err)
	}

	pbBytes, err := hex.DecodeString("0a0362617a")
	if err != nil {
		t.Fatal(err)
	}

	p3, err := api.Object().Put(ctx, bytes.NewReader(pbBytes), opt.Object.InputEnc("protobuf"))
	if err != nil {
		t.Fatal(err)
	}

	if p1.String() != "/ipfs/QmQeGyS87nyijii7kFt1zbe4n2PsXTFimzsdxyE9qh9TST" {
		t.Errorf("unexpected path: %s", p1.String())
	}

	if p2.String() != "/ipfs/QmNeYRbCibmaMMK6Du6ChfServcLqFvLJF76PzzF76SPrZ" {
		t.Errorf("unexpected path: %s", p2.String())
	}

	if p3.String() != "/ipfs/QmZreR7M2t7bFXAdb1V5FtQhjk4t36GnrvueLJowJbQM9m" {
		t.Errorf("unexpected path: %s", p3.String())
	}
}

105
func (tp *provider) TestObjectGet(t *testing.T) {
106 107
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
108
	api, err := tp.makeAPI(ctx)
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
	if err != nil {
		t.Fatal(err)
	}

	p1, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"foo"}`))
	if err != nil {
		t.Fatal(err)
	}

	nd, err := api.Object().Get(ctx, p1)
	if err != nil {
		t.Fatal(err)
	}

	if string(nd.RawData()[len(nd.RawData())-3:]) != "foo" {
		t.Fatal("got non-matching data")
	}
}

128
func (tp *provider) TestObjectData(t *testing.T) {
129 130
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
131
	api, err := tp.makeAPI(ctx)
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
	if err != nil {
		t.Fatal(err)
	}

	p1, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"foo"}`))
	if err != nil {
		t.Fatal(err)
	}

	r, err := api.Object().Data(ctx, p1)
	if err != nil {
		t.Fatal(err)
	}

	data, err := ioutil.ReadAll(r)
	if err != nil {
		t.Fatal(err)
	}

	if string(data) != "foo" {
		t.Fatal("got non-matching data")
	}
}

156
func (tp *provider) TestObjectLinks(t *testing.T) {
157 158
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
159
	api, err := tp.makeAPI(ctx)
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
	if err != nil {
		t.Fatal(err)
	}

	p1, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"foo"}`))
	if err != nil {
		t.Fatal(err)
	}

	p2, err := api.Object().Put(ctx, strings.NewReader(`{"Links":[{"Name":"bar", "Hash":"`+p1.Cid().String()+`"}]}`))
	if err != nil {
		t.Fatal(err)
	}

	links, err := api.Object().Links(ctx, p2)
	if err != nil {
		t.Fatal(err)
	}

	if len(links) != 1 {
		t.Errorf("unexpected number of links: %d", len(links))
	}

	if links[0].Cid.String() != p1.Cid().String() {
		t.Fatal("cids didn't batch")
	}

	if links[0].Name != "bar" {
		t.Fatal("unexpected link name")
	}
}

192
func (tp *provider) TestObjectStat(t *testing.T) {
193 194
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
195
	api, err := tp.makeAPI(ctx)
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
	if err != nil {
		t.Fatal(err)
	}

	p1, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"foo"}`))
	if err != nil {
		t.Fatal(err)
	}

	p2, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"bazz", "Links":[{"Name":"bar", "Hash":"`+p1.Cid().String()+`", "Size":3}]}`))
	if err != nil {
		t.Fatal(err)
	}

	stat, err := api.Object().Stat(ctx, p2)
	if err != nil {
		t.Fatal(err)
	}

	if stat.Cid.String() != p2.Cid().String() {
		t.Error("unexpected stat.Cid")
	}

	if stat.NumLinks != 1 {
		t.Errorf("unexpected stat.NumLinks")
	}

	if stat.BlockSize != 51 {
		t.Error("unexpected stat.BlockSize")
	}

	if stat.LinksSize != 47 {
		t.Errorf("unexpected stat.LinksSize: %d", stat.LinksSize)
	}

	if stat.DataSize != 4 {
		t.Error("unexpected stat.DataSize")
	}

	if stat.CumulativeSize != 54 {
		t.Error("unexpected stat.DataSize")
	}
}

240
func (tp *provider) TestObjectAddLink(t *testing.T) {
241 242
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
243
	api, err := tp.makeAPI(ctx)
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
	if err != nil {
		t.Fatal(err)
	}

	p1, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"foo"}`))
	if err != nil {
		t.Fatal(err)
	}

	p2, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"bazz", "Links":[{"Name":"bar", "Hash":"`+p1.Cid().String()+`", "Size":3}]}`))
	if err != nil {
		t.Fatal(err)
	}

	p3, err := api.Object().AddLink(ctx, p2, "abc", p2)
	if err != nil {
		t.Fatal(err)
	}

	links, err := api.Object().Links(ctx, p3)
	if err != nil {
		t.Fatal(err)
	}

	if len(links) != 2 {
		t.Errorf("unexpected number of links: %d", len(links))
	}

	if links[0].Name != "abc" {
		t.Errorf("unexpected link 0 name: %s", links[0].Name)
	}

	if links[1].Name != "bar" {
		t.Errorf("unexpected link 1 name: %s", links[1].Name)
	}
}

281
func (tp *provider) TestObjectAddLinkCreate(t *testing.T) {
282 283
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
284
	api, err := tp.makeAPI(ctx)
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
	if err != nil {
		t.Fatal(err)
	}

	p1, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"foo"}`))
	if err != nil {
		t.Fatal(err)
	}

	p2, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"bazz", "Links":[{"Name":"bar", "Hash":"`+p1.Cid().String()+`", "Size":3}]}`))
	if err != nil {
		t.Fatal(err)
	}

	p3, err := api.Object().AddLink(ctx, p2, "abc/d", p2)
	if err == nil {
		t.Fatal("expected an error")
	}
303
	if !strings.Contains(err.Error(), "no link by that name") {
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
		t.Fatalf("unexpected error: %s", err.Error())
	}

	p3, err = api.Object().AddLink(ctx, p2, "abc/d", p2, opt.Object.Create(true))
	if err != nil {
		t.Fatal(err)
	}

	links, err := api.Object().Links(ctx, p3)
	if err != nil {
		t.Fatal(err)
	}

	if len(links) != 2 {
		t.Errorf("unexpected number of links: %d", len(links))
	}

	if links[0].Name != "abc" {
		t.Errorf("unexpected link 0 name: %s", links[0].Name)
	}

	if links[1].Name != "bar" {
		t.Errorf("unexpected link 1 name: %s", links[1].Name)
	}
}

330
func (tp *provider) TestObjectRmLink(t *testing.T) {
331 332
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
333
	api, err := tp.makeAPI(ctx)
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
	if err != nil {
		t.Fatal(err)
	}

	p1, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"foo"}`))
	if err != nil {
		t.Fatal(err)
	}

	p2, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"bazz", "Links":[{"Name":"bar", "Hash":"`+p1.Cid().String()+`", "Size":3}]}`))
	if err != nil {
		t.Fatal(err)
	}

	p3, err := api.Object().RmLink(ctx, p2, "bar")
	if err != nil {
		t.Fatal(err)
	}

	links, err := api.Object().Links(ctx, p3)
	if err != nil {
		t.Fatal(err)
	}

	if len(links) != 0 {
		t.Errorf("unexpected number of links: %d", len(links))
	}
}

363
func (tp *provider) TestObjectAddData(t *testing.T) {
364 365
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
366
	api, err := tp.makeAPI(ctx)
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
	if err != nil {
		t.Fatal(err)
	}

	p1, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"foo"}`))
	if err != nil {
		t.Fatal(err)
	}

	p2, err := api.Object().AppendData(ctx, p1, strings.NewReader("bar"))
	if err != nil {
		t.Fatal(err)
	}

	r, err := api.Object().Data(ctx, p2)
	if err != nil {
		t.Fatal(err)
	}

	data, err := ioutil.ReadAll(r)

	if string(data) != "foobar" {
		t.Error("unexpected data")
	}
}

393
func (tp *provider) TestObjectSetData(t *testing.T) {
394 395
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
396
	api, err := tp.makeAPI(ctx)
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
	if err != nil {
		t.Fatal(err)
	}

	p1, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"foo"}`))
	if err != nil {
		t.Fatal(err)
	}

	p2, err := api.Object().SetData(ctx, p1, strings.NewReader("bar"))
	if err != nil {
		t.Fatal(err)
	}

	r, err := api.Object().Data(ctx, p2)
	if err != nil {
		t.Fatal(err)
	}

	data, err := ioutil.ReadAll(r)

	if string(data) != "bar" {
		t.Error("unexpected data")
	}
}

423
func (tp *provider) TestDiffTest(t *testing.T) {
424 425
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
426
	api, err := tp.makeAPI(ctx)
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
	if err != nil {
		t.Fatal(err)
	}

	p1, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"foo"}`))
	if err != nil {
		t.Fatal(err)
	}

	p2, err := api.Object().Put(ctx, strings.NewReader(`{"Data":"bar"}`))
	if err != nil {
		t.Fatal(err)
	}

	changes, err := api.Object().Diff(ctx, p1, p2)
	if err != nil {
		t.Fatal(err)
	}

	if len(changes) != 1 {
		t.Fatal("unexpected changes len")
	}

	if changes[0].Type != iface.DiffMod {
		t.Fatal("unexpected change type")
	}

	if changes[0].Before.String() != p1.String() {
		t.Fatal("unexpected before path")
	}

	if changes[0].After.String() != p2.String() {
		t.Fatal("unexpected before path")
	}
}