dagmodifier_test.go 11.8 KB
Newer Older
1 2 3 4 5 6
package mod

import (
	"fmt"
	"io"
	"io/ioutil"
7
	"os"
8 9
	"testing"

Jeromy's avatar
Jeromy committed
10
	"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/ipfs/go-datastore/sync"
11 12 13 14 15 16 17 18 19 20 21
	"github.com/ipfs/go-ipfs/blocks/blockstore"
	bs "github.com/ipfs/go-ipfs/blockservice"
	"github.com/ipfs/go-ipfs/exchange/offline"
	imp "github.com/ipfs/go-ipfs/importer"
	"github.com/ipfs/go-ipfs/importer/chunk"
	h "github.com/ipfs/go-ipfs/importer/helpers"
	trickle "github.com/ipfs/go-ipfs/importer/trickle"
	mdag "github.com/ipfs/go-ipfs/merkledag"
	ft "github.com/ipfs/go-ipfs/unixfs"
	uio "github.com/ipfs/go-ipfs/unixfs/io"
	u "github.com/ipfs/go-ipfs/util"
22

Jeromy's avatar
Jeromy committed
23
	ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/ipfs/go-datastore"
24
	context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
25 26
)

27
func getMockDagServ(t testing.TB) mdag.DAGService {
28 29 30
	dstore := ds.NewMapDatastore()
	tsds := sync.MutexWrap(dstore)
	bstore := blockstore.NewBlockstore(tsds)
31
	bserv := bs.New(bstore, offline.Exchange(bstore))
32
	return mdag.NewDAGService(bserv)
33 34
}

35
func getMockDagServAndBstore(t testing.TB) (mdag.DAGService, blockstore.GCBlockstore) {
36 37 38
	dstore := ds.NewMapDatastore()
	tsds := sync.MutexWrap(dstore)
	bstore := blockstore.NewBlockstore(tsds)
39
	bserv := bs.New(bstore, offline.Exchange(bstore))
40
	dserv := mdag.NewDAGService(bserv)
41
	return dserv, bstore
42 43
}

44
func getNode(t testing.TB, dserv mdag.DAGService, size int64) ([]byte, *mdag.Node) {
45
	in := io.LimitReader(u.NewTimeSeededRand(), size)
Jeromy's avatar
Jeromy committed
46
	node, err := imp.BuildTrickleDagFromReader(dserv, sizeSplitterGen(500)(in))
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 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 105 106 107 108 109
	if err != nil {
		t.Fatal(err)
	}

	dr, err := uio.NewDagReader(context.Background(), node, dserv)
	if err != nil {
		t.Fatal(err)
	}

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

	return b, node
}

func testModWrite(t *testing.T, beg, size uint64, orig []byte, dm *DagModifier) []byte {
	newdata := make([]byte, size)
	r := u.NewTimeSeededRand()
	r.Read(newdata)

	if size+beg > uint64(len(orig)) {
		orig = append(orig, make([]byte, (size+beg)-uint64(len(orig)))...)
	}
	copy(orig[beg:], newdata)

	nmod, err := dm.WriteAt(newdata, int64(beg))
	if err != nil {
		t.Fatal(err)
	}

	if nmod != int(size) {
		t.Fatalf("Mod length not correct! %d != %d", nmod, size)
	}

	nd, err := dm.GetNode()
	if err != nil {
		t.Fatal(err)
	}

	err = trickle.VerifyTrickleDagStructure(nd, dm.dagserv, h.DefaultLinksPerBlock, 4)
	if err != nil {
		t.Fatal(err)
	}

	rd, err := uio.NewDagReader(context.Background(), nd, dm.dagserv)
	if err != nil {
		t.Fatal(err)
	}

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

	err = arrComp(after, orig)
	if err != nil {
		t.Fatal(err)
	}
	return orig
}

110 111 112 113 114 115
func sizeSplitterGen(size int64) chunk.SplitterGen {
	return func(r io.Reader) chunk.Splitter {
		return chunk.NewSizeSplitter(r, size)
	}
}

