ipns_test.go 6.72 KB
Newer Older
1 2 3
package ipns

import (
4
	"bytes"
5
	"crypto/rand"
6
	"io/ioutil"
7
	"os"
8 9
	"runtime"
	"strings"
10
	"testing"
Jeromy's avatar
Jeromy committed
11
	"time"
12 13

	fstest "github.com/jbenet/go-ipfs/Godeps/_workspace/src/bazil.org/fuse/fs/fstestutil"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
14 15
	core "github.com/jbenet/go-ipfs/core"
	u "github.com/jbenet/go-ipfs/util"
16 17
)

18 19 20 21 22 23 24 25 26 27 28
func maybeSkipFuseTests(t *testing.T) bool {
	v := "TEST_NO_FUSE"
	n := strings.ToLower(os.Getenv(v))
	skip := n != "" && n != "false" && n != "f"

	if skip {
		t.Skipf("Skipping FUSE tests (%s=%s)", v, n)
	}
	return skip
}

29 30 31 32 33 34
func randBytes(size int) []byte {
	b := make([]byte, size)
	rand.Read(b)
	return b
}

Jeromy's avatar
Jeromy committed
35
func writeFile(t *testing.T, size int, path string) []byte {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
36 37 38 39
	return writeFileData(t, randBytes(size), path)
}

func writeFileData(t *testing.T, data []byte, path string) []byte {
Jeromy's avatar
Jeromy committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
	fi, err := os.Create(path)
	if err != nil {
		t.Fatal(err)
	}

	n, err := fi.Write(data)
	if err != nil {
		t.Fatal(err)
	}

	if n != len(data) {
		t.Fatal("Didnt write proper amount!")
	}

	err = fi.Close()
55 56 57 58
	if err != nil {
		t.Fatal(err)
	}

Jeromy's avatar
Jeromy committed
59
	return data
Jeromy's avatar
Jeromy committed
60 61 62
}

func setupIpnsTest(t *testing.T, node *core.IpfsNode) (*core.IpfsNode, *fstest.Mount) {
63 64
	maybeSkipFuseTests(t)

Jeromy's avatar
Jeromy committed
65 66 67 68 69 70 71 72 73
	var err error
	if node == nil {
		node, err = core.NewMockNode()
		if err != nil {
			t.Fatal(err)
		}
	}

	fs, err := NewIpns(node, "")
74 75 76 77 78 79 80
	if err != nil {
		t.Fatal(err)
	}
	mnt, err := fstest.MountedT(t, fs)
	if err != nil {
		t.Fatal(err)
	}
Jeromy's avatar
Jeromy committed
81 82 83 84

	return node, mnt
}

85
// Test writing a file and reading it back
Jeromy's avatar
Jeromy committed
86 87
func TestIpnsBasicIO(t *testing.T) {
	_, mnt := setupIpnsTest(t, nil)
88
	defer mnt.Close()
89

Jeromy's avatar
Jeromy committed
90
	fname := mnt.Dir + "/local/testfile"
Jeromy's avatar
Jeromy committed
91
	data := writeFile(t, 12345, fname)
92

Jeromy's avatar
Jeromy committed
93
	rbuf, err := ioutil.ReadFile(fname)
94 95 96 97
	if err != nil {
		t.Fatal(err)
	}

Jeromy's avatar
Jeromy committed
98 99
	if !bytes.Equal(rbuf, data) {
		t.Fatal("Incorrect Read!")
100
	}
Jeromy's avatar
Jeromy committed
101
}
102

103
// Test to make sure file changes persist over mounts of ipns
Jeromy's avatar
Jeromy committed
104 105
func TestFilePersistence(t *testing.T) {
	node, mnt := setupIpnsTest(t, nil)
106

Jeromy's avatar
Jeromy committed
107
	fname := "/local/atestfile"
Jeromy's avatar
Jeromy committed
108
	data := writeFile(t, 127, mnt.Dir+fname)
109

Jeromy's avatar
Jeromy committed
110 111 112 113 114 115 116 117 118
	// Wait for publish: TODO: make publish happen faster in tests
	time.Sleep(time.Millisecond * 40)

	mnt.Close()

	node, mnt = setupIpnsTest(t, node)
	defer mnt.Close()

	rbuf, err := ioutil.ReadFile(mnt.Dir + fname)
119 120 121 122
	if err != nil {
		t.Fatal(err)
	}

123
	if !bytes.Equal(rbuf, data) {
Jeromy's avatar
Jeromy committed
124
		t.Fatalf("File data changed between mounts! sizes differ: %d != %d", len(data), len(rbuf))
125 126
	}
}
Jeromy's avatar
Jeromy committed
127

128
// Test to make sure the filesystem reports file sizes correctly
Jeromy's avatar
Jeromy committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
func TestFileSizeReporting(t *testing.T) {
	_, mnt := setupIpnsTest(t, nil)
	defer mnt.Close()

	fname := mnt.Dir + "/local/sizecheck"
	data := writeFile(t, 5555, fname)

	finfo, err := os.Stat(fname)
	if err != nil {
		t.Fatal(err)
	}

	if finfo.Size() != int64(len(data)) {
		t.Fatal("Read incorrect size from stat!")
	}
}
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200

