func TestCallbacksWork(t *testing.T) { fake := new(FakeStream) var sent int64 var recv int64 sentCB := func(n int64, proto protocol.ID, p peer.ID) { sent += n } recvCB := func(n int64, proto protocol.ID, p peer.ID) { recv += n } ms := newMeteredStream(fake, protocol.ID("TEST"), peer.ID("PEER"), recvCB, sentCB) toWrite := int64(100000) toRead := int64(100000) fake.ReadBuf = io.LimitReader(u.NewTimeSeededRand(), toRead) writeData := io.LimitReader(u.NewTimeSeededRand(), toWrite) n, err := io.Copy(ms, writeData) if err != nil { t.Fatal(err) } if n != toWrite { t.Fatal("incorrect write amount") } if toWrite != sent { t.Fatal("incorrectly reported writes", toWrite, sent) } n, err = io.Copy(ioutil.Discard, ms) if err != nil { t.Fatal(err) } if n != toRead { t.Fatal("incorrect read amount") } if toRead != recv { t.Fatal("incorrectly reported reads") } }
func TestBalancedDag(t *testing.T) { ds := mdtest.Mock() buf := make([]byte, 10000) u.NewTimeSeededRand().Read(buf) r := bytes.NewReader(buf) nd, err := BuildDagFromReader(ds, chunk.DefaultSplitter(r), nil) if err != nil { t.Fatal(err) } dr, err := uio.NewDagReader(context.TODO(), nd, ds) if err != nil { t.Fatal(err) } out, err := ioutil.ReadAll(dr) if err != nil { t.Fatal(err) } if !bytes.Equal(out, buf) { t.Fatal("bad read") } }
func randNode() (*mdag.Node, key.Key) { nd := new(mdag.Node) nd.Data = make([]byte, 32) util.NewTimeSeededRand().Read(nd.Data) k, _ := nd.Key() return nd, k }
func randBuf(t *testing.T, size int) []byte { buf := make([]byte, size) if _, err := u.NewTimeSeededRand().Read(buf); err != nil { t.Fatal("failed to read enough randomness") } return buf }
func TestSeekEndSingleBlockFile(t *testing.T) { nbytes := int64(100) should := make([]byte, nbytes) u.NewTimeSeededRand().Read(should) read := bytes.NewReader(should) ds := mdtest.Mock(t) nd, err := buildTestDag(read, ds, &chunk.SizeSplitter{5000}) if err != nil { t.Fatal(err) } rs, err := uio.NewDagReader(context.Background(), nd, ds) if err != nil { t.Fatal(err) } seeked, err := rs.Seek(0, os.SEEK_END) if err != nil { t.Fatal(err) } if seeked != nbytes { t.Fatal("Failed to seek to end") } }
func TestRabinChunking(t *testing.T) { data := make([]byte, 1024*1024*16) util.NewTimeSeededRand().Read(data) r := NewRabin(bytes.NewReader(data), 1024*256) var chunks [][]byte for { chunk, err := r.NextBytes() if err != nil { if err == io.EOF { break } t.Fatal(err) } chunks = append(chunks, chunk) } fmt.Printf("average block size: %d\n", len(data)/len(chunks)) unchunked := bytes.Join(chunks, nil) if !bytes.Equal(unchunked, data) { fmt.Printf("%d %d\n", len(unchunked), len(data)) t.Fatal("data was chunked incorrectly") } }
func TestBuilderConsistency(t *testing.T) { nbytes := 100000 buf := new(bytes.Buffer) io.CopyN(buf, u.NewTimeSeededRand(), int64(nbytes)) should := dup(buf.Bytes()) dagserv := mdtest.Mock(t) nd, err := buildTestDag(buf, dagserv, chunk.DefaultSplitter) if err != nil { t.Fatal(err) } r, err := uio.NewDagReader(context.Background(), nd, dagserv) if err != nil { t.Fatal(err) } out, err := ioutil.ReadAll(r) if err != nil { t.Fatal(err) } err = arrComp(out, should) if err != nil { t.Fatal(err) } }
func testFileConsistency(t *testing.T, bs chunk.BlockSplitter, nbytes int) { should := make([]byte, nbytes) u.NewTimeSeededRand().Read(should) read := bytes.NewReader(should) ds := mdtest.Mock(t) nd, err := buildTestDag(read, ds, bs) if err != nil { t.Fatal(err) } r, err := uio.NewDagReader(context.Background(), nd, ds) if err != nil { t.Fatal(err) } out, err := ioutil.ReadAll(r) if err != nil { t.Fatal(err) } err = arrComp(out, should) if err != nil { t.Fatal(err) } }
func BenchmarkDagmodWrite(b *testing.B) { b.StopTimer() dserv, pins := getMockDagServ(b) _, n := getNode(b, dserv, 0, pins) ctx, cancel := context.WithCancel(context.Background()) defer cancel() wrsize := 4096 dagmod, err := NewDagModifier(ctx, n, dserv, pins, sizeSplitterGen(512)) 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") } } }
func randNode() (*merkledag.Node, key.Key) { node := new(merkledag.Node) node.Data = make([]byte, 32) util.NewTimeSeededRand().Read(node.Data) k, _ := node.Key() return node, k }
func TestIndirectBlocks(t *testing.T) { splitter := &chunk.SizeSplitter{512} nbytes := 1024 * 1024 buf := make([]byte, nbytes) u.NewTimeSeededRand().Read(buf) read := bytes.NewReader(buf) ds := mdtest.Mock(t) dag, err := buildTestDag(read, ds, splitter) if err != nil { t.Fatal(err) } reader, err := uio.NewDagReader(context.Background(), dag, ds) if err != nil { t.Fatal(err) } out, err := ioutil.ReadAll(reader) if err != nil { t.Fatal(err) } if !bytes.Equal(out, buf) { t.Fatal("Not equal!") } }
func TestEnumerateChildren(t *testing.T) { bsi := bstest.Mocks(1) ds := NewDAGService(bsi[0]) read := io.LimitReader(u.NewTimeSeededRand(), 1024*1024) root, err := imp.BuildDagFromReader(ds, chunk.NewSizeSplitter(read, 512)) if err != nil { t.Fatal(err) } ks := key.NewKeySet() err = EnumerateChildren(context.Background(), ds, root, ks) if err != nil { t.Fatal(err) } var traverse func(n *Node) traverse = func(n *Node) { // traverse dag and check for _, lnk := range n.Links { k := key.Key(lnk.Hash) if !ks.Has(k) { t.Fatal("missing key in set!") } child, err := ds.Get(context.Background(), k) if err != nil { t.Fatal(err) } traverse(child) } } traverse(root) }
func TestFetchGraph(t *testing.T) { var dservs []DAGService bsis := bstest.Mocks(2) for _, bsi := range bsis { dservs = append(dservs, NewDAGService(bsi)) } read := io.LimitReader(u.NewTimeSeededRand(), 1024*32) root, err := imp.BuildDagFromReader(dservs[0], chunk.NewSizeSplitter(read, 512)) if err != nil { t.Fatal(err) } err = FetchGraph(context.TODO(), root, dservs[1]) if err != nil { t.Fatal(err) } // create an offline dagstore and ensure all blocks were fetched bs := bserv.New(bsis[1].Blockstore, offline.Exchange(bsis[1].Blockstore)) offline_ds := NewDAGService(bs) ks := key.NewKeySet() err = EnumerateChildren(context.Background(), offline_ds, root, ks) if err != nil { t.Fatal(err) } }
func RandTestBogusPrivateKey() (TestBogusPrivateKey, error) { r := u.NewTimeSeededRand() k := make([]byte, 5) if _, err := io.ReadFull(r, k); err != nil { return nil, err } return TestBogusPrivateKey(k), nil }
func getRandFile(t *testing.T, ds dag.DAGService, size int64) *dag.Node { r := io.LimitReader(u.NewTimeSeededRand(), size) nd, err := importer.BuildDagFromReader(ds, chunk.DefaultSplitter(r)) if err != nil { t.Fatal(err) } return nd }
// RandPeerID generates random "valid" peer IDs. it does not NEED to generate // keys because it is as if we lost the key right away. fine to read randomness // and hash it. to generate proper keys and corresponding PeerID, use: // sk, pk, _ := testutil.RandKeyPair() // id, _ := peer.IDFromPublicKey(pk) func RandPeerID() (peer.ID, error) { buf := make([]byte, 16) if _, err := io.ReadFull(u.NewTimeSeededRand(), buf); err != nil { return "", err } h := u.Hash(buf) return peer.ID(h), nil }
func getBalancedDag(t testing.TB, size int64, blksize int) (*dag.Node, dag.DAGService) { ds := mdtest.Mock(t) r := io.LimitReader(u.NewTimeSeededRand(), size) nd, err := BuildDagFromReader(r, ds, &chunk.SizeSplitter{blksize}, nil) if err != nil { t.Fatal(err) } return nd, ds }
func getTrickleDag(t testing.TB, size int64, blksize int64) (*dag.Node, dag.DAGService) { ds := mdtest.Mock() r := io.LimitReader(u.NewTimeSeededRand(), size) nd, err := BuildTrickleDagFromReader(ds, chunk.NewSizeSplitter(r, blksize), nil) if err != nil { t.Fatal(err) } return nd, ds }
func TestMetadata(t *testing.T) { ctx := context.Background() // Make some random node ds := getDagserv(t) data := make([]byte, 1000) u.NewTimeSeededRand().Read(data) r := bytes.NewReader(data) nd, err := importer.BuildDagFromReader(ds, chunk.DefaultSplitter(r)) if err != nil { t.Fatal(err) } k, err := nd.Key() if err != nil { t.Fatal(err) } m := new(ft.Metadata) m.MimeType = "THIS IS A TEST" // Such effort, many compromise ipfsnode := &core.IpfsNode{DAG: ds} mdk, err := AddMetadataTo(ipfsnode, k.B58String(), m) if err != nil { t.Fatal(err) } rec, err := Metadata(ipfsnode, mdk) if err != nil { t.Fatal(err) } if rec.MimeType != m.MimeType { t.Fatalf("something went wrong in conversion: '%s' != '%s'", rec.MimeType, m.MimeType) } retnode, err := ds.Get(ctx, key.B58KeyDecode(mdk)) if err != nil { t.Fatal(err) } ndr, err := uio.NewDagReader(ctx, retnode, ds) if err != nil { t.Fatal(err) } out, err := ioutil.ReadAll(ndr) if err != nil { t.Fatal(err) } if !bytes.Equal(out, data) { t.Fatal("read incorrect data") } }
func makeRandomObject() (string, error) { // do some math to make a size x := rand.Intn(120) + 1 y := rand.Intn(120) + 1 z := rand.Intn(120) + 1 size := x * y * z r := io.LimitReader(u.NewTimeSeededRand(), int64(size)) sleep() return sh.Add(r) }
func randObj(t *testing.T, nd *core.IpfsNode, size int64) (*dag.Node, []byte) { buf := make([]byte, size) u.NewTimeSeededRand().Read(buf) read := bytes.NewReader(buf) obj, err := importer.BuildTrickleDagFromReader(nd.DAG, chunk.DefaultSplitter(read), nil) if err != nil { t.Fatal(err) } return obj, buf }
func getTestDag(t *testing.T, ds dag.DAGService, size int64, blksize int64) (*dag.Node, []byte) { data := make([]byte, size) u.NewTimeSeededRand().Read(data) r := bytes.NewReader(data) nd, err := buildTestDag(ds, chunk.NewSizeSplitter(r, blksize)) if err != nil { t.Fatal(err) } return nd, data }
func TestMultiWrite(t *testing.T) { dserv, pins := getMockDagServ(t) _, n := getNode(t, dserv, 0, pins) ctx, cancel := context.WithCancel(context.Background()) defer cancel() dagmod, err := NewDagModifier(ctx, n, dserv, pins, sizeSplitterGen(512)) 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)") } size, err := dagmod.Size() if err != nil { t.Fatal(err) } if size != int64(i+1) { t.Fatal("Size was reported incorrectly") } } 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) } }
// This test appends one byte at a time to an empty file func TestMultipleAppends(t *testing.T) { ds := mdtest.Mock() // TODO: fix small size appends and make this number bigger nbytes := int64(1000) should := make([]byte, nbytes) u.NewTimeSeededRand().Read(should) read := bytes.NewReader(nil) nd, err := buildTestDag(ds, chunk.NewSizeSplitter(read, 500)) if err != nil { t.Fatal(err) } dbp := &h.DagBuilderParams{ Dagserv: ds, Maxlinks: 4, } spl := chunk.SizeSplitterGen(500) ctx := context.Background() for i := 0; i < len(should); i++ { blks, errs := chunk.Chan(spl(bytes.NewReader(should[i : i+1]))) nnode, err := TrickleAppend(ctx, nd, dbp.New(blks, errs)) if err != nil { t.Fatal(err) } err = VerifyTrickleDagStructure(nnode, ds, dbp.Maxlinks, layerRepeat) if err != nil { t.Fatal(err) } fread, err := uio.NewDagReader(ctx, nnode, ds) if err != nil { t.Fatal(err) } out, err := ioutil.ReadAll(fread) if err != nil { t.Fatal(err) } err = arrComp(out, should[:i+1]) if err != nil { t.Fatal(err) } } }
func TestAppend(t *testing.T) { nbytes := int64(128 * 1024) should := make([]byte, nbytes) u.NewTimeSeededRand().Read(should) // Reader for half the bytes read := bytes.NewReader(should[:nbytes/2]) ds := mdtest.Mock() nd, err := buildTestDag(ds, chunk.NewSizeSplitter(read, 500)) if err != nil { t.Fatal(err) } dbp := &h.DagBuilderParams{ Dagserv: ds, Maxlinks: h.DefaultLinksPerBlock, } r := bytes.NewReader(should[nbytes/2:]) blks, errs := chunk.Chan(chunk.NewSizeSplitter(r, 500)) ctx := context.Background() nnode, err := TrickleAppend(ctx, nd, dbp.New(blks, errs)) if err != nil { t.Fatal(err) } err = VerifyTrickleDagStructure(nnode, ds, dbp.Maxlinks, layerRepeat) if err != nil { t.Fatal(err) } fread, err := uio.NewDagReader(ctx, nnode, ds) if err != nil { t.Fatal(err) } out, err := ioutil.ReadAll(fread) if err != nil { t.Fatal(err) } err = arrComp(out, should) if err != nil { t.Fatal(err) } }
func newSender() (chan sendChans, func(s inet.Stream)) { scc := make(chan sendChans) return scc, func(s inet.Stream) { sc := newSendChans() scc <- sc defer func() { s.Close() sc.closed <- struct{}{} }() buf := make([]byte, 65536) buf2 := make([]byte, 65536) u.NewTimeSeededRand().Read(buf) for { select { case <-sc.close_: return case <-sc.send: } // send a randomly sized subchunk from := rand.Intn(len(buf) / 2) to := rand.Intn(len(buf) / 2) sendbuf := buf[from : from+to] log.Debugf("sender sending %d bytes", len(sendbuf)) n, err := s.Write(sendbuf) if err != nil { log.Debug("sender error. exiting:", err) return } log.Debugf("sender wrote %d bytes", n) sc.sent <- struct{}{} if n, err = io.ReadFull(s, buf2[:len(sendbuf)]); err != nil { log.Debug("sender error. failed to read:", err) return } log.Debugf("sender read %d bytes", n) sc.read <- struct{}{} } } }
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) } }
func TestAppend(t *testing.T) { nbytes := int64(128 * 1024) should := make([]byte, nbytes) u.NewTimeSeededRand().Read(should) // Reader for half the bytes read := bytes.NewReader(should[:nbytes/2]) ds := mdtest.Mock(t) nd, err := buildTestDag(read, ds, &chunk.SizeSplitter{500}) if err != nil { t.Fatal(err) } dbp := &h.DagBuilderParams{ Dagserv: ds, Maxlinks: h.DefaultLinksPerBlock, } spl := &chunk.SizeSplitter{500} blks := spl.Split(bytes.NewReader(should[nbytes/2:])) nnode, err := TrickleAppend(nd, dbp.New(blks)) if err != nil { t.Fatal(err) } err = VerifyTrickleDagStructure(nnode, ds, dbp.Maxlinks, layerRepeat) if err != nil { t.Fatal(err) } fread, err := uio.NewDagReader(context.TODO(), nnode, ds) if err != nil { t.Fatal(err) } out, err := ioutil.ReadAll(fread) if err != nil { t.Fatal(err) } err = arrComp(out, should) if err != nil { t.Fatal(err) } }
func TestMultiWriteCoal(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) } 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 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 }