116
func TestDagModifierBasic(t *testing.T) {
117 118
	dserv := getMockDagServ(t)
	b, n := getNode(t, dserv, 50000)
119 120 121
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

122
	dagmod, err := NewDagModifier(ctx, n, dserv, sizeSplitterGen(512))
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
	if err != nil {
		t.Fatal(err)
	}

	// Within zero block
	beg := uint64(15)
	length := uint64(60)

	t.Log("Testing mod within zero block")
	b = testModWrite(t, beg, length, b, dagmod)

	// Within bounds of existing file
	beg = 1000
	length = 4000
	t.Log("Testing mod within bounds of existing multiblock file.")
	b = testModWrite(t, beg, length, b, dagmod)

	// Extend bounds
	beg = 49500
	length = 4000

	t.Log("Testing mod that extends file.")
	b = testModWrite(t, beg, length, b, dagmod)

	// "Append"
	beg = uint64(len(b))
	length = 3000
	t.Log("Testing pure append")
	b = testModWrite(t, beg, length, b, dagmod)

	// Verify reported length
	node, err := dagmod.GetNode()
	if err != nil {
		t.Fatal(err)
	}

	size, err := ft.DataSize(node.Data)
	if err != nil {
		t.Fatal(err)
	}

	expected := uint64(50000 + 3500 + 3000)
	if size != expected {
		t.Fatalf("Final reported size is incorrect [%d != %d]", size, expected)
	}
}

func TestMultiWrite(t *testing.T) {
171 172
	dserv := getMockDagServ(t)
	_, n := getNode(t, dserv, 0)
173 174 175 176

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

177
	dagmod, err := NewDagModifier(ctx, n, dserv, sizeSplitterGen(512))
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
	if err != nil {
		t.Fatal(err)
	}

	data := make([]byte, 4000)
	u.NewTimeSeededRand().Read(data)

	for i := 0; i < len(data); i++ {
		n, err := dagmod.WriteAt(data[i:i+1], int64(i))
		if err != nil {
			t.Fatal(err)
		}
		if n != 1 {
			t.Fatal("Somehow wrote the wrong number of bytes! (n != 1)")
		}
193 194 195 196 197 198 199 200 201

		size, err := dagmod.Size()
		if err != nil {
			t.Fatal(err)
		}

		if size != int64(i+1) {
			t.Fatal("Size was reported incorrectly")
		}
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
	}
	nd, err := dagmod.GetNode()
	if err != nil {
		t.Fatal(err)
	}

	read, err := uio.NewDagReader(context.Background(), nd, dserv)
	if err != nil {
		t.Fatal(err)
	}
	rbuf, err := ioutil.ReadAll(read)
	if err != nil {
		t.Fatal(err)
	}

	err = arrComp(rbuf, data)
	if err != nil {
		t.Fatal(err)
	}
}

func TestMultiWriteAndFlush(t *testing.T) {
224 225
	dserv := getMockDagServ(t)
	_, n := getNode(t, dserv, 0)
226 227 228 229

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

230
	dagmod, err := NewDagModifier(ctx, n, dserv, sizeSplitterGen(512))
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
	if err != nil {
		t.Fatal(err)
	}

	data := make([]byte, 20)
	u.NewTimeSeededRand().Read(data)

	for i := 0; i < len(data); i++ {
		n, err := dagmod.WriteAt(data[i:i+1], int64(i))
		if err != nil {
			t.Fatal(err)
		}
		if n != 1 {
			t.Fatal("Somehow wrote the wrong number of bytes! (n != 1)")
		}
246
		err = dagmod.Sync()
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
		if err != nil {
			t.Fatal(err)
		}
	}
	nd, err := dagmod.GetNode()
	if err != nil {
		t.Fatal(err)
	}

	read, err := uio.NewDagReader(context.Background(), nd, dserv)
	if err != nil {
		t.Fatal(err)
	}
	rbuf, err := ioutil.ReadAll(read)
	if err != nil {
		t.Fatal(err)
	}

	err = arrComp(rbuf, data)
	if err != nil {
		t.Fatal(err)
	}
}

func TestWriteNewFile(t *testing.T) {
272 273
	dserv := getMockDagServ(t)
	_, n := getNode(t, dserv, 0)
274 275 276 277

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

278
	dagmod, err := NewDagModifier(ctx, n, dserv, sizeSplitterGen(512))
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
	if err != nil {
		t.Fatal(err)
	}

	towrite := make([]byte, 2000)
	u.NewTimeSeededRand().Read(towrite)

	nw, err := dagmod.Write(towrite)
	if err != nil {
		t.Fatal(err)
	}
	if nw != len(towrite) {
		t.Fatal("Wrote wrong amount")
	}

	nd, err := dagmod.GetNode()
	if err != nil {
		t.Fatal(err)
	}

	read, err := uio.NewDagReader(ctx, nd, dserv)
	if err != nil {
		t.Fatal(err)
	}

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

	if err := arrComp(data, towrite); err != nil {
		t.Fatal(err)
	}
}

