@cryptotaxi247 / kubo / commits / 02f6bd63b

test: add basic dagreader test

License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>

Jakub Sztandera committed Aug 18, 2016 at 21:09 UTC 02f6bd63baf4a8966932f641a63967c1a56d0343
1 file changed +90
unixfs/io/dagserv_test.go new
+90
@@ -0,0 +1,90 @@
1 +package io
2 +
3 +import (
4 + "bytes"
5 + "fmt"
6 + "io"
7 + "io/ioutil"
8 + "testing"
9 +
10 + "github.com/ipfs/go-ipfs/blocks/blockstore"
11 + bs "github.com/ipfs/go-ipfs/blockservice"
12 + "github.com/ipfs/go-ipfs/exchange/offline"
13 + imp "github.com/ipfs/go-ipfs/importer"
14 + "github.com/ipfs/go-ipfs/importer/chunk"
15 + mdag "github.com/ipfs/go-ipfs/merkledag"
16 +
17 + ds "gx/ipfs/QmTxLSvdhwg68WJimdS6icLPhZi28aTp6b7uihC2Yb47Xk/go-datastore"
18 + "gx/ipfs/QmTxLSvdhwg68WJimdS6icLPhZi28aTp6b7uihC2Yb47Xk/go-datastore/sync"
19 + u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
20 + context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
21 +)
22 +
23 +func getMockDagServ(t testing.TB) mdag.DAGService {
24 + dstore := ds.NewMapDatastore()
25 + tsds := sync.MutexWrap(dstore)
26 + bstore := blockstore.NewBlockstore(tsds)
27 + bserv := bs.New(bstore, offline.Exchange(bstore))
28 + return mdag.NewDAGService(bserv)
29 +}
30 +
31 +func sizeSplitterGen(size int64) chunk.SplitterGen {
32 + return func(r io.Reader) chunk.Splitter {
33 + return chunk.NewSizeSplitter(r, size)
34 + }
35 +}
36 +
37 +func getNode(t testing.TB, dserv mdag.DAGService, data []byte) *mdag.Node {
38 + in := bytes.NewReader(data)
39 + node, err := imp.BuildTrickleDagFromReader(dserv, sizeSplitterGen(500)(in))
40 + if err != nil {
41 + t.Fatal(err)
42 + }
43 +
44 + return node
45 +}
46 +
47 +func getRandomNode(t testing.TB, dserv mdag.DAGService, size int64) ([]byte, *mdag.Node) {
48 + in := io.LimitReader(u.NewTimeSeededRand(), size)
49 + buf, err := ioutil.ReadAll(in)
50 + if err != nil {
51 + t.Fatal(err)
52 + }
53 +
54 + node := getNode(t, dserv, buf)
55 + return buf, node
56 +}
57 +
58 +func arrComp(a, b []byte) error {
59 + if len(a) != len(b) {
60 + return fmt.Errorf("Arrays differ in length. %d != %d", len(a), len(b))
61 + }
62 + for i, v := range a {
63 + if v != b[i] {
64 + return fmt.Errorf("Arrays differ at index: %d", i)
65 + }
66 + }
67 + return nil
68 +}
69 +
70 +func TestBasicRead(t *testing.T) {
71 + dserv := getMockDagServ(t)
72 + inbuf, node := getRandomNode(t, dserv, 1024)
73 + ctx, closer := context.WithCancel(context.Background())
74 + defer closer()
75 +
76 + reader, err := NewDagReader(ctx, node, dserv)
77 + if err != nil {
78 + t.Fatal(err)
79 + }
80 +
81 + outbuf, err := ioutil.ReadAll(reader)
82 + if err != nil {
83 + t.Fatal(err)
84 + }
85 +
86 + err = arrComp(inbuf, outbuf)
87 + if err != nil {
88 + t.Fatal(err)
89 + }
90 +}