importer_test.go 1.07 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
package importer

import (
	"bytes"
	"crypto/rand"
	"io"
	"io/ioutil"
	"testing"

	dag "github.com/jbenet/go-ipfs/merkledag"
)

func TestFileConsistency(t *testing.T) {
	buf := new(bytes.Buffer)
	io.CopyN(buf, rand.Reader, 512*32)
	should := buf.Bytes()
	nd, err := NewDagFromReaderWithSplitter(buf, SplitterBySize(512))
	if err != nil {
		t.Fatal(err)
	}
21
	r, err := dag.NewDagReader(nd, nil)
22 23 24 25 26 27 28 29 30 31 32 33 34
	if err != nil {
		t.Fatal(err)
	}

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

	if !bytes.Equal(out, should) {
		t.Fatal("Output not the same as input.")
	}
}
35 36 37 38 39 40 41 42 43 44

//Test where calls to read are smaller than the chunk size
func TestFileConsistencyLargeBlocks(t *testing.T) {
	buf := new(bytes.Buffer)
	io.CopyN(buf, rand.Reader, 4096*32)
	should := buf.Bytes()
	nd, err := NewDagFromReaderWithSplitter(buf, SplitterBySize(4096))
	if err != nil {
		t.Fatal(err)
	}
45
	r, err := dag.NewDagReader(nd, nil)
46 47 48 49 50 51 52 53 54 55 56 57 58
	if err != nil {
		t.Fatal(err)
	}

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

	if !bytes.Equal(out, should) {
		t.Fatal("Output not the same as input.")
	}
}