func TestMultiWriteCoal(t *testing.T) {
315 316
	dserv := getMockDagServ(t)
	_, n := getNode(t, dserv, 0)
317 318 319 320

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

321
	dagmod, err := NewDagModifier(ctx, n, dserv, sizeSplitterGen(512))
322 323 324 325 326 327 328 329 330 331 332 333 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
	if err != nil {
		t.Fatal(err)
	}

	data := make([]byte, 1000)
	u.NewTimeSeededRand().Read(data)

	for i := 0; i < len(data); i++ {
		n, err := dagmod.WriteAt(data[:i+1], 0)
		if err != nil {
			fmt.Println("FAIL AT ", i)
			t.Fatal(err)
		}
		if n != i+1 {
			t.Fatal("Somehow wrote the wrong number of bytes! (n != 1)")
		}

	}
	nd, err := dagmod.GetNode()
	if err != nil {
		t.Fatal(err)
	}

	read, err := uio.NewDagReader(context.Background(), nd, dserv)
	if err != nil {
		t.Fatal(err)
	}
	rbuf, err := ioutil.ReadAll(read)
	if err != nil {
		t.Fatal(err)
	}

	err = arrComp(rbuf, data)
	if err != nil {
		t.Fatal(err)
	}
}

func TestLargeWriteChunks(t *testing.T) {
361 362
	dserv := getMockDagServ(t)
	_, n := getNode(t, dserv, 0)
363 364 365 366

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

367
	dagmod, err := NewDagModifier(ctx, n, dserv, sizeSplitterGen(512))
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 393 394 395 396 397 398 399
	if err != nil {
		t.Fatal(err)
	}

	wrsize := 1000
	datasize := 10000000
	data := make([]byte, datasize)

	u.NewTimeSeededRand().Read(data)

	for i := 0; i < datasize/wrsize; i++ {
		n, err := dagmod.WriteAt(data[i*wrsize:(i+1)*wrsize], int64(i*wrsize))
		if err != nil {
			t.Fatal(err)
		}
		if n != wrsize {
			t.Fatal("failed to write buffer")
		}
	}

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

	if err = arrComp(out, data); err != nil {
		t.Fatal(err)
	}

}

func TestDagTruncate(t *testing.T) {
400 401
	dserv := getMockDagServ(t)
	b, n := getNode(t, dserv, 50000)
402 403 404
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

405
	dagmod, err := NewDagModifier(ctx, n, dserv, sizeSplitterGen(512))
406 407 408 409 410 411 412 413
	if err != nil {
		t.Fatal(err)
	}

	err = dagmod.Truncate(12345)
	if err != nil {
		t.Fatal(err)
	}
414
	size, err := dagmod.Size()
415 416 417 418
	if err != nil {
		t.Fatal(err)
	}

419 420
	if size != 12345 {
		t.Fatal("size was incorrect!")
421 422
	}

423
	_, err = dagmod.Seek(0, os.SEEK_SET)
424 425 426 427
	if err != nil {
		t.Fatal(err)
	}

428
	out, err := ioutil.ReadAll(dagmod)
429 430 431 432
	if err != nil {
		t.Fatal(err)
	}

433
	if err = arrComp(out, b[:12345]); err != nil {
434 435 436
		t.Fatal(err)
	}

437
	err = dagmod.Truncate(10)
438 439 440 441
	if err != nil {
		t.Fatal(err)
	}

442
	size, err = dagmod.Size()
443 444 445
	if err != nil {
		t.Fatal(err)
	}
446 447 448

	if size != 10 {
		t.Fatal("size was incorrect!")
449 450
	}
}
Jeromy's avatar
Jeromy committed
451

