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

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

13
	key "github.com/ipfs/go-ipfs/blocks/key"
14
	bserv "github.com/ipfs/go-ipfs/blockservice"
15
	bstest "github.com/ipfs/go-ipfs/blockservice/test"
16 17 18 19
	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"
20
	mdpb "github.com/ipfs/go-ipfs/merkledag/pb"
21
	dstest "github.com/ipfs/go-ipfs/merkledag/test"
22
	uio "github.com/ipfs/go-ipfs/unixfs/io"
23
	u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
Jeromy's avatar
Jeromy committed
24
	"gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
25 26 27 28
)

func TestNode(t *testing.T) {

29 30 31
	n1 := NodeWithData([]byte("beep"))
	n2 := NodeWithData([]byte("boop"))
	n3 := NodeWithData([]byte("beep boop"))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
32 33 34 35 36 37 38 39 40
	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)
41
		fmt.Println("data:", string(n.Data()))
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
42 43 44 45 46 47

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

48
		e, err := n.EncodeProtobuf(false)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
49 50 51 52 53 54 55 56 57 58 59 60
		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
61 62 63 64

		k, err := n.Key()
		if err != nil {
			t.Error(err)
65
		} else if k != key.Key(h) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
66 67 68 69
			t.Error("Key is not equivalent to multihash")
		} else {
			fmt.Println("key: ", k)
		}
70 71

		SubtestNodeStat(t, n)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
72 73 74 75 76 77
	}

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

79
func SubtestNodeStat(t *testing.T, n *Node) {
80
	enc, err := n.EncodeProtobuf(true)
81
	if err != nil {
82
		t.Error("n.EncodeProtobuf(true) failed")
83 84 85 86 87 88 89 90 91
		return
	}

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

92 93 94 95 96 97
	k, err := n.Key()
	if err != nil {
		t.Error("n.Key() failed")
		return
	}

98 99 100
	expected := NodeStat{
		NumLinks:       len(n.Links),
		BlockSize:      len(enc),
101 102
		LinksSize:      len(enc) - len(n.Data()), // includes framing.
		DataSize:       len(n.Data()),
103
		CumulativeSize: int(cumSize),
104
		Hash:           k.B58String(),
105 106 107 108 109 110 111 112
	}

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

113
	if expected != *actual {
114
		t.Errorf("n.Stat incorrect.\nexpect: %s\nactual: %s", expected, actual)
115 116 117 118 119
	} else {
		fmt.Printf("n.Stat correct: %s\n", actual)
	}
}

120 121 122
type devZero struct{}

func (_ devZero) Read(b []byte) (int, error) {
rht's avatar
rht committed
123
	for i := range b {
124 125 126 127 128
		b[i] = 0
	}
	return len(b), nil
}

129
func TestBatchFetch(t *testing.T) {
Jeromy's avatar
Jeromy committed
130 131
	read := io.LimitReader(u.NewTimeSeededRand(), 1024*32)
	runBatchFetchTest(t, read)
132
}
133 134

func TestBatchFetchDupBlock(t *testing.T) {
Jeromy's avatar
Jeromy committed
135 136
	read := io.LimitReader(devZero{}, 1024*32)
	runBatchFetchTest(t, read)
137 138
}

Jeromy's avatar
Jeromy committed
139
func runBatchFetchTest(t *testing.T, read io.Reader) {
140
	ctx := context.Background()
141
	var dagservs []DAGService
142
	for _, bsi := range bstest.Mocks(5) {
143 144
		dagservs = append(dagservs, NewDAGService(bsi))
	}
Jeromy's avatar
Jeromy committed
145

146
	spl := chunk.NewSizeSplitter(read, 512)
Jeromy's avatar
Jeromy committed
147

Jeromy's avatar
Jeromy committed
148
	root, err := imp.BuildDagFromReader(dagservs[0], spl)
Jeromy's avatar
Jeromy committed
149 150 151 152
	if err != nil {
		t.Fatal(err)
	}

153 154
	t.Log("finished setup.")

155
	dagr, err := uio.NewDagReader(ctx, root, dagservs[0])
156 157 158
	if err != nil {
		t.Fatal(err)
	}
Jeromy's avatar
Jeromy committed
159 160

	expected, err := ioutil.ReadAll(dagr)
161 162 163 164
	if err != nil {
		t.Fatal(err)
	}

165
	_, err = dagservs[0].Add(root)
166 167 168 169 170 171 172 173 174 175 176
	if err != nil {
		t.Fatal(err)
	}

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

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

177
	wg := sync.WaitGroup{}
178 179
	errs := make(chan error)

180
	for i := 1; i < len(dagservs); i++ {
181
		wg.Add(1)
182
		go func(i int) {
183
			defer wg.Done()
184
			first, err := dagservs[i].Get(ctx, k)
185
			if err != nil {
186
				errs <- err
187 188 189
			}
			fmt.Println("Got first node back.")

190
			read, err := uio.NewDagReader(ctx, first, dagservs[i])
191
			if err != nil {
192
				errs <- err
193 194 195
			}
			datagot, err := ioutil.ReadAll(read)
			if err != nil {
196
				errs <- err
197 198 199
			}

			if !bytes.Equal(datagot, expected) {
200
				errs <- errors.New("Got bad data back!")
201 202 203 204
			}
		}(i)
	}

205 206 207 208 209 210 211 212 213 214
	go func() {
		wg.Wait()
		close(errs)
	}()

	for err := range errs {
		if err != nil {
			t.Fatal(err)
		}
	}
215
}
216 217 218 219 220 221 222

