コード例 #1
0
ファイル: model_test.go プロジェクト: keep94/vsafe
func TestEncryptDecrypt(t *testing.T) {
	e := vsafe.Entry{
		Title:    "title",
		Desc:     "desc",
		UName:    "foo",
		Password: "******",
		Special:  "baz"}
	key := &vsafe.Key{Id: 1, Value: kdf.Random(32)}
	encrypted := e
	if err := encrypted.Encrypt(key); err != nil {
		t.Fatalf("Got error encrypting: %v", err)
	}
	if encrypted.UName == e.UName || encrypted.Password == e.Password || encrypted.Special == e.Special {
		t.Error("Encrypted is the same as plain text")
	}
	if encrypted.Title != e.Title || encrypted.Desc != e.Desc {
		t.Error("Encrypted fields should be the same as plain text")
	}
	decrypted := encrypted
	if err := decrypted.Decrypt(key); err != nil {
		t.Fatalf("Got error encrypting: %v", err)
	}
	// Owner of entity changes to key Id
	e.Owner = key.Id
	if decrypted != e {
		t.Errorf("Expected %v, got %v", e, decrypted)
	}
	if err := decrypted.Decrypt(&vsafe.Key{Id: 2, Value: kdf.Random(32)}); err != vsafe.ErrKeyMismatch {
		t.Errorf("Expected ErrKeyMismatch, got %v", err)
	}
}
コード例 #2
0
ファイル: fixture.go プロジェクト: keep94/vsafe
func UpdateEntry(t *testing.T, store UpdateEntryStore) {
	var first, second vsafe.Entry
	var firstResult, secondResult vsafe.Entry
	createEntries(t, store, &first, &second)
	first.Owner = 23
	first.Url = url2
	first.Title = "abar"
	first.Desc = "aaa"
	first.UName = "back"
	first.Password = "******"
	first.Special = "new again"
	if err := store.UpdateEntry(nil, &first); err != nil {
		t.Fatalf("Got error updating database: %v", err)
	}
	if err := store.EntryById(nil, first.Id, &firstResult); err != nil {
		t.Fatalf("Got error reading database by id: %v", err)
	}
	if err := store.EntryById(nil, second.Id, &secondResult); err != nil {
		t.Fatalf("Got error reading database by id: %v", err)
	}
	assertEntryEqual(t, &first, &firstResult)
	assertEntryEqual(t, &second, &secondResult)
}