dagmodifier_test.go 5.25 KB
Newer Older
1 2 3 4 5 6 7 8
package io

import (
	"fmt"
	"io"
	"io/ioutil"
	"testing"

Jeromy's avatar
Jeromy committed
9
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
10 11
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
	"github.com/jbenet/go-ipfs/blocks/blockstore"
12
	bs "github.com/jbenet/go-ipfs/blockservice"
13
	"github.com/jbenet/go-ipfs/exchange/offline"
14
	imp "github.com/jbenet/go-ipfs/importer"
15 16 17 18 19
	"github.com/jbenet/go-ipfs/importer/chunk"
	mdag "github.com/jbenet/go-ipfs/merkledag"
	ft "github.com/jbenet/go-ipfs/unixfs"
	u "github.com/jbenet/go-ipfs/util"

20
	ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
21
	logging "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-logging"
22 23
)

24
func getMockDagServ(t *testing.T) mdag.DAGService {
25
	dstore := ds.NewMapDatastore()
26 27
	tsds := sync.MutexWrap(dstore)
	bstore := blockstore.NewBlockstore(tsds)
28
	bserv, err := bs.New(bstore, offline.Exchange(bstore))
29 30 31
	if err != nil {
		t.Fatal(err)
	}
32
	return mdag.NewDAGService(bserv)
33 34
}

35
func getNode(t *testing.T, dserv mdag.DAGService, size int64) ([]byte, *mdag.Node) {
36 37
	in := io.LimitReader(u.NewTimeSeededRand(), size)
	node, err := imp.BuildDagFromReader(in, dserv, nil, &chunk.SizeSplitter{500})
38 39 40 41
	if err != nil {
		t.Fatal(err)
	}

Jeromy's avatar
Jeromy committed
42
	dr, err := NewDagReader(context.TODO(), node, dserv)
43 44 45 46 47 48 49 50 51 52 53 54 55 56
	if err != nil {
		t.Fatal(err)
	}

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

	return b, node
}

func testModWrite(t *testing.T, beg, size uint64, orig []byte, dm *DagModifier) []byte {
	newdata := make([]byte, size)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
57
	r := u.NewTimeSeededRand()
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
	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, uint64(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)
	}

Jeromy's avatar
Jeromy committed
79
	rd, err := NewDagReader(context.TODO(), nd, dm.dagserv)
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
	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
}

func TestDagModifierBasic(t *testing.T) {
Jeromy's avatar
Jeromy committed
97
	t.Skip("DAGModifier needs to be fixed to work with indirect blocks.")
98 99 100 101 102
	logging.SetLevel(logging.CRITICAL, "blockservice")
	logging.SetLevel(logging.CRITICAL, "merkledag")
	dserv := getMockDagServ(t)
	b, n := getNode(t, dserv, 50000)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
103
	dagmod, err := NewDagModifier(n, dserv, &chunk.SizeSplitter{Size: 512})
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
	if err != nil {
		t.Fatal(err)
	}

	// Within zero block
	beg := uint64(15)
	length := uint64(60)

	t.Log("Testing mod within zero block")
	b = testModWrite(t, beg, length, b, dagmod)

	// Within bounds of existing file
	beg = 1000
	length = 4000
	t.Log("Testing mod within bounds of existing file.")
	b = testModWrite(t, beg, length, b, dagmod)

	// Extend bounds
	beg = 49500
	length = 4000

	t.Log("Testing mod that extends file.")
	b = testModWrite(t, beg, length, b, dagmod)

	// "Append"
	beg = uint64(len(b))
	length = 3000
	b = testModWrite(t, beg, length, b, dagmod)

	// Verify reported length
	node, err := dagmod.GetNode()
	if err != nil {
		t.Fatal(err)
	}

	size, err := ft.DataSize(node.Data)
	if err != nil {
		t.Fatal(err)
	}

	expected := uint64(50000 + 3500 + 3000)
	if size != expected {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
146
		t.Fatalf("Final reported size is incorrect [%d != %d]", size, expected)
147 148 149
	}
}

150
func TestMultiWrite(t *testing.T) {
Jeromy's avatar
Jeromy committed
151
	t.Skip("DAGModifier needs to be fixed to work with indirect blocks.")
152 153 154
	dserv := getMockDagServ(t)
	_, n := getNode(t, dserv, 0)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
155
	dagmod, err := NewDagModifier(n, dserv, &chunk.SizeSplitter{Size: 512})
156 157 158 159 160
	if err != nil {
		t.Fatal(err)
	}

	data := make([]byte, 4000)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
161
	u.NewTimeSeededRand().Read(data)
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

	for i := 0; i < len(data); i++ {
		n, err := dagmod.WriteAt(data[i:i+1], uint64(i))
		if err != nil {
			t.Fatal(err)
		}
		if n != 1 {
			t.Fatal("Somehow wrote the wrong number of bytes! (n != 1)")
		}
	}
	nd, err := dagmod.GetNode()
	if err != nil {
		t.Fatal(err)
	}

Jeromy's avatar
Jeromy committed
177
	read, err := NewDagReader(context.TODO(), nd, dserv)
178 179 180 181 182 183 184 185 186 187 188 189 190 191
	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)
	}
}

192
func TestMultiWriteCoal(t *testing.T) {
Jeromy's avatar
Jeromy committed
193
	t.Skip("Skipping test until DagModifier is fixed")
194 195 196
	dserv := getMockDagServ(t)
	_, n := getNode(t, dserv, 0)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
197
	dagmod, err := NewDagModifier(n, dserv, &chunk.SizeSplitter{Size: 512})
198 199 200 201 202
	if err != nil {
		t.Fatal(err)
	}

	data := make([]byte, 4000)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
203
	u.NewTimeSeededRand().Read(data)
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218

	for i := 0; i < len(data); i++ {
		n, err := dagmod.WriteAt(data[:i+1], 0)
		if err != nil {
			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)
	}

Jeromy's avatar
Jeromy committed
219
	read, err := NewDagReader(context.TODO(), nd, dserv)
220 221 222 223 224 225 226 227 228 229 230 231 232 233
	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)
	}
}

234 235 236 237 238 239 240 241 242 243 244
func arrComp(a, b []byte) error {
	if len(a) != len(b) {
		return fmt.Errorf("Arrays differ in length. %d != %d", len(a), len(b))
	}
	for i, v := range a {
		if v != b[i] {
			return fmt.Errorf("Arrays differ at index: %d", i)
		}
	}
	return nil
}