Exemple #1
0
func TestBtnEncryptWriter_PartialWrite(t *testing.T) {
	payload := util.RandomBytes(1024 * 1024)

	var b bytes.Buffer
	bew, err := btncrypt.NewWriteCloser(&b, tu.TestCipher(), len(payload))
	if err != nil {
		t.Errorf("Failed to create BtnEncryptWriter: %v", err)
	}

	verifyWrite(t, bew, payload[:3])
	verifyWrite(t, bew, payload[3:1024])
	verifyWrite(t, bew, payload[1024:4096])
	verifyWrite(t, bew, payload[4096:])

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

	plain, err := btncrypt.Decrypt(tu.TestCipher(), b.Bytes(), len(payload))
	if err != nil {
		t.Errorf("Failed to decrypt: %v", err)
	}

	if !bytes.Equal(payload, plain) {
		t.Errorf("Failed to restore original payload")
	}
}
Exemple #2
0
func TestFileWriteRead(t *testing.T) {
	snapshotio := inodedb.NewSimpleDBStateSnapshotIO()
	txio := inodedb.NewSimpleDBTransactionLogIO()
	idb, err := inodedb.NewEmptyDB(snapshotio, txio)
	if err != nil {
		t.Errorf("NewEmptyDB failed: %v", err)
		return
	}

	bs := tu.TestFileBlobStore()
	fs := otaru.NewFileSystem(idb, bs, tu.TestCipher())
	h, err := fs.OpenFileFullPath("/hello.txt", flags.O_CREATE|flags.O_RDWR, 0666)
	if err != nil {
		t.Errorf("OpenFileFullPath failed: %v", err)
		return
	}

	err = h.PWrite(tu.HelloWorld, 0)
	if err != nil {
		t.Errorf("PWrite failed: %v", err)
	}

	buf := make([]byte, 32)
	n, err := h.ReadAt(buf, 0)
	if err != nil {
		t.Errorf("PRead failed: %v", err)
	}
	buf = buf[:n]
	if n != len(tu.HelloWorld) {
		t.Errorf("n: %d", n)
	}
	if !bytes.Equal(tu.HelloWorld, buf) {
		t.Errorf("PRead content != PWrite content: %v", buf)
	}
}
Exemple #3
0
func TestEncrypt_Short(t *testing.T) {
	payload := []byte("short string")
	envelope, err := btncrypt.Encrypt(tu.TestCipher(), payload)
	if err != nil {
		t.Errorf("Failed to encrypt: %v", err)
	}

	plain, err := btncrypt.Decrypt(tu.TestCipher(), envelope, len(payload))
	if err != nil {
		t.Errorf("Failed to decrypt: %v", err)
	}

	if !bytes.Equal(plain, payload) {
		t.Errorf("Failed to restore original payload")
	}
}
Exemple #4
0
func TestEncrypt_Long(t *testing.T) {
	payload := util.RandomBytes(1024 * 1024)

	envelope, err := btncrypt.Encrypt(tu.TestCipher(), payload)
	if err != nil {
		t.Errorf("Failed to encrypt: %v", err)
	}

	plain, err := btncrypt.Decrypt(tu.TestCipher(), envelope, len(payload))
	if err != nil {
		t.Errorf("Failed to decrypt: %v", err)
	}

	if !bytes.Equal(payload, plain) {
		t.Errorf("Failed to restore original payload")
	}
}
func testDBTransactionIOWithRootKey(rootKeyStr string) *datastore.DBTransactionLogIO {
	homedir := os.Getenv("HOME")
	projectName := util.StringFromFileOrDie(path.Join(homedir, ".otaru", "projectname.txt"), "projectName")
	bs, err := datastore.NewDBTransactionLogIO(projectName, rootKeyStr, tu.TestCipher(), testClientSource())
	if err != nil {
		log.Fatalf("Failed to create DBTransactionLogIO: %v", err)
	}
	return bs
}
func TestCachedBackedVersion_SaveRestore(t *testing.T) {
	bs := tu.TestFileBlobStore()

	cbv := cachedblobstore.NewCachedBackendVersion(bs, tu.TestQueryVersion)
	cbv.Set("foobar", 123)
	if err := cbv.SaveStateToBlobstore(tu.TestCipher(), bs); err != nil {
		t.Errorf("Failed to save state: %v", err)
		return
	}

	cbv2 := cachedblobstore.NewCachedBackendVersion(bs, tu.TestQueryVersion)
	if err := cbv2.RestoreStateFromBlobstore(tu.TestCipher(), bs); err != nil {
		t.Errorf("Failed to restore  state: %v", err)
	}
	v, err := cbv2.Query("foobar")
	if err != nil {
		t.Errorf("Unexpected Query() err: %v", err)
		return
	}
	if v != 123 {
		t.Errorf("Unexpected Query() result. v: %d", v)
		return
	}
}
Exemple #7
0
func TestBtnEncryptWriter_WriteAtOnce(t *testing.T) {
	//payload := RandomBytes(1024 * 1024)
	payload := []byte("short string")

	var b bytes.Buffer
	bew, err := btncrypt.NewWriteCloser(&b, tu.TestCipher(), len(payload))
	if err != nil {
		t.Errorf("Failed to create BtnEncryptWriter: %v", err)
	}

	verifyWrite(t, bew, payload)
	if err := bew.Close(); err != nil {
		t.Errorf("bew.Close failed: %v", err)
	}

	plain, err := btncrypt.Decrypt(tu.TestCipher(), b.Bytes(), len(payload))
	if err != nil {
		t.Errorf("Failed to decrypt: %v", err)
	}

	if !bytes.Equal(payload, plain) {
		t.Errorf("Failed to restore original payload")
	}
}
func TestSS_SaveRestore(t *testing.T) {
	loc := datastore.NewINodeDBSSLocator(authtu.TestDSConfig(testRootKey()))
	if _, err := loc.DeleteAll(); err != nil {
		t.Errorf("Failed to loc.DeleteAll: %v", err)
	}

	bs := tu.TestFileBlobStore()
	sio := blobstoredbstatesnapshotio.New(bs, tu.TestCipher(), loc)

	db, err := inodedb.NewEmptyDB(sio, inodedb.NewSimpleDBTransactionLogIO())
	if err != nil {
		t.Errorf("Failed to NewEmptyDB: %v", err)
		return
	}
	if err := db.Sync(); err != nil {
		t.Errorf("Failed to Sync DB: %v", err)
		return
	}

	_, err = inodedb.NewDB(sio, inodedb.NewSimpleDBTransactionLogIO())
	if err != nil {
		t.Errorf("Failed to NewDB: %v", err)
	}
}
func TestSS_AutoAvoidCorruptedSnapshot(t *testing.T) {
	loc := datastore.NewINodeDBSSLocator(authtu.TestDSConfig(testRootKey()))
	if _, err := loc.DeleteAll(); err != nil {
		t.Errorf("Failed to loc.DeleteAll: %v", err)
	}

	bs := tu.TestFileBlobStore()
	sio := blobstoredbstatesnapshotio.New(bs, tu.TestCipher(), loc)
	txlogio := inodedb.NewSimpleDBTransactionLogIO()

	{
		db, err := inodedb.NewEmptyDB(sio, txlogio)
		if err != nil {
			t.Errorf("Failed to NewEmptyDB: %v", err)
			return
		}

		// create 1st snapshot
		if err := db.Sync(); err != nil {
			t.Errorf("Failed to Sync DB: %v", err)
			return
		}

		// apply some mod to inodedb
		nlock, err := db.LockNode(inodedb.AllocateNewNodeID)
		if err != nil {
			t.Errorf("Failed to LockNode: %v", err)
			return
		}

		tx := inodedb.DBTransaction{Ops: []inodedb.DBOperation{
			&inodedb.CreateNodeOp{NodeLock: nlock, OrigPath: "/hoge.txt", Type: inodedb.FileNodeT},
			&inodedb.HardLinkOp{NodeLock: inodedb.NodeLock{1, inodedb.NoTicket}, Name: "hoge.txt", TargetID: nlock.ID},
		}}
		if _, err := db.ApplyTransaction(tx); err != nil {
			t.Errorf("Failed to apply tx: %v", err)
			return
		}

		if err := db.UnlockNode(nlock); err != nil {
			t.Errorf("Failed to UnlockNode: %v", err)
			return
		}

		// create 2nd snapshot
		if err := db.Sync(); err != nil {
			t.Errorf("Failed to Sync DB (2): %v", err)
			return
		}
	}

	if _, err := inodedb.NewDB(sio, txlogio); err != nil {
		t.Errorf("Failed to NewDB (uncorrupted): %v", err)
		return
	}

	// destroy latest snapshot (corrupt data)
	ssbp, err := loc.Locate(0)
	if err != nil {
		t.Errorf("Failed to locate latest ssbp: %v", err)
		return
	}
	{
		wc, err := bs.OpenWriter(ssbp)
		if err != nil {
			t.Errorf("Failed to OpenWriter: %v", err)
			return
		}
		if _, err := wc.Write([]byte("hoge")); err != nil {
			t.Errorf("Failed to Write: %v", err)
		}
		wc.Close()
	}

	{
		_, err = inodedb.NewDB(sio, txlogio)
		if err != nil {
			t.Errorf("Failed to NewDB (corrupted): %v", err)
			return
		}
	}

	// destroy latest snapshot (remove ss blob)
	if err := bs.RemoveBlob(ssbp); err != nil {
		t.Errorf("Failed to RemoveBlob: %v", err)
	}

	{
		_, err = inodedb.NewDB(sio, txlogio)
		if err != nil {
			t.Errorf("Failed to NewDB (ss blob removed): %v", err)
			return
		}
	}
}
Exemple #10
0
func TestDSConfig(rootKeyStr string) *datastore.Config {
	projectName := TestConfig().ProjectName
	return datastore.NewConfig(projectName, rootKeyStr, tu.TestCipher(), TestTokenSource())
}