452 453 454
func TestSparseWrite(t *testing.T) {
	dserv := getMockDagServ(t)
	_, n := getNode(t, dserv, 0)
455 456 457
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

458
	dagmod, err := NewDagModifier(ctx, n, dserv, sizeSplitterGen(512))
459 460 461 462
	if err != nil {
		t.Fatal(err)
	}

463 464
	buf := make([]byte, 5000)
	u.NewTimeSeededRand().Read(buf[2500:])
465

466
	wrote, err := dagmod.WriteAt(buf[2500:], 2500)
467 468 469 470
	if err != nil {
		t.Fatal(err)
	}

471 472
	if wrote != 2500 {
		t.Fatal("incorrect write amount")
473 474
	}

475
	_, err = dagmod.Seek(0, os.SEEK_SET)
476 477 478 479
	if err != nil {
		t.Fatal(err)
	}

480
	out, err := ioutil.ReadAll(dagmod)
481 482 483 484
	if err != nil {
		t.Fatal(err)
	}

485
	if err = arrComp(out, buf); err != nil {
486 487 488 489
		t.Fatal(err)
	}
}

Jeromy's avatar
Jeromy committed
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
func TestSeekPastEndWrite(t *testing.T) {
	dserv := getMockDagServ(t)
	_, n := getNode(t, dserv, 0)
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	dagmod, err := NewDagModifier(ctx, n, dserv, sizeSplitterGen(512))
	if err != nil {
		t.Fatal(err)
	}

	buf := make([]byte, 5000)
	u.NewTimeSeededRand().Read(buf[2500:])

	nseek, err := dagmod.Seek(2500, os.SEEK_SET)
	if err != nil {
		t.Fatal(err)
	}

	if nseek != 2500 {
		t.Fatal("failed to seek")
	}

	wrote, err := dagmod.Write(buf[2500:])
	if err != nil {
		t.Fatal(err)
	}

	if wrote != 2500 {
		t.Fatal("incorrect write amount")
	}

	_, err = dagmod.Seek(0, os.SEEK_SET)
	if err != nil {
		t.Fatal(err)
	}

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

	if err = arrComp(out, buf); err != nil {
		t.Fatal(err)
	}
}

Jeromy's avatar
Jeromy committed
537 538
func BenchmarkDagmodWrite(b *testing.B) {
	b.StopTimer()
539 540
	dserv := getMockDagServ(b)
	_, n := getNode(b, dserv, 0)
Jeromy's avatar
Jeromy committed
541 542 543 544 545
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	wrsize := 4096

546
	dagmod, err := NewDagModifier(ctx, n, dserv, sizeSplitterGen(512))
Jeromy's avatar
Jeromy committed
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
	if err != nil {
		b.Fatal(err)
	}

	buf := make([]byte, b.N*wrsize)
	u.NewTimeSeededRand().Read(buf)
	b.StartTimer()
	b.SetBytes(int64(wrsize))
	for i := 0; i < b.N; i++ {
		n, err := dagmod.Write(buf[i*wrsize : (i+1)*wrsize])
		if err != nil {
			b.Fatal(err)
		}
		if n != wrsize {
			b.Fatal("Wrote bad size")
		}
	}
}

566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
func arrComp(a, b []byte) error {
	if len(a) != len(b) {
		return fmt.Errorf("Arrays differ in length. %d != %d", len(a), len(b))
	}
	for i, v := range a {
		if v != b[i] {
			return fmt.Errorf("Arrays differ at index: %d", i)
		}
	}
	return nil
}

func printDag(nd *mdag.Node, ds mdag.DAGService, indent int) {
	pbd, err := ft.FromBytes(nd.Data)
	if err != nil {
		panic(err)
	}

	for i := 0; i < indent; i++ {
		fmt.Print(" ")
	}
	fmt.Printf("{size = %d, type = %s, children = %d", pbd.GetFilesize(), pbd.GetType().String(), len(pbd.GetBlocksizes()))
	if len(nd.Links) > 0 {
		fmt.Println()
	}
	for _, lnk := range nd.Links {
Jeromy's avatar
Jeromy committed
592
		child, err := lnk.GetNode(context.Background(), ds)
593 594 595 596 597 598 599 600 601 602 603 604
		if err != nil {
			panic(err)
		}
		printDag(child, ds, indent+1)
	}
	if len(nd.Links) > 0 {
		for i := 0; i < indent; i++ {
			fmt.Print(" ")
		}
	}
	fmt.Println("}")
}