func assertCanGet(t *testing.T, ds DAGService, n *Node) {
	k, err := n.Key()
	if err != nil {
		t.Fatal(err)
	}

223
	if _, err := ds.Get(context.Background(), k); err != nil {
224 225 226 227
		t.Fatal(err)
	}
}

jbenet's avatar
jbenet committed
228
func TestEmptyKey(t *testing.T) {
Juan Benet's avatar
Juan Benet committed
229
	ds := dstest.Mock()
jbenet's avatar
jbenet committed
230 231 232 233 234 235
	_, err := ds.Get(context.Background(), key.Key(""))
	if err != ErrNotFound {
		t.Error("dag service should error when key is nil", err)
	}
}

236
func TestCantGet(t *testing.T) {
237
	ds := dstest.Mock()
238
	a := NodeWithData([]byte("A"))
239 240 241 242 243 244

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

245
	_, err = ds.Get(context.Background(), k)
246 247 248 249
	if !strings.Contains(err.Error(), "not found") {
		t.Fatal("expected err not found, got: ", err)
	}
}
250 251

func TestFetchGraph(t *testing.T) {
Jeromy's avatar
Jeromy committed
252
	var dservs []DAGService
253
	bsis := bstest.Mocks(2)
Jeromy's avatar
Jeromy committed
254 255 256
	for _, bsi := range bsis {
		dservs = append(dservs, NewDAGService(bsi))
	}
257 258

	read := io.LimitReader(u.NewTimeSeededRand(), 1024*32)
Jeromy's avatar
Jeromy committed
259
	root, err := imp.BuildDagFromReader(dservs[0], chunk.NewSizeSplitter(read, 512))
260 261 262 263
	if err != nil {
		t.Fatal(err)
	}

Jeromy's avatar
Jeromy committed
264
	err = FetchGraph(context.TODO(), root, dservs[1])
265 266 267 268
	if err != nil {
		t.Fatal(err)
	}

Jeromy's avatar
Jeromy committed
269
	// create an offline dagstore and ensure all blocks were fetched
270
	bs := bserv.New(bsis[1].Blockstore, offline.Exchange(bsis[1].Blockstore))
271

Jeromy's avatar
Jeromy committed
272 273 274
	offline_ds := NewDAGService(bs)
	ks := key.NewKeySet()

275
	err = EnumerateChildren(context.Background(), offline_ds, root, ks, false)
276 277 278 279 280 281
	if err != nil {
		t.Fatal(err)
	}
}

func TestEnumerateChildren(t *testing.T) {
282
	bsi := bstest.Mocks(1)
283 284 285
	ds := NewDAGService(bsi[0])

	read := io.LimitReader(u.NewTimeSeededRand(), 1024*1024)
Jeromy's avatar
Jeromy committed
286
	root, err := imp.BuildDagFromReader(ds, chunk.NewSizeSplitter(read, 512))
287 288 289 290 291
	if err != nil {
		t.Fatal(err)
	}

	ks := key.NewKeySet()
292
	err = EnumerateChildren(context.Background(), ds, root, ks, false)
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)
	}

	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)
}
315 316 317 318 319 320 321

func TestFetchFailure(t *testing.T) {
	ds := dstest.Mock()
	ds_bad := dstest.Mock()

	top := new(Node)
	for i := 0; i < 10; i++ {
322
		nd := NodeWithData([]byte{byte('a' + i)})
323 324 325 326 327 328 329 330 331 332 333 334
		_, err := ds.Add(nd)
		if err != nil {
			t.Fatal(err)
		}

		err = top.AddNodeLinkClean(fmt.Sprintf("AA%d", i), nd)
		if err != nil {
			t.Fatal(err)
		}
	}

	for i := 0; i < 10; i++ {
335
		nd := NodeWithData([]byte{'f', 'a' + byte(i)})
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
		_, err := ds_bad.Add(nd)
		if err != nil {
			t.Fatal(err)
		}

		err = top.AddNodeLinkClean(fmt.Sprintf("BB%d", i), nd)
		if err != nil {
			t.Fatal(err)
		}
	}

	getters := GetDAG(context.Background(), ds, top)
	for i, getter := range getters {
		_, err := getter.Get(context.Background())
		if err != nil && i < 10 {
			t.Fatal(err)
		}
		if err == nil && i >= 10 {
			t.Fatal("should have failed request")
		}
	}
}
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381

func TestUnmarshalFailure(t *testing.T) {
	badData := []byte("hello world")

	_, err := DecodeProtobuf(badData)
	if err == nil {
		t.Fatal("shouldnt succeed to parse this")
	}

	// now with a bad link
	pbn := &mdpb.PBNode{Links: []*mdpb.PBLink{{Hash: []byte("not a multihash")}}}
	badlink, err := pbn.Marshal()
	if err != nil {
		t.Fatal(err)
	}

	_, err = DecodeProtobuf(badlink)
	if err == nil {
		t.Fatal("should have failed to parse node with bad link")
	}

	n := &Node{}
	n.Marshal()
}