예제 #1
0
// Regression test for copying larger file to the mount.
func TestTouchWrite(t *testing.T) {
	withMount(t, func(mount *Mount) {
		for _, size := range DataSizes {
			name := fmt.Sprintf("/empty_%d", size)
			if err := mount.Store.Touch(name); err != nil {
				t.Errorf("Could not touch an empty file: %v", err)
				return
			}

			path := filepath.Join(mount.Dir, name)

			// Write a simple file via the fuse layer:
			helloData := testutil.CreateDummyBuf(size)
			err := ioutil.WriteFile(path, helloData, 0644)
			if err != nil {
				t.Errorf("Could not write simple file via fuse layer: %v", err)
				return
			}

			if !checkForCorrectFile(t, path, helloData) {
				break
			}
		}
	})
}
예제 #2
0
// Test if encrypting the same plaintext twice yields
// the same ciphertext. This is a crucial property for brig, although it
// has some security implications (i.e. no real random etc.)
func TestEncryptedTheSame(t *testing.T) {
	sourceData := testutil.CreateDummyBuf(3 * defaultMaxBlockSize)
	encOne := &bytes.Buffer{}
	encTwo := &bytes.Buffer{}

	n1, err := Encrypt(TestKey, bytes.NewReader(sourceData), encOne)
	if err != nil {
		t.Errorf("TestEncryptedTheSame: Encrypting first failed: %v", err)
		return
	}

	n2, err := Encrypt(TestKey, bytes.NewReader(sourceData), encTwo)
	if err != nil {
		t.Errorf("TestEncryptedTheSame: Encrypting second failed: %v", err)
		return
	}

	if n1 != n2 {
		t.Errorf("TestEncryptedTheSame: Ciphertexts differ in length.")
		return
	}

	if !bytes.Equal(encOne.Bytes(), encTwo.Bytes()) {
		t.Errorf("TestEncryptedTheSame: Ciphertext differ, you failed.")
		t.Errorf("\tOne: %v", encOne.Bytes())
		t.Errorf("\tTwo: %v", encTwo.Bytes())
		return
	}
}
예제 #3
0
func TestAddCat(t *testing.T) {
	sizes := []int64{
		0, 1, 2, 4, 1024, 4096, 16 * 1024,
	}

	for _, size := range sizes {
		data := testutil.CreateDummyBuf(size)
		path := fmt.Sprintf("dummy_%d", size)

		withEmptyStore(t, func(st *store.Store) {
			if err := st.StageFromReader(path, bytes.NewReader(data), compress.AlgoNone); err != nil {
				t.Errorf("Adding of `%s` failed: %v", path, err)
				return
			}

			recvBuf := &bytes.Buffer{}
			if err := st.Cat(path, recvBuf); err != nil {
				t.Errorf("Catting of `%s` failed: %v", path, err)
				return
			}

			if !bytes.Equal(recvBuf.Bytes(), data) {
				t.Errorf("Data differs between add and cat")
				return
			}
		})
	}
}
예제 #4
0
func TestMove(t *testing.T) {
	data := testutil.CreateDummyBuf(1024)

	withEmptyStore(t, func(st *store.Store) {
		if err := createDummySetup(t, st, data); err != nil {
			return
		}

		check := func(path string, expect []byte) {
			recvBuf := &bytes.Buffer{}
			if err := st.Cat(path, recvBuf); err != nil {
				t.Errorf("Catting of `%s` failed: %v", path, err)
				return
			}

			if !bytes.Equal(recvBuf.Bytes(), expect) {
				t.Errorf("Data differs between add/move/cat")
				return
			}
		}

		check("/dummy", data)

		if err := st.Move("/dummy", "/new_dummy", true); err != nil {
			t.Errorf("Move failed: %v", err)
			return
		}

		if err := st.Cat("/dummy", &bytes.Buffer{}); !store.IsNoSuchFileError(err) {
			t.Errorf("Move: dummy still reachable")
			return
		}

		check("/new_dummy", data)

		if err := st.Move("/dummy", "/new_dummy", true); !store.IsNoSuchFileError(err) {
			t.Errorf("Move could move dead file: %v", err)
			return
		}

		if err := st.Move("/dir", "/other", true); err != nil {
			t.Errorf("Move could not move dir: %v", err)
			return
		}

		check("/other/a", []byte{})
		check("/other/b", []byte{})

		if err := st.Cat("/dir/a", &bytes.Buffer{}); !store.IsNoSuchFileError(err) {
			t.Errorf("Move: /dir/a still reachable")
			return
		}

		if err := st.Cat("/dir/b", &bytes.Buffer{}); !store.IsNoSuchFileError(err) {
			t.Errorf("Move: /dir/b still reachable")
			return
		}
	})
}
예제 #5
0
func testCompressDecompress(t *testing.T, size int64, algo AlgorithmType, useReadFrom, useWriteTo bool) {
	// Fake data file is written to disk,
	// as compression reader has to be a ReadSeeker.
	testutil.Remover(t, ZipFilePath)
	data := testutil.CreateDummyBuf(size)
	zipFileDest := openDest(t, ZipFilePath)

	defer testutil.Remover(t, ZipFilePath)

	// Compress.
	w, err := NewWriter(zipFileDest, algo)
	if err != nil {
		t.Errorf("Writer init failed %v", err)
		return

	}

	if _, err := testutil.DumbCopy(w, bytes.NewReader(data), useReadFrom, useWriteTo); err != nil {
		t.Errorf("Compress failed %v", err)
		return
	}

	if err := w.Close(); err != nil {
		t.Errorf("Compression writer close failed: %v", err)
		return
	}

	if err := zipFileDest.Close(); err != nil {
		t.Errorf("close(zipFileDest) failed: %v", err)
		return
	}

	// Read compressed file into buffer.
	dataUncomp := bytes.NewBuffer(nil)
	dataFromZip := openSrc(t, ZipFilePath)

	// Uncompress.
	r := NewReader(dataFromZip)
	if _, err := testutil.DumbCopy(dataUncomp, r, useReadFrom, useWriteTo); err != nil {
		t.Errorf("Decompression failed: %v", err)
		return
	}
	if err := dataFromZip.Close(); err != nil {
		t.Errorf("Zip close failed: %v", err)
		return
	}

	// Compare.
	got, want := dataUncomp.Bytes(), data
	if !bytes.Equal(got, want) {
		t.Error("Uncompressed data and input data does not match.")
		t.Errorf("\tGOT:   %v", util.OmitBytes(got, 10))
		t.Errorf("\tWANT:  %v", util.OmitBytes(want, 10))
		return
	}
}
예제 #6
0
func testAuthProcess(t *testing.T, size int64) {
	withLoopbackConnection(t, func(a, b net.Conn) {
		aliPriv, aliPub, err := genKeyPair(t)
		if err != nil {
			return
		}

		bobPriv, bobPub, err := genKeyPair(t)
		if err != nil {
			return
		}

		autherA := NewAuthReadWriter(a, aliPriv, bobPub)
		autherB := NewAuthReadWriter(b, bobPriv, aliPub)

		expect := testutil.CreateDummyBuf(size)
		answer := make([]byte, len(expect))

		// Sort out connection based troubles quickly:
		// Just send a normal message over the conn.
		if _, err := a.Write(expect); err != nil {
			t.Errorf("Normal write failed: %v", err)
			return
		}

		if _, err := b.Read(answer); err != nil && (err != io.EOF && size == 0) {
			t.Errorf("Normal read failed: %v", err)
			return
		}

		if !bytes.Equal(expect, answer) {
			t.Errorf("Normal transmission failed; want `%s`, got `%s`", expect, answer)
			return
		}

		answer = make([]byte, len(expect))

		go func() {
			if _, err := autherA.Write(expect); err != nil {
				t.Errorf("Auth Write failed: %v", err)
				return
			}
		}()

		if _, err := autherB.Read(answer); err != nil {
			t.Errorf("Auth Read failed: %v", err)
			return
		}

		if !bytes.Equal(expect, answer) {
			t.Errorf("auth transmission failed; want `%s`, got `%s`", expect, answer)
			return
		}
	})
}
예제 #7
0
func TestBigFile(t *testing.T) {
	src := testutil.CreateDummyBuf(147611)
	dst := &bytes.Buffer{}

	srcEnc := &bytes.Buffer{}
	wEnc, err := encrypt.NewWriter(srcEnc, TestKey)
	if err != nil {
		t.Errorf("Cannot create write-encryption layer: %v", err)
		return
	}

	if err := wEnc.Close(); err != nil {
		t.Errorf("Cannot close write-encryption layer: %v", err)
		return
	}

	wDec, err := encrypt.NewReader(bytes.NewReader(srcEnc.Bytes()), TestKey)
	if err != nil {
		t.Errorf("Cannot create read-encryption layer: %v", err)
		return
	}

	// Act a bit like the fuse layer:
	lay := NewLayer(wDec)
	lay.Truncate(0)

	bufSize := 128 * 1024
	if _, err := io.CopyBuffer(lay, bytes.NewReader(src), make([]byte, bufSize)); err != nil {
		t.Errorf("Could not encrypt data")
		return
	}

	lay.Truncate(int64(len(src)))

	if _, err := lay.Seek(0, os.SEEK_SET); err != nil {
		t.Errorf("Seeking to 0 in big file failed: %v", err)
		return
	}

	n, err := io.CopyBuffer(dst, lay, make([]byte, bufSize))
	if err != nil {
		t.Errorf("Could not copy big file data over overlay: %v", err)
		return
	}

	if n != int64(len(src)) {
		t.Errorf("Did not fully copy big file: got %d, should be %d bytes", n, len(src))
		return
	}

	if !bytes.Equal(dst.Bytes(), src) {
		t.Errorf("Source and destination buffers differ.")
		return
	}
}
예제 #8
0
func TestRemove(t *testing.T) {
	data := testutil.CreateDummyBuf(1024)

	withEmptyStore(t, func(st *store.Store) {
		if err := createDummySetup(t, st, data); err != nil {
			return
		}

		if _, err := st.Mkdir("/empty_dir"); err != nil {
			t.Errorf("Could not mkdir /empty_dir: %v", err)
			return
		}

		if err := st.Remove("/dummy", false); err != nil {
			t.Errorf("Could not remove /dummy: %v", err)
			return
		}

		if err := st.Remove("/dummy", false); !store.IsNoSuchFileError(err) {
			t.Errorf("Could remove /dummy twice: %v", err)
			return
		}

		if err := st.Remove("/dir", false); err != store.ErrNotEmpty {
			t.Errorf("Remove did not deny removing non-empty dir: %v", err)
			return
		}

		if err := st.Remove("/dir", true); err != nil {
			t.Errorf("Could not remove /dir recursively: %v", err)
			return
		}

		if err := st.Remove("/empty_dir", false); err != nil {
			t.Errorf("Could not remove /empty_dir non-recursively: %v", err)
			return
		}

		if err := st.ViewDir("/", func(root *store.Directory) error {
			if root.Size() != 0 {
				return fmt.Errorf("Size of the tree is not 0 after deletion (%d)", root.Size())
			}

			return nil
		}); err != nil {
			t.Errorf("Failed to get root: %v", err)
			return
		}
	})
}
예제 #9
0
func TestWriteAndRead(t *testing.T) {
	s64k := int64(64 * 1024)
	sizes := []int64{
		0, 1, 10, s64k, s64k - 1, s64k + 1,
		s64k * 2, s64k * 1024,
	}

	for _, size := range sizes {
		t.Logf("Testing stream at size %d", size)
		regularData := testutil.CreateDummyBuf(size)
		randomData := testutil.CreateRandomDummyBuf(size, 42)

		for algoType, _ := range compress.AlgoMap {
			testWriteAndRead(t, regularData, algoType)
			testWriteAndRead(t, randomData, algoType)
		}
	}
}
예제 #10
0
func TestWrite(t *testing.T) {
	withMount(t, func(mount *Mount) {
		for _, size := range DataSizes {
			helloData := testutil.CreateDummyBuf(size)
			path := filepath.Join(mount.Dir, fmt.Sprintf("hello_%d", size))

			// Write a simple file via the fuse layer:
			err := ioutil.WriteFile(path, helloData, 0644)
			if err != nil {
				t.Errorf("Could not write simple file via fuse layer: %v", err)
				return
			}

			if !checkForCorrectFile(t, path, helloData) {
				break
			}
		}
	})
}
예제 #11
0
func TestRead(t *testing.T) {
	withMount(t, func(mount *Mount) {
		for _, size := range DataSizes {
			helloData := testutil.CreateDummyBuf(size)

			// Add a simple file:
			name := fmt.Sprintf("hello_%d", size)
			reader := bytes.NewReader(helloData)
			if err := mount.Store.StageFromReader("/"+name, reader, compress.AlgoNone); err != nil {
				t.Errorf("Adding simple file from reader failed: %v", err)
				return
			}

			path := filepath.Join(mount.Dir, name)
			if !checkForCorrectFile(t, path, helloData) {
				break
			}
		}
	})
}
예제 #12
0
func testProtocol(t *testing.T, compress bool) {
	b := &bytes.Buffer{}
	alice := NewProtocol(b, compress)
	bob := NewProtocol(b, compress)

	// Test with varying potential for compression:
	for i := 0; i < 5; i++ {
		msg := &wire.Response{
			ReqType: wire.RequestType_DUMMY.Enum(),
			Data:    testutil.CreateDummyBuf(int64(i) * 255),
		}

		if err := alice.Send(msg); err != nil {
			t.Errorf("Send failed: %v", err)
			return
		}

		remoteMsg := &wire.Response{
			ReqType: wire.RequestType_DUMMY.Enum(),
			ID:      proto.Int64(0),
		}

		if err := bob.Recv(remoteMsg); err != nil {
			t.Errorf("Recv failed: %v", err)
			return
		}

		if !bytes.Equal(msg.GetData(), remoteMsg.GetData()) {
			t.Errorf("Data differs.")
			t.Errorf("\tWANT: %v", msg.GetData())
			t.Errorf("\tGOT:  %v", remoteMsg.GetData())
			return
		}

		if msg.GetReqType() != remoteMsg.GetReqType() {
			t.Errorf("Types differ.")
			return
		}
	}
}
예제 #13
0
func TestStoreSync(t *testing.T) {
	data := testutil.CreateDummyBuf(1024)
	path := "/i/have/a/file.png"

	withIpfsStore(t, "alice", func(alice *Store) {
		withIpfsStore(t, "bob", func(bob *Store) {
			if err := alice.StageFromReader(path, bytes.NewReader(data), compress.AlgoNone); err != nil {
				t.Errorf("Failed to stage alice' file: %v", err)
				return
			}

			if err := bob.StageFromReader(path, bytes.NewReader(data), compress.AlgoNone); err != nil {
				t.Errorf("Failed to stage bob's file: %v", err)
				return
			}
			if err := bob.StageFromReader(path+".surprise", bytes.NewReader(data), compress.AlgoNone); err != nil {
				t.Errorf("Failed to stage bob's file: %v", err)
				return
			}

			if err := alice.SyncWith(bob); err != nil {
				t.Errorf("Sync failed: %v", err)
				return
			}

			fmt.Println("+=======")

			if err := alice.SyncWith(bob); err != nil {
				t.Errorf("Sync failed: %v", err)
				return
			}
		})

		printTree(alice.fs)
	})
}
예제 #14
0
func testSeek(t *testing.T, N int64, readFrom, writeTo bool) {
	sourceData := testutil.CreateDummyBuf(N)
	source := bytes.NewBuffer(sourceData)
	shared := &bytes.Buffer{}

	t.Logf("Testing seek for size %d", N)

	enc, err := NewWriter(shared, TestKey)
	if err != nil {
		t.Errorf("Creating an encrypted writer failed: %v", err)
		return
	}

	// Encrypt:
	if _, err = testutil.DumbCopy(enc, source, readFrom, writeTo); err != nil {
		t.Errorf("copy(enc, source) failed %v", err)
		return
	}

	// This needs to be here, since close writes
	// left over data to the write stream
	if err = enc.Close(); err != nil {
		t.Errorf("close(enc): %v", err)
		return
	}

	sharedReader := bytes.NewReader(shared.Bytes())
	decLayer, err := NewReader(sharedReader, TestKey)
	if err != nil {
		t.Errorf("creating new reader failed: %v", err)
		return
	}

	lastJump := int64(0)

	for _, test := range SeekTests {
		realOffset := int64(math.Floor(.5 + test.Offset*float64(N)))

		whence := map[int]string{
			0: "SEEK_SET",
			1: "SEEK_CUR",
			2: "SEEK_END",
		}[test.Whence]

		exptOffset := int64(0)
		switch test.Whence {
		case os.SEEK_SET:
			exptOffset = realOffset
		case os.SEEK_CUR:
			exptOffset = lastJump + realOffset
		case os.SEEK_END:
			exptOffset = N + realOffset
		default:
			panic("Bad whence")
		}

		t.Logf(
			" => Seek(%v, %v) -> %v (size: %v)",
			realOffset,
			whence,
			exptOffset,
			N,
		)

		jumpedTo, err := decLayer.Seek(realOffset, test.Whence)
		if err != test.Error {
			if err != io.EOF && N != 0 {
				t.Errorf(
					"Seek(%v, %v) produced an error: %v (should be %v)",
					realOffset,
					whence,
					err,
					test.Error,
				)
			}
			return
		}

		if test.Error != nil {
			continue
		}

		if jumpedTo != exptOffset {
			t.Errorf(
				"Seek(%v, %v) jumped badly. Should be %v, was %v",
				realOffset,
				whence,
				exptOffset,
				jumpedTo,
			)
			return
		}

		lastJump = jumpedTo

		// Decrypt and check if the contents are okay:
		dest := bytes.NewBuffer(nil)

		copiedBytes, err := testutil.DumbCopy(dest, decLayer, readFrom, writeTo)
		if err != nil {
			t.Errorf("Decrypt failed: %v", err)
			return
		}

		if copiedBytes != N-jumpedTo {
			t.Errorf("Copied different amount of decrypted data than expected.")
			t.Errorf("Should be %v, was %v bytes.", N-jumpedTo, copiedBytes)
		}

		// Check the data actually matches the source data.
		if !bytes.Equal(sourceData[jumpedTo:], dest.Bytes()) {
			t.Errorf("Seeked data does not match expectations.")
			t.Errorf("\tEXPECTED: %v", util.OmitBytes(sourceData[jumpedTo:], 10))
			t.Errorf("\tGOT:      %v", util.OmitBytes(dest.Bytes(), 10))
			return
		}

		// Jump back, so the other tests continue to work:
		jumpedAgain, err := decLayer.Seek(jumpedTo, os.SEEK_SET)
		if err != nil {
			t.Errorf("Seeking not possible after reading: %v", err)
			return
		}

		if jumpedTo != jumpedAgain {
			t.Errorf("Jumping back to original pos failed.")
			t.Errorf("Should be %v, was %v.", jumpedTo, jumpedAgain)
			return
		}
	}
}
예제 #15
0
func testSeek(t *testing.T, size, offset int64, algo AlgorithmType, useReadFrom, useWriteTo bool) {
	// Fake data file is written to disk,
	// as compression reader has to be a ReadSeeker.
	testutil.Remover(t, ZipFilePath)
	data := testutil.CreateDummyBuf(size)
	zipFileDest := openDest(t, ZipFilePath)

	// Compress.
	w, err := NewWriter(zipFileDest, algo)
	if err != nil {
		t.Errorf("Writer init failed %v", err)
		return
	}
	if _, err := testutil.DumbCopy(w, bytes.NewReader(data), useReadFrom, useWriteTo); err != nil {
		t.Errorf("Compress failed %v", err)
		return
	}
	defer testutil.Remover(t, ZipFilePath)

	if err := w.Close(); err != nil {
		t.Errorf("Compression writer close failed: %v", err)
		return
	}

	if err := zipFileDest.Close(); err != nil {
		t.Errorf("close(zipFileDest) failed: %v", err)
		return
	}

	// Read compressed file into buffer.
	dataUncomp := bytes.NewBuffer(nil)
	dataFromZip := openSrc(t, ZipFilePath)
	zr := NewReader(dataFromZip)

	// Set specific offset before read.
	_, err = zr.Seek(offset, os.SEEK_SET)
	if err == io.EOF && offset < size && offset > -1 {
		t.Errorf("Seek failed even with EOF: %d <= %d", offset, size)
		return
	}
	if err != io.EOF && err != nil {
		t.Errorf("Seek failed: %v", err)
		return
	}

	// Read starting at a specific offset.
	if _, err := testutil.DumbCopy(dataUncomp, zr, useReadFrom, useWriteTo); err != nil {
		t.Errorf("Decompression failed: %v", err)
		return
	}
	if err := dataFromZip.Close(); err != nil {
		t.Errorf("Zip close failed: %v", err)
		return
	}

	// Compare.
	maxOffset := offset
	if offset > size {
		maxOffset = size
	}

	if offset < 0 {
		maxOffset = 0
	}

	got, want := dataUncomp.Bytes(), data[maxOffset:]
	if !bytes.Equal(got, want) {
		t.Error("Uncompressed data and input data does not match.")
		t.Errorf("\tGOT:   %v", util.OmitBytes(got, 10))
		t.Errorf("\tWANT:  %v", util.OmitBytes(want, 10))
		return
	}
}
예제 #16
0
func TestCommit(t *testing.T) {
	data := testutil.CreateDummyBuf(1024)

	withEmptyStore(t, func(st *store.Store) {
		if err := createDummySetup(t, st, data); err != nil {
			return
		}

		if err := st.MakeCommit("Im cool."); err != nil {
			t.Errorf("Making first commit failed: %v", err)
			return
		}
	})
	/*
		withIpfsStore(t, "alice", func(st *store.Store) {
			head, err := st.Head()
			if err != nil {
				t.Errorf("Unable to peek head at beginning: %v", err)
				return
			}

			if len(head.Checkpoints) != 0 {
				t.Errorf("Initial commit has Checkpoints?")
				return
			}

			if !strings.Contains(strings.ToLower(head.Message), "initial") {
				t.Errorf("Initial commit does not name itself like that.")
				return
			}

			// Stage is empty after initial commit:
			if err := st.MakeCommit("empty."); err != store.ErrEmptyStage {
				t.Errorf("Empty commits are now allowed: %v", err)
				return
			}

			if err := st.Touch("/hello.world"); err != nil {
				t.Errorf("Unable to touch empty file: %v", err)
				return
			}

			if err := st.MakeCommit("testing around"); err != nil {
				t.Errorf("Could not commit: %v", err)
				return
			}

			head, err = st.Head()
			if err != nil {
				t.Errorf("Unable to peek head the second time: %v", err)
				return
			}

			if head.Message != "testing around" {
				t.Errorf("Bad commit message: %s", head.Message)
				return
			}

			if len(head.Checkpoints) != 1 {
				t.Errorf("More or less Checkpoints than expected: %d", len(head.Checkpoints))
				return
			}

			checkpoint := head.Checkpoints[0]
			if checkpoint.Change != store.ChangeAdd {
				t.Errorf("Empty file was not added?")
				return
			}
		})
	*/
}