// Test to make sure you cant create multiple entries with the same name
func TestDoubleEntryFailure(t *testing.T) {
	_, mnt := setupIpnsTest(t, nil)
	defer mnt.Close()

	dname := mnt.Dir + "/local/thisisadir"
	err := os.Mkdir(dname, 0777)
	if err != nil {
		t.Fatal(err)
	}

	err = os.Mkdir(dname, 0777)
	if err == nil {
		t.Fatal("Should have gotten error one creating new directory.")
	}
}

func TestAppendFile(t *testing.T) {
	_, mnt := setupIpnsTest(t, nil)
	defer mnt.Close()

	fname := mnt.Dir + "/local/file"
	data := writeFile(t, 1300, fname)

	fi, err := os.OpenFile(fname, os.O_RDWR|os.O_APPEND, 0666)
	if err != nil {
		t.Fatal(err)
	}

	nudata := randBytes(500)

	n, err := fi.Write(nudata)
	if err != nil {
		t.Fatal(err)
	}
	err = fi.Close()
	if err != nil {
		t.Fatal(err)
	}

	if n != len(nudata) {
		t.Fatal("Failed to write enough bytes.")
	}

	data = append(data, nudata...)

	rbuf, err := ioutil.ReadFile(fname)
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(rbuf, data) {
		t.Fatal("Data inconsistent!")
	}
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
func TestFastRepublish(t *testing.T) {

	// make timeout noticeable.
	osrt := shortRepublishTimeout
	shortRepublishTimeout = time.Millisecond * 100

	olrt := longRepublishTimeout
	longRepublishTimeout = time.Second

	node, mnt := setupIpnsTest(t, nil)

	h, err := node.Identity.PrivKey.GetPublic().Hash()
	if err != nil {
		t.Fatal(err)
	}
	pubkeyHash := u.Key(h).Pretty()

	// set them back
	defer func() {
		shortRepublishTimeout = osrt
		longRepublishTimeout = olrt
		mnt.Close()
	}()

	closed := make(chan struct{})
	dataA := []byte("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
	dataB := []byte("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")

	fname := mnt.Dir + "/local/file"

	// get first resolved hash
	log.Debug("publishing first hash")
	writeFileData(t, dataA, fname) // random
	<-time.After(shortRepublishTimeout * 11 / 10)
	log.Debug("resolving first hash")
	resolvedHash, err := node.Namesys.Resolve(pubkeyHash)
	if err != nil {
		t.Fatal("resolve err:", pubkeyHash, err)
	}

	// constantly keep writing to the file
	go func() {
		for {
			select {
			case <-closed:
				return

			case <-time.After(shortRepublishTimeout * 8 / 10):
				writeFileData(t, dataB, fname)
			}
		}
	}()

	hasPublished := func() bool {
		res, err := node.Namesys.Resolve(pubkeyHash)
		if err != nil {
			t.Fatal("resolve err: %v", err)
		}
		return res != resolvedHash
	}

	// test things

	// at this point, should not have written dataA and not have written dataB
	rbuf, err := ioutil.ReadFile(fname)
	if err != nil || !bytes.Equal(rbuf, dataA) {
		t.Fatal("Data inconsistent! %v %v", err, string(rbuf))
	}

	if hasPublished() {
		t.Fatal("published (wrote)")
	}

	<-time.After(shortRepublishTimeout * 11 / 10)

	// at this point, should have written written dataB, but not published it
	rbuf, err = ioutil.ReadFile(fname)
	if err != nil || !bytes.Equal(rbuf, dataB) {
		t.Fatal("Data inconsistent! %v %v", err, string(rbuf))
	}

	if hasPublished() {
		t.Fatal("published (wrote)")
	}

	<-time.After(longRepublishTimeout * 11 / 10)

	// at this point, should have written written dataB, and published it
	rbuf, err = ioutil.ReadFile(fname)
	if err != nil || !bytes.Equal(rbuf, dataB) {
		t.Fatal("Data inconsistent! %v %v", err, string(rbuf))
	}

	if !hasPublished() {
		t.Fatal("not published")
	}

	close(closed)
}

301 302
// Test writing a medium sized file one byte at a time
func TestMultiWrite(t *testing.T) {
303 304 305 306 307
	if runtime.GOOS == "darwin" {
		link := "https://github.com/jbenet/go-ipfs/issues/147"
		t.Skipf("Skipping as is broken in OSX. See %s", link)
	}

308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
	_, mnt := setupIpnsTest(t, nil)
	defer mnt.Close()

	fpath := mnt.Dir + "/local/file"
	fi, err := os.Create(fpath)
	if err != nil {
		t.Fatal(err)
	}

	data := randBytes(1001)
	for i := 0; i < len(data); i++ {
		n, err := fi.Write(data[i : i+1])
		if err != nil {
			t.Fatal(err)
		}
		if n != 1 {
			t.Fatal("Somehow wrote the wrong number of bytes! (n != 1)")
		}
	}
	fi.Close()

	rbuf, err := ioutil.ReadFile(fpath)
	if err != nil {
		t.Fatal(err)
	}

	if !bytes.Equal(rbuf, data) {
		t.Fatal("File on disk did not match bytes written")
	}
}