merkledag_test.go 4.38 KB
Newer Older
1
package merkledag_test
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
2 3

import (
4
	"bytes"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
5
	"fmt"
6 7
	"io"
	"io/ioutil"
8
	"sync"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
9
	"testing"
10

11 12 13 14 15 16 17 18 19 20 21 22 23
	ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
	dssync "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
	"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
	bstore "github.com/ipfs/go-ipfs/blocks/blockstore"
	blockservice "github.com/ipfs/go-ipfs/blockservice"
	bserv "github.com/ipfs/go-ipfs/blockservice"
	offline "github.com/ipfs/go-ipfs/exchange/offline"
	imp "github.com/ipfs/go-ipfs/importer"
	chunk "github.com/ipfs/go-ipfs/importer/chunk"
	. "github.com/ipfs/go-ipfs/merkledag"
	"github.com/ipfs/go-ipfs/pin"
	uio "github.com/ipfs/go-ipfs/unixfs/io"
	u "github.com/ipfs/go-ipfs/util"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
24 25
)

Jeromy's avatar
Jeromy committed
26 27 28 29 30 31
type dagservAndPinner struct {
	ds DAGService
	mp pin.ManualPinner
}

func getDagservAndPinner(t *testing.T) dagservAndPinner {
32 33
	db := dssync.MutexWrap(ds.NewMapDatastore())
	bs := bstore.NewBlockstore(db)
Jeromy's avatar
Jeromy committed
34 35 36 37 38 39 40 41 42 43 44 45
	blockserv, err := bserv.New(bs, offline.Exchange(bs))
	if err != nil {
		t.Fatal(err)
	}
	dserv := NewDAGService(blockserv)
	mpin := pin.NewPinner(db, dserv).GetManual()
	return dagservAndPinner{
		ds: dserv,
		mp: mpin,
	}
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
46 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
func TestNode(t *testing.T) {

	n1 := &Node{Data: []byte("beep")}
	n2 := &Node{Data: []byte("boop")}
	n3 := &Node{Data: []byte("beep boop")}
	if err := n3.AddNodeLink("beep-link", n1); err != nil {
		t.Error(err)
	}
	if err := n3.AddNodeLink("boop-link", n2); err != nil {
		t.Error(err)
	}

	printn := func(name string, n *Node) {
		fmt.Println(">", name)
		fmt.Println("data:", string(n.Data))

		fmt.Println("links:")
		for _, l := range n.Links {
			fmt.Println("-", l.Name, l.Size, l.Hash)
		}

		e, err := n.Encoded(false)
		if err != nil {
			t.Error(err)
		} else {
			fmt.Println("encoded:", e)
		}

		h, err := n.Multihash()
		if err != nil {
			t.Error(err)
		} else {
			fmt.Println("hash:", h)
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
80 81 82 83 84 85 86 87 88

		k, err := n.Key()
		if err != nil {
			t.Error(err)
		} else if k != u.Key(h) {
			t.Error("Key is not equivalent to multihash")
		} else {
			fmt.Println("key: ", k)
		}
89 90

		SubtestNodeStat(t, n)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
91 92 93 94 95 96
	}

	printn("beep", n1)
	printn("boop", n2)
	printn("beep boop", n3)
}
97

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
func SubtestNodeStat(t *testing.T, n *Node) {
	enc, err := n.Encoded(true)
	if err != nil {
		t.Error("n.Encoded(true) failed")
		return
	}

	cumSize, err := n.Size()
	if err != nil {
		t.Error("n.Size() failed")
		return
	}

	expected := NodeStat{
		NumLinks:       len(n.Links),
		BlockSize:      len(enc),
		LinksSize:      len(enc) - len(n.Data), // includes framing.
		DataSize:       len(n.Data),
		CumulativeSize: int(cumSize),
	}

	actual, err := n.Stat()
	if err != nil {
		t.Error("n.Stat() failed")
		return
	}

125
	if expected != *actual {
126 127 128 129 130 131
		t.Error("n.Stat incorrect.\nexpect: %s\nactual: %s", expected, actual)
	} else {
		fmt.Printf("n.Stat correct: %s\n", actual)
	}
}

132 133 134 135 136 137 138 139 140
type devZero struct{}

func (_ devZero) Read(b []byte) (int, error) {
	for i, _ := range b {
		b[i] = 0
	}
	return len(b), nil
}

141
func TestBatchFetch(t *testing.T) {
Jeromy's avatar
Jeromy committed
142 143
	read := io.LimitReader(u.NewTimeSeededRand(), 1024*32)
	runBatchFetchTest(t, read)
144
}
145 146

func TestBatchFetchDupBlock(t *testing.T) {
Jeromy's avatar
Jeromy committed
147 148
	read := io.LimitReader(devZero{}, 1024*32)
	runBatchFetchTest(t, read)
149 150
}

Jeromy's avatar
Jeromy committed
151
func runBatchFetchTest(t *testing.T, read io.Reader) {
152 153 154 155
	var dagservs []DAGService
	for _, bsi := range blockservice.Mocks(t, 5) {
		dagservs = append(dagservs, NewDAGService(bsi))
	}
Jeromy's avatar
Jeromy committed
156 157 158 159 160 161 162 163

	spl := &chunk.SizeSplitter{512}

	root, err := imp.BuildDagFromReader(read, dagservs[0], nil, spl)
	if err != nil {
		t.Fatal(err)
	}

164 165
	t.Log("finished setup.")

Jeromy's avatar
Jeromy committed
166
	dagr, err := uio.NewDagReader(context.TODO(), root, dagservs[0])
167 168 169
	if err != nil {
		t.Fatal(err)
	}
Jeromy's avatar
Jeromy committed
170 171

	expected, err := ioutil.ReadAll(dagr)
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
	if err != nil {
		t.Fatal(err)
	}

	err = dagservs[0].AddRecursive(root)
	if err != nil {
		t.Fatal(err)
	}

	t.Log("Added file to first node.")

	k, err := root.Key()
	if err != nil {
		t.Fatal(err)
	}

188
	wg := sync.WaitGroup{}
189
	for i := 1; i < len(dagservs); i++ {
190
		wg.Add(1)
191
		go func(i int) {
192
			defer wg.Done()
Jeromy's avatar
Jeromy committed
193
			first, err := dagservs[i].Get(context.Background(), k)
194 195 196 197 198
			if err != nil {
				t.Fatal(err)
			}
			fmt.Println("Got first node back.")

Jeromy's avatar
Jeromy committed
199
			read, err := uio.NewDagReader(context.TODO(), first, dagservs[i])
200 201 202 203 204 205 206 207 208 209 210 211 212 213
			if err != nil {
				t.Fatal(err)
			}
			datagot, err := ioutil.ReadAll(read)
			if err != nil {
				t.Fatal(err)
			}

			if !bytes.Equal(datagot, expected) {
				t.Fatal("Got bad data back!")
			}
		}(i)
	}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
214
	wg.Wait()
215
}