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) } }
func decryptHelper(key *vsafe.Key, entry *vsafe.Entry) (err error) { if err = entry.Decrypt(key); err != nil { if err == vsafe.ErrKeyMismatch { err = ErrNoSuchId } return } return nil }
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) }
func doSingleImport( t db.Transaction, store vsafedb.AddEntryRunner, jentry *jsonEntry, key *vsafe.Key) error { var entry vsafe.Entry var err error entry.Url, err = url.Parse(jentry.Url) if err != nil { return err } entry.Title = jentry.Title entry.Desc = jentry.Desc entry.UName = jentry.UName entry.Password = jentry.Password entry.Special = jentry.Special _, err = vsafedb.AddEntry(store, t, key, &entry) return err }
func (f *FakeStore) AddEntry(t db.Transaction, e *vsafe.Entry) error { e.Id = int64(len(*f) + 1) stored := *e *f = append(*f, &stored